Skip to content

Commit

Permalink
More drag-and-drop functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Aug 29, 2023
1 parent 0eacd91 commit bbfd62f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 27 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "sgine"
ThisBuild / organization := "org.sgine"
ThisBuild / version := "2.0.0b4-SNAPSHOT"
ThisBuild / version := "2.0.0b4.1-SNAPSHOT"

val scala213 = "2.13.11"

Expand All @@ -26,7 +26,7 @@ ThisBuild / scmInfo := Some(
)
)
ThisBuild / developers := List(
Developer(id="darkfrog", name="Matt Hicks", email="matt@matthicks.com", url=url("http://matthicks.com"))
Developer(id="darkfrog", name="Matt Hicks", email="matt@matthicks.com", url=url("https://matthicks.com"))
)

ThisBuild / fork := true
Expand Down
40 changes: 27 additions & 13 deletions core/src/main/scala/org/sgine/dnd/DragAndDropSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import org.sgine.{Overlay, Pointer}
import reactify._

trait DragAndDropSupport extends DimensionedSupport with PointerSupport {
private var dragging: Option[DimensionedSupport] = None
type Drag <: DimensionedSupport

private var current: Option[DropSupport] = None
private var dragging: Option[Drag] = None

private var current: Option[(DropSupport, Boolean)] = None

pointer.draggable @= true
pointer.dragged.attach { evt =>
Expand All @@ -19,30 +21,42 @@ trait DragAndDropSupport extends DimensionedSupport with PointerSupport {
Overlay.children += c
dragging = Some(c)
}
val d = dragging.get
val drag = dragging.get
val previous = current
val screen = screenOption().get
current = screen.hitsAtPointer().collectFirst {
case drop: DropSupport if drop.accepts(this) => drop
case drop: DropSupport => (drop, drop.accepts(this))
}
if (current != previous) {
previous.foreach { drop =>
drop.out(this)
previous.foreach {
case (drop, accept) =>
drop.out(this, accept)
out(drag, drop, accept)
}
current.foreach { drop =>
drop.over(this)
current.foreach {
case (drop, accept) =>
drop.over(this, accept)
over(drag, drop, accept)
}
}
if (evt.finished) {
current.foreach { drop =>
drop.receive(this)
current = None
current.foreach {
case (drop, accept) =>
drop.receive(this, accept)
receive(drag, drop, accept)
current = None
}

d.removeFromParent()
drag.removeFromParent()
dragging = None
}
}

protected def createDragComponent(): DimensionedSupport
protected def createDragComponent(): Drag

protected def out(drag: Drag, drop: DropSupport, accept: Boolean): Unit = {}

protected def over(drag: Drag, drop: DropSupport, accept: Boolean): Unit = {}

protected def receive(drag: Drag, drop: DropSupport, accept: Boolean): Unit = {}
}
6 changes: 3 additions & 3 deletions core/src/main/scala/org/sgine/dnd/DropSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import org.sgine.component.{DimensionedSupport, PointerSupport}
trait DropSupport extends DimensionedSupport with PointerSupport {
def accepts(c: DragAndDropSupport): Boolean

def over(c: DragAndDropSupport): Unit
def over(c: DragAndDropSupport, accept: Boolean): Unit

def out(c: DragAndDropSupport): Unit
def out(c: DragAndDropSupport, accept: Boolean): Unit

def receive(c: DragAndDropSupport): Unit
def receive(c: DragAndDropSupport, accept: Boolean): Unit
}
2 changes: 2 additions & 0 deletions core/src/main/scala/org/sgine/tooltip/Tooltip.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ object Tooltip extends MutableContainer[Tooltip] {
t.visible @= true
}

def hide(): Unit = active @= None

override def update(delta: Double): Unit = {
super.update(delta)

Expand Down
37 changes: 28 additions & 9 deletions core/src/test/scala/examples/DragAndDropExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,49 @@ import reactify._

object DragAndDropExample extends Example {
private object Ball extends Image("basketball.png") with DragAndDropSupport {
override type Drag = Image

center := screen.center * 0.5
middle := screen.middle

override protected def createDragComponent(): DimensionedSupport = {
override protected def createDragComponent(): Image = {
val img = new Image("basketball.png")
img.color @= Color.White.withAlpha(0.5)
img
}

override protected def out(drag: Image, drop: DropSupport, accept: Boolean): Unit = {
drag.color @= Color.White.withAlpha(0.5)
}

override protected def over(drag: Image, drop: DropSupport, accept: Boolean): Unit = if (accept) {
drag.color @= Color.White
} else {
drag.color @= Color.White.withAlpha(0.5)
}
}

private object Crate extends Image("crate.jpg") with DropSupport {
center := screen.center * 1.5
middle := screen.middle
private class Crate(accept: Boolean) extends Image("crate.jpg") with DropSupport {
override def accepts(c: DragAndDropSupport): Boolean = accept

override def accepts(c: DragAndDropSupport): Boolean = true
override def over(c: DragAndDropSupport, accept: Boolean): Unit = color @= (if (accept) Color.Blue else Color.Red)

override def over(c: DragAndDropSupport): Unit = color @= Color.Blue
override def out(c: DragAndDropSupport, accept: Boolean): Unit = color @= Color.White

override def out(c: DragAndDropSupport): Unit = color @= Color.White
override def receive(c: DragAndDropSupport, accept: Boolean): Unit = color @= (if (accept) Color.Green else Color.Red)
}

private object AcceptCrate extends Crate(accept = true) {
center := screen.center * 1.5
middle := screen.middle
}

override def receive(c: DragAndDropSupport): Unit = color @= Color.Green
private object RejectCrate extends Crate(accept = false) {
center := screen.center
middle := screen.middle
}

override protected lazy val component: Component = Container(
Ball, Crate
Ball, RejectCrate, AcceptCrate
)
}

0 comments on commit bbfd62f

Please sign in to comment.