summaryrefslogtreecommitdiffhomepage
path: root/test/z_index.rb
blob: e3014f726906abeea329eee6b03fecb7778ef802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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