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 BackgroundPattern 0 0 304 304
BorderT 0 25 2 10 BorderB 308 25 2 8
Circle 0 0 24 24 BorderT 305 25 2 10
Computer 50 0 16 16 Circle 305 0 24 24
CornerBL 102 0 8 8 Computer 355 0 16 16
CornerBR 111 0 8 8 CornerBL 407 0 8 8
CornerTL 84 0 8 10 CornerBR 416 0 8 8
CornerTR 93 0 8 10 CornerTL 389 0 8 10
DefaultNode 25 0 24 24 CornerTR 398 0 8 10
Empty 6 25 1 1 DefaultNode 330 0 24 24
Screen 67 0 16 16 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 { class WorkspaceView extends Widget {
private val nodes = ArrayBuffer[Node](new ComputerNode, new ScreenNode) private val nodes = ArrayBuffer[Node](new ComputerNode, new ScreenNode)
private var selection: Option[Node] = None private var movingNode: Option[Node] = None
private var startMousePos = Vector2D(0, 0) private var movingCamera = false
private var startNodePos = Vector2D(0, 0) private var oldMousePos = Vector2D(0, 0)
private var offset = Vector2D(0, 0) private var offset = Vector2D(0, 0)
override val layout = new NoLayout(this) override val layout = new NoLayout(this)
@ -23,33 +23,51 @@ class WorkspaceView extends Widget {
eventHandlers += { eventHandlers += {
case event: MouseEvent => case event: MouseEvent =>
if (event.state == MouseEvent.State.Press && event.button == MouseEvent.Button.Left) if (event.state == MouseEvent.State.Press) {
selectNode(event) event.button match {
else { case MouseEvent.Button.Left => startMovingNode(event)
selection.foreach(_.clearHighlight()) case MouseEvent.Button.Middle => startMovingCamera()
selection = None 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 mousePos = UiHandler.mousePosition
val gridPos = mousePos - offset val gridPos = mousePos - offset
selection = nodes.find(_.bounds.contains(gridPos)) movingNode = nodes.find(_.bounds.contains(gridPos))
for (node <- selection) { for (node <- movingNode) {
startMousePos = mousePos oldMousePos = mousePos
startNodePos = node.position
node.highlightMoving() node.highlightMoving()
} }
} }
private def startMovingCamera(): Unit = {
val mousePos = UiHandler.mousePosition
oldMousePos = mousePos
movingCamera = true
}
override def update(): Unit = { override def update(): Unit = {
super.update() super.update()
for (node <- selection) { if (movingCamera) {
val mousePos = UiHandler.mousePosition val mousePos = UiHandler.mousePosition
val translation = mousePos - startMousePos val translation = mousePos - oldMousePos
node.position = startNodePos + translation oldMousePos = mousePos
offset += translation
}
for (node <- movingNode) {
val mousePos = UiHandler.mousePosition
val translation = mousePos - oldMousePos
oldMousePos = mousePos
node.position += translation
for (obstacle <- nodes) { for (obstacle <- nodes) {
if (obstacle != node) if (obstacle != node)
@ -69,9 +87,22 @@ class WorkspaceView extends Widget {
} }
override def draw(g: Graphics): Unit = { override def draw(g: Graphics): Unit = {
g.foreground = RGBAColor(20, 20, 20) g.save()
g.sprite = "Empty"
g.rect(position.x, position.y, size.width, size.height) 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) g.translate(offset.x + position.x, offset.y + position.y)
nodes.foreach(_.draw(g)) nodes.foreach(_.draw(g))