[orx-shapes] Add support for multiple selections

This commit is contained in:
Edwin Jakobs
2023-10-23 21:28:47 +02:00
parent c28cedbb8c
commit 9336bbfa18
2 changed files with 59 additions and 30 deletions

View File

@@ -19,6 +19,10 @@ class ContourAdjuster(var contour: ShapeContour) {
*/ */
var edgeIndices = listOf(0) var edgeIndices = listOf(0)
private var vertexWorkingSet = emptyList<Int>()
private var edgeWorkingSet = emptyList<Int>()
/** /**
* the selected vertex * the selected vertex
*/ */
@@ -27,6 +31,19 @@ class ContourAdjuster(var contour: ShapeContour) {
return ContourAdjusterVertex(this, vertexIndices.first()) return ContourAdjusterVertex(this, vertexIndices.first())
} }
val vertices: Sequence<ContourAdjusterVertex>
get() {
vertexWorkingSet = vertexIndices
return sequence {
while (vertexWorkingSet.isNotEmpty()) {
val head = vertexWorkingSet.first()
vertexWorkingSet = vertexWorkingSet.drop(1)
yield(ContourAdjusterVertex(this@ContourAdjuster, head))
}
}
}
/** /**
* the selected edge * the selected edge
*/ */
@@ -35,6 +52,18 @@ class ContourAdjuster(var contour: ShapeContour) {
return ContourAdjusterEdge(this, edgeIndices.first()) return ContourAdjusterEdge(this, edgeIndices.first())
} }
val edges: Sequence<ContourAdjusterEdge>
get() {
edgeWorkingSet = edgeIndices
return sequence {
while (edgeWorkingSet.isNotEmpty()) {
val head = edgeWorkingSet.first()
edgeWorkingSet = edgeWorkingSet.drop(1)
yield(ContourAdjusterEdge(this@ContourAdjuster, head))
}
}
}
/** /**
* select a vertex by index * select a vertex by index
*/ */
@@ -70,7 +99,7 @@ class ContourAdjuster(var contour: ShapeContour) {
fun selectVertices(predicate: (Int, ContourVertex) -> Boolean) { fun selectVertices(predicate: (Int, ContourVertex) -> Boolean) {
vertexIndices = vertexIndices =
(0 until if (contour.closed) contour.segments.size else contour.segments.size + 1).filter { index -> (0 until if (contour.closed) contour.segments.size else contour.segments.size + 1).filter { index ->
predicate(index, ContourVertex(contour, index) ) predicate(index, ContourVertex(contour, index))
} }
} }
@@ -102,13 +131,11 @@ class ContourAdjuster(var contour: ShapeContour) {
fun selectEdges(predicate: (Int, ContourEdge) -> Boolean) { fun selectEdges(predicate: (Int, ContourEdge) -> Boolean) {
vertexIndices = vertexIndices =
(0 until if (contour.closed) contour.segments.size else contour.segments.size + 1).filter { index -> (0 until if (contour.closed) contour.segments.size else contour.segments.size + 1).filter { index ->
predicate(index, ContourEdge(contour, index) ) predicate(index, ContourEdge(contour, index))
} }
} }
fun updateSelection(adjustments: List<SegmentOperation>) { fun updateSelection(adjustments: List<SegmentOperation>) {
var newVertexIndices = vertexIndices
var newEdgeIndices = edgeIndices
for (adjustment in adjustments) { for (adjustment in adjustments) {
when (adjustment) { when (adjustment) {
@@ -120,29 +147,28 @@ class ContourAdjuster(var contour: ShapeContour) {
it it
} }
} }
newVertexIndices = insert(newVertexIndices) vertexIndices = insert(vertexIndices)
newEdgeIndices = insert(newEdgeIndices) edgeIndices = insert(edgeIndices)
vertexWorkingSet = insert(vertexWorkingSet)
edgeWorkingSet = insert(edgeWorkingSet)
} }
is SegmentOperation.Remove -> { is SegmentOperation.Remove -> {
fun remove(list: List<Int>) = list.mapNotNull {
if (it in adjustment.index..<adjustment.index + adjustment.amount) {
null
} else if (it > adjustment.index) {
it - adjustment.amount
} else {
it
}
}
// TODO: handling of vertices in open contours is wrong here // TODO: handling of vertices in open contours is wrong here
newVertexIndices = newVertexIndices.mapNotNull { vertexIndices = remove(vertexIndices)
if (it in adjustment.index ..< adjustment.index+adjustment.amount) { edgeIndices = remove(edgeIndices)
null vertexWorkingSet = remove(vertexWorkingSet)
} else if (it > adjustment.index) { edgeWorkingSet = remove(edgeWorkingSet)
it - adjustment.amount
} else {
it
}
}
newEdgeIndices = newEdgeIndices.mapNotNull {
if (it in adjustment.index ..< adjustment.index+adjustment.amount) {
null
} else if (it > adjustment.index) {
it - adjustment.amount
} else {
it
}
}
} }
} }
} }

View File

@@ -1,10 +1,7 @@
import org.openrndr.application import org.openrndr.application
import org.openrndr.color.ColorRGBa import org.openrndr.color.ColorRGBa
import org.openrndr.extra.shapes.adjust.adjustContour import org.openrndr.extra.shapes.adjust.adjustContour
import org.openrndr.math.Vector2
import org.openrndr.shape.Circle import org.openrndr.shape.Circle
import kotlin.math.cos
import kotlin.math.sin
fun main() { fun main() {
application { application {
@@ -18,9 +15,15 @@ fun main() {
Circle(drawer.bounds.center, 300.0).contour Circle(drawer.bounds.center, 300.0).contour
contour = adjustContour(contour) { contour = adjustContour(contour) {
for (i in 0 until 4) { selectEdges(0, 1, 2, 3)
selectEdge(i) edges.forEachIndexed { index, it ->
edge.sub(0.2, 0.8) it.replaceWith(0.5)
// if (index == seconds.mod(4.0).toInt()) {
// it.replaceWith(0.5)
// } else {
// val v = cos(seconds) * 0.15 + 0.25
// it.sub(0.5 - v, 0.5 + v)
// }
} }
} }
drawer.stroke = ColorRGBa.RED drawer.stroke = ColorRGBa.RED