From f620def8c2e5b876edc9236d1e1b6bca22eed796 Mon Sep 17 00:00:00 2001 From: Tom Black Date: Fri, 13 Nov 2015 00:32:38 -0500 Subject: Adding controller callback --- ext/ruby2d/ruby2d.c | 12 ++++++++++++ lib/ruby2d/application.rb | 4 ++-- lib/ruby2d/dsl.rb | 4 ++-- lib/ruby2d/window.rb | 12 ++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c index ff8ae1b..0681e27 100644 --- a/ext/ruby2d/ruby2d.c +++ b/ext/ruby2d/ruby2d.c @@ -92,6 +92,17 @@ void on_key_down(const char *key) { } +/* + * Simple 2D `on_controller` input callback function + */ +void on_controller(bool is_axis, int axis, int val, bool is_btn, int btn) { + rb_funcall(self, rb_intern("controller_callback"), 5, + is_axis ? Qtrue : Qfalse, INT2NUM(axis), INT2NUM(val), + is_btn ? Qtrue : Qfalse, INT2NUM(btn) + ); +} + + /* * Simple 2D `update` callback function */ @@ -246,6 +257,7 @@ static VALUE ruby2d_show(VALUE s) { window->on_key = on_key; window->on_key_down = on_key_down; + window->on_controller = on_controller; S2D_Show(window); diff --git a/lib/ruby2d/application.rb b/lib/ruby2d/application.rb index ebef2b5..3883a88 100644 --- a/lib/ruby2d/application.rb +++ b/lib/ruby2d/application.rb @@ -12,8 +12,8 @@ module Ruby2D::Application @@window.set(opts) end - def on(mouse: nil, key: nil, &proc) - @@window.on(mouse: mouse, key: key, &proc) + def on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc) + @@window.on(mouse: mouse, key: key, key_down: key_down, controller: controller, &proc) end def add(o) diff --git a/lib/ruby2d/dsl.rb b/lib/ruby2d/dsl.rb index c5bed09..2c8db94 100644 --- a/lib/ruby2d/dsl.rb +++ b/lib/ruby2d/dsl.rb @@ -13,8 +13,8 @@ module Ruby2D::DSL Ruby2D::Application.set(opts) end - def on(mouse: nil, key: nil, &proc) - Ruby2D::Application.on(mouse: mouse, key: key, &proc) + def on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc) + Ruby2D::Application.on(mouse: mouse, key: key, key_down: key_down, controller: controller, &proc) end def update(&proc) diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb index 6011d42..200a1e9 100644 --- a/lib/ruby2d/window.rb +++ b/lib/ruby2d/window.rb @@ -13,6 +13,7 @@ module Ruby2D @objects = [] @keys = {} @keys_down = {} + @controller = {} @update_proc = Proc.new {} end @@ -88,6 +89,11 @@ module Ruby2D true end + unless controller.nil? + reg_controller(controller, &proc) + end + end + def key_callback(key) key.downcase! if @keys.has_key? key @@ -128,5 +134,11 @@ module Ruby2D end end + # Register controller string with proc + def reg_controller(event, &proc) + @controller[event] = proc + true + end + end end -- cgit v1.2.3