Moving camera

This commit is contained in:
LeshaInc 2019-08-31 23:52:26 +03:00
parent c5b4ca5c2a
commit c62d67fec0
No known key found for this signature in database
GPG Key ID: B4855290FC36DE72
4 changed files with 62 additions and 30 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -1,11 +1,12 @@
BorderB 3 25 2 8
BorderT 0 25 2 10
Circle 0 0 24 24
Computer 50 0 16 16
CornerBL 102 0 8 8
CornerBR 111 0 8 8
CornerTL 84 0 8 10
CornerTR 93 0 8 10
DefaultNode 25 0 24 24
Empty 6 25 1 1
Screen 67 0 16 16
BackgroundPattern 0 0 304 304
BorderB 308 25 2 8
BorderT 305 25 2 10
Circle 305 0 24 24
Computer 355 0 16 16
CornerBL 407 0 8 8
CornerBR 416 0 8 8
CornerTL 389 0 8 10
CornerTR 398 0 8 10
DefaultNode 330 0 24 24
Empty 311 25 1 1
Screen 372 0 16 16

View File

@ -12,9 +12,9 @@ import scala.collection.mutable.ArrayBuffer
class WorkspaceView extends Widget {
private val nodes = ArrayBuffer[Node](new ComputerNode, new ScreenNode)
private var selection: Option[Node] = None
private var startMousePos = Vector2D(0, 0)
private var startNodePos = Vector2D(0, 0)
private var movingNode: Option[Node] = None
private var movingCamera = false
private var oldMousePos = Vector2D(0, 0)
private var offset = Vector2D(0, 0)
override val layout = new NoLayout(this)
@ -23,33 +23,51 @@ class WorkspaceView extends Widget {
eventHandlers += {
case event: MouseEvent =>
if (event.state == MouseEvent.State.Press && event.button == MouseEvent.Button.Left)
selectNode(event)
else {
selection.foreach(_.clearHighlight())
selection = None
if (event.state == MouseEvent.State.Press) {
event.button match {
case MouseEvent.Button.Left => startMovingNode(event)
case MouseEvent.Button.Middle => startMovingCamera()
case _ =>
}
} else {
movingNode.foreach(_.clearHighlight())
movingNode = None
movingCamera = false
}
}
private def selectNode(event: MouseEvent): Unit = {
private def startMovingNode(event: MouseEvent): Unit = {
val mousePos = UiHandler.mousePosition
val gridPos = mousePos - offset
selection = nodes.find(_.bounds.contains(gridPos))
movingNode = nodes.find(_.bounds.contains(gridPos))
for (node <- selection) {
startMousePos = mousePos
startNodePos = node.position
for (node <- movingNode) {
oldMousePos = mousePos
node.highlightMoving()
}
}
private def startMovingCamera(): Unit = {
val mousePos = UiHandler.mousePosition
oldMousePos = mousePos
movingCamera = true
}
override def update(): Unit = {
super.update()
for (node <- selection) {
if (movingCamera) {
val mousePos = UiHandler.mousePosition
val translation = mousePos - startMousePos
node.position = startNodePos + translation
val translation = mousePos - oldMousePos
oldMousePos = mousePos
offset += translation
}
for (node <- movingNode) {
val mousePos = UiHandler.mousePosition
val translation = mousePos - oldMousePos
oldMousePos = mousePos
node.position += translation
for (obstacle <- nodes) {
if (obstacle != node)
@ -69,9 +87,22 @@ class WorkspaceView extends Widget {
}
override def draw(g: Graphics): Unit = {
g.foreground = RGBAColor(20, 20, 20)
g.sprite = "Empty"
g.rect(position.x, position.y, size.width, size.height)
g.save()
val backgroundOffsetX = if (offset.x > 0) 304f - offset.x % 304f else -offset.x % 304f
val backgroundOffsetY = if (offset.y > 0) 304f - offset.y % 304f else -offset.y % 304f
val numRepeatsX = math.ceil((size.width + backgroundOffsetX) / 304f).asInstanceOf[Int]
val numRepeatsY = math.ceil((size.height + backgroundOffsetY) / 304f).asInstanceOf[Int]
g.translate(-backgroundOffsetX, -backgroundOffsetY)
for (x <- 0 to numRepeatsX) {
for (y <- 0 to numRepeatsY) {
g.sprite("BackgroundPattern", x * 304f, y * 304f, 304f, 304f)
}
}
g.restore()
g.translate(offset.x + position.x, offset.y + position.y)
nodes.foreach(_.draw(g))