]> git.jsancho.org Git - guile-irrlicht.git/commitdiff
make-event-receiver event-type
authorJavier Sancho <jsf@jsancho.org>
Tue, 7 Apr 2020 06:42:03 +0000 (08:42 +0200)
committerJavier Sancho <jsf@jsancho.org>
Tue, 7 Apr 2020 06:42:03 +0000 (08:42 +0200)
Makefile.am
src/device.cpp
src/event-receiver.cpp [new file with mode: 0644]
src/event-receiver.h [new file with mode: 0644]
src/guile-irrlicht.cpp

index c0dfc0a5147493407b82956ea15f64dbdbb81d41..2cd0772ad77d035fc696be3f567189d83da125d2 100644 (file)
@@ -33,6 +33,7 @@ libguile_irrlicht_la_SOURCES = \
   src/device.cpp \
   src/dimension2d.cpp \
   src/driver-types.cpp \
+  src/event-receiver.cpp \
   src/file-archive.cpp \
   src/file-system.cpp \
   src/gui-element.cpp \
index 41d6193344190efb3fc45c536a513a82308d09fd..0a4d2c686560e1814719291e6246701aec0fbd44 100644 (file)
@@ -25,6 +25,7 @@
 #include "device.h"
 #include "dimension2d.h"
 #include "driver-types.h"
+#include "event-receiver.h"
 #include "wchar.h"
 #include "wrapped.h"
 
@@ -75,7 +76,8 @@ extern "C" {
                          scm_to_uint32 (bits),
                          scm_to_bool (fullscreen),
                          scm_to_bool (stencilbuffer),
-                         scm_to_bool (vsync));
+                         scm_to_bool (vsync),
+                         scm_is_false (receiver) ? 0 : unwrap_event_receiver (receiver));
     return wrap_device (device);
   }
 
diff --git a/src/event-receiver.cpp b/src/event-receiver.cpp
new file mode 100644 (file)
index 0000000..f5d673b
--- /dev/null
@@ -0,0 +1,112 @@
+/* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine
+
+   Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
+
+   This file is part of guile-irrlicht.
+
+   guile-irrlicht is free software; you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 3 of the
+   License, or (at your option) any later version.
+
+   guile-irrlicht is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with guile-irrlicht. If not, see
+   <http://www.gnu.org/licenses/>.
+*/
+
+#include <irrlicht/irrlicht.h>
+#include <libguile.h>
+#include "event-receiver.h"
+#include "wrapped.h"
+
+extern "C" {
+
+  void
+  init_event_receiver (void)
+  {
+    init_event_receiver_type ();
+    init_event_type ();
+    scm_c_define_gsubr ("event-type", 1, 0, 0, (scm_t_subr)event_type);
+    scm_c_define_gsubr ("make-event-receiver", 1, 0, 0, (scm_t_subr)make_event_receiver);
+    scm_c_export ("event-type", "make-event-receiver", NULL);
+  }
+
+  DEFINE_WRAPPED_TYPE (irr::IEventReceiver*, "event-receiver",
+                       init_event_receiver_type, event_receiver_p,
+                       wrap_event_receiver, unwrap_event_receiver);
+
+  DEFINE_WRAPPED_TYPE (irr::SEvent*, "event",
+                       init_event_type, event_p,
+                       wrap_event, unwrap_event);
+
+  SCM
+  event_type (SCM event)
+  {
+    return scm_from_event_type (unwrap_event (event)->EventType);
+  }
+
+  SCM
+  make_event_receiver (SCM proc_on_event)
+  {
+    class CustomReceiver : public irr::IEventReceiver
+    {
+      SCM scm_on_event;
+
+    public:
+      CustomReceiver (SCM on_event)
+      {
+        scm_on_event = on_event;
+      }
+
+      virtual bool OnEvent (const irr::SEvent& event)
+      {
+        return scm_to_bool (scm_call_1 (scm_on_event, wrap_event ((irr::SEvent*)&event)));
+      }
+    };
+
+    CustomReceiver* receiver = new CustomReceiver (proc_on_event);
+    return wrap_event_receiver (receiver);
+  }
+
+  SCM
+  scm_from_event_type (irr::EEVENT_TYPE event_type)
+  {
+    switch (event_type)
+      {
+      case irr::EET_GUI_EVENT:
+        return scm_from_utf8_symbol ("gui-event");
+        break;
+
+      case irr::EET_MOUSE_INPUT_EVENT:
+        return scm_from_utf8_symbol ("mouse-input-event");
+        break;
+
+      case irr::EET_KEY_INPUT_EVENT:
+        return scm_from_utf8_symbol ("key-input-event");
+        break;
+
+      case irr::EET_JOYSTICK_INPUT_EVENT:
+        return scm_from_utf8_symbol ("joystick-input-event");
+        break;
+
+      case irr::EET_LOG_TEXT_EVENT:
+        return scm_from_utf8_symbol ("log-text-event");
+        break;
+
+      case irr::EET_USER_EVENT:
+        return scm_from_utf8_symbol ("user-event");
+        break;
+
+      default:
+        SCM type = scm_from_uint (event_type);
+        scm_error (scm_arg_type_key, NULL, "Wrong event type: ~S",
+                   scm_list_1 (type), scm_list_1 (type));
+      }
+  }
+
+}
diff --git a/src/event-receiver.h b/src/event-receiver.h
new file mode 100644 (file)
index 0000000..3b1cd72
--- /dev/null
@@ -0,0 +1,51 @@
+/* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine
+
+   Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
+
+   This file is part of guile-irrlicht.
+
+   guile-irrlicht is free software; you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 3 of the
+   License, or (at your option) any later version.
+
+   guile-irrlicht is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with guile-irrlicht. If not, see
+   <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __GUILE_IRRLICHT_EVENT_RECEIVER_H_INCLUDED__
+#define __GUILE_IRRLICHT_EVENT_RECEIVER_H_INCLUDED__
+
+#include <irrlicht/irrlicht.h>
+#include <libguile.h>
+#include "wrapped.h"
+
+extern "C" {
+
+  void
+  init_event_receiver (void);
+
+  DECLARE_WRAPPED_TYPE (irr::IEventReceiver*, init_event_receiver_type,
+                        event_receiver_p, wrap_event_receiver, unwrap_event_receiver);
+
+  DECLARE_WRAPPED_TYPE (irr::SEvent*, init_event_type,
+                        event_p, wrap_event, unwrap_event);
+
+  SCM
+  event_type (SCM event);
+
+  SCM
+  make_event_receiver (SCM proc_on_event);
+
+  SCM
+  scm_from_event_type (irr::EEVENT_TYPE event_type);
+
+}
+
+#endif
index 35e27f108879944c2120031b8c3721d2d55a0c67..f81cff7c73730aeba74dc477fd5c8496da62b8f9 100644 (file)
@@ -27,6 +27,7 @@
 #include "camera-scene-node.h"
 #include "cursor-control.h"
 #include "device.h"
+#include "event-receiver.h"
 #include "file-archive.h"
 #include "file-system.h"
 #include "gui-element.h"
@@ -59,6 +60,7 @@ extern "C" {
     init_camera_scene_node ();
     init_cursor_control ();
     init_device ();
+    init_event_receiver ();
     init_file_archive ();
     init_file_system ();
     init_gui_element ();