summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorlstrzebinczyk <[email protected]>2017-04-26 18:32:25 +0200
committerTom Black <[email protected]>2017-05-05 16:57:24 -0400
commit2c164654e5ebec39a3ae2c47d352349332f50ee3 (patch)
tree189a16ca51b5fdd579b23fac639aac15665896d9 /test
parent3cd7a36b88ae7cc4441417cf44eb1c44ea131f81 (diff)
downloadruby2d-2c164654e5ebec39a3ae2c47d352349332f50ee3.tar.gz
ruby2d-2c164654e5ebec39a3ae2c47d352349332f50ee3.zip
another take on z-index
Diffstat (limited to 'test')
-rw-r--r--test/z_index.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/test/z_index.rb b/test/z_index.rb
new file mode 100644
index 0000000..e3014f7
--- /dev/null
+++ b/test/z_index.rb
@@ -0,0 +1,46 @@
+require 'ruby2d'
+
+set title: "Hello z-index"
+set width: 500, height: 500
+
+class ZIndexGenerator
+ def initialize
+ @z_index = 0
+ end
+
+ def get
+ @z_index += 1
+ @z_index
+ end
+end
+
+@z_index_generator = ZIndexGenerator.new
+
+class Ruby2D::Square
+ def contains?(x, y)
+ x > @x and x < @x + @width and y > @y and y < @y + @height
+ end
+end
+
+objects = []
+objects << Square.new(50, 50, 200, "red", @z_index_generator.get)
+objects << Square.new(100, 50, 200, "blue", @z_index_generator.get)
+
+on :mouse_down do |event|
+ x = event.x
+ y = event.y
+ case event.button
+ when :left
+ # Find square that you clicked, and set it's z-index to highest one in set
+ objects.sort!{|a, b| -a.z <=> -b.z }
+ first_object = objects.find do |object|
+ object.contains?(x, y)
+ end
+ first_object.z = @z_index_generator.get if first_object
+ when :right
+ # Add new square with z-index of zero, with the middle at mouse position
+ objects << Square.new(x - 100, y - 100, 200, "random", -objects.count)
+ end
+end
+
+show