summaryrefslogtreecommitdiffstats
path: root/src/livecode/input.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/livecode/input.cpp')
-rw-r--r--src/livecode/input.cpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/livecode/input.cpp b/src/livecode/input.cpp
new file mode 100644
index 000000000..4dab43fc9
--- /dev/null
+++ b/src/livecode/input.cpp
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Input state keeper for the livecoding tool
+ *
+ * Authors:
+ * Sol Bekic <s+inkscape@s-ol.nu>
+ * Copyright (C) 2019 Authors
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ */
+
+#include "livecode/input.h"
+#include "livecode/api.h"
+#include "desktop.h"
+
+namespace Inkscape {
+namespace Livecode {
+
+
+Mouse::Mouse(API &api)
+ : api(api)
+{
+}
+
+void Mouse::finish_frame()
+{
+ prev_mask = last_mask;
+ prev_pos = last_pos;
+}
+
+void Mouse::push_event(GdkEvent *event)
+{
+ switch (event->type) {
+ case GDK_BUTTON_PRESS: {
+ last_mask |= 1 << event->button.button - 1;
+ break;
+ }
+ case GDK_BUTTON_RELEASE: {
+ last_mask &= ~(1 << event->button.button - 1);
+ break;
+ }
+ case GDK_MOTION_NOTIFY: {
+ last_pos = Geom::Point(event->motion.x, event->motion.y) * api.dt2ui();
+ break;
+ }
+ default:
+ break;
+ }
+}
+
+InputState Mouse::event(guint button) const {
+ guint const flag = 1 << button - 1;
+ bool last = last_mask & flag;
+ bool prev = prev_mask & flag;
+
+ if (last && prev) return INPUTSTATE_HELD;
+ else if (last) return INPUTSTATE_PRESSED;
+ else if (prev) return INPUTSTATE_RELEASED;
+ return INPUTSTATE_NONE;
+}
+
+bool Mouse::pressed(guint button) const {
+ guint const flag = 1 << button - 1;
+ return (last_mask & flag) && !(prev_mask & flag);
+}
+
+bool Mouse::held(guint button) const {
+ return last_mask & (1 << button -1);
+}
+
+bool Mouse::released(guint button) const {
+ guint const flag = 1 << button - 1;
+ return (prev_mask & flag) && !(last_mask & flag);
+}
+
+Geom::Point Mouse::pos() const {
+ return ui_pos() * api.ui2doc();
+}
+Geom::Point Mouse::delta() const {
+ return ui_delta() * api.ui2doc_vec();
+}
+
+Geom::Point Mouse::ui_pos() const {
+ return last_pos;
+}
+Geom::Point Mouse::ui_delta() const {
+ return last_pos - prev_pos;
+}
+
+
+}
+}
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :