summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTom Black <[email protected]>2015-11-13 00:32:38 -0500
committerTom Black <[email protected]>2015-11-13 00:32:38 -0500
commitf620def8c2e5b876edc9236d1e1b6bca22eed796 (patch)
tree3dfb6742d91cd079324dc319a4ff5e28fc855142
parent2e9601e22e7efe53cf4c879224a830d961213edf (diff)
downloadruby2d-f620def8c2e5b876edc9236d1e1b6bca22eed796.tar.gz
ruby2d-f620def8c2e5b876edc9236d1e1b6bca22eed796.zip
Adding controller callback
-rw-r--r--ext/ruby2d/ruby2d.c12
-rw-r--r--lib/ruby2d/application.rb4
-rw-r--r--lib/ruby2d/dsl.rb4
-rw-r--r--lib/ruby2d/window.rb12
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
@@ -93,6 +93,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
*/
void update() {
@@ -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