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 "gui.h"
7
8void GUIInit(struct GUIParams* params) {
9 memset(params->inputHistory, 0, sizeof(params->inputHistory));
10 strncpy(params->currentPath, params->basePath, PATH_MAX);
11}
12
13void GUIPollInput(struct GUIParams* params, uint32_t* newInputOut, uint32_t* heldInput) {
14 uint32_t input = params->pollInput(¶ms->keyMap);
15 uint32_t newInput = 0;
16 for (int i = 0; i < GUI_INPUT_MAX; ++i) {
17 if (input & (1 << i)) {
18 ++params->inputHistory[i];
19 } else {
20 params->inputHistory[i] = -1;
21 }
22 if (!params->inputHistory[i] || (params->inputHistory[i] >= 30 && !(params->inputHistory[i] % 5))) {
23 newInput |= (1 << i);
24 }
25 }
26 if (newInputOut) {
27 *newInputOut = newInput;
28 }
29 if (heldInput) {
30 *heldInput = input;
31 }
32}
33
34enum GUICursorState GUIPollCursor(struct GUIParams* params, unsigned* x, unsigned* y) {
35 if (!params->pollCursor) {
36 return GUI_CURSOR_NOT_PRESENT;
37 }
38 enum GUICursorState state = params->pollCursor(x, y);
39 if (params->cursorState == GUI_CURSOR_DOWN) {
40 int dragX = *x - params->cx;
41 int dragY = *y - params->cy;
42 if (dragX * dragX + dragY * dragY > 25) {
43 params->cursorState = GUI_CURSOR_DRAGGING;
44 return GUI_CURSOR_DRAGGING;
45 }
46 if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) {
47 params->cursorState = GUI_CURSOR_UP;
48 return GUI_CURSOR_CLICKED;
49 }
50 } else {
51 params->cx = *x;
52 params->cy = *y;
53 }
54 if (params->cursorState == GUI_CURSOR_DRAGGING) {
55 if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) {
56 params->cursorState = GUI_CURSOR_UP;
57 return GUI_CURSOR_UP;
58 }
59 return GUI_CURSOR_DRAGGING;
60 }
61 params->cursorState = state;
62 return params->cursorState;
63}
64
65void GUIInvalidateKeys(struct GUIParams* params) {
66 for (int i = 0; i < GUI_INPUT_MAX; ++i) {
67 params->inputHistory[i] = 0;
68 }
69}