Mostly port ColoursScreen

This commit is contained in:
UnknownShadow200 2018-12-08 21:35:57 +11:00
parent eb8a8e0933
commit 1f982f753d
6 changed files with 137 additions and 152 deletions

View File

@ -4,6 +4,7 @@
#include "Gui.h"
#include "Game.h"
#include "Drawer2D.h"
#include "ExtMath.h"
/*########################################################################################################################*
*---------------------------------------------------------Screen base-----------------------------------------------------*
@ -249,9 +250,14 @@ static void UseModeEnhanced(void* w, int x, int y) { ChooseMode_Click(false, f
static void UseModeClassicHax(void* w, int x, int y) { ChooseMode_Click(true, true); }
static void UseModeClassic(void* w, int x, int y) { ChooseMode_Click(true, false); }
static void ChooseModeScreen_InitWidgets(struct ChooseModeScreen* s) {
static String titleText = String_FromConst("Choose game mode");
struct LScreen* s_ = (struct LScreen*)s;
static void ChooseModeScreen_Init(struct LScreen* s_) {
struct ChooseModeScreen* s = (struct ChooseModeScreen*)s_;
static String titleText = String_FromConst("Choose game mode");
s->LblHelp.Hidden = !s->FirstTime;
s->BtnBack.Hidden = s->FirstTime;
if (s->NumWidgets) return;
s->Widgets = s->_widgets;
LScreen_Label(s_, &s->LblTitle, "");
@ -299,14 +305,6 @@ static void ChooseModeScreen_Reposition(struct LScreen* s_) {
LWidget_SetLocation(&s->BtnBack, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 170);
}
static void ChooseModeScreen_Init(struct LScreen* s_) {
struct ChooseModeScreen* s = (struct ChooseModeScreen*)s_;
if (!s->NumWidgets) ChooseModeScreen_InitWidgets(s);
s->LblHelp.Hidden = !s->FirstTime;
s->BtnBack.Hidden = s->FirstTime;
}
static void ChooseModeScreen_DrawAll(struct LScreen* s_) {
int midX = Game_Width / 2, midY = Game_Height / 2;
LScreen_DrawAll(s_);
@ -343,15 +341,15 @@ CC_NOINLINE static void ColoursScreen_Update(struct ColoursScreen* s, int i, Bit
String tmp; char tmpBuffer[3];
String_InitArray(tmp, tmpBuffer);
Convert_ParseUInt8(&tmp, &col.R);
String_AppendInt(&tmp, col.R);
LInput_SetText(&s->IptColours[i + 0], &tmp, &Launcher_TextFont);
tmp.length = 0;
Convert_ParseUInt8(&tmp, &col.G);
String_AppendInt(&tmp, col.G);
LInput_SetText(&s->IptColours[i + 1], &tmp, &Launcher_TextFont);
tmp.length = 0;
Convert_ParseUInt8(&tmp, &col.B);
String_AppendInt(&tmp, col.B);
LInput_SetText(&s->IptColours[i + 2], &tmp, &Launcher_TextFont);
}
@ -363,11 +361,88 @@ CC_NOINLINE static void ColoursScreen_UpdateAll(struct ColoursScreen* s) {
ColoursScreen_Update(s, 12, Launcher_ButtonForeActiveCol);
}
static void ColoursScreen_InitWidgets(struct ColoursScreen* s) {
struct LScreen* s_ = (struct LScreen*)s;
float colourAcc;
void MouseWheelChanged(float delta) {
//int steps = Utils.AccumulateWheelDelta(ref colourAcc, delta);
//AdjustSelectedColour(steps);
}
static void ColoursScreen_TextChanged(struct LInput* w) {
struct ColoursScreen* s = &ColoursScreen_Instance;
int index = LScreen_IndexOf((struct LScreen*)s, w);
BitmapCol* col;
uint8_t r, g, b;
if (index < 3) col = &Launcher_BackgroundCol;
else if (index < 6) col = &Launcher_ButtonBorderCol;
else if (index < 9) col = &Launcher_ButtonHighlightCol;
else if (index < 12) col = &Launcher_ButtonForeCol;
else col = &Launcher_ButtonForeActiveCol;
/* if index of G input, changes to index of R input */
index = (index / 3) * 3;
if (!Convert_ParseUInt8(&s->IptColours[index + 0].Text, &r)) return;
if (!Convert_ParseUInt8(&s->IptColours[index + 1].Text, &g)) return;
if (!Convert_ParseUInt8(&s->IptColours[index + 2].Text, &b)) return;
Launcher_SaveSkin();
Launcher_SaveOptions = true;
col->R = r; col->G = g; col->B = b;
Launcher_Redraw();
}
static void ColoursScreen_AdjustSelected(struct LScreen* s, int delta) {
struct LInput* w;
int index, newCol;
uint8_t col;
if (!s->SelectedWidget) return;
index = LScreen_IndexOf(s, s->SelectedWidget);
if (index >= 15) return;
w = (struct LInput*)s->SelectedWidget;
if (!Convert_ParseUInt8(&w->Text, &col)) return;
newCol = col + delta;
Math_Clamp(newCol, 0, 255);
w->Text.length = 0;
String_AppendInt(&w->Text, newCol);
if (w->CaretPos >= w->Text.length) w->CaretPos = -1;
ColoursScreen_TextChanged(w);
}
static void ColoursScreen_KeyDown(struct LScreen* s, Key key) {
if (key == KEY_LEFT) {
ColoursScreen_AdjustSelected(s, -1);
} else if (key == KEY_RIGHT) {
ColoursScreen_AdjustSelected(s, +1);
} else if (key == KEY_UP) {
ColoursScreen_AdjustSelected(s, +10);
} else if (key == KEY_DOWN) {
ColoursScreen_AdjustSelected(s, -10);
} else {
LScreen_KeyDown(s, key);
}
}
static void ColoursScreen_ResetAll(void* widget, int x, int y) {
Launcher_ResetSkin();
ColoursScreen_UpdateAll(&ColoursScreen_Instance);
Launcher_Redraw();
}
static void ColoursScreen_Init(struct LScreen* s_) {
struct ColoursScreen* s = (struct ColoursScreen*)s_;
int i;
if (s->NumWidgets) return;
s->Widgets = s->_widgets;
for (i = 0; i < 5 * 3; i++) {
LScreen_Input(s_, &s->IptColours[i], 55, false, NULL);
s->IptColours[i].TextChanged = ColoursScreen_TextChanged;
}
LScreen_Label(s_, &s->LblNames[0], "Background");
@ -383,11 +458,13 @@ static void ColoursScreen_InitWidgets(struct ColoursScreen* s) {
LScreen_Button(s_, &s->BtnDefault, 160, 35, "Default colours");
LScreen_Button(s_, &s->BtnBack, 80, 35, "Back");
s->BtnDefault.OnClick = ColoursScreen_ResetAll;
s->BtnBack.OnClick = SwitchToSettings;
ColoursScreen_UpdateAll(s);
}
static void ColoursScreen_Reposition(struct ColoursScreen* s) {
struct LScreen* s_ = (struct LScreen*)s;
static void ColoursScreen_Reposition(struct LScreen* s_) {
struct ColoursScreen* s = (struct ColoursScreen*)s_;
int i, y;
for (i = 0; i < 5; i++) {
y = -100 + 40 * i;
@ -410,86 +487,15 @@ static void ColoursScreen_Reposition(struct ColoursScreen* s) {
LWidget_SetLocation(&s->BtnBack, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 170);
}
/*void Init() {
base.Init();
view.Init();
widgets[view.defIndex].OnClick = ResetColours;
widgets[view.defIndex + 1].OnClick = SwitchToSettings;
SetupInputHandlers();
for (int i = 0; i < widgets.Length; i++) {
InputWidget input = widgets[i] as InputWidget;
if (input == null) continue;
input.Chars.TextChanged = TextChanged;
}
Resize();
struct LScreen* ColoursScreen_MakeInstance(void) {
struct ColoursScreen* s = &ColoursScreen_Instance;
LScreen_Reset((struct LScreen*)s);
s->Init = ColoursScreen_Init;
s->Reposition = ColoursScreen_Reposition;
s->KeyDown = ColoursScreen_KeyDown;
return (struct LScreen*)s;
}
void ResetColours(int x, int y) {
Launcher_ResetSkin();
view.MakeAllRGBTriplets(true);
game.RedrawBackground();
Resize();
}
float colourAcc;
void MouseWheelChanged(float delta) {
int steps = Utils.AccumulateWheelDelta(ref colourAcc, delta);
AdjustSelectedColour(steps);
}
void KeyDown(Key key) {
if (key == KEY_LEFT) {
AdjustSelectedColour(-1);
} else if (key == KEY_RIGHT) {
AdjustSelectedColour(+1);
} else if (key == KEY_UP) {
AdjustSelectedColour(+10);
} else if (key == KEY_DOWN) {
AdjustSelectedColour(-10);
} else {
base.KeyDown(key);
}
}
void AdjustSelectedColour(int delta) {
if (curInput == null) return;
int index = IndexOfWidget(curInput);
if (index >= 15) return;
byte col;
if (!Byte.TryParse(curInput.Text, out col)) return;
int newCol = col + delta;
Utils.Clamp(ref newCol, 0, 255);
curInput.Text = newCol.ToString();
if (curInput.Chars.CaretPos >= curInput.Text.Length)
curInput.Chars.CaretPos = -1;
TextChanged(curInput);
}
void TextChanged(InputWidget widget) {
int index = IndexOfWidget(widget);
BitmapCol* col;
uint8_t r, g, b;
if (index < 3) col = &Launcher_BackgroundCol;
else if (index < 6) col = &Launcher_ButtonBorderCol;
else if (index < 9) col = &Launcher_ButtonHighlightCol;
else if (index < 12) col = &Launcher_ButtonForeCol;
else col = &Launcher_ButtonForeActiveCol;
/* if index of G input, changes to index of R input */
/* index = (index / 3) * 3;
if (!Convert_ParseUInt8(widgets[index + 0].Text, &r)) return;
if (!Convert_ParseUInt8(widgets[index + 1].Text, &g)) return;
if (!Convert_ParseUInt8(widgets[index + 2].Text, &b)) return;
col->R = r; col->G = g; col->B = b;
Launcher_ResetPixels();
Resize();
}*/
/*########################################################################################################################*
*------------------------------------------------------DirectConnectScreen------------------------------------------------*
@ -543,7 +549,7 @@ static void DirectConnectScreen_Save(const String* user, const String* mppass, c
Launcher_SaveOptions = true;
}
static void StartClient(void* w, int x, int y) {
static void DirectConnectScreen_StartClient(void* w, int x, int y) {
static String loopbackIp = String_FromConst("127.0.0.1");
static String defMppass = String_FromConst("(none)");
String* user = &DirectConnectScreen_Instance.IptUsername.Text;
@ -579,8 +585,9 @@ static void StartClient(void* w, int x, int y) {
Launcher_StartGame(user, mppass, &ip, &port, &String_Empty);
}
static void DirectConnectScreen_InitWidgets(struct DirectConnectScreen* s) {
struct LScreen* s_ = (struct LScreen*)s;
static void DirectConnectScreen_Init(struct LScreen* s_) {
struct DirectConnectScreen* s = (struct DirectConnectScreen*)s_;
if (s->NumWidgets) return;
s->Widgets = s->_widgets;
LScreen_Input(s_, &s->IptUsername, 330, false, "&gUsername..");
@ -591,9 +598,8 @@ static void DirectConnectScreen_InitWidgets(struct DirectConnectScreen* s) {
LScreen_Button(s_, &s->BtnBack, 80, 35, "Back");
LScreen_Label(s_, &s->LblStatus, "");
s->BtnConnect.OnClick = StartClient;
s->BtnConnect.OnClick = DirectConnectScreen_StartClient;
s->BtnBack.OnClick = SwitchToMain;
/* Init input text from options */
DirectConnectScreen_Load(s);
}
@ -609,11 +615,6 @@ static void DirectConnectScreen_Reposition(struct LScreen* s_) {
LWidget_SetLocation(&s->LblStatus, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 70);
}
static void DirectConnectScreen_Init(struct LScreen* s_) {
struct DirectConnectScreen* s = (struct DirectConnectScreen*)s_;
if (!s->NumWidgets) DirectConnectScreen_InitWidgets(s);
}
struct LScreen* DirectConnectScreen_MakeInstance(void) {
struct DirectConnectScreen* s = &DirectConnectScreen_Instance;
LScreen_Reset((struct LScreen*)s);
@ -634,8 +635,13 @@ static struct SettingsScreen {
struct LWidget* _widgets[7];
} SettingsScreen_Instance;
static void SettingsScreen_InitWidgets(struct SettingsScreen* s) {
struct LScreen* s_ = (struct LScreen*)s;
static void SettingsScreen_Init(struct LScreen* s_) {
struct SettingsScreen* s = (struct SettingsScreen*)s_;
s->BtnColours.Hidden = Launcher_ClassicBackground;
s->LblColours.Hidden = Launcher_ClassicBackground;
if (s->NumWidgets) return;
s->Widgets = s->_widgets;
LScreen_Button(s_, &s->BtnUpdates, 110, 35, "Updates");
@ -669,14 +675,6 @@ static void SettingsScreen_Reposition(struct LScreen* s_) {
LWidget_SetLocation(&s->BtnBack, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 170);
}
static void SettingsScreen_Init(struct LScreen* s_) {
struct SettingsScreen* s = (struct SettingsScreen*)s_;
if (!s->NumWidgets) SettingsScreen_InitWidgets(s);
s->BtnColours.Hidden = Launcher_ClassicBackground;
s->LblColours.Hidden = Launcher_ClassicBackground;
}
struct LScreen* SettingsScreen_MakeInstance(void) {
struct SettingsScreen* s = &SettingsScreen_Instance;
LScreen_Reset((struct LScreen*)s);

View File

@ -22,22 +22,6 @@ void LWidget_CalcPosition(void* widget) {
w->Y = Gui_CalcPos(w->VerAnchor, w->YOffset, w->Height, Game_Height);
}
void LWidget_Reset(void* widget) {
struct LWidget* w = widget;
w->Hovered = false;
w->Selected = false;
w->Hidden = false;
w->X = 0; w->Y = 0;
w->Width = 0; w->Height = 0;
w->HorAnchor = ANCHOR_MIN;
w->VerAnchor = ANCHOR_MIN;
w->XOffset = 0; w->YOffset = 0;
w->TabSelectable = false;
w->OnClick = NULL;
w->VTABLE = NULL;
}
/*########################################################################################################################*
*------------------------------------------------------ButtonWidget-------------------------------------------------------*
@ -135,7 +119,6 @@ static struct LWidgetVTABLE lbutton_VTABLE = {
NULL, NULL /* Select */
};
void LButton_Init(struct LButton* w, int width, int height) {
Widget_Reset(w);
w->VTABLE = &lbutton_VTABLE;
w->TabSelectable = true;
w->Width = width; w->Height = height;
@ -304,7 +287,6 @@ static struct LWidgetVTABLE linput_VTABLE = {
LInput_Redraw, LInput_Redraw /* Select */
};
void LInput_Init(struct LInput* w, int width, int height, const char* hintText, const FontDesc* hintFont) {
Widget_Reset(w);
w->VTABLE = &linput_VTABLE;
w->TabSelectable = true;
String_InitArray(w->Text, w->_TextBuffer);
@ -485,7 +467,6 @@ static struct LWidgetVTABLE llabel_VTABLE = {
NULL, NULL /* Select */
};
void LLabel_Init(struct LLabel* w) {
Widget_Reset(w);
w->VTABLE = &llabel_VTABLE;
String_InitArray(w->Text, w->_TextBuffer);
}
@ -553,7 +534,6 @@ static struct LWidgetVTABLE lslider_VTABLE = {
NULL, NULL /* Select */
};
void LSlider_Init(struct LSlider* w, int width, int height) {
Widget_Reset(w);
w->VTABLE = &lslider_VTABLE;
w->Width = width; w->Height = height;
w->MaxValue = 100;

View File

@ -41,7 +41,6 @@ struct LWidgetVTABLE {
struct LWidget { LWidget_Layout };
void LWidget_SetLocation(void* widget, uint8_t horAnchor, uint8_t verAnchor, int xOffset, int yOffset);
void LWidget_CalcPosition(void* widget);
void LWidget_Reset(void* widget);
struct LButton {
LWidget_Layout

View File

@ -18,7 +18,6 @@ void RedrawLastInput() { }
void Launcher_SaveSecureOpt(const char* opt, const String* data, const String* key) { }
void Launcher_LoadSecureOpt(const char* opt, String* data, const String* key) { }
void UpdateCheckTask_Run(void) { }
struct LScreen* ColoursScreen_MakeInstance(void) { return NULL; }
struct LScreen* MainScreen_MakeInstance(void) { return NULL; }
struct LScreen* ResourcesScreen_MakeInstance(void) { return NULL; }
struct LScreen* ServersScreen_MakeInstance(void) { return NULL; }
@ -50,24 +49,23 @@ void Launcher_ShowError(ReturnCode res, const char* place) {
void Launcher_SetScreen(struct LScreen* screen) {
if (Launcher_Screen) Launcher_Screen->Free(Launcher_Screen);
Launcher_ResetPixels();
Launcher_Screen = screen;
screen->Init(screen);
screen->Reposition(screen);
screen->DrawAll(screen);
/* for hovering over active button etc */
screen->MouseMove(screen, 0, 0);
Launcher_Redraw();
}
/*########################################################################################################################*
*---------------------------------------------------------Event handler---------------------------------------------------*
*#########################################################################################################################*/
static void Launcher_RedrawAll(void* obj) {
Launcher_ResetPixels();
if (Launcher_Screen) Launcher_Screen->DrawAll(Launcher_Screen);
fullRedraw = true;
static void Launcher_MaybeRedraw(void* obj) {
/* Only redraw when launcher has been initialised */
if (Launcher_Screen) Launcher_Redraw();
}
static void Launcher_ReqeustRedraw(void* obj) {
@ -84,7 +82,7 @@ static void Launcher_OnResize(void* obj) {
Window_InitRaw(&Launcher_Framebuffer);
if (Launcher_Screen) Launcher_Screen->Reposition(Launcher_Screen);
Launcher_RedrawAll(NULL);
Launcher_Redraw();
}
static bool Launcher_IsShutdown(int key) {
@ -126,7 +124,7 @@ static void Launcher_MouseMove(void* obj, int deltaX, int deltaY) {
static void Launcher_Display(void) {
Rect2D r;
if (pendingRedraw) {
Launcher_RedrawAll(NULL);
Launcher_Redraw();
pendingRedraw = false;
}
@ -150,7 +148,7 @@ static void Launcher_Init(void) {
Event_RegisterVoid(&WindowEvents_Resized, NULL, Launcher_OnResize);
Event_RegisterVoid(&WindowEvents_StateChanged, NULL, Launcher_OnResize);
Event_RegisterVoid(&WindowEvents_FocusChanged, NULL, Launcher_RedrawAll);
Event_RegisterVoid(&WindowEvents_FocusChanged, NULL, Launcher_MaybeRedraw);
Event_RegisterVoid(&WindowEvents_Redraw, NULL, Launcher_ReqeustRedraw);
Event_RegisterInt(&KeyEvents_Down, NULL, Launcher_KeyDown);
@ -173,7 +171,7 @@ static void Launcher_Free(void) {
int i;
Event_UnregisterVoid(&WindowEvents_Resized, NULL, Launcher_OnResize);
Event_UnregisterVoid(&WindowEvents_StateChanged, NULL, Launcher_OnResize);
Event_UnregisterVoid(&WindowEvents_FocusChanged, NULL, Launcher_RedrawAll);
Event_UnregisterVoid(&WindowEvents_FocusChanged, NULL, Launcher_MaybeRedraw);
Event_UnregisterVoid(&WindowEvents_Redraw, NULL, Launcher_ReqeustRedraw);
Event_UnregisterInt(&KeyEvents_Down, NULL, Launcher_KeyDown);
@ -203,7 +201,9 @@ void Launcher_Run(void) {
Drawer2D_Component.Init();
Game_UpdateClientSize();
Drawer2D_BitmappedText = false;
Launcher_LoadSkin();
Launcher_Init();
Launcher_TryLoadTexturePack();
@ -223,7 +223,7 @@ void Launcher_Run(void) {
} else {
Launcher_SetScreen(MainScreen_MakeInstance());
}*/
Launcher_SetScreen(DirectConnectScreen_MakeInstance());
Launcher_SetScreen(SettingsScreen_MakeInstance());
for (;;) {
Window_ProcessEvents();
@ -447,6 +447,12 @@ void Launcher_ResetPixels(void) {
Launcher_Dirty = true;
}
void Launcher_Redraw(void) {
Launcher_ResetPixels();
Launcher_Screen->DrawAll(Launcher_Screen);
fullRedraw = true;
}
/*########################################################################################################################*
*--------------------------------------------------------Starter/Updater--------------------------------------------------*

View File

@ -53,10 +53,12 @@ void Launcher_SaveSkin(void);
/* Attempts to load font and terrain from texture pack. */
void Launcher_TryLoadTexturePack(void);
/* Redraws all pixels with default background. */
/* NOTE: Also draws titlebar at top, if active screen permits it. */
/* NOTE: Also draws titlebar at top, if current screen permits it. */
void Launcher_ResetPixels(void);
/* Redraws the specified region with the background pixels. */
void Launcher_ResetArea(int x, int y, int width, int height);
/* Resets pixels to default, then draws widgets of current screen over it. */
void Launcher_Redraw(void);
/* Sets currently active screen/menu, freeing old one. */
void Launcher_SetScreen(struct LScreen* screen);

View File

@ -427,8 +427,8 @@ void Window_GetClipboardText(String* value) {
isUnicode = false;
}
if (!hGlobal) { CloseClipboard(); return; }
LPVOID src = GlobalLock(hGlobal);
DWORD size = GlobalSize(hGlobal);
LPVOID src = GlobalLock(hGlobal);
SIZE_T size = GlobalSize(hGlobal);
/* ignore trailing NULL at end */
/* TODO: Verify it's always there */