Skip to content

Commit

Permalink
PAINTROID-396 Multiline tool with movable intermediate points
Browse files Browse the repository at this point in the history
make vertices appear, implement paint changing
  • Loading branch information
Lenkomotive committed Jul 5, 2023
1 parent 0bc32bf commit 005263c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import android.graphics.Path
import org.catrobat.paintroid.command.Command
import org.catrobat.paintroid.contract.LayerContracts

class PathCommand(val paint: Paint, path: Path) : Command {
class PathCommand(var paint: Paint, path: Path) : Command {

var path = path; private set

Expand All @@ -40,4 +40,8 @@ class PathCommand(val paint: Paint, path: Path) : Command {
fun updatePath(newPath: Path) {
this.path = newPath
}

fun updatePaint(newPaint: Paint) {
this.paint = newPaint
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.graphics.Paint
import android.graphics.PointF
import android.graphics.RectF
import org.catrobat.paintroid.command.Command
import org.catrobat.paintroid.command.implementation.PathCommand

const val VERTEX_WIDTH = 30.0f

Expand Down Expand Up @@ -35,6 +36,14 @@ class DynamicLineToolVertex(vertexCenter: PointF?, outgoingPathCommand: Command?
)
}

fun updateOutgoingPath(updatedOutgoingPath: Command?) {
this.outgoingPathCommand = updatedOutgoingPath
}

fun updateIngoingPath(updatedIngoingPath: Command?) {
this.ingoingPathCommand = updatedIngoingPath
}

companion object {
private const val RECT_PAINT_ALPHA = 128
private const val RECT_PAINT_STROKE_WIDTH = 2f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.catrobat.paintroid.tools.implementation

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PointF
import android.util.Log
import android.view.View
Expand Down Expand Up @@ -45,8 +44,9 @@ class DynamicLineTool(
private var endCoordinate: PointF? = null
private var startCoordinateIsSet: Boolean = false
private var vertexStack: Deque<DynamicLineToolVertex> = ArrayDeque()
private var lineIsFinal: Boolean = false
private var currentPathCommand: PathCommand? = null
private var isFirstPath: Boolean = true
private var createNewVertex: Boolean = true

init {
brushToolOptionsView.setBrushChangedListener(CommonBrushChangedListener(this))
Expand All @@ -69,6 +69,21 @@ class DynamicLineTool(
super.handleDown(coordinate)
}

override fun onClickOnButton() {
Log.e(TAG, " ✓ clicked")
startCoordinateIsSet = false
currentPathCommand = null
createNewVertex = true
isFirstPath = true
}

fun onClickOnPlus() {
startCoordinate = endCoordinate?.let { copyPointF(it) }
currentPathCommand = null
createNewVertex = true
Log.e(TAG, "+ clicked")
}

override fun toolPositionCoordinates(coordinate: PointF): PointF = coordinate

override fun draw(canvas: Canvas) {
Expand All @@ -83,24 +98,14 @@ class DynamicLineTool(
}
}
}
drawShape(canvas)
}

override fun drawShape(canvas: Canvas) {
TODO("Not yet implemented")
}

override fun onClickOnButton() {
Log.e(TAG, " ✓ clicked")
startCoordinateIsSet = false
lineIsFinal = true
currentPathCommand = null
}

fun onClickOnPlus() {
startCoordinate = endCoordinate?.let { copyPointF(it) }
lineIsFinal = true
currentPathCommand = null
Log.e(TAG, "+ clicked")
vertexStack.forEach {
it.vertex?.let { vertex -> canvas.drawRect(vertex, DynamicLineToolVertex.getPaint())
}
}
}

private fun hideToolOptions() {
Expand Down Expand Up @@ -159,12 +164,28 @@ class DynamicLineTool(
coordinate ?: return false
hideToolOptions()
super.handleMove(coordinate)
undoAdjustingPath()
updateLastVertexPosition(copyPointF(coordinate))

endCoordinate = copyPointF(coordinate)
Log.e(TAG, "Startcoordinate x: " + startCoordinate!!.x.toString() + " y: " + startCoordinate!!.y.toString())
Log.e(TAG, "Endcoordinate x: " + endCoordinate!!.x.toString() + " y: " + endCoordinate!!.y.toString())
return true
}

private fun updateLastVertexPosition(newCenter: PointF) {
if (vertexStack.isNotEmpty()) {
vertexStack.last.updateVertex(newCenter)
}
}

private fun undoAdjustingPath() {
if (currentPathCommand != null) {
commandManager.undoIgnoringColorChanges()
currentPathCommand = null
}
}

override fun handleUp(coordinate: PointF?): Boolean {
coordinate ?: return false
showToolOptions()
Expand All @@ -181,24 +202,67 @@ class DynamicLineTool(
currentPathCommand = commandFactory.createPathCommand(toolPaint.paint, currentlyDrawnPath) as PathCommand
commandManager.addCommand(currentPathCommand)
}

if (createNewVertex) {
// if is first path we need two vertices
if (isFirstPath) {
var vertexCenter = startCoordinate?.let { copyPointF(it) }
var outgoingVertex = DynamicLineToolVertex(vertexCenter, currentPathCommand, null)
vertexStack.add(outgoingVertex)

vertexCenter = copyPointF(coordinate)
var ingoingVertex = DynamicLineToolVertex(vertexCenter, null, currentPathCommand)
vertexStack.add(ingoingVertex)

isFirstPath = false
} else {
// last vertex has a new outgoing path
vertexStack.last.updateOutgoingPath(currentPathCommand)
// new vertex has no outgoing, but ingoing path
var vertexCenter = copyPointF(coordinate)
var ingoingVertex = DynamicLineToolVertex(vertexCenter, null, currentPathCommand)
vertexStack.add(ingoingVertex)
}
createNewVertex = false
}
updateLastVertexPosition(coordinate)

var lenk = vertexStack

return true
}

override fun changePaintColor(color: Int) {
super.changePaintColor(color)
updatePaintColor()
brushToolOptionsView.invalidate()
}

override fun changePaintStrokeWidth(strokeWidth: Int) {
super.changePaintStrokeWidth(strokeWidth)
updatePaintStroke()
brushToolOptionsView.invalidate()
}

override fun changePaintStrokeCap(cap: Paint.Cap) {
super.changePaintStrokeCap(cap)
updatePaintStroke()
brushToolOptionsView.invalidate()
}

private fun updatePaintColor() {
if (currentPathCommand != null) {
(currentPathCommand as PathCommand).updatePaint(toolPaint.paint)
}
}

private fun updatePaintStroke() {
if (currentPathCommand != null) {
(currentPathCommand as PathCommand).updatePaint(toolPaint.paint)
commandManager.executeAllCommands()
}
}

private fun copyPointF(coordinate: PointF): PointF = PointF(coordinate.x, coordinate.y)

private fun createPath(startCoordinate: PointF?, endCoordinate: PointF): SerializablePath {
Expand Down

0 comments on commit 005263c

Please sign in to comment.