Launcher: Eliminate all uses of Mouse_X/Y

This commit is contained in:
UnknownShadow200 2020-12-27 16:11:05 +11:00
parent 06b4595e95
commit 07b0086f9e
6 changed files with 45 additions and 42 deletions

View File

@ -97,6 +97,7 @@ void Event_UnregisterAll(void) {
InputEvents.Up.Count = 0; InputEvents.Up.Count = 0;
InputEvents.Wheel.Count = 0; InputEvents.Wheel.Count = 0;
InputEvents.TextChanged.Count = 0; InputEvents.TextChanged.Count = 0;
InputEvents.BindChanged.Count = 0;
PointerEvents.Moved.Count = 0; PointerEvents.Moved.Count = 0;
PointerEvents.Down.Count = 0; PointerEvents.Down.Count = 0;

View File

@ -171,7 +171,8 @@ CC_VAR extern struct _InputEventsList {
struct Event_Input Down; /* Key or button is pressed. Arg is a member of Key enumeration */ struct Event_Input Down; /* Key or button is pressed. Arg is a member of Key enumeration */
struct Event_Int Up; /* Key or button is released. Arg is a member of Key enumeration */ struct Event_Int Up; /* Key or button is released. Arg is a member of Key enumeration */
struct Event_Float Wheel; /* Mouse wheel is moved/scrolled (Arg is wheel delta) */ struct Event_Float Wheel; /* Mouse wheel is moved/scrolled (Arg is wheel delta) */
struct Event_String TextChanged; /* HTML text input changed */ struct Event_String TextChanged; /* Text in the on-screen input keyboard changed (for Mobile) */
struct Event_Int BindChanged; /* Key binding changed. Arg is a member of KeyBind enumeration*/
} InputEvents; } InputEvents;
CC_VAR extern struct _PointerEventsList { CC_VAR extern struct _PointerEventsList {

View File

@ -358,6 +358,7 @@ static void KeyBind_Save(void) {
void KeyBind_Set(KeyBind binding, int key) { void KeyBind_Set(KeyBind binding, int key) {
KeyBinds[binding] = key; KeyBinds[binding] = key;
KeyBind_Save(); KeyBind_Save();
Event_RaiseInt(&InputEvents.BindChanged, binding);
} }
/* Initialises and loads key bindings from options */ /* Initialises and loads key bindings from options */

View File

@ -59,18 +59,18 @@ static void LScreen_Tick(struct LScreen* s) {
static void LScreen_HoverWidget(struct LScreen* s, struct LWidget* w) { } static void LScreen_HoverWidget(struct LScreen* s, struct LWidget* w) { }
static void LScreen_UnhoverWidget(struct LScreen* s, struct LWidget* w) { } static void LScreen_UnhoverWidget(struct LScreen* s, struct LWidget* w) { }
CC_NOINLINE static void LScreen_SelectWidget(struct LScreen* s, struct LWidget* w, cc_bool was) { CC_NOINLINE static void LScreen_SelectWidget(struct LScreen* s, int idx, struct LWidget* w, cc_bool was) {
if (!w) return; if (!w) return;
w->selected = true; w->selected = true;
s->selectedWidget = w; s->selectedWidget = w;
if (w->VTABLE->OnSelect) w->VTABLE->OnSelect(w, was); if (w->VTABLE->OnSelect) w->VTABLE->OnSelect(w, idx, was);
} }
CC_NOINLINE static void LScreen_UnselectWidget(struct LScreen* s, struct LWidget* w) { CC_NOINLINE static void LScreen_UnselectWidget(struct LScreen* s, int idx, struct LWidget* w) {
if (!w) return; if (!w) return;
w->selected = false; w->selected = false;
s->selectedWidget = NULL; s->selectedWidget = NULL;
if (w->VTABLE->OnUnselect) w->VTABLE->OnUnselect(w); if (w->VTABLE->OnUnselect) w->VTABLE->OnUnselect(w, idx);
} }
static void LScreen_HandleTab(struct LScreen* s) { static void LScreen_HandleTab(struct LScreen* s) {
@ -89,8 +89,8 @@ static void LScreen_HandleTab(struct LScreen* s) {
w = s->widgets[i]; w = s->widgets[i];
if (w->hidden || !w->tabSelectable) continue; if (w->hidden || !w->tabSelectable) continue;
LScreen_UnselectWidget(s, s->selectedWidget); LScreen_UnselectWidget(s, 0, s->selectedWidget);
LScreen_SelectWidget(s, w, false); LScreen_SelectWidget(s, 0, w, false);
return; return;
} }
} }
@ -131,8 +131,8 @@ static void LScreen_MouseDown(struct LScreen* s, int idx) {
struct LWidget* over = LScreen_WidgetAt(s, idx); struct LWidget* over = LScreen_WidgetAt(s, idx);
struct LWidget* prev = s->selectedWidget; struct LWidget* prev = s->selectedWidget;
if (prev && over != prev) LScreen_UnselectWidget(s, prev); if (prev && over != prev) LScreen_UnselectWidget(s, idx, prev);
if (over) LScreen_SelectWidget(s, over, over == prev); if (over) LScreen_SelectWidget(s, idx, over, over == prev);
} }
static void LScreen_MouseUp(struct LScreen* s, int idx) { static void LScreen_MouseUp(struct LScreen* s, int idx) {
@ -141,7 +141,7 @@ static void LScreen_MouseUp(struct LScreen* s, int idx) {
/* if user moves mouse away, it doesn't count */ /* if user moves mouse away, it doesn't count */
if (over != prev) { if (over != prev) {
LScreen_UnselectWidget(s, prev); LScreen_UnselectWidget(s, idx, prev);
} else if (over && over->OnClick) { } else if (over && over->OnClick) {
over->OnClick(over, idx); over->OnClick(over, idx);
} }
@ -166,7 +166,7 @@ static void LScreen_MouseMove(struct LScreen* s, int idx, int deltaX, int deltaY
s->HoverWidget(s, over); s->HoverWidget(s, over);
if (!over->VTABLE->MouseMove) return; if (!over->VTABLE->MouseMove) return;
over->VTABLE->MouseMove(over, deltaX, deltaY, overSame); over->VTABLE->MouseMove(over, idx, deltaX, deltaY, overSame);
} }
} }
static void LScreen_MouseWheel(struct LScreen* s, float delta) { } static void LScreen_MouseWheel(struct LScreen* s, float delta) { }
@ -1307,7 +1307,7 @@ static void ServersScreen_Show(struct LScreen* s_) {
/* This is so typing on keyboard by default searchs server list */ /* This is so typing on keyboard by default searchs server list */
/* But don't do that when it would cause on-screen keyboard to show */ /* But don't do that when it would cause on-screen keyboard to show */
if (WindowInfo.SoftKeyboard) return; if (WindowInfo.SoftKeyboard) return;
LScreen_SelectWidget(s_, (struct LWidget*)&s->iptSearch, false); LScreen_SelectWidget(s_, 0, (struct LWidget*)&s->iptSearch, false);
} }
static void ServersScreen_Tick(struct LScreen* s_) { static void ServersScreen_Tick(struct LScreen* s_) {
@ -1372,7 +1372,7 @@ static void ServersScreen_KeyDown(struct LScreen* s_, int key, cc_bool was) {
static void ServersScreen_MouseUp(struct LScreen* s_, int idx) { static void ServersScreen_MouseUp(struct LScreen* s_, int idx) {
struct ServersScreen* s = (struct ServersScreen*)s_; struct ServersScreen* s = (struct ServersScreen*)s_;
s->table.VTABLE->OnUnselect(&s->table); s->table.VTABLE->OnUnselect(&s->table, idx);
LScreen_MouseUp(s_, idx); LScreen_MouseUp(s_, idx);
} }

View File

@ -170,7 +170,7 @@ static void LButton_Draw(void* widget) {
Launcher_MarkDirty(w->x, w->y, w->width, w->height); Launcher_MarkDirty(w->x, w->y, w->width, w->height);
} }
static void LButton_Hover(void* w, int x, int y, cc_bool wasOver) { static void LButton_Hover(void* w, int idx, int x, int y, cc_bool wasOver) {
/* only need to redraw when changing from unhovered to hovered */ /* only need to redraw when changing from unhovered to hovered */
if (!wasOver) LWidget_Draw(w); if (!wasOver) LWidget_Draw(w);
} }
@ -386,11 +386,11 @@ static void LInput_AdvanceCaretPos(struct LInput* w, cc_bool forwards) {
LWidget_Redraw(w); LWidget_Redraw(w);
} }
static void LInput_MoveCaretToCursor(struct LInput* w) { static void LInput_MoveCaretToCursor(struct LInput* w, int idx) {
cc_string text; char textBuffer[STRING_SIZE]; cc_string text; char textBuffer[STRING_SIZE];
int x = Pointers[idx].x, y = Pointers[idx].y;
struct DrawTextArgs args; struct DrawTextArgs args;
int i, charX, charWidth; int i, charX, charWidth;
int x = Mouse_X, y = Mouse_Y;
/* Input widget may have been selected by pressing tab */ /* Input widget may have been selected by pressing tab */
/* In which case cursor is completely outside, so ignore */ /* In which case cursor is completely outside, so ignore */
@ -418,17 +418,17 @@ static void LInput_MoveCaretToCursor(struct LInput* w) {
} }
} }
static void LInput_Select(void* widget, cc_bool wasSelected) { static void LInput_Select(void* widget, int idx, cc_bool wasSelected) {
struct LInput* w = (struct LInput*)widget; struct LInput* w = (struct LInput*)widget;
caretStart = DateTime_CurrentUTC_MS(); caretStart = DateTime_CurrentUTC_MS();
LInput_MoveCaretToCursor(w); LInput_MoveCaretToCursor(w, idx);
/* TODO: Only draw outer border */ /* TODO: Only draw outer border */
if (wasSelected) return; if (wasSelected) return;
LWidget_Draw(widget); LWidget_Draw(widget);
Window_OpenKeyboard(&w->text, w->type); Window_OpenKeyboard(&w->text, w->type);
} }
static void LInput_Unselect(void* widget) { static void LInput_Unselect(void* widget, int idx) {
caretStart = 0; caretStart = 0;
/* TODO: Only draw outer border */ /* TODO: Only draw outer border */
LWidget_Draw(widget); LWidget_Draw(widget);
@ -994,13 +994,13 @@ static void LTable_KeyDown(void* widget, int key, cc_bool was) {
LTable_SetSelectedTo(w, index); LTable_SetSelectedTo(w, index);
} }
static void LTable_MouseMove(void* widget, int deltaX, int deltaY, cc_bool wasOver) { static void LTable_MouseMove(void* widget, int idx, int deltaX, int deltaY, cc_bool wasOver) {
struct LTable* w = (struct LTable*)widget; struct LTable* w = (struct LTable*)widget;
int x = Mouse_X - w->x, y = Mouse_Y - w->y, col; int x = Pointers[idx].x - w->x, y = Pointers[idx].y - w->y, col;
if (w->draggingScrollbar) { if (w->draggingScrollbar) {
float scale = w->height / (float)w->rowsCount; float scale = w->height / (float)w->rowsCount;
int row = (int)((y - w->mouseOffset) / scale); int row = (int)((y - w->dragYOffset) / scale);
/* avoid expensive redraw when possible */ /* avoid expensive redraw when possible */
if (w->topRow == row) return; if (w->topRow == row) return;
@ -1017,8 +1017,8 @@ static void LTable_MouseMove(void* widget, int deltaX, int deltaY, cc_bool wasOv
} }
} }
static void LTable_RowsClick(struct LTable* w) { static void LTable_RowsClick(struct LTable* w, int idx) {
int mouseY = Mouse_Y - w->rowsBegY; int mouseY = Pointers[idx].y - w->rowsBegY;
int row = w->topRow + mouseY / w->rowHeight; int row = w->topRow + mouseY / w->rowHeight;
TimeMS now; TimeMS now;
@ -1035,8 +1035,8 @@ static void LTable_RowsClick(struct LTable* w) {
} }
/* Handles clicking on column headers (either resizes a column or sort rows) */ /* Handles clicking on column headers (either resizes a column or sort rows) */
static void LTable_HeadersClick(struct LTable* w) { static void LTable_HeadersClick(struct LTable* w, int idx) {
int x, i, mouseX = Mouse_X; int x, i, mouseX = Pointers[idx].x;
for (i = 0, x = w->x; i < w->numColumns; i++) { for (i = 0, x = w->x; i < w->numColumns; i++) {
/* clicked on gridline, begin dragging */ /* clicked on gridline, begin dragging */
@ -1063,8 +1063,8 @@ static void LTable_HeadersClick(struct LTable* w) {
} }
/* Handles clicking on the scrollbar on right edge of table */ /* Handles clicking on the scrollbar on right edge of table */
static void LTable_ScrollbarClick(struct LTable* w) { static void LTable_ScrollbarClick(struct LTable* w, int idx) {
int y, height, mouseY = Mouse_Y - w->y; int y, height, mouseY = Pointers[idx].y - w->y;
LTable_GetScrollbarCoords(w, &y, &height); LTable_GetScrollbarCoords(w, &y, &height);
if (mouseY < y) { if (mouseY < y) {
@ -1072,24 +1072,24 @@ static void LTable_ScrollbarClick(struct LTable* w) {
} else if (mouseY >= y + height) { } else if (mouseY >= y + height) {
w->topRow += w->visibleRows; w->topRow += w->visibleRows;
} else { } else {
w->mouseOffset = mouseY - y; w->dragYOffset = mouseY - y;
} }
w->draggingScrollbar = true; w->draggingScrollbar = true;
LTable_ClampTopRow(w); LTable_ClampTopRow(w);
} }
static void LTable_MouseDown(void* widget, cc_bool wasSelected) { static void LTable_MouseDown(void* widget, int idx, cc_bool wasSelected) {
struct LTable* w = (struct LTable*)widget; struct LTable* w = (struct LTable*)widget;
if (Mouse_X >= WindowInfo.Width - scrollbarWidth) { if (Pointers[idx].x >= WindowInfo.Width - scrollbarWidth) {
LTable_ScrollbarClick(w); LTable_ScrollbarClick(w, idx);
w->_lastRow = -1; w->_lastRow = -1;
} else if (Mouse_Y < w->rowsBegY) { } else if (Pointers[idx].y < w->rowsBegY) {
LTable_HeadersClick(w); LTable_HeadersClick(w, idx);
w->_lastRow = -1; w->_lastRow = -1;
} else { } else {
LTable_RowsClick(w); LTable_RowsClick(w, idx);
} }
LWidget_Draw(w); LWidget_Draw(w);
} }
@ -1103,11 +1103,11 @@ static void LTable_MouseWheel(void* widget, float delta) {
} }
/* Stops an in-progress dragging of resizing column. */ /* Stops an in-progress dragging of resizing column. */
static void LTable_StopDragging(void* widget) { static void LTable_StopDragging(void* widget, int idx) {
struct LTable* w = (struct LTable*)widget; struct LTable* w = (struct LTable*)widget;
w->draggingColumn = -1; w->draggingColumn = -1;
w->draggingScrollbar = false; w->draggingScrollbar = false;
w->mouseOffset = 0; w->dragYOffset = 0;
} }
void LTable_Reposition(struct LTable* w) { void LTable_Reposition(struct LTable* w) {
@ -1152,7 +1152,7 @@ void LTable_Init(struct LTable* w, struct FontDesc* rowFont) {
} }
void LTable_Reset(struct LTable* w) { void LTable_Reset(struct LTable* w) {
LTable_StopDragging(w); LTable_StopDragging(w, 0);
LTable_Reposition(w); LTable_Reposition(w);
w->topRow = 0; w->topRow = 0;

View File

@ -18,15 +18,15 @@ struct LWidgetVTABLE {
/* Called when key is pressed and this widget is selected. */ /* Called when key is pressed and this widget is selected. */
void (*KeyPress)(void* widget, char c); void (*KeyPress)(void* widget, char c);
/* Called when mouse hovers/moves over this widget. */ /* Called when mouse hovers/moves over this widget. */
void (*MouseMove)(void* widget, int deltaX, int deltaY, cc_bool wasOver); void (*MouseMove)(void* widget, int idx, int deltaX, int deltaY, cc_bool wasOver);
/* Called when mouse moves away from this widget. */ /* Called when mouse moves away from this widget. */
void (*MouseLeft)(void* widget); void (*MouseLeft)(void* widget);
/* Called when mouse clicks on this widget. */ /* Called when mouse clicks on this widget. */
/* NOTE: This function is just for general widget behaviour. */ /* NOTE: This function is just for general widget behaviour. */
/* OnClick callback is for per-widget instance behaviour. */ /* OnClick callback is for per-widget instance behaviour. */
void (*OnSelect)(void* widget, cc_bool wasSelected); void (*OnSelect)(void* widget, int idx, cc_bool wasSelected);
/* Called when mouse clicks on another widget. */ /* Called when mouse clicks on another widget. */
void (*OnUnselect)(void* widget); void (*OnUnselect)(void* widget, int idx);
/* Called when mouse wheel is scrolled and this widget is selected. */ /* Called when mouse wheel is scrolled and this widget is selected. */
void (*MouseWheel)(void* widget, float delta); void (*MouseWheel)(void* widget, float delta);
/* Called when on-screen keyboard text changed. */ /* Called when on-screen keyboard text changed. */
@ -175,7 +175,7 @@ struct LTable {
/* Index of table column currently being dragged. */ /* Index of table column currently being dragged. */
int draggingColumn; int draggingColumn;
cc_bool draggingScrollbar; /* Is scrollbar is currently being dragged */ cc_bool draggingScrollbar; /* Is scrollbar is currently being dragged */
int mouseOffset; /* Offset of mouse for scrollbar dragging */ int dragYOffset; /* Offset of mouse for scrollbar dragging */
float _wheelAcc; /* mouse wheel accumulator */ float _wheelAcc; /* mouse wheel accumulator */
int _lastRow; /* last clicked row (for doubleclick join) */ int _lastRow; /* last clicked row (for doubleclick join) */