mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-14 01:55:19 -04:00
Move more stuff to launcher backend
This commit is contained in:
parent
bfa6b42166
commit
4ca550539f
100
src/LBackend.c
100
src/LBackend.c
@ -96,7 +96,15 @@ void LBackend_DrawLogo(struct Bitmap* bmp, const char* title) {
|
|||||||
Launcher_DrawLogo(&logoFont, title, bmp);
|
Launcher_DrawLogo(&logoFont, title, bmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LBackend_SetScreen(struct LScreen* s) { }
|
static void OnPointerMove(void* obj, int idx);
|
||||||
|
void LBackend_SetScreen(struct LScreen* s) {
|
||||||
|
int i;
|
||||||
|
/* for hovering over active button etc */
|
||||||
|
for (i = 0; i < Pointers_Count; i++) {
|
||||||
|
OnPointerMove(s, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void LBackend_CloseScreen(struct LScreen* s) { }
|
void LBackend_CloseScreen(struct LScreen* s) { }
|
||||||
|
|
||||||
static void LBackend_LayoutDimensions(struct LWidget* w) {
|
static void LBackend_LayoutDimensions(struct LWidget* w) {
|
||||||
@ -261,17 +269,92 @@ void LBackend_Tick(void) {
|
|||||||
*#########################################################################################################################*/
|
*#########################################################################################################################*/
|
||||||
static void ReqeustRedraw(void* obj) { LBackend_Redraw(); }
|
static void ReqeustRedraw(void* obj) { LBackend_Redraw(); }
|
||||||
|
|
||||||
|
CC_NOINLINE static struct LWidget* GetWidgetAt(struct LScreen* s, int idx) {
|
||||||
|
struct LWidget* w;
|
||||||
|
int i, x = Pointers[idx].x, y = Pointers[idx].y;
|
||||||
|
|
||||||
|
for (i = 0; i < s->numWidgets; i++) {
|
||||||
|
w = s->widgets[i];
|
||||||
|
if (Gui_Contains(w->x, w->y, w->width, w->height, x, y)) return w;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static void OnPointerDown(void* obj, int idx) {
|
static void OnPointerDown(void* obj, int idx) {
|
||||||
Launcher_Active->MouseDown(Launcher_Active, idx);
|
struct LScreen* s = Launcher_Active;
|
||||||
|
struct LWidget* over;
|
||||||
|
struct LWidget* prev;
|
||||||
|
|
||||||
|
if (!s) return;
|
||||||
|
over = GetWidgetAt(s, idx);
|
||||||
|
prev = s->selectedWidget;
|
||||||
|
|
||||||
|
if (prev && over != prev) LScreen_UnselectWidget(s, idx, prev);
|
||||||
|
if (over) LScreen_SelectWidget(s, idx, over, over == prev);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void OnPointerUp(void* obj, int idx) {
|
static void OnPointerUp(void* obj, int idx) {
|
||||||
Launcher_Active->MouseUp(Launcher_Active, idx);
|
struct LScreen* s = Launcher_Active;
|
||||||
|
struct LWidget* over;
|
||||||
|
struct LWidget* prev;
|
||||||
|
|
||||||
|
if (!s) return;
|
||||||
|
over = GetWidgetAt(s, idx);
|
||||||
|
prev = s->selectedWidget;
|
||||||
|
|
||||||
|
/* if user moves mouse away, it doesn't count */
|
||||||
|
if (over != prev) {
|
||||||
|
LScreen_UnselectWidget(s, idx, prev);
|
||||||
|
} else if (over && over->OnClick) {
|
||||||
|
over->OnClick(over);
|
||||||
|
}
|
||||||
|
/* TODO eliminate this hack */
|
||||||
|
s->MouseUp(s, idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void OnPointerMove(void* obj, int idx) {
|
static void OnPointerMove(void* obj, int idx) {
|
||||||
if (!Launcher_Active) return;
|
struct LScreen* s = Launcher_Active;
|
||||||
Launcher_Active->MouseMove(Launcher_Active, idx);
|
struct LWidget* over;
|
||||||
|
struct LWidget* prev;
|
||||||
|
cc_bool overSame;
|
||||||
|
|
||||||
|
if (!s) return;
|
||||||
|
over = GetWidgetAt(s, idx);
|
||||||
|
prev = s->hoveredWidget;
|
||||||
|
overSame = prev == over;
|
||||||
|
|
||||||
|
if (prev && !overSame) {
|
||||||
|
prev->hovered = false;
|
||||||
|
s->hoveredWidget = NULL;
|
||||||
|
|
||||||
|
if (prev->OnUnhover) prev->OnUnhover(prev);
|
||||||
|
if (prev->VTABLE->MouseLeft) prev->VTABLE->MouseLeft(prev);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (over) {
|
||||||
|
over->hovered = true;
|
||||||
|
s->hoveredWidget = over;
|
||||||
|
|
||||||
|
if (over->OnHover) over->OnHover(over);
|
||||||
|
if (!over->VTABLE->MouseMove) return;
|
||||||
|
over->VTABLE->MouseMove(over, idx, overSame);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void OnKeyPress(void* obj, int c) {
|
||||||
|
struct LWidget* selected = Launcher_Active->selectedWidget;
|
||||||
|
if (!selected) return;
|
||||||
|
|
||||||
|
if (!selected->VTABLE->KeyPress) return;
|
||||||
|
selected->VTABLE->KeyPress(selected, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void OnTextChanged(void* obj, const cc_string* str) {
|
||||||
|
struct LWidget* selected = Launcher_Active->selectedWidget;
|
||||||
|
if (!selected) return;
|
||||||
|
|
||||||
|
if (!selected->VTABLE->TextChanged) return;
|
||||||
|
selected->VTABLE->TextChanged(selected, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void HookEvents(void) {
|
static void HookEvents(void) {
|
||||||
@ -279,6 +362,9 @@ static void HookEvents(void) {
|
|||||||
Event_Register_(&PointerEvents.Down, NULL, OnPointerDown);
|
Event_Register_(&PointerEvents.Down, NULL, OnPointerDown);
|
||||||
Event_Register_(&PointerEvents.Up, NULL, OnPointerUp);
|
Event_Register_(&PointerEvents.Up, NULL, OnPointerUp);
|
||||||
Event_Register_(&PointerEvents.Moved, NULL, OnPointerMove);
|
Event_Register_(&PointerEvents.Moved, NULL, OnPointerMove);
|
||||||
|
|
||||||
|
Event_Register_(&InputEvents.Press, NULL, OnKeyPress);
|
||||||
|
Event_Register_(&InputEvents.TextChanged, NULL, OnTextChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -429,7 +515,7 @@ static Rect2D caretRect, lastCaretRect;
|
|||||||
|
|
||||||
void LBackend_InputInit(struct LInput* w, int width) {
|
void LBackend_InputInit(struct LInput* w, int width) {
|
||||||
w->width = Display_ScaleX(width);
|
w->width = Display_ScaleX(width);
|
||||||
w->height = Display_ScaleY(30);
|
w->height = Display_ScaleY(LINPUT_HEIGHT);
|
||||||
w->minWidth = w->width;
|
w->minWidth = w->width;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -669,7 +755,7 @@ void LBackend_LabelDraw(struct LLabel* w) {
|
|||||||
*#########################################################################################################################*/
|
*#########################################################################################################################*/
|
||||||
void LBackend_LineInit(struct LLine* w, int width) {
|
void LBackend_LineInit(struct LLine* w, int width) {
|
||||||
w->width = Display_ScaleX(width);
|
w->width = Display_ScaleX(width);
|
||||||
w->height = Display_ScaleY(2);
|
w->height = Display_ScaleY(LLINE_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LBackend_LineDraw(struct LLine* w) {
|
void LBackend_LineDraw(struct LLine* w) {
|
||||||
|
@ -31,17 +31,6 @@ CC_NOINLINE static int LScreen_IndexOf(struct LScreen* s, void* w) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
CC_NOINLINE static struct LWidget* LScreen_WidgetAt(struct LScreen* s, int idx) {
|
|
||||||
struct LWidget* w;
|
|
||||||
int i = 0, x = Pointers[idx].x, y = Pointers[idx].y;
|
|
||||||
|
|
||||||
for (i = 0; i < s->numWidgets; i++) {
|
|
||||||
w = s->widgets[i];
|
|
||||||
if (Gui_Contains(w->x, w->y, w->width, w->height, x, y)) return w;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void LScreen_DoLayout(struct LScreen* s) {
|
static void LScreen_DoLayout(struct LScreen* s) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < s->numWidgets; i++)
|
for (i = 0; i < s->numWidgets; i++)
|
||||||
@ -55,14 +44,14 @@ static void LScreen_Tick(struct LScreen* s) {
|
|||||||
if (w && w->VTABLE->Tick) w->VTABLE->Tick(w);
|
if (w && w->VTABLE->Tick) w->VTABLE->Tick(w);
|
||||||
}
|
}
|
||||||
|
|
||||||
CC_NOINLINE static void LScreen_SelectWidget(struct LScreen* s, int idx, struct LWidget* w, cc_bool was) {
|
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, idx, was);
|
if (w->VTABLE->OnSelect) w->VTABLE->OnSelect(w, idx, was);
|
||||||
}
|
}
|
||||||
|
|
||||||
CC_NOINLINE static void LScreen_UnselectWidget(struct LScreen* s, int idx, struct LWidget* w) {
|
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;
|
||||||
@ -111,60 +100,7 @@ static void LScreen_KeyDown(struct LScreen* s, int key, cc_bool was) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LScreen_KeyPress(struct LScreen* s, char key) {
|
static void LScreen_MouseUp(struct LScreen* s, int idx) { }
|
||||||
if (!s->selectedWidget) return;
|
|
||||||
if (!s->selectedWidget->VTABLE->KeyPress) return;
|
|
||||||
s->selectedWidget->VTABLE->KeyPress(s->selectedWidget, key);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void LScreen_TextChanged(struct LScreen* s, const cc_string* str) {
|
|
||||||
if (!s->selectedWidget) return;
|
|
||||||
if (!s->selectedWidget->VTABLE->TextChanged) return;
|
|
||||||
s->selectedWidget->VTABLE->TextChanged(s->selectedWidget, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void LScreen_MouseDown(struct LScreen* s, int idx) {
|
|
||||||
struct LWidget* over = LScreen_WidgetAt(s, idx);
|
|
||||||
struct LWidget* prev = s->selectedWidget;
|
|
||||||
|
|
||||||
if (prev && over != prev) LScreen_UnselectWidget(s, idx, prev);
|
|
||||||
if (over) LScreen_SelectWidget(s, idx, over, over == prev);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void LScreen_MouseUp(struct LScreen* s, int idx) {
|
|
||||||
struct LWidget* over = LScreen_WidgetAt(s, idx);
|
|
||||||
struct LWidget* prev = s->selectedWidget;
|
|
||||||
|
|
||||||
/* if user moves mouse away, it doesn't count */
|
|
||||||
if (over != prev) {
|
|
||||||
LScreen_UnselectWidget(s, idx, prev);
|
|
||||||
} else if (over && over->OnClick) {
|
|
||||||
over->OnClick(over);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void LScreen_MouseMove(struct LScreen* s, int idx) {
|
|
||||||
struct LWidget* over = LScreen_WidgetAt(s, idx);
|
|
||||||
struct LWidget* prev = s->hoveredWidget;
|
|
||||||
cc_bool overSame = prev == over;
|
|
||||||
|
|
||||||
if (prev && !overSame) {
|
|
||||||
prev->hovered = false;
|
|
||||||
s->hoveredWidget = NULL;
|
|
||||||
|
|
||||||
if (prev->OnUnhover) prev->OnUnhover(prev);
|
|
||||||
if (prev->VTABLE->MouseLeft) prev->VTABLE->MouseLeft(prev);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (over) {
|
|
||||||
over->hovered = true;
|
|
||||||
s->hoveredWidget = over;
|
|
||||||
|
|
||||||
if (over->OnHover) over->OnHover(over);
|
|
||||||
if (!over->VTABLE->MouseMove) return;
|
|
||||||
over->VTABLE->MouseMove(over, idx, overSame);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static void LScreen_MouseWheel(struct LScreen* s, float delta) { }
|
static void LScreen_MouseWheel(struct LScreen* s, float delta) { }
|
||||||
|
|
||||||
static void LScreen_DrawBackground(struct LScreen* s, struct Bitmap* bmp) {
|
static void LScreen_DrawBackground(struct LScreen* s, struct Bitmap* bmp) {
|
||||||
@ -185,12 +121,8 @@ CC_NOINLINE static void LScreen_Reset(struct LScreen* s) {
|
|||||||
s->Layout = LScreen_DoLayout;
|
s->Layout = LScreen_DoLayout;
|
||||||
s->Tick = LScreen_Tick;
|
s->Tick = LScreen_Tick;
|
||||||
s->KeyDown = LScreen_KeyDown;
|
s->KeyDown = LScreen_KeyDown;
|
||||||
s->KeyPress = LScreen_KeyPress;
|
|
||||||
s->MouseDown = LScreen_MouseDown;
|
|
||||||
s->MouseUp = LScreen_MouseUp;
|
s->MouseUp = LScreen_MouseUp;
|
||||||
s->MouseMove = LScreen_MouseMove;
|
|
||||||
s->MouseWheel = LScreen_MouseWheel;
|
s->MouseWheel = LScreen_MouseWheel;
|
||||||
s->TextChanged = LScreen_TextChanged;
|
|
||||||
s->DrawBackground = LScreen_DrawBackground;
|
s->DrawBackground = LScreen_DrawBackground;
|
||||||
s->ResetArea = Launcher_DrawBackground;
|
s->ResetArea = Launcher_DrawBackground;
|
||||||
|
|
||||||
@ -1368,7 +1300,6 @@ 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, idx);
|
s->table.VTABLE->OnUnselect(&s->table, idx);
|
||||||
LScreen_MouseUp(s_, idx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServersScreen_SetActive(void) {
|
void ServersScreen_SetActive(void) {
|
||||||
|
@ -18,12 +18,8 @@ typedef void (*LScreen_Func)(struct LScreen* s);
|
|||||||
LScreen_Func Tick; /* Repeatedly called multiple times every second. */ \
|
LScreen_Func Tick; /* Repeatedly called multiple times every second. */ \
|
||||||
void (*DrawBackground)(struct LScreen* s, struct Bitmap* bmp); \
|
void (*DrawBackground)(struct LScreen* s, struct Bitmap* bmp); \
|
||||||
void (*KeyDown)(struct LScreen* s, int key, cc_bool wasDown); \
|
void (*KeyDown)(struct LScreen* s, int key, cc_bool wasDown); \
|
||||||
void (*KeyPress)(struct LScreen* s, char c); \
|
|
||||||
void (*MouseDown)(struct LScreen* s, int idx); \
|
|
||||||
void (*MouseUp)(struct LScreen* s, int idx); \
|
void (*MouseUp)(struct LScreen* s, int idx); \
|
||||||
void (*MouseMove)(struct LScreen* s, int idx); \
|
|
||||||
void (*MouseWheel)(struct LScreen* s, float delta); \
|
void (*MouseWheel)(struct LScreen* s, float delta); \
|
||||||
void (*TextChanged)(struct LScreen* s, const cc_string* str); \
|
|
||||||
void (*ResetArea)(struct Bitmap* bmp, int x, int y, int width, int height); \
|
void (*ResetArea)(struct Bitmap* bmp, int x, int y, int width, int height); \
|
||||||
struct LWidget* onEnterWidget; /* Default widget to auto-click when Enter is pressed. Can be NULL. */ \
|
struct LWidget* onEnterWidget; /* Default widget to auto-click when Enter is pressed. Can be NULL. */ \
|
||||||
struct LWidget* hoveredWidget; /* Widget the mouse is currently hovering over. */ \
|
struct LWidget* hoveredWidget; /* Widget the mouse is currently hovering over. */ \
|
||||||
@ -34,6 +30,9 @@ typedef void (*LScreen_Func)(struct LScreen* s);
|
|||||||
const char* title; /* Titlebar text */
|
const char* title; /* Titlebar text */
|
||||||
|
|
||||||
struct LScreen { LScreen_Layout };
|
struct LScreen { LScreen_Layout };
|
||||||
|
|
||||||
|
void LScreen_SelectWidget(struct LScreen* s, int idx, struct LWidget* w, cc_bool was);
|
||||||
|
void LScreen_UnselectWidget(struct LScreen* s, int idx, struct LWidget* w);
|
||||||
|
|
||||||
void ChooseModeScreen_SetActive(cc_bool firstTime);
|
void ChooseModeScreen_SetActive(cc_bool firstTime);
|
||||||
void ColoursScreen_SetActive(void);
|
void ColoursScreen_SetActive(void);
|
||||||
|
@ -110,6 +110,7 @@ CC_NOINLINE void LInput_ClearText(struct LInput* w);
|
|||||||
CC_NOINLINE void LInput_AppendString(struct LInput* w, const cc_string* str);
|
CC_NOINLINE void LInput_AppendString(struct LInput* w, const cc_string* str);
|
||||||
/* Sets the currently entered text to the given string */
|
/* Sets the currently entered text to the given string */
|
||||||
CC_NOINLINE void LInput_SetString(struct LInput* w, const cc_string* str);
|
CC_NOINLINE void LInput_SetString(struct LInput* w, const cc_string* str);
|
||||||
|
#define LINPUT_HEIGHT 30
|
||||||
|
|
||||||
/* Represents non-interactable text. */
|
/* Represents non-interactable text. */
|
||||||
struct LLabel {
|
struct LLabel {
|
||||||
@ -129,6 +130,7 @@ struct LLine {
|
|||||||
};
|
};
|
||||||
CC_NOINLINE void LLine_Init(struct LLine* w, int width, const struct LLayout* layouts);
|
CC_NOINLINE void LLine_Init(struct LLine* w, int width, const struct LLayout* layouts);
|
||||||
CC_NOINLINE BitmapCol LLine_GetColor(void);
|
CC_NOINLINE BitmapCol LLine_GetColor(void);
|
||||||
|
#define LLINE_HEIGHT 2
|
||||||
|
|
||||||
/* Represents a slider bar that may or may not be modifiable by the user. */
|
/* Represents a slider bar that may or may not be modifiable by the user. */
|
||||||
struct LSlider {
|
struct LSlider {
|
||||||
|
@ -42,17 +42,12 @@ static void CloseActiveScreen(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Launcher_SetScreen(struct LScreen* screen) {
|
void Launcher_SetScreen(struct LScreen* screen) {
|
||||||
int i;
|
|
||||||
CloseActiveScreen();
|
CloseActiveScreen();
|
||||||
Launcher_Active = screen;
|
Launcher_Active = screen;
|
||||||
if (!screen->numWidgets) screen->Init(screen);
|
if (!screen->numWidgets) screen->Init(screen);
|
||||||
|
|
||||||
screen->Show(screen);
|
screen->Show(screen);
|
||||||
screen->Layout(screen);
|
screen->Layout(screen);
|
||||||
/* for hovering over active button etc */
|
|
||||||
for (i = 0; i < Pointers_Count; i++) {
|
|
||||||
screen->MouseMove(screen, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
LBackend_SetScreen(screen);
|
LBackend_SetScreen(screen);
|
||||||
LBackend_Redraw();
|
LBackend_Redraw();
|
||||||
@ -187,14 +182,6 @@ static void OnInputDown(void* obj, int key, cc_bool was) {
|
|||||||
Launcher_Active->KeyDown(Launcher_Active, key, was);
|
Launcher_Active->KeyDown(Launcher_Active, key, was);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void OnKeyPress(void* obj, int c) {
|
|
||||||
Launcher_Active->KeyPress(Launcher_Active, c);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void OnTextChanged(void* obj, const cc_string* str) {
|
|
||||||
Launcher_Active->TextChanged(Launcher_Active, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void OnMouseWheel(void* obj, float delta) {
|
static void OnMouseWheel(void* obj, float delta) {
|
||||||
Launcher_Active->MouseWheel(Launcher_Active, delta);
|
Launcher_Active->MouseWheel(Launcher_Active, delta);
|
||||||
}
|
}
|
||||||
@ -208,9 +195,7 @@ static void Launcher_Init(void) {
|
|||||||
Event_Register_(&WindowEvents.StateChanged, NULL, OnResize);
|
Event_Register_(&WindowEvents.StateChanged, NULL, OnResize);
|
||||||
|
|
||||||
Event_Register_(&InputEvents.Down, NULL, OnInputDown);
|
Event_Register_(&InputEvents.Down, NULL, OnInputDown);
|
||||||
Event_Register_(&InputEvents.Press, NULL, OnKeyPress);
|
|
||||||
Event_Register_(&InputEvents.Wheel, NULL, OnMouseWheel);
|
Event_Register_(&InputEvents.Wheel, NULL, OnMouseWheel);
|
||||||
Event_Register_(&InputEvents.TextChanged, NULL, OnTextChanged);
|
|
||||||
|
|
||||||
Utils_EnsureDirectory("texpacks");
|
Utils_EnsureDirectory("texpacks");
|
||||||
Utils_EnsureDirectory("audio");
|
Utils_EnsureDirectory("audio");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user