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 for (int i = 0; i < GUI_INPUT_MAX; ++i) {
20 if (input & (1 << i)) {
21 ++params->inputHistory[i];
22 } else {
23 params->inputHistory[i] = -1;
24 }
25 if (!params->inputHistory[i] || (params->inputHistory[i] >= KEY_DELAY && !(params->inputHistory[i] % KEY_REPEAT))) {
26 newInput |= (1 << i);
27 }
28 }
29 if (newInputOut) {
30 *newInputOut = newInput;
31 }
32 if (heldInput) {
33 *heldInput = input;
34 }
35}
36
37enum GUICursorState GUIPollCursor(struct GUIParams* params, unsigned* x, unsigned* y) {
38 if (!params->pollCursor) {
39 return GUI_CURSOR_NOT_PRESENT;
40 }
41 enum GUICursorState state = params->pollCursor(x, y);
42 if (params->cursorState == GUI_CURSOR_DOWN) {
43 int dragX = *x - params->cx;
44 int dragY = *y - params->cy;
45 if (dragX * dragX + dragY * dragY > 25) {
46 params->cursorState = GUI_CURSOR_DRAGGING;
47 return GUI_CURSOR_DRAGGING;
48 }
49 if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) {
50 params->cursorState = GUI_CURSOR_UP;
51 return GUI_CURSOR_CLICKED;
52 }
53 } else {
54 params->cx = *x;
55 params->cy = *y;
56 }
57 if (params->cursorState == GUI_CURSOR_DRAGGING) {
58 if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) {
59 params->cursorState = GUI_CURSOR_UP;
60 return GUI_CURSOR_UP;
61 }
62 return GUI_CURSOR_DRAGGING;
63 }
64 params->cursorState = state;
65 return params->cursorState;
66}
67
68void GUIInvalidateKeys(struct GUIParams* params) {
69 for (int i = 0; i < GUI_INPUT_MAX; ++i) {
70 params->inputHistory[i] = 0;
71 }
72}