From ed47d1adda063a02b9886f8b28f3a81426aae2cf Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 5 Mar 2022 10:54:24 +1100 Subject: [PATCH] mc:// urls can be pasted into direct connect screen input fields now The username/address/mppass input fields will all be automatically set to the corresponding extracted values from the mc:// url --- src/Graphics_D3D11.c | 2 +- src/LScreens.c | 17 +++++++++++++++++ src/LWidgets.c | 6 ++++++ src/LWidgets.h | 12 +++++++----- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Graphics_D3D11.c b/src/Graphics_D3D11.c index 7a432efe6..7d50e6e07 100644 --- a/src/Graphics_D3D11.c +++ b/src/Graphics_D3D11.c @@ -213,7 +213,7 @@ GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipm if (!Math_IsPowOf2(bmp->width) || !Math_IsPowOf2(bmp->height)) { Logger_Abort("Textures must have power of two dimensions"); } - if (Gfx.LostContext) return NULL; + if (Gfx.LostContext) return 0; D3D11_TEXTURE2D_DESC desc = { 0 }; desc.Width = bmp->width; diff --git a/src/LScreens.c b/src/LScreens.c index cae78303d..49c9abaab 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -532,6 +532,19 @@ static struct DirectConnectScreen { struct LWidget* _widgets[6]; } DirectConnectScreen_Instance; +static void DirectConnectScreen_UrlFilter(cc_string* str) { + static const cc_string prefix = String_FromConst("mc://"); + cc_string parts[6]; + if (!String_CaselessStarts(str, &prefix)) return; + /* mc://[ip:port]/[username]/[mppass] */ + if (String_UNSAFE_Split(str, '/', parts, 6) != 5) return; + + LInput_SetString(&DirectConnectScreen_Instance.iptAddress, &parts[2]); + LInput_SetString(&DirectConnectScreen_Instance.iptUsername, &parts[3]); + LInput_SetString(&DirectConnectScreen_Instance.iptMppass, &parts[4]); + str->length = 0; +} + CC_NOINLINE static void DirectConnectScreen_SetStatus(const char* text) { struct LLabel* w = &DirectConnectScreen_Instance.lblStatus; LLabel_SetConst(w, text); @@ -617,6 +630,10 @@ static void DirectConnectScreen_Init(struct LScreen* s_) { LButton_Init(s_, &s->btnBack, 80, 35, "Back"); LLabel_Init(s_, &s->lblStatus, ""); + s->iptUsername.ClipboardFilter = DirectConnectScreen_UrlFilter; + s->iptAddress.ClipboardFilter = DirectConnectScreen_UrlFilter; + s->iptMppass.ClipboardFilter = DirectConnectScreen_UrlFilter; + s->btnConnect.OnClick = DirectConnectScreen_StartClient; s->btnBack.OnClick = SwitchToMain; /* Init input text from options */ diff --git a/src/LWidgets.c b/src/LWidgets.c index d929c4218..c36f5ed7c 100644 --- a/src/LWidgets.c +++ b/src/LWidgets.c @@ -457,6 +457,12 @@ void LInput_Clear(struct LInput* w) { LWidget_Redraw(w); } +void LInput_SetString(struct LInput* w, const cc_string* str) { + LInput_SetText(w, str); + if (w->TextChanged) w->TextChanged(w); + LWidget_Redraw(w); +} + /*########################################################################################################################* *------------------------------------------------------LabelWidget--------------------------------------------------------* diff --git a/src/LWidgets.h b/src/LWidgets.h index d67d0fb6d..639e4d151 100644 --- a/src/LWidgets.h +++ b/src/LWidgets.h @@ -96,16 +96,18 @@ CC_NOINLINE void LInput_Init(struct LScreen* s, struct LInput* w, int width, con CC_NOINLINE void LInput_SetText(struct LInput* w, const cc_string* text); CC_NOINLINE void LInput_ClearText(struct LInput* w); -/* Appends a character to the currently entered text. */ +/* Appends a character to the currently entered text */ CC_NOINLINE void LInput_Append(struct LInput* w, char c); -/* Appends a string to the currently entered text. */ +/* Appends a string to the currently entered text */ CC_NOINLINE void LInput_AppendString(struct LInput* w, const cc_string* str); -/* Removes the character preceding the caret in the currently entered text. */ +/* Removes the character preceding the caret in the currently entered text */ CC_NOINLINE void LInput_Backspace(struct LInput* w); -/* Removes the character at the caret in the currently entered text. */ +/* Removes the character at the caret in the currently entered text */ CC_NOINLINE void LInput_Delete(struct LInput* w); -/* Resets the currently entered text to an empty string. */ +/* Resets the currently entered text to an empty string */ CC_NOINLINE void LInput_Clear(struct LInput* w); +/* Sets the currently entered text to the given string */ +CC_NOINLINE void LInput_SetString(struct LInput* w, const cc_string* str); /* Represents non-interactable text. */ struct LLabel {