summaryrefslogtreecommitdiffhomepage
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
parent3cd7a36b88ae7cc4441417cf44eb1c44ea131f81 (diff)
downloadruby2d-2c164654e5ebec39a3ae2c47d352349332f50ee3.tar.gz
ruby2d-2c164654e5ebec39a3ae2c47d352349332f50ee3.zip
another take on z-index
-rw-r--r--lib/ruby2d/image.rb3
-rw-r--r--lib/ruby2d/quad.rb3
-rw-r--r--lib/ruby2d/rectangle.rb3
-rw-r--r--lib/ruby2d/renderable.rb8
-rw-r--r--lib/ruby2d/sprite.rb10
-rw-r--r--lib/ruby2d/square.rb3
-rw-r--r--lib/ruby2d/text.rb3
-rw-r--r--lib/ruby2d/triangle.rb3
-rw-r--r--lib/ruby2d/window.rb9
-rw-r--r--test/z_index.rb46
10 files changed, 80 insertions, 11 deletions
diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb
index 587a3c0..85ee076 100644
--- a/lib/ruby2d/image.rb
+++ b/lib/ruby2d/image.rb
@@ -7,7 +7,7 @@ module Ruby2D
attr_accessor :x, :y, :width, :height, :data
attr_reader :path, :color
- def initialize(x, y, path)
+ def initialize(x, y, path, z=0)
unless RUBY_ENGINE == 'opal'
unless File.exists? path
@@ -17,6 +17,7 @@ module Ruby2D
@type_id = 3
@x, @y, @path = x, y, path
+ @z = z
@color = Color.new([1, 1, 1, 1])
init(path)
add
diff --git a/lib/ruby2d/quad.rb b/lib/ruby2d/quad.rb
index 5175513..60994a5 100644
--- a/lib/ruby2d/quad.rb
+++ b/lib/ruby2d/quad.rb
@@ -15,9 +15,10 @@ module Ruby2D
attr_reader :color
- def initialize(x1=0, y1=0, x2=100, y2=0, x3=100, y3=100, x4=100, y4=100, c='white')
+ def initialize(x1=0, y1=0, x2=100, y2=0, x3=100, y3=100, x4=100, y4=100, c='white', z=0)
@type_id = 2
@x1, @y1, @x2, @y2, @x3, @y3, @x4, @y4 = x1, y1, x2, y2, x3, y3, x4, y4
+ @z = z
self.color = c
add
diff --git a/lib/ruby2d/rectangle.rb b/lib/ruby2d/rectangle.rb
index 14fef3f..37ff3c7 100644
--- a/lib/ruby2d/rectangle.rb
+++ b/lib/ruby2d/rectangle.rb
@@ -5,9 +5,10 @@ module Ruby2D
attr_reader :x, :y, :width, :height
- def initialize(x=0, y=0, w=200, h=100, c='white')
+ def initialize(x=0, y=0, w=200, h=100, c='white', z=0)
@type_id = 2
@x, @y, @width, @height = x, y, w, h
+ @z = z
update_coords(x, y, w, h)
self.color = c
diff --git a/lib/ruby2d/renderable.rb b/lib/ruby2d/renderable.rb
index c8fac82..888c29f 100644
--- a/lib/ruby2d/renderable.rb
+++ b/lib/ruby2d/renderable.rb
@@ -1,5 +1,13 @@
module Ruby2D
module Renderable
+ attr_reader :z
+
+ def z=(z)
+ remove
+ @z = z
+ add
+ end
+
def add
if Module.const_defined? :DSL
Application.add(self)
diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb
index d5695a1..ad8dc17 100644
--- a/lib/ruby2d/sprite.rb
+++ b/lib/ruby2d/sprite.rb
@@ -4,9 +4,10 @@ module Ruby2D
class Sprite
attr_accessor :x, :y, :clip_x, :clip_y, :clip_w, :clip_h, :data
-
- def initialize(x, y, path)
-
+ attr_reader :z
+
+ def initialize(x, y, path, z=0)
+
# unless File.exists? path
# raise Error, "Cannot find image file `#{path}`"
# end
@@ -19,7 +20,8 @@ module Ruby2D
@current_animation = nil
@current_frame = 0
@current_frame_time = 0
-
+ @z = z
+
init(path)
if Module.const_defined? :DSL
Application.add(self)
diff --git a/lib/ruby2d/square.rb b/lib/ruby2d/square.rb
index 43d6c14..66347ec 100644
--- a/lib/ruby2d/square.rb
+++ b/lib/ruby2d/square.rb
@@ -5,10 +5,11 @@ module Ruby2D
attr_reader :size
- def initialize(x=0, y=0, s=100, c='white')
+ def initialize(x=0, y=0, s=100, c='white', z=0)
@type_id = 2
@x, @y = x, y
@width = @height = @size = s
+ @z = z
update_coords(x, y, s, s)
self.color = c
diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb
index 736c8a1..b3b4529 100644
--- a/lib/ruby2d/text.rb
+++ b/lib/ruby2d/text.rb
@@ -7,7 +7,7 @@ module Ruby2D
attr_accessor :x, :y, :data
attr_reader :text, :size, :width, :height, :font, :color
- def initialize(x=0, y=0, text="Hello World!", size=20, font=nil, c="white")
+ def initialize(x=0, y=0, text="Hello World!", size=20, font=nil, c='white', z=0)
# if File.exists? font
@font = font
@@ -17,6 +17,7 @@ module Ruby2D
@type_id = 5
@x, @y, @size = x, y, size
+ @z = z
@text = text.to_s
self.color = c
init
diff --git a/lib/ruby2d/triangle.rb b/lib/ruby2d/triangle.rb
index c6bac41..cf6e24a 100644
--- a/lib/ruby2d/triangle.rb
+++ b/lib/ruby2d/triangle.rb
@@ -9,11 +9,12 @@ module Ruby2D
:x3, :y3, :c3
attr_reader :color, :type_id
- def initialize(x1=50, y1=0, x2=100, y2=100, x3=0, y3=100, c='white')
+ def initialize(x1=50, y1=0, x2=100, y2=100, x3=0, y3=100, c='white', z=0)
@type_id = 1
@x1, @y1 = x1, y1
@x2, @y2 = x2, y2
@x3, @y3 = x3, y3
+ @z = z
self.color = c
add
diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb
index 4ae1d6e..2b22efa 100644
--- a/lib/ruby2d/window.rb
+++ b/lib/ruby2d/window.rb
@@ -230,7 +230,14 @@ module Ruby2D
def add_object(o)
- @objects.push(o)
+ index = @objects.index do |object|
+ object.z > o.z
+ end
+ if index
+ @objects.insert(index, o)
+ else
+ @objects.push(o)
+ end
true
else
false
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