diff --git a/src/d_dehacked.cpp b/src/d_dehacked.cpp index 36c32bf5bb..9cfd745203 100644 --- a/src/d_dehacked.cpp +++ b/src/d_dehacked.cpp @@ -2293,7 +2293,7 @@ static int PatchStrings (int dummy) holdstring.StripRight(); if (holdstring.Len() > 0 && holdstring[holdstring.Len()-1] == '\\') { - holdstring.Truncate((long)holdstring.Len()-1); + holdstring.Truncate(holdstring.Len()-1); Line2 = igets (); } else diff --git a/src/g_mapinfo.cpp b/src/g_mapinfo.cpp index bb641b23bb..cbda6568e1 100644 --- a/src/g_mapinfo.cpp +++ b/src/g_mapinfo.cpp @@ -589,7 +589,7 @@ bool FMapInfoParser::ParseLookupName(FString &dest) } while (sc.CheckString(",")); // strip off the last newline - dest.Truncate(long(dest.Len()-1)); + dest.Truncate(dest.Len()-1); return false; } } diff --git a/src/menu/loadsavemenu.cpp b/src/menu/loadsavemenu.cpp index 74c9434803..ec92a591f8 100644 --- a/src/menu/loadsavemenu.cpp +++ b/src/menu/loadsavemenu.cpp @@ -598,6 +598,7 @@ protected: // this needs to be kept in memory so that the texture can access it when it needs to. bool mEntering; char savegamestring[SAVESTRINGSIZE]; + DTextEnterMenu *mInput = nullptr; DLoadSaveMenu(DMenu *parent = nullptr, DListMenuDescriptor *desc = nullptr); void OnDestroy() override; @@ -760,15 +761,9 @@ void DLoadSaveMenu::Drawer () } else { + FString s = mInput->GetText() + SmallFont->GetCursor(); screen->DrawText (SmallFont, CR_WHITE, - listboxLeft+1, listboxTop+rowHeight*i+CleanYfac, savegamestring, - DTA_CleanNoMove, true, TAG_DONE); - - char curs[2] = { SmallFont->GetCursor(), 0 }; - screen->DrawText (SmallFont, CR_WHITE, - listboxLeft+1+SmallFont->StringWidth (savegamestring)*CleanXfac, - listboxTop+rowHeight*i+CleanYfac, - curs, + listboxLeft+1, listboxTop+rowHeight*i+CleanYfac, s, DTA_CleanNoMove, true, TAG_DONE); } } @@ -1055,18 +1050,20 @@ bool DSaveMenu::MenuEvent (int mkey, bool fromcontroller) { savegamestring[0] = 0; } - DMenu *input = new DTextEnterMenu(this, savegamestring, SAVESTRINGSIZE, 1, fromcontroller); - M_ActivateMenu(input); + mInput = new DTextEnterMenu(this, savegamestring, SAVESTRINGSIZE, 1, fromcontroller); + M_ActivateMenu(mInput); mEntering = true; } else if (mkey == MKEY_Input) { mEntering = false; - manager->DoSave(Selected, savegamestring); + manager->DoSave(Selected, mInput->GetText()); + mInput = nullptr; } else if (mkey == MKEY_Abort) { mEntering = false; + mInput = nullptr; } return false; } diff --git a/src/menu/menu.cpp b/src/menu/menu.cpp index 595563c0dc..1b93083e19 100644 --- a/src/menu/menu.cpp +++ b/src/menu/menu.cpp @@ -73,7 +73,7 @@ DMenu *DMenu::CurrentMenu; DEFINE_ACTION_FUNCTION(DMenu, GetCurrentMenu) { - ACTION_RETURN_POINTER(DMenu::CurrentMenu); + ACTION_RETURN_OBJECT(DMenu::CurrentMenu); } int DMenu::MenuTime; @@ -113,7 +113,7 @@ DEFINE_ACTION_FUNCTION(DMenuDescriptor, GetDescriptor) PARAM_NAME(name); DMenuDescriptor **desc = MenuDescriptors.CheckKey(name); auto retn = desc ? *desc : nullptr; - ACTION_RETURN_POINTER(retn); + ACTION_RETURN_OBJECT(retn); } size_t DListMenuDescriptor::PropagateMark() @@ -425,7 +425,7 @@ DEFINE_ACTION_FUNCTION(DMenu, GetItem) { PARAM_SELF_PROLOGUE(DMenu); PARAM_NAME(name); - ACTION_RETURN_POINTER(self->GetItem(name)); + ACTION_RETURN_OBJECT(self->GetItem(name)); } diff --git a/src/menu/menu.h b/src/menu/menu.h index 1d7f6def9c..9e64fdd8e0 100644 --- a/src/menu/menu.h +++ b/src/menu/menu.h @@ -269,7 +269,7 @@ public: virtual void Ticker (); virtual void Drawer (); virtual bool DimAllowed (); - /*virtual */bool TranslateKeyboardEvents(); + bool TranslateKeyboardEvents(); virtual void Close(); virtual bool MouseEvent(int type, int x, int y); @@ -448,7 +448,8 @@ class DTextEnterMenu : public DMenu { DECLARE_ABSTRACT_CLASS(DTextEnterMenu, DMenu) - char *mEnterString; +public: + FString mEnterString; unsigned int mEnterSize; unsigned int mEnterPos; int mSizeMode; // 1: size is length in chars. 2: also check string width @@ -460,17 +461,15 @@ class DTextEnterMenu : public DMenu // [TP] bool AllowColors; -public: // [TP] Added allowcolors - DTextEnterMenu(DMenu *parent, char *textbuffer, int maxlen, int sizemode, bool showgrid, bool allowcolors = false); + DTextEnterMenu(DMenu *parent, const char *textbuffer, int maxlen, int sizemode, bool showgrid, bool allowcolors = false); void Drawer (); bool MenuEvent (int mkey, bool fromcontroller); bool Responder(event_t *ev); - //bool TranslateKeyboardEvents(); bool MouseEvent(int type, int x, int y); - + FString GetText(); }; diff --git a/src/menu/menuinput.cpp b/src/menu/menuinput.cpp index e28b141a11..9a0b717b51 100644 --- a/src/menu/menuinput.cpp +++ b/src/menu/menuinput.cpp @@ -61,6 +61,8 @@ static const char InputGridChars[INPUTGRID_WIDTH * INPUTGRID_HEIGHT] = CVAR(Bool, m_showinputgrid, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) +DEFINE_FIELD(DTextEnterMenu, mInputGridOkay) + //============================================================================= // // @@ -68,15 +70,14 @@ CVAR(Bool, m_showinputgrid, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) //============================================================================= // [TP] Added allowcolors -DTextEnterMenu::DTextEnterMenu(DMenu *parent, char *textbuffer, int maxlen, int sizemode, bool showgrid, bool allowcolors) +DTextEnterMenu::DTextEnterMenu(DMenu *parent, const char *textbuffer, int maxlen, int sizemode, bool showgrid, bool allowcolors) : DMenu(parent) { mEnterString = textbuffer; - mEnterSize = maxlen; - mEnterPos = (unsigned)strlen(textbuffer); + mEnterSize = maxlen < 0 ? UINT_MAX : unsigned(maxlen); mSizeMode = sizemode; mInputGridOkay = showgrid || m_showinputgrid; - if (mEnterPos > 0) + if (mEnterString.IsNotEmpty()) { InputGridX = INPUTGRID_WIDTH - 1; InputGridY = INPUTGRID_HEIGHT - 1; @@ -96,12 +97,17 @@ DTextEnterMenu::DTextEnterMenu(DMenu *parent, char *textbuffer, int maxlen, int // //============================================================================= -/* -bool DTextEnterMenu::TranslateKeyboardEvents() +FString DTextEnterMenu::GetText() { - return mInputGridOkay; + return mEnterString; } -*/ + +//============================================================================= +// +// +// +//============================================================================= + //============================================================================= // @@ -117,21 +123,19 @@ bool DTextEnterMenu::Responder(event_t *ev) if (ev->subtype == EV_GUI_Char) { mInputGridOkay = false; - if (mEnterPos < mEnterSize && + if (mEnterString.Len() < mEnterSize && (mSizeMode == 2/*entering player name*/ || (size_t)SmallFont->StringWidth(mEnterString) < (mEnterSize-1)*8)) { - mEnterString[mEnterPos] = (char)ev->data1; - mEnterString[++mEnterPos] = 0; + mEnterString.AppendFormat("%c", (char)ev->data1); } return true; } char ch = (char)ev->data1; if ((ev->subtype == EV_GUI_KeyDown || ev->subtype == EV_GUI_KeyRepeat) && ch == '\b') { - if (mEnterPos > 0) + if (mEnterString.IsNotEmpty()) { - mEnterPos--; - mEnterString[mEnterPos] = 0; + mEnterString.Truncate(mEnterString.Len() - 1); } } else if (ev->subtype == EV_GUI_KeyDown) @@ -145,15 +149,15 @@ bool DTextEnterMenu::Responder(event_t *ev) } else if (ch == '\r') { - if (mEnterString[0]) + if (mEnterString.IsNotEmpty()) { // [TP] If we allow color codes, colorize the string now. if (AllowColors) - strbin(mEnterString); + mEnterString = strbin1(mEnterString); DMenu *parent = mParentMenu; - Close(); parent->CallMenuEvent(MKEY_Input, false); + Close(); return true; } } @@ -247,9 +251,9 @@ bool DTextEnterMenu::MenuEvent (int key, bool fromcontroller) return true; case MKEY_Clear: - if (mEnterPos > 0) + if (mEnterString.IsNotEmpty()) { - mEnterString[--mEnterPos] = 0; + mEnterString.Truncate(mEnterString.Len() - 1); } return true; @@ -260,7 +264,7 @@ bool DTextEnterMenu::MenuEvent (int key, bool fromcontroller) ch = InputGridChars[InputGridX + InputGridY * INPUTGRID_WIDTH]; if (ch == 0) // end { - if (mEnterString[0] != '\0') + if (mEnterString.IsNotEmpty()) { DMenu *parent = mParentMenu; Close(); @@ -270,16 +274,15 @@ bool DTextEnterMenu::MenuEvent (int key, bool fromcontroller) } else if (ch == '\b') // bs { - if (mEnterPos > 0) + if (mEnterString.IsNotEmpty()) { - mEnterString[--mEnterPos] = 0; + mEnterString.Truncate(mEnterString.Len() - 1); } } - else if (mEnterPos < mEnterSize && + else if (mEnterString.Len() < mEnterSize && (mSizeMode == 2/*entering player name*/ || (size_t)SmallFont->StringWidth(mEnterString) < (mEnterSize-1)*8)) { - mEnterString[mEnterPos] = ch; - mEnterString[++mEnterPos] = 0; + mEnterString += char(ch); } } return true; @@ -370,4 +373,24 @@ void DTextEnterMenu::Drawer () } } Super::Drawer(); -} \ No newline at end of file +} + + +DEFINE_ACTION_FUNCTION(DTextEnterMenu, Open) +{ + PARAM_PROLOGUE; + PARAM_OBJECT(parent, DMenu); + PARAM_STRING(text); + PARAM_INT(maxlen); + PARAM_INT(sizemode); + PARAM_BOOL(fromcontroller); + auto m = new DTextEnterMenu(parent, text.GetChars(), maxlen, sizemode, fromcontroller, false); + M_ActivateMenu(m); + ACTION_RETURN_OBJECT(m); +} + +DEFINE_ACTION_FUNCTION(DTextEnterMenu, GetText) +{ + PARAM_SELF_PROLOGUE(DTextEnterMenu); + ACTION_RETURN_STRING(self->GetText()); +} diff --git a/src/scripting/zscript/ast.cpp b/src/scripting/zscript/ast.cpp index c4c8a12a51..81c7ff6d35 100644 --- a/src/scripting/zscript/ast.cpp +++ b/src/scripting/zscript/ast.cpp @@ -112,7 +112,7 @@ public: } else { // Move hanging ( characters to the new line - Str.Truncate(long(Str.Len() - ConsecOpens)); + Str.Truncate(Str.Len() - ConsecOpens); NestDepth -= ConsecOpens; } Str << '\n'; diff --git a/src/scripting/zscript/zcc_compile.cpp b/src/scripting/zscript/zcc_compile.cpp index 500336df5e..4647e6b26c 100644 --- a/src/scripting/zscript/zcc_compile.cpp +++ b/src/scripting/zscript/zcc_compile.cpp @@ -2598,7 +2598,7 @@ void ZCCCompiler::CompileStates() statename << FName(part->Id) << '.'; part = static_cast(part->SiblingNext); } while (part != sg->Label); - statename.Truncate((long)statename.Len() - 1); // remove the last '.' in the label name + statename.Truncate(statename.Len() - 1); // remove the last '.' in the label name if (sg->Offset != nullptr) { int offset = IntConstFromNode(sg->Offset, c->Type()); diff --git a/src/zstring.cpp b/src/zstring.cpp index 665b9312ee..83a2a20d81 100644 --- a/src/zstring.cpp +++ b/src/zstring.cpp @@ -368,15 +368,15 @@ FString &FString::CopyCStrPart(const char *tail, size_t tailLen) return *this; } -void FString::Truncate(long newlen) +void FString::Truncate(size_t newlen) { - if (newlen <= 0) + if (newlen == 0) { Data()->Release(); NullString.RefCount++; Chars = &NullString.Nothing[0]; } - else if (newlen < (long)Len()) + else if (newlen < Len()) { ReallocBuffer (newlen); Chars[newlen] = '\0'; diff --git a/src/zstring.h b/src/zstring.h index c58fb111a3..b925a39b1f 100644 --- a/src/zstring.h +++ b/src/zstring.h @@ -301,7 +301,7 @@ public: bool IsEmpty() const { return Len() == 0; } bool IsNotEmpty() const { return Len() != 0; } - void Truncate (long newlen); + void Truncate (size_t newlen); void Remove(size_t index, size_t remlen); int Compare (const FString &other) const { return strcmp (Chars, other.Chars); } diff --git a/wadsrc/static/zscript.txt b/wadsrc/static/zscript.txt index 637d5b66e7..ba5c3d19b0 100644 --- a/wadsrc/static/zscript.txt +++ b/wadsrc/static/zscript.txt @@ -14,6 +14,7 @@ #include "zscript/menu/joystickmenu.txt" #include "zscript/menu/playerdisplay.txt" #include "zscript/menu/playermenu.txt" +#include "zscript/menu/textentermenu.txt" #include "zscript/inventory/inventory.txt" #include "zscript/inventory/inv_misc.txt" diff --git a/wadsrc/static/zscript/menu/optionmenuitems.txt b/wadsrc/static/zscript/menu/optionmenuitems.txt index 338c365991..418773903d 100644 --- a/wadsrc/static/zscript/menu/optionmenuitems.txt +++ b/wadsrc/static/zscript/menu/optionmenuitems.txt @@ -1083,22 +1083,24 @@ class OptionMenuFieldBase : OptionMenuItem class OptionMenuItemTextField : OptionMenuFieldBase { + TextEnterMenu mEnter; + OptionMenuItemTextField Init (String label, Name command, CVar graycheck = null) { Super.Init(label, command, graycheck); - mEntering = false; + mEnter = null; return self; } override String Represent() { - if (mEntering) return mEditName .. SmallFont.GetCursor(); + if (mEnter) return mEnter.GetText() .. SmallFont.GetCursor(); else return GetCVarString(); } override int Draw(OptionMenuDescriptor desc, int y, int indent, bool selected) { - if (mEntering) + if (mEnter) { // reposition the text so that the cursor is visible when in entering mode. String text = Represent(); @@ -1114,30 +1116,23 @@ class OptionMenuItemTextField : OptionMenuFieldBase if (mkey == Menu.MKEY_Enter) { Menu.MenuSound("menu/choose"); - mEditName = GetCVarString(); - mEntering = true; - - //Menu* input = new DTextEnterMenu (Menu.CurrentMenu, mEditName, sizeof mEditName, 2, fromcontroller); - //M_ActivateMenu(input); + mEnter = TextEnterMenu.Open(Menu.GetCurrentMenu(), GetCVarString(), -1, 2, fromcontroller); return true; } else if (mkey == Menu.MKEY_Input) { - if (mCVar) mCVar.SetString(mEditName); - mEntering = false; + if (mCVar) mCVar.SetString(mEnter.GetText()); + mEnter = null; return true; } else if (mkey == Menu.MKEY_Abort) { - mEntering = false; + mEnter = null; return true; } return Super.MenuEvent(mkey, fromcontroller); } - - bool mEntering; - String mEditName; } diff --git a/wadsrc/static/zscript/menu/playermenu.txt b/wadsrc/static/zscript/menu/playermenu.txt index 6b055e0a0a..389c34043e 100644 --- a/wadsrc/static/zscript/menu/playermenu.txt +++ b/wadsrc/static/zscript/menu/playermenu.txt @@ -46,8 +46,7 @@ class ListMenuItemPlayerNameBox : ListMenuItemSelectable int mFontColor; int mFrameSize; String mPlayerName; - String mEditName; - bool mEntering; + TextEnterMenu mEnter; //============================================================================= // @@ -63,7 +62,7 @@ class ListMenuItemPlayerNameBox : ListMenuItemSelectable mFontColor = desc.mFontColor; mFrameSize = frameofs; mPlayerName = ""; - mEntering = false; + mEnter = null; } //============================================================================= @@ -80,7 +79,7 @@ class ListMenuItemPlayerNameBox : ListMenuItemSelectable mFontColor = color; mFrameSize = frameofs; mPlayerName = ""; - mEntering = false; + mEnter = null; } //============================================================================= @@ -164,13 +163,13 @@ class ListMenuItemPlayerNameBox : ListMenuItemSelectable // Draw player name box int x = mXpos + mFont.StringWidth(text) + 16 + mFrameSize; DrawBorder (x, mYpos - mFrameSize, MAXPLAYERNAME+1); - if (!mEntering) + if (!mEnter) { screen.DrawText (SmallFont, Font.CR_UNTRANSLATED, x + mFrameSize, mYpos, mPlayerName, DTA_Clean, true); } else { - let printit = mEditName .. SmallFont.GetCursor(); + let printit = mEnter.GetText() .. SmallFont.GetCursor(); screen.DrawText (SmallFont, Font.CR_UNTRANSLATED, x + mFrameSize, mYpos, printit, DTA_Clean, true); } } @@ -186,21 +185,18 @@ class ListMenuItemPlayerNameBox : ListMenuItemSelectable if (mkey == Menu.MKEY_Enter) { Menu.MenuSound ("menu/choose"); - mEditName = mPlayerName; - mEntering = true; - //DMenu *input = new DTextEnterMenu(Menu.CurrentMenu, mEditName, MAXPLAYERNAME, 2, fromcontroller); - //M_ActivateMenu(input); + mEnter = TextEnterMenu.Open(Menu.GetCurrentMenu(), mPlayerName, MAXPLAYERNAME, 2, fromcontroller); return true; } else if (mkey == Menu.MKEY_Input) { - mPlayerName = mEditName; - mEntering = false; + mPlayerName = mEnter.GetText(); + mEnter = null; return true; } else if (mkey == Menu.MKEY_Abort) { - mEntering = false; + mEnter = null; return true; } return false; diff --git a/wadsrc/static/zscript/menu/textentermenu.txt b/wadsrc/static/zscript/menu/textentermenu.txt new file mode 100644 index 0000000000..7974a62bd0 --- /dev/null +++ b/wadsrc/static/zscript/menu/textentermenu.txt @@ -0,0 +1,15 @@ + +// This is only the parts that are needed to make the menu fully work right now. More to come later. +class TextEnterMenu : Menu native +{ + native bool mInputGridOkay; + + native static TextEnterMenu Open(Menu parent, String text, int maxlen, int sizemode, bool fromcontroller); + native String GetText(); + + + override bool TranslateKeyboardEvents() + { + return mInputGridOkay; + } +} \ No newline at end of file