summaryrefslogtreecommitdiffhomepage
path: root/test/mouse.rb
diff options
context:
space:
mode:
authorTom Black <[email protected]>2017-04-08 23:17:18 -0400
committerTom Black <[email protected]>2017-04-13 15:22:05 -0400
commit10d9acbe39a6e649e8752c3d06d1157d43b6e73a (patch)
tree0da91cd30d4e900d4b1d5c4d0e5b0d656997811e /test/mouse.rb
parent193315380ed9c714e5f78b7d1b1224cfc68ccaaa (diff)
downloadruby2d-10d9acbe39a6e649e8752c3d06d1157d43b6e73a.tar.gz
ruby2d-10d9acbe39a6e649e8752c3d06d1157d43b6e73a.zip
Add mouse events, introduce event structs
Diffstat (limited to 'test/mouse.rb')
-rw-r--r--test/mouse.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/test/mouse.rb b/test/mouse.rb
new file mode 100644
index 0000000..7daa738
--- /dev/null
+++ b/test/mouse.rb
@@ -0,0 +1,53 @@
+require 'ruby2d'
+
+set title: "Ruby 2D — Mouse", width: 400, height: 300
+
+on :mouse do |event|
+ puts event
+end
+
+s1 = Square.new(5, 5, 25, [1, 1, 0, 1]) # mouse down square
+s2 = Square.new(188, 10, 25) # mouse scroll square
+s3 = Square.new(188, 137, 25, [1, 1, 1, 1]) # mouse move delta square
+s4 = Square.new(35, 5, 10) # mouse move position square
+
+on :mouse_down do |event|
+ case event.button
+ when :left
+ s1.color = [1, 0, 0, 1]
+ when :middle
+ s1.color = [0, 0, 1, 1]
+ when :right
+ s1.color = [0, 1, 0, 1]
+ end
+ s1.x = event.x
+ s1.y = event.y
+end
+
+on :mouse_up do |event|
+ s1.color = [1, 1, 0, 1]
+ s1.x = event.x
+ s1.y = event.y
+end
+
+on :mouse_scroll do |event|
+ s2.x = s2.x + event.delta_x
+ s2.y = s2.y + event.delta_y
+end
+
+on :mouse_move do |event|
+ s3.x = 188 + event.delta_x
+ s3.y = 137 + event.delta_y
+ s4.x = event.x - 5
+ s4.y = event.y - 5
+end
+
+# Crosshairs
+Rectangle.new(199, 0, 2, 300, [1, 0, 0, 1])
+Rectangle.new(0, 149, 400, 2, [1, 0, 0, 1])
+
+on :key_down do |event|
+ close if event.key == 'escape'
+end
+
+show