@@ -2,23 +2,28 @@ package org.catrobat.paintroid.tools.implementation
2
2
3
3
import android.graphics.Canvas
4
4
import android.graphics.Paint
5
+ import android.graphics.Path
5
6
import android.graphics.PointF
6
7
import android.util.Log
7
8
import android.view.View
8
9
import androidx.test.espresso.idling.CountingIdlingResource
9
- import org.catrobat.paintroid.MainActivity
10
10
import org.catrobat.paintroid.command.CommandManager
11
+ import org.catrobat.paintroid.command.implementation.PathCommand
12
+ import org.catrobat.paintroid.command.serialization.SerializablePath
11
13
import org.catrobat.paintroid.tools.ContextCallback
12
14
import org.catrobat.paintroid.tools.ToolPaint
13
15
import org.catrobat.paintroid.tools.ToolType
14
16
import org.catrobat.paintroid.tools.Workspace
15
17
import org.catrobat.paintroid.tools.common.CommonBrushChangedListener
16
18
import org.catrobat.paintroid.tools.common.CommonBrushPreviewListener
19
+ import org.catrobat.paintroid.tools.helper.DynamicLineToolVertex
17
20
import org.catrobat.paintroid.tools.options.BrushToolOptionsView
18
21
import org.catrobat.paintroid.tools.options.ToolOptionsViewController
19
22
import org.catrobat.paintroid.ui.viewholder.TopBarViewHolder
23
+ import java.util.*
24
+ import java.util.ArrayDeque
20
25
21
- class DynamicLineTool (
26
+ class DynamicLineTool (
22
27
private val brushToolOptionsView : BrushToolOptionsView ,
23
28
contextCallback : ContextCallback ,
24
29
toolOptionsViewController : ToolOptionsViewController ,
@@ -36,9 +41,12 @@ class DynamicLineTool (
36
41
commandManager
37
42
) {
38
43
override var toolType: ToolType = ToolType .DYNAMICLINE
39
- var startCoordinate: PointF ? = null
40
- var endCoordinate: PointF ? = null
41
- var startCoordinateIsSet: Boolean = false
44
+ private var startCoordinate: PointF ? = null
45
+ private var endCoordinate: PointF ? = null
46
+ private var startCoordinateIsSet: Boolean = false
47
+ private var vertexStack: Deque <DynamicLineToolVertex > = ArrayDeque ()
48
+ private var lineIsFinal: Boolean = false
49
+ private var currentPathCommand: PathCommand ? = null
42
50
43
51
init {
44
52
brushToolOptionsView.setBrushChangedListener(CommonBrushChangedListener (this ))
@@ -51,7 +59,6 @@ class DynamicLineTool (
51
59
brushToolOptionsView.setCurrentPaint(toolPaint.paint)
52
60
brushToolOptionsView.setStrokeCapButtonChecked(toolPaint.strokeCap)
53
61
topBarViewHolder?.hidePlusButton()
54
-
55
62
}
56
63
57
64
override fun handleUpAnimations (coordinate : PointF ? ) {
@@ -85,11 +92,15 @@ class DynamicLineTool (
85
92
override fun onClickOnButton () {
86
93
Log .e(TAG , " ✓ clicked" )
87
94
startCoordinateIsSet = false
95
+ lineIsFinal = true
96
+ currentPathCommand = null
88
97
}
89
98
90
99
fun onClickOnPlus () {
100
+ startCoordinate = endCoordinate?.let { copyPointF(it) }
101
+ lineIsFinal = true
102
+ currentPathCommand = null
91
103
Log .e(TAG , " + clicked" )
92
-
93
104
}
94
105
95
106
private fun hideToolOptions () {
@@ -135,28 +146,41 @@ class DynamicLineTool (
135
146
override fun handleDown (coordinate : PointF ? ): Boolean {
136
147
coordinate ? : return false
137
148
topBarViewHolder?.showPlusButton()
149
+ super .handleDown(coordinate)
138
150
startCoordinate = if (! startCoordinateIsSet) {
139
151
copyPointF(coordinate).also { startCoordinateIsSet = true }
140
152
} else {
141
153
startCoordinate
142
154
}
143
- super .handleDown(coordinate)
144
155
return true
145
156
}
146
157
147
158
override fun handleMove (coordinate : PointF ? ): Boolean {
148
159
coordinate ? : return false
149
160
hideToolOptions()
150
161
super .handleMove(coordinate)
151
-
152
162
endCoordinate = copyPointF(coordinate)
153
- Log .e(TAG , endCoordinate!! .x.toString() + " " + endCoordinate!! .y.toString())
163
+ Log .e(TAG , " Startcoordinate x: " + startCoordinate!! .x.toString() + " y: " + startCoordinate!! .y.toString())
164
+ Log .e(TAG , " Endcoordinate x: " + endCoordinate!! .x.toString() + " y: " + endCoordinate!! .y.toString())
154
165
return true
155
166
}
156
167
157
168
override fun handleUp (coordinate : PointF ? ): Boolean {
169
+ coordinate ? : return false
158
170
showToolOptions()
159
171
super .handleUp(coordinate)
172
+
173
+ var currentlyDrawnPath = createPath(startCoordinate, coordinate)
174
+ // This would mean we are updating an existing path
175
+ if (currentPathCommand != null ) {
176
+ // either update an existing command
177
+ (currentPathCommand as PathCommand ).updatePath(currentlyDrawnPath)
178
+ commandManager.executeAllCommands()
179
+ } else {
180
+ // or create a new one
181
+ currentPathCommand = commandFactory.createPathCommand(toolPaint.paint, currentlyDrawnPath) as PathCommand
182
+ commandManager.addCommand(currentPathCommand)
183
+ }
160
184
return true
161
185
}
162
186
@@ -175,12 +199,19 @@ class DynamicLineTool (
175
199
brushToolOptionsView.invalidate()
176
200
}
177
201
178
- private fun copyPointF (coordinate : PointF ): PointF {
179
- return PointF (coordinate.x, coordinate.y)
202
+ private fun copyPointF (coordinate : PointF ): PointF = PointF (coordinate.x, coordinate.y)
203
+
204
+ private fun createPath (startCoordinate : PointF ? , endCoordinate : PointF ): SerializablePath {
205
+ return SerializablePath ().apply {
206
+ if (startCoordinate != null && endCoordinate != null ) {
207
+ moveTo(startCoordinate.x, startCoordinate.y)
208
+ lineTo(endCoordinate.x, endCoordinate.y)
209
+ }
210
+ }
180
211
}
181
212
182
213
companion object {
183
214
var topBarViewHolder: TopBarViewHolder ? = null
184
215
const val TAG = " DynamicLineTool"
185
216
}
186
- }
217
+ }
0 commit comments