src/util/gui.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba-util/gui.h>
7
8#define KEY_DELAY 45
9#define KEY_REPEAT 5
10
11void GUIInit(struct GUIParams* params) {
12 memset(params->inputHistory, 0, sizeof(params->inputHistory));
13 strncpy(params->currentPath, params->basePath, PATH_MAX);
14}
15
16void GUIPollInput(struct GUIParams* params, uint32_t* newInputOut, uint32_t* heldInput) {
17 uint32_t input = params->pollInput(¶ms->keyMap);
18 uint32_t newInput = 0;
19 int i;
20 for (i = 0; i < GUI_INPUT_MAX; ++i) {
21 if (input & (1 << i)) {
22 ++params->inputHistory[i];
23 } else {
24 params->inputHistory[i] = -1;
25 }
26 if (!params->inputHistory[i] || (params->inputHistory[i] >= KEY_DELAY && !(params->inputHistory[i] % KEY_REPEAT))) {
27 newInput |= (1 << i);
28 }
29 }
30 if (newInputOut) {
31 *newInputOut = newInput;
32 }
33 if (heldInput) {
34 *heldInput = input;
35 }
36}
37
38enum GUICursorState GUIPollCursor(struct GUIParams* params, unsigned* x, unsigned* y) {
39 if (!params->pollCursor) {
40 return GUI_CURSOR_NOT_PRESENT;
41 }
42 enum GUICursorState state = params->pollCursor(x, y);
43 if (params->cursorState == GUI_CURSOR_DOWN) {
44 int dragX = *x - params->cx;
45 int dragY = *y - params->cy;
46 if (dragX * dragX + dragY * dragY > 25) {
47 params->cursorState = GUI_CURSOR_DRAGGING;
48 return GUI_CURSOR_DRAGGING;
49 }
50 if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) {
51 params->cursorState = GUI_CURSOR_UP;
52 return GUI_CURSOR_CLICKED;
53 }
54 } else {
55 params->cx = *x;
56 params->cy = *y;
57 }
58 if (params->cursorState == GUI_CURSOR_DRAGGING) {
59 if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) {
60 params->cursorState = GUI_CURSOR_UP;
61 return GUI_CURSOR_UP;
62 }
63 return GUI_CURSOR_DRAGGING;
64 }
65 params->cursorState = state;
66 return params->cursorState;
67}
68
69void GUIInvalidateKeys(struct GUIParams* params) {
70 int i;
71 for (i = 0; i < GUI_INPUT_MAX; ++i) {
72 params->inputHistory[i] = 0;
73 }
74}