Skip to content

Commit

Permalink
Merge pull request #156 from valb3r/feature/FBP-152-close-anchors
Browse files Browse the repository at this point in the history
Feature/fbp 152 close anchors
  • Loading branch information
valb3r authored Jul 12, 2020
2 parents 3dd3bf8 + fb99dfb commit 38d892c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ enum class Colors(val color: JBColor) {
SUBPROCESS_TEXT_COLOR(JBColor(Color(0x292B2D), Color(0xBBBBBB))),
INNER_TEXT_COLOR(JBColor(Color(0x292B2D), Color(0xBBBBBB))),
ARROW_COLOR(JBColor(Color(0x292B2D), Color(0xBBBBBB))),
ANCHOR_COLOR(JBColor(Color(0xFFAA00), Color(0xFFAA00))),
ANCHOR_COLOR(JBColor(Color(0xFFAA00), Color(0xC09E56))),
CLOSE_ANCHOR_COLOR(JBColor(Color(0xFFC0CB), Color(0xFFC0CB))),
DEBUG_ELEMENT_COLOR(JBColor(Color(0xFFFF00), Color(0x535353))),
WAYPOINT_COLOR(JBColor(Color(0xFF0000), Color(0xFF0000))),
MID_WAYPOINT_COLOR(JBColor(Color(0x0088FF), Color(0x0000FF))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ enum class AreaType(val nests: Boolean = false) {
EDGE,
SHAPE,
SELECTS_DRAG_TARGET,
SHAPE_ATTACHED_TO_ELEMENT,
SHAPE_THAT_NESTS(true),
PARENT_PROCESS_SHAPE(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal fun setCanvas(canvas: Canvas): Canvas {

class Canvas(private val settings: CanvasConstants) : JPanel() {
private val stateProvider = currentStateProvider()
private val closeAnchorRadius = 100.0f;

private var selectedElements: MutableSet<DiagramElementId> = mutableSetOf()
private var camera = Camera(settings.defaultCameraOrigin, Point2D.Float(settings.defaultZoomRatio, settings.defaultZoomRatio))
Expand Down Expand Up @@ -149,7 +150,6 @@ class Canvas(private val settings: CanvasConstants) : JPanel() {
}

fun attractToAnchors(ctx: ElementInteractionContext): ElementInteractionContext {
val anchors: MutableSet<AnchorDetails> = mutableSetOf()
val cameraPoint = camera.toCameraView(ctx.dragCurrent)
val dragged = ctx.draggedIds.minBy {
val bounds = areaByElement?.get(it)?.area?.bounds2D ?: Rectangle2D.Float()
Expand Down Expand Up @@ -179,10 +179,16 @@ class Canvas(private val settings: CanvasConstants) : JPanel() {
}
}

val anchors: MutableSet<AnchorDetails> = mutableSetOf()
val closeAnchors: MutableSet<Point2D.Float> = mutableSetOf()
for ((_, searchIn) in anchorsToSearchIn?.filter { it.value.index <= draggedArea.index }.orEmpty()) {
for (draggedAnchor in draggedAnchors) {
val targetAnchors = if (AreaType.SHAPE == draggedType) searchIn.anchorsForShape else searchIn.anchorsForWaypoints
for (anchor in targetAnchors) {
if (anchor.point.distance(draggedAnchor.point) < closeAnchorRadius) {
closeAnchors.add(anchor.point)
}

val attractsX = abs(draggedAnchor.point.x - anchor.point.x) < settings.anchorAttractionThreshold
val attractsY = abs(draggedAnchor.point.y - anchor.point.y) < settings.anchorAttractionThreshold

Expand All @@ -202,10 +208,11 @@ class Canvas(private val settings: CanvasConstants) : JPanel() {
val anchorY = anchors.filter { it.type == AnchorType.VERTICAL }.minBy { it.anchor.distance(it.objectAnchor) }

val selectedAnchors: AnchorHit = if (null == pointAnchor) applyOrthoAnchors(anchorX, anchorY, ctx) else applyPointAnchor(pointAnchor, ctx)
val allAnchors = selectedAnchors.copy(closeAnchors = closeAnchors.toList())

return ctx.copy(
dragCurrent = Point2D.Float(selectedAnchors.dragged.x, selectedAnchors.dragged.y),
anchorsHit = selectedAnchors
anchorsHit = allAnchors
)
}

Expand Down Expand Up @@ -241,7 +248,7 @@ class Canvas(private val settings: CanvasConstants) : JPanel() {
this.selectedElements.addAll(
elemsUnderRect(
interactionCtx.dragSelectionRect!!.toRect(),
excludeAreas = setOf(AreaType.PARENT_PROCESS_SHAPE, AreaType.SHAPE_THAT_NESTS, AreaType.SHAPE_ATTACHED_TO_ELEMENT)
excludeAreas = setOf(AreaType.PARENT_PROCESS_SHAPE, AreaType.SHAPE_THAT_NESTS, AreaType.SELECTS_DRAG_TARGET)
)
)

Expand Down Expand Up @@ -319,15 +326,15 @@ class Canvas(private val settings: CanvasConstants) : JPanel() {
val objectAnchorY = anchorY?.objectAnchor?.y ?: ctx.dragCurrent.y
anchorX?.apply { selectedAnchors[AnchorType.HORIZONTAL] = this.anchor }
anchorY?.apply { selectedAnchors[AnchorType.VERTICAL] = this.anchor }
return AnchorHit(Point2D.Float(targetX, targetY), Point2D.Float(objectAnchorX, objectAnchorY), selectedAnchors)
return AnchorHit(Point2D.Float(targetX, targetY), Point2D.Float(objectAnchorX, objectAnchorY), selectedAnchors, emptyList())
}

private fun applyPointAnchor(anchor: AnchorDetails, ctx: ElementInteractionContext): AnchorHit {
val selectedAnchors: MutableMap<AnchorType, Point2D.Float> = mutableMapOf()
val targetX = ctx.dragCurrent.x + anchor.anchor.x - anchor.objectAnchor.x
val targetY = ctx.dragCurrent.y + anchor.anchor.y - anchor.objectAnchor.y
selectedAnchors[AnchorType.POINT] = anchor.anchor
return AnchorHit(Point2D.Float(targetX, targetY), Point2D.Float(targetX, targetY), selectedAnchors)
return AnchorHit(Point2D.Float(targetX, targetY), Point2D.Float(targetX, targetY), selectedAnchors, emptyList())
}

private fun bpmnElemsUnderDragCurrent(): Map<BpmnElementId, AreaWithZindex> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fun lastRenderedState(): RenderedState? {

class DefaultBpmnProcessRenderer(val icons: IconProvider) : BpmnProcessRenderer {
private val undoRedoStartMargin = 20.0f
private val closeAnchorRadius = 2f
private val anchorRadius = 5f
private val actionsIcoSize = 15f

Expand Down Expand Up @@ -286,6 +287,8 @@ class DefaultBpmnProcessRenderer(val icons: IconProvider) : BpmnProcessRenderer
AnchorType.POINT -> canvas.drawCircle(it.value, anchorRadius, Colors.ANCHOR_COLOR.color)
}
}

anchors.closeAnchors.forEach { canvas.drawCircle(it, closeAnchorRadius, Colors.CLOSE_ANCHOR_COLOR.color) }
}

private fun isActive(elemId: DiagramElementId, state: RenderState): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class ElementInteractionContext(
val dragCurrent: Point2D.Float
)

data class AnchorHit(val dragged: Point2D.Float, val objectAnchor: Point2D.Float, val anchors: Map<AnchorType, Point2D.Float>)
data class AnchorHit(val dragged: Point2D.Float, val objectAnchor: Point2D.Float, val anchors: Map<AnchorType, Point2D.Float>, val closeAnchors: List<Point2D.Float>)

enum class AnchorType {
VERTICAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ class AnyShapeNestableIconShape(
}

override fun areaType(): AreaType {
return AreaType.SHAPE_ATTACHED_TO_ELEMENT
return AreaType.SELECTS_DRAG_TARGET
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ class CurrentStateProvider {
continue
}

updatedElementByStaticId[key] = WithParentId(event.newParentId, value.element)
if (event.propagateToXml) {
updatedElementByStaticId[key] = WithParentId(event.newParentId, value.element, event.newParentId)
} else {
updatedElementByStaticId[key] = WithParentId(event.newParentId, value.element, updatedElementByStaticId[key]?.parentIdForXml ?: event.newParentId)
}
}
}
else -> throw IllegalStateException("Can't handle event ${event.javaClass}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.valb3r.bpmn.intellij.plugin.bpmn.api.info.PropertyType
import com.valb3r.bpmn.intellij.plugin.events.BpmnParentChangedEvent
import com.valb3r.bpmn.intellij.plugin.events.DraggedToEvent
import com.valb3r.bpmn.intellij.plugin.events.StringValueUpdatedEvent
import com.valb3r.bpmn.intellij.plugin.render.lastRenderedState
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldBeLessThan
import org.amshove.kluent.shouldHaveSingleItem
Expand All @@ -32,6 +33,7 @@ internal class BoundaryEventAttachTest: BaseUiTest() {
canvas.paintComponent(graphics)
canvas.stopDragOrSelect()
canvas.paintComponent(graphics)
lastRenderedState()!!.state.currentState.elementByBpmnId[optionalBoundaryErrorEventBpmnId]!!.parentIdForXml.shouldBeEqualTo(parentProcessBpmnId)

argumentCaptor<List<Event>>().apply {
verify(fileCommitter).executeCommitAndGetHash(any(), capture(), any(), any())
Expand Down Expand Up @@ -65,6 +67,7 @@ internal class BoundaryEventAttachTest: BaseUiTest() {
canvas.paintComponent(graphics)
canvas.stopDragOrSelect()
canvas.paintComponent(graphics)
lastRenderedState()!!.state.currentState.elementByBpmnId[optionalBoundaryErrorEventBpmnId]!!.parentIdForXml.shouldBeEqualTo(subprocessBpmnId)

argumentCaptor<List<Event>>().apply {
verify(fileCommitter).executeCommitAndGetHash(any(), capture(), any(), any())
Expand Down Expand Up @@ -98,6 +101,7 @@ internal class BoundaryEventAttachTest: BaseUiTest() {
canvas.paintComponent(graphics)
canvas.stopDragOrSelect()
canvas.paintComponent(graphics)
lastRenderedState()!!.state.currentState.elementByBpmnId[optionalBoundaryErrorEventBpmnId]!!.parentIdForXml.shouldBeEqualTo(subprocessBpmnId)

argumentCaptor<List<Event>>().apply {
verify(fileCommitter).executeCommitAndGetHash(any(), capture(), any(), any())
Expand Down Expand Up @@ -130,6 +134,7 @@ internal class BoundaryEventAttachTest: BaseUiTest() {
canvas.paintComponent(graphics)
canvas.stopDragOrSelect()
canvas.paintComponent(graphics)
lastRenderedState()!!.state.currentState.elementByBpmnId[optionalBoundaryErrorEventBpmnId]!!.parentIdForXml.shouldBeEqualTo(parentProcessBpmnId)

argumentCaptor<List<Event>>().apply {
verify(fileCommitter).executeCommitAndGetHash(any(), capture(), any(), any())
Expand Down

0 comments on commit 38d892c

Please sign in to comment.