diff --git a/sprites/BackgroundPattern.png b/sprites/BackgroundPattern.png new file mode 100644 index 0000000..db8c6b3 Binary files /dev/null and b/sprites/BackgroundPattern.png differ diff --git a/src/main/resources/ocelot/desktop/spritesheet.png b/src/main/resources/ocelot/desktop/spritesheet.png index 59cf9f8..09c0066 100644 Binary files a/src/main/resources/ocelot/desktop/spritesheet.png and b/src/main/resources/ocelot/desktop/spritesheet.png differ diff --git a/src/main/resources/ocelot/desktop/spritesheet.txt b/src/main/resources/ocelot/desktop/spritesheet.txt index 026c94e..ce7a45c 100644 --- a/src/main/resources/ocelot/desktop/spritesheet.txt +++ b/src/main/resources/ocelot/desktop/spritesheet.txt @@ -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 diff --git a/src/main/scala/ocelot/desktop/ui/widget/WorkspaceView.scala b/src/main/scala/ocelot/desktop/ui/widget/WorkspaceView.scala index 9436c83..e42fc1f 100644 --- a/src/main/scala/ocelot/desktop/ui/widget/WorkspaceView.scala +++ b/src/main/scala/ocelot/desktop/ui/widget/WorkspaceView.scala @@ -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))