summaryrefslogtreecommitdiffhomepage
path: root/lib
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 /lib
parent193315380ed9c714e5f78b7d1b1224cfc68ccaaa (diff)
downloadruby2d-10d9acbe39a6e649e8752c3d06d1157d43b6e73a.tar.gz
ruby2d-10d9acbe39a6e649e8752c3d06d1157d43b6e73a.zip
Add mouse events, introduce event structs
Diffstat (limited to 'lib')
-rw-r--r--lib/ruby2d/application.rb4
-rw-r--r--lib/ruby2d/dsl.rb4
-rw-r--r--lib/ruby2d/window.rb248
3 files changed, 134 insertions, 122 deletions
diff --git a/lib/ruby2d/application.rb b/lib/ruby2d/application.rb
index 8de526d..a63b00d 100644
--- a/lib/ruby2d/application.rb
+++ b/lib/ruby2d/application.rb
@@ -12,8 +12,8 @@ module Ruby2D::Application
@@window.set(opts)
end
- def on(args = {}, &proc)
- @@window.on(args, &proc)
+ def on(event, &proc)
+ @@window.on(event, &proc)
end
def on_key(&proc)
diff --git a/lib/ruby2d/dsl.rb b/lib/ruby2d/dsl.rb
index 910c100..99b44af 100644
--- a/lib/ruby2d/dsl.rb
+++ b/lib/ruby2d/dsl.rb
@@ -9,8 +9,8 @@ module Ruby2D::DSL
Application.set(opts)
end
- def on(args = {}, &proc)
- Application.on(args, &proc)
+ def on(event, &proc)
+ Application.on(event, &proc)
end
def on_key(&proc)
diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb
index 5bc68b1..4ae1d6e 100644
--- a/lib/ruby2d/window.rb
+++ b/lib/ruby2d/window.rb
@@ -6,27 +6,32 @@ module Ruby2D
attr_reader :objects
attr_accessor :mouse_x, :mouse_y, :frames, :fps
+ MouseEvent = Struct.new(:type, :button, :direction, :x, :y, :delta_x, :delta_y)
+ KeyEvent = Struct.new(:type, :key)
+ ControllerEvent = Struct.new(:which, :type, :axis, :value, :button)
+
def initialize(args = {})
- @title = args[:title] || "Ruby 2D"
- @background = Color.new([0.0, 0.0, 0.0, 1.0])
- @width = args[:width] || 640
- @height = args[:height] || 480
+ @title = args[:title] || "Ruby 2D"
+ @background = Color.new([0.0, 0.0, 0.0, 1.0])
+ @width = args[:width] || 640
+ @height = args[:height] || 480
@viewport_width, @viewport_height = nil, nil
- @resizable = false
- @borderless = false
- @fullscreen = false
- @highdpi = false
- @frames = 0
- @fps_cap = args[:fps] || 60
- @fps = @fps_cap
- @vsync = args[:vsync] || true
- @mouse_x = 0; @mouse_y = 0
- @objects = []
- @keys_down, @keys, @keys_up, @mouse, @controller = {}, {}, {}, {}, {}
- @on_key_proc = Proc.new {}
- @on_controller_proc = Proc.new {}
- @update_proc = Proc.new {}
- @diagnostics = false
+ @resizable = false
+ @borderless = false
+ @fullscreen = false
+ @highdpi = false
+ @frames = 0
+ @fps_cap = args[:fps] || 60
+ @fps = @fps_cap
+ @vsync = args[:vsync] || true
+ @mouse_x, @mouse_y = 0, 0
+ @objects = []
+ @key, @key_down, @key_held, @key_up = [], [], [], []
+ @mouse, @mouse_down, @mouse_up, @mouse_scroll, @mouse_move = [], [], [], [], []
+ @controller, @controller_axis = [], []
+ @controller_button_down, @controller_button_up = [], []
+ @update_proc = Proc.new {}
+ @diagnostics = false
end
def get(sym)
@@ -100,86 +105,123 @@ module Ruby2D
true
end
- # def on(mouse: nil, key: nil, key_up: nil, key_down: nil, controller: nil, &proc)
- def on(args = {}, &proc)
- mouse = args[:mouse]
- key = args[:key]
- key_up = args[:key_up]
- key_down = args[:key_down]
- controller = args[:controller]
+ def on(event, &proc)
+ case event
+ when :key
+ @key.push(proc)
+ when :key_down
+ @key_down.push(proc)
+ when :key_held
+ @key_held.push(proc)
+ when :key_up
+ @key_up.push(proc)
+ when :mouse
+ @mouse.push(proc)
+ when :mouse_up
+ @mouse_up.push(proc)
+ when :mouse_down
+ @mouse_down.push(proc)
+ when :mouse_scroll
+ @mouse_scroll.push(proc)
+ when :mouse_move
+ @mouse_move.push(proc)
+ when :controller
+ @controller.push(proc)
+ when :controller_axis
+ @controller_axis.push(proc)
+ when :controller_button
+ @controller_button.push(proc)
+ end
+ end
+
+ def key_callback(type, key)
+ # puts "===", "type: #{type}", "key: #{key}"
- unless mouse.nil?
- reg_mouse(mouse, &proc)
- end
+ key = key.downcase
- unless key_down.nil?
- reg_key_down(key_down, &proc)
+ # All key events
+ @key.each do |e|
+ e.call(KeyEvent.new(type, key))
end
- unless key.nil?
- reg_key(key, &proc)
- end
+ case type
+ # When key is pressed, fired once
+ when :down
+ @key_down.each do |e|
+ e.call(KeyEvent.new(type, key))
+ end
+ # When key is being held down, fired every frame
+ when :held
+ @key_held.each do |e|
+ e.call(KeyEvent.new(type, key))
+ end
+ # When key released, fired once
+ when :up
+ @key_up.each do |e|
+ e.call(KeyEvent.new(type, key))
+ end
+ end
+ end
+
+ def mouse_callback(type, button, direction, x, y, delta_x, delta_y)
+ # Convert to symbols (see MRuby bug in native extension)
+ button = button.to_sym unless button == nil
+ direction = direction.to_sym unless direction == nil
- unless key_up.nil?
- reg_key_up(key_up, &proc)
+ # All mouse events
+ @mouse.each do |e|
+ e.call(MouseEvent.new(type, button, direction, x, y, delta_x, delta_y))
end
- unless controller.nil?
- reg_controller(controller, &proc)
- end
- end
-
- def mouse_callback(btn, x, y)
- if @mouse.has_key? 'any'
- @mouse[btn].call(x, y)
+ case type
+ # When mouse button pressed
+ when :down
+ @mouse_down.each do |e|
+ e.call(MouseEvent.new(type, button, nil, x, y, nil, nil))
+ end
+ # When mouse button released
+ when :up
+ @mouse_up.each do |e|
+ e.call(MouseEvent.new(type, button, nil, x, y, nil, nil))
+ end
+ # When mouse motion / movement
+ when :scroll
+ @mouse_scroll.each do |e|
+ e.call(MouseEvent.new(type, nil, direction, nil, nil, delta_x, delta_y))
+ end
+ # When mouse scrolling, wheel or trackpad
+ when :move
+ @mouse_move.each do |e|
+ e.call(MouseEvent.new(type, nil, nil, x, y, delta_x, delta_y))
+ end
+ end
+ end
+
+ def controller_callback(which, type, axis, value, button)
+ # All controller events
+ @controller.each do |e|
+ e.call(ControllerEvent.new(which, type, axis, value, button))
end
- end
-
- def on_key(&proc)
- @on_key_proc = proc
- true
- end
-
- def key_down_callback(key)
- key = key.downcase
- if @keys_down.has_key? 'any'
- @keys_down['any'].call
- end
- if @keys_down.has_key? key
- @keys_down[key].call
- end
- end
-
- def key_callback(key)
- key = key.downcase
- @on_key_proc.call(key)
- if @keys.has_key? 'any'
- @keys['any'].call
- end
- if @keys.has_key? key
- @keys[key].call
- end
- end
-
- def key_up_callback(key)
- key = key.downcase
- if @keys_up.has_key? 'any'
- @keys_up['any'].call
- end
- if @keys_up.has_key? key
- @keys_up[key].call
+
+ case type
+ # When controller axis motion, like analog sticks
+ when :axis
+ @controller_axis.each do |e|
+ e.call(ControllerEvent.new(which, type, axis, value, nil))
+ end
+ # When controller button is pressed
+ when :button_down
+ @controller_button_down.each do |e|
+ e.call(ControllerEvent.new(which, type, nil, nil, button))
+ end
+ # When controller button is released
+ when :button_up
+ @controller_button_up.each do |e|
+ e.call(ControllerEvent.new(which, type, nil, nil, button))
+ end
end
end
- def on_controller(&proc)
- @on_controller_proc = proc
- true
- end
-
- def controller_callback(which, is_axis, axis, val, is_btn, btn, pressed)
- @on_controller_proc.call(which, is_axis, axis, val, is_btn, btn, pressed)
- end
-
def update_callback
@update_proc.call
end
@@ -195,35 +237,5 @@ module Ruby2D
end
end
- # Register key string with proc
- def reg_key_down(key, &proc)
- @keys_down[key] = proc
- true
- end
-
- # Register key string with proc
- def reg_key(key, &proc)
- @keys[key] = proc
- true
- end
-
- # Register key string with proc
- def reg_key_up(key, &proc)
- @keys_up[key] = proc
- true
- end
-
- # Register mouse button string with proc
- def reg_mouse(btn, &proc)
- @mouse[btn] = proc
- true
- end
-
- # Register controller string with proc
- def reg_controller(event, &proc)
- @controller[event] = proc
- true
- end
-
end
end