| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | |
| 21 | #ifndef UISYS_H |
| 22 | #define UISYS_H |
| 23 | |
| 24 | ///////////////////////////////////////////////////////////////////////////// |
| 25 | // DEFINITIONS |
| 26 | |
| 27 | #define UIMSEBTN_PRESSED 1 |
| 28 | #define UIMSEBTN_RELEASED 2 |
| 29 | #define UIMSEBTN_CLICKED 3 |
| 30 | |
| 31 | #define UIKEY_PRESSED 1 |
| 32 | #define UIKEY_RELEASED 2 |
| 33 | #define UIKEY_CLICKED 3 |
| 34 | |
| 35 | // variables (OnMouseDown, etc) |
| 36 | #define UILMSEBTN 1 |
| 37 | #define UIRMSEBTN 2 |
| 38 | |
| 39 | // mouse click properties |
| 40 | #define UI_DBLCLICK_DELAY 0.60f |
| 41 | #define UI_DBLCLICK_MSEDELTA 4 |
| 42 | |
| 43 | struct tUIInitInfo { |
| 44 | int window_font; // default font for windows |
| 45 | int w, h; // width and height of screen. |
| 46 | }; |
| 47 | |
| 48 | struct tUIInput { |
| 49 | int mx, my, last_mx, last_my; |
| 50 | int b1_status, b1_last_status; |
| 51 | int b1_count; |
| 52 | int key, last_key; |
| 53 | int key_status, last_key_status; |
| 54 | float cur_time; |
| 55 | |
| 56 | bool key_first_press; // if the key was really pressed or just held down. |
| 57 | bool printscreen; |
| 58 | }; |
| 59 | |
| 60 | // user interface frame time |
| 61 | extern float UIFrameTime; |
| 62 | |
| 63 | // user input structure |
| 64 | extern tUIInput UI_input; |
| 65 | extern int UI_screen_width, UI_screen_height; |
| 66 | |
| 67 | class oeApplication; |
| 68 | |
| 69 | ////////////////////////////////////////////////////////////////////////////// |
| 70 | // MACROS |
| 71 | |
| 72 | #define ISKEYPRESSED(_k) \ |
| 73 | ((UI_input.key == (_k) && (UI_input.key_status == UIKEY_PRESSED || UI_input.key_status == UIKEY_CLICKED)) ? true \ |
| 74 | : false) |
| 75 | #define ISKEYRELEASED(_k) ((UI_input.key == (_k) && UI_input.key_status == UIKEY_RELEASED) ? true : false) |
| 76 | |
| 77 | // inlined supporter functions |
| 78 | static inline bool PT_IN_GADGET(UIWindow *wnd, UIGadget *gadget, int x, int y) { |
| 79 | int gl, gt, gr, gb; |
| 80 | gl = wnd->X() + gadget->X(); |
| 81 | gt = wnd->Y() + gadget->Y(); |
| 82 | gr = wnd->X() + gadget->X() + gadget->W(); |
| 83 | gb = wnd->Y() + gadget->Y() + gadget->H(); |
| 84 | |
| 85 | if (x >= gl && x < gr) |
| 86 | if (y >= gt && y < gb) |
| 87 | return true; |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | static inline bool PT_IN_RECT(int x, int y, int l, int t, int w, int h) { |
| 93 | if (x >= l && x < (l + w) && y >= t && y < (t + h)) |
| 94 | return true; |
| 95 | else |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | static inline int SCREEN_TO_WND_X(UIWindow *wnd, int x) { return (x - wnd->X()); } |
| 100 | |
| 101 | static inline int SCREEN_TO_WND_Y(UIWindow *wnd, int y) { return (y - wnd->Y()); } |
| 102 | |
| 103 | static inline int SCREEN_TO_GAD_X(UIGadget *gad, int x) { return (SCREEN_TO_WND_X(gad->GetWindow(), x) - gad->X()); } |
| 104 | |
| 105 | static inline int SCREEN_TO_GAD_Y(UIGadget *gad, int y) { return (SCREEN_TO_WND_Y(gad->GetWindow(), y) - gad->Y()); } |
| 106 | |
| 107 | #define LOCK_FOCUS(_gadget) (_gadget)->LockFocus(); |
| 108 | |
| 109 | #define UNLOCK_FOCUS(_gadget) (_gadget)->UnlockFocus(); |
| 110 | |
| 111 | static inline float UI_TIME() { return UI_input.cur_time; } |
| 112 | |
| 113 | ////////////////////////////////////////////////////////////////////////////// |
| 114 | // FUNCTIONS |
| 115 | |
| 116 | // call this function to initialize the UI system. pass a viewport where all UI will occur |
| 117 | void ui_Init(oeApplication *app, tUIInitInfo *init_info); |
| 118 | |
| 119 | // closes UI system. do this when setting a new surface. |
| 120 | void ui_Close(); |
| 121 | |
| 122 | // set user interface screen resolution |
| 123 | void ui_SetScreenMode(int w, int h); |
| 124 | |
| 125 | // adds a window to the ui list. it is topmost, and has focus. |
| 126 | void ui_AddWindow(UIWindow *wnd); |
| 127 | |
| 128 | // removes a window from ui list.if topmost, the next topmost window has focus. |
| 129 | void ui_RemoveAllWindows(); |
| 130 | |
| 131 | // removes a window from ui list.if topmost, the next topmost window has focus. |
| 132 | void ui_RemoveWindow(UIWindow *wnd); |
| 133 | |
| 134 | // does a ui frame given a list of windows. returns result list. |
| 135 | int ui_DoFrame(bool input = true); |
| 136 | |
| 137 | // does a ui frame and gets mouse and key information. |
| 138 | int ui_DoFrame(tUIInput *input, bool doinput = true); |
| 139 | |
| 140 | // ability to load/use mouse cursors |
| 141 | void ui_UseCursor(const char *fname); |
| 142 | |
| 143 | // hide and show cursor. effects are cumulative (returns whether the cursor was already shown or hidden.) |
| 144 | bool ui_ShowCursor(); // return value false if cursor is currently visible. |
| 145 | bool ui_HideCursor(); // return value true if cursor was already hidden |
| 146 | bool ui_IsCursorVisible(); // is the cursor visible? |
| 147 | |
| 148 | // frees ui input cache |
| 149 | void ui_Flush(); |
| 150 | |
| 151 | // does screen shot |
| 152 | void ui_DoScreenshot(); |
| 153 | |
| 154 | #endif |