summaryrefslogtreecommitdiff
path: root/Runtime/Events/InputEvent.cpp
blob: c5994efb356bd2faf8349faca51fc55485884d83 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "InputEvent.h"
#ifdef GAMELAB_EDITOR
#include "Editor/GUI/WindowUtil.h"
#endif

static Vector2f GetInputMousePosition(HWND window)
{
#if GAMELAB_EDITOR
    POINT mousePos;
    ::GetCursorPos(&mousePos);
    ::ScreenToClient(window, &mousePos);
    int offsetX = 0, offsetY = 0;
    HWND offsetWindow;
    WindowUtil::GetEditorMouseOffset(&offsetX, &offsetY, &offsetWindow);
    if (offsetWindow == window)
    {
        mousePos.x += offsetX;
        mousePos.y += offsetY;
    }
    return Vector2f(mousePos.x, mousePos.y);
#else

    //return GetInputManager().GetMousePosition();

#endif
}

InputEvent::InputEvent(UINT message, WPARAM wParam, LPARAM lParam, HWND window)
{
    static Vector2f s_LastMousePos(0.0f, 0.0f);

    isMouse = false, isKey = false;

    switch (message) {
    case WM_LBUTTONDOWN:
        type = InputEvent_MouseDown;
        isMouse = true;
        button = Mouse_LeftButton;
        clickCount = 1;
        break;
    case WM_RBUTTONDOWN:
        type = InputEvent_MouseDown;
        isMouse = true;
        button = Mouse_RightButton;
        clickCount = 1;
        break;
    case WM_MBUTTONDOWN:
        type = InputEvent_MouseDown;
        isMouse = true;
        button = Mouse_MiddleButton;
        clickCount = 1;
        break;
    case WM_LBUTTONUP:
        type = InputEvent_MouseUp;
        isMouse = true;
        button = Mouse_LeftButton;
        clickCount = 1;
        break;
    case WM_RBUTTONUP:
        type = InputEvent_MouseUp;
        isMouse = true;
        button = Mouse_RightButton;
        clickCount = 1;
        break;
    case WM_MBUTTONUP:
        type = InputEvent_MouseUp;
        isMouse = true;
        button = Mouse_MiddleButton;
        clickCount = 1;
        break;
    case WM_LBUTTONDBLCLK:
        type = InputEvent_MouseDown;
        isMouse = true;
        button = Mouse_LeftButton;
        clickCount = 2;
        break;
    case WM_RBUTTONDBLCLK:
        type = InputEvent_MouseDown;
        isMouse = true;
        button = Mouse_RightButton;
        clickCount = 2;
        break;
    case WM_MBUTTONDBLCLK:
        type = InputEvent_MouseDown;
        isMouse = true;
        button = Mouse_MiddleButton;
        clickCount = 2;
        break;
    case WM_MOUSEMOVE:
        type = InputEvent_MouseMove;
        isMouse = true;
        button = 0;
        if (wParam & MK_LBUTTON)
        {
            type = InputEvent_MouseDrag;
            button |= Mouse_LeftButton;
        }
        if (wParam & MK_RBUTTON)
        {
            type = InputEvent_MouseDrag;
            button |= Mouse_RightButton;
        }
        if (wParam & MK_MBUTTON)
        {
            type = InputEvent_MouseDrag;
            button |= Mouse_MiddleButton;
        }
        break;
    case WM_KEYUP:
        type = InputEvent_KeyUp;
        isKey = true;
        break;
    case WM_KEYDOWN:
        type = InputEvent_KeyDown;
        isKey = true;
        break;
    case WM_SYSKEYUP:
        type = InputEvent_KeyUp;
        isKey = true;
        break;
    case WM_SYSKEYDOWN:
        type = InputEvent_KeyDown;
        isKey = true;
        break;
    case WM_CHAR:
        if (!(lParam & (1 << 31))) // key is pressed
        {
            type = InputEvent_KeyDown;
            isKey = true;
        }
        break;
    case WM_MOUSEWHEEL:
        mouseDelta.y = -GET_WHEEL_DELTA_WPARAM(wParam) / float(WHEEL_DELTA) * 3.0f;
        type = InputEvent_ScrollWheel;
        isMouse = true;
        button = 0; 
        break;
    case WM_MOUSEHWHEEL:
        mouseDelta.x = GET_WHEEL_DELTA_WPARAM(wParam) / float(WHEEL_DELTA) * 3.0f;
        type = InputEvent_ScrollWheel;
        isMouse = true;
        button = 0;
        break;
    default:
        type = InputEvent_Ignore;
        break;
    }

    // Handle modifiers
    modifiers = 0;
    if (type != InputEvent_Ignore)
    {
        if (GetKeyState(VK_SHIFT) < 0)
            modifiers |= Modifier_Shift;
        if (GetKeyState(VK_CONTROL) < 0)
            modifiers |= Modifier_Control;
        if (GetKeyState(VK_MENU) < 0)
            modifiers |= Modifier_Alt;
        if (GetKeyState(VK_LWIN) < 0 || GetKeyState(VK_RWIN) < 0)
            modifiers |= Modifier_Command;
        if (GetKeyState(VK_CAPITAL) < 0)
            modifiers |= Modifier_CapsLock;
    }

    if (isMouse)
    {
        Vector2f p = GetInputMousePosition(window);
        mousePosition = p;

        pressure = 1.0f; // TODO ?

        // mouse wheel events already captured scroll distance in delta
        if (type == InputEvent_ScrollWheel && message == WM_MOUSEWHEEL)
        {
            // nothing
        }
        else
        {
            mouseDelta = mousePosition - s_LastMousePos;

            //if (type == InputEvent_MouseDrag && s_wantsMouseJumping)
            //    DoMouseJumpingTroughScreenEdges(window, mousePosition, s_LastMousePos);
            //else
                s_LastMousePos = mousePosition;

            // We get quite a lot of move/drag events that have delta of zero. Filter those
            // out.
            if ((type == InputEvent_MouseMove || type == InputEvent_MouseDrag) && mouseDelta.x == 0.0f && mouseDelta.y == 0.0f)
                type = InputEvent_Ignore;

        }
    }
    else
    {
        Vector2f p = GetInputMousePosition(window);

        button = 0;
        pressure = 0.0f;
        mouseDelta = Vector2f(0.0f, 0.0f);
        clickCount = 0;
        mousePosition = p;
    }

    // handle keyboard
    character = keycode = 0;

    if (isKey)
    {
        //if (message == WM_CHAR)
        //{
        //    character = wParam;
        //    if (character == 127 || character < ' ' && character != 13 && character != 10 && character != 9) // ignore control characters other than Enter or Tab
        //        type = InputEvent_Ignore;
        //}
        //else
        //{
        //    if (wParam == VK_CONTROL)
        //    {
        //        wParam = (lParam & (1 << 24)) ? VK_RCONTROL : VK_LCONTROL;
        //    }
        //    if (wParam == VK_MENU)
        //    {
        //        wParam = (lParam & (1 << 24)) ? VK_RMENU : VK_LMENU;
        //    }
        //    keycode = InputKeycodeFromVKEY(wParam);
        //    if (keycode == SDLK_UNKNOWN)
        //        type = InputEvent::kIgnore;
        //    if (((wParam >= VK_F1) || (wParam >= VK_PRIOR && wParam <= VK_HELP)) && (wParam < VK_OEM_1 || wParam > VK_OEM_8))
        //        modifiers |= InputEvent::kFunctionKey;
        //}

        //// The unity convention is that function keys are anything that requires special handling
        //// when dealing with a text entry field. Hence, backspace is 
        //if (keycode == SDLK_BACKSPACE)
        //    modifiers |= InputEvent::kFunctionKey;

        //// "Return" gives an ASCII-13 (Carriage return). For sanity, we turn that into ASCII-10(linefeed, \n)
        //if (character == 13)
        //    character = 10;
    }
}

void InputEvent::CastToTable(LuaBind::State& state) const
{
	lua_newtable(state);
	int table = state.GetTop();

    // "type"
	lua_pushnumber(state, type);
	lua_setfield(state, table, "type");

    // "mousePosition"
    state.PushLuaObject(mousePosition);
    lua_setfield(state, table, "mousePosition");

    if (isMouse)
    {
        // "mouseDelta" 
        state.PushLuaObject(mouseDelta);
        lua_setfield(state, table, "mouseDelta");

        // "button" 
        lua_pushnumber(state, button);
        lua_setfield(state, table, "button");

        // "clickCount" 
        lua_pushnumber(state, clickCount);
        lua_setfield(state, table, "clickCount");
    }

    if (isKey)
    {

    }

    state.SetTop(table);
}