summaryrefslogtreecommitdiffstats
path: root/src/livecode/api/input.cpp
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2019-12-02 14:32:42 +0000
committers-ol <s-ol@users.noreply.github.com>2019-12-02 14:32:42 +0000
commit81fe2250a70c892eeb195725117d566a042e5c93 (patch)
treee2c70bd1b0f9e9d9063f6ff7aa11f6fa6b8ced80 /src/livecode/api/input.cpp
parentadd basic livecode toolbar (diff)
downloadinkscape-81fe2250a70c892eeb195725117d566a042e5c93.tar.gz
inkscape-81fe2250a70c892eeb195725117d566a042e5c93.zip
add draft janet interface for livecoding
Diffstat (limited to 'src/livecode/api/input.cpp')
-rw-r--r--src/livecode/api/input.cpp113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/livecode/api/input.cpp b/src/livecode/api/input.cpp
new file mode 100644
index 000000000..0fc820bbd
--- /dev/null
+++ b/src/livecode/api/input.cpp
@@ -0,0 +1,113 @@
+#include "livecode/api/api.h"
+
+#include <sstream>
+
+namespace Inkscape {
+namespace Livecode {
+
+extern "C" Janet cfun_mouse_pos(int32_t argc, Janet *argv) {
+ janet_fixarity(argc, 0);
+ return janet_wrap_point(ctx().mouse().pos());
+}
+
+extern "C" Janet cfun_mouse_delta(int32_t argc, Janet *argv) {
+ janet_fixarity(argc, 0);
+ return janet_wrap_point(ctx().mouse().delta());
+}
+
+extern "C" Janet cfun_mouse_ui_pos(int32_t argc, Janet *argv) {
+ janet_fixarity(argc, 0);
+ return janet_wrap_point(ctx().mouse().ui_pos());
+}
+
+extern "C" Janet cfun_mouse_ui_delta(int32_t argc, Janet *argv) {
+ janet_fixarity(argc, 0);
+ return janet_wrap_point(ctx().mouse().ui_delta());
+}
+
+Janet janet_wrap_inputstate(InputState const &state) {
+ switch (state) {
+ case INPUTSTATE_NONE:
+ return janet_wrap_nil();
+ case INPUTSTATE_HOVER:
+ return janet_ckeywordv("hover");
+ case INPUTSTATE_PRESSED:
+ return janet_ckeywordv("pressed");
+ case INPUTSTATE_HELD:
+ return janet_ckeywordv("held");
+ case INPUTSTATE_RELEASED:
+ return janet_ckeywordv("released");
+ default:
+ janet_panic("unexpected event value");
+ }
+}
+
+extern "C" Janet cfun_mouse_event(int32_t argc, Janet *argv) {
+ janet_arity(argc, 0, 1);
+
+ guint button = janet_getsize(argv, 1);
+ if (button < 1 || button > 5) {
+ janet_panic("expected button to be between 1 and 5");
+ }
+
+ return janet_wrap_inputstate(ctx().mouse().event(button));
+}
+
+#define BUTTONMETHOD(name) \
+extern "C" Janet cfun_mouse_##name(int32_t argc, Janet *argv) { \
+ janet_arity(argc, 0, 1); \
+ guint button = janet_getsize(argv, 1); \
+ if (button < 1 || button > 5) { \
+ janet_panic("expected button to be between 1 and 5"); \
+ } \
+ return janet_wrap_boolean(ctx().mouse().name(button)); \
+}
+
+BUTTONMETHOD(pressed)
+BUTTONMETHOD(held)
+BUTTONMETHOD(released)
+
+const JanetReg it_cfuns[] = {
+ {
+ "mouse/pos", cfun_mouse_pos,
+ "(mouse/pos)\n\nGet the current mouse position as a geom/point."
+ },
+ {
+ "mouse/delta", cfun_mouse_delta,
+ "(mouse/pos)\n\nGet the mouse motion since last frame as a geom/point."
+ },
+ {
+ "mouse/ui-pos", cfun_mouse_ui_pos,
+ "(mouse/pos)\n\nGet the current mouse position as a geom/point, in UI space."
+ },
+ {
+ "mouse/ui-delta", cfun_mouse_ui_delta,
+ "(mouse/pos)\n\nGet the mouse motion since last frame as a geom/point, in UI space."
+ },
+ {
+ "mouse/event", cfun_mouse_event,
+ "(mouse/event [button])\n\nGet the current event/status for a mouse button.\n"
+ "Returns either nil, :pressed, :held or :released."
+ },
+ {
+ "mouse/pressed", cfun_mouse_pressed,
+ "(mouse/pressed [button])\n\nCheck whether a mouse button was pressed this frame.\n"
+ },
+ {
+ "mouse/held", cfun_mouse_held,
+ "(mouse/held [button])\n\nCheck whether a mouse button was held down this frame.\n"
+ },
+ {
+ "mouse/released", cfun_mouse_released,
+ "(mouse/released [button])\n\nCheck whether a mouse button was released this frame.\n"
+ },
+ {NULL, NULL, NULL}
+};
+
+
+void janet_lib_input(JanetTable *env) {
+ janet_cfuns(env, NULL, it_cfuns);
+}
+
+}
+}