summaryrefslogtreecommitdiffstats
path: root/src/livecode/api/input.cpp
blob: 0fc820bbdf6407be14e1a09f65bb22b7039000e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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);
}

}
}