@@ -25,12 +25,16 @@ import javafx.beans.property.SimpleIntegerProperty
25
25
import javafx.beans.value.ChangeListener
26
26
import javafx.collections.ObservableList
27
27
import javafx.event.EventHandler
28
+ import javafx.geometry.Point2D
29
+ import javafx.geometry.Rectangle2D
28
30
import javafx.scene.Group
29
31
import javafx.scene.Node
30
32
import javafx.scene.SceneAntialiasing
31
33
import javafx.scene.SubScene
32
34
import javafx.scene.input.MouseEvent
35
+ import javafx.scene.paint.Color
33
36
import javafx.scene.robot.Robot
37
+ import javafx.scene.shape.Rectangle
34
38
import javafx.scene.transform.Rotate
35
39
import javafx.scene.transform.Scale
36
40
import java.util.concurrent.Callable
@@ -84,6 +88,10 @@ internal constructor(width: Int, height: Int,
84
88
85
89
private val updatableViews = arrayListOf<View >()
86
90
91
+ val entitySelectionRectangle: EntitySelectionRectangle by lazy {
92
+ EntitySelectionRectangle (this )
93
+ }
94
+
87
95
/* *
88
96
* If set to true, Game Scene will require calling step()
89
97
* to advance each frame.
@@ -363,6 +371,8 @@ internal constructor(width: Int, height: Int,
363
371
364
372
timer.clear()
365
373
374
+ entitySelectionRectangle.lastSelection.clear()
375
+
366
376
viewport.unbind()
367
377
gameRoot.children.clear()
368
378
uiRoot.children.clear()
@@ -461,4 +471,76 @@ class GameView(val node: Node, zIndex: Int) {
461
471
set(value) {
462
472
zProperty.value = value
463
473
}
474
+ }
475
+
476
+ class EntitySelectionRectangle (
477
+ private val gameScene : GameScene
478
+ ) : Rectangle(), View {
479
+
480
+ private val world = gameScene.gameWorld
481
+ private val input = gameScene.input
482
+
483
+ /* *
484
+ * This list is populated on [stopSelection].
485
+ */
486
+ val lastSelection: MutableList <Entity > = arrayListOf ()
487
+
488
+ private var selectionStart: Point2D = Point2D .ZERO
489
+
490
+ init {
491
+ fill = Color .web(" darkblue" , 0.4 )
492
+ stroke = Color .LIGHTBLUE
493
+ isVisible = false
494
+ }
495
+
496
+ fun startSelection () {
497
+ if (scene == null ) {
498
+ gameScene.addUINode(this )
499
+ }
500
+
501
+ selectionStart = input.mousePositionUI
502
+ translateX = selectionStart.x
503
+ translateY = selectionStart.y
504
+ width = 0.0
505
+ height = 0.0
506
+
507
+ isVisible = true
508
+ }
509
+
510
+ fun stopSelection () {
511
+ isVisible = false
512
+
513
+ lastSelection.clear()
514
+
515
+ lastSelection.addAll(
516
+ world.getEntitiesInRange(Rectangle2D (translateX, translateY, width, height))
517
+ )
518
+ }
519
+
520
+ override fun onUpdate (tpf : Double ) {
521
+ if (isVisible) {
522
+ val dx = input.mouseXUI - selectionStart.x
523
+ val dy = input.mouseYUI - selectionStart.y
524
+
525
+ if (dx > 0 ) {
526
+ width = dx
527
+ } else {
528
+ translateX = input.mouseXUI
529
+ width = - dx
530
+ }
531
+
532
+ if (dy > 0 ) {
533
+ height = dy
534
+ } else {
535
+ translateY = input.mouseYUI
536
+ height = - dy
537
+ }
538
+ }
539
+ }
540
+
541
+ override fun getNode (): Node {
542
+ return this
543
+ }
544
+
545
+ override fun dispose () { }
464
546
}
0 commit comments