- made adjustments to text input menu to work with scripts.

This commit is contained in:
Christoph Oelckers 2017-02-12 21:45:37 +01:00
parent f0e925c5a7
commit b6ad14a614
14 changed files with 107 additions and 81 deletions

View file

@ -2293,7 +2293,7 @@ static int PatchStrings (int dummy)
holdstring.StripRight(); holdstring.StripRight();
if (holdstring.Len() > 0 && holdstring[holdstring.Len()-1] == '\\') if (holdstring.Len() > 0 && holdstring[holdstring.Len()-1] == '\\')
{ {
holdstring.Truncate((long)holdstring.Len()-1); holdstring.Truncate(holdstring.Len()-1);
Line2 = igets (); Line2 = igets ();
} }
else else

View file

@ -589,7 +589,7 @@ bool FMapInfoParser::ParseLookupName(FString &dest)
} }
while (sc.CheckString(",")); while (sc.CheckString(","));
// strip off the last newline // strip off the last newline
dest.Truncate(long(dest.Len()-1)); dest.Truncate(dest.Len()-1);
return false; return false;
} }
} }

View file

@ -598,6 +598,7 @@ protected:
// this needs to be kept in memory so that the texture can access it when it needs to. // this needs to be kept in memory so that the texture can access it when it needs to.
bool mEntering; bool mEntering;
char savegamestring[SAVESTRINGSIZE]; char savegamestring[SAVESTRINGSIZE];
DTextEnterMenu *mInput = nullptr;
DLoadSaveMenu(DMenu *parent = nullptr, DListMenuDescriptor *desc = nullptr); DLoadSaveMenu(DMenu *parent = nullptr, DListMenuDescriptor *desc = nullptr);
void OnDestroy() override; void OnDestroy() override;
@ -760,15 +761,9 @@ void DLoadSaveMenu::Drawer ()
} }
else else
{ {
FString s = mInput->GetText() + SmallFont->GetCursor();
screen->DrawText (SmallFont, CR_WHITE, screen->DrawText (SmallFont, CR_WHITE,
listboxLeft+1, listboxTop+rowHeight*i+CleanYfac, savegamestring, listboxLeft+1, listboxTop+rowHeight*i+CleanYfac, s,
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,
DTA_CleanNoMove, true, TAG_DONE); DTA_CleanNoMove, true, TAG_DONE);
} }
} }
@ -1055,18 +1050,20 @@ bool DSaveMenu::MenuEvent (int mkey, bool fromcontroller)
{ {
savegamestring[0] = 0; savegamestring[0] = 0;
} }
DMenu *input = new DTextEnterMenu(this, savegamestring, SAVESTRINGSIZE, 1, fromcontroller); mInput = new DTextEnterMenu(this, savegamestring, SAVESTRINGSIZE, 1, fromcontroller);
M_ActivateMenu(input); M_ActivateMenu(mInput);
mEntering = true; mEntering = true;
} }
else if (mkey == MKEY_Input) else if (mkey == MKEY_Input)
{ {
mEntering = false; mEntering = false;
manager->DoSave(Selected, savegamestring); manager->DoSave(Selected, mInput->GetText());
mInput = nullptr;
} }
else if (mkey == MKEY_Abort) else if (mkey == MKEY_Abort)
{ {
mEntering = false; mEntering = false;
mInput = nullptr;
} }
return false; return false;
} }

View file

@ -73,7 +73,7 @@ DMenu *DMenu::CurrentMenu;
DEFINE_ACTION_FUNCTION(DMenu, GetCurrentMenu) DEFINE_ACTION_FUNCTION(DMenu, GetCurrentMenu)
{ {
ACTION_RETURN_POINTER(DMenu::CurrentMenu); ACTION_RETURN_OBJECT(DMenu::CurrentMenu);
} }
int DMenu::MenuTime; int DMenu::MenuTime;
@ -113,7 +113,7 @@ DEFINE_ACTION_FUNCTION(DMenuDescriptor, GetDescriptor)
PARAM_NAME(name); PARAM_NAME(name);
DMenuDescriptor **desc = MenuDescriptors.CheckKey(name); DMenuDescriptor **desc = MenuDescriptors.CheckKey(name);
auto retn = desc ? *desc : nullptr; auto retn = desc ? *desc : nullptr;
ACTION_RETURN_POINTER(retn); ACTION_RETURN_OBJECT(retn);
} }
size_t DListMenuDescriptor::PropagateMark() size_t DListMenuDescriptor::PropagateMark()
@ -425,7 +425,7 @@ DEFINE_ACTION_FUNCTION(DMenu, GetItem)
{ {
PARAM_SELF_PROLOGUE(DMenu); PARAM_SELF_PROLOGUE(DMenu);
PARAM_NAME(name); PARAM_NAME(name);
ACTION_RETURN_POINTER(self->GetItem(name)); ACTION_RETURN_OBJECT(self->GetItem(name));
} }

View file

@ -269,7 +269,7 @@ public:
virtual void Ticker (); virtual void Ticker ();
virtual void Drawer (); virtual void Drawer ();
virtual bool DimAllowed (); virtual bool DimAllowed ();
/*virtual */bool TranslateKeyboardEvents(); bool TranslateKeyboardEvents();
virtual void Close(); virtual void Close();
virtual bool MouseEvent(int type, int x, int y); virtual bool MouseEvent(int type, int x, int y);
@ -448,7 +448,8 @@ class DTextEnterMenu : public DMenu
{ {
DECLARE_ABSTRACT_CLASS(DTextEnterMenu, DMenu) DECLARE_ABSTRACT_CLASS(DTextEnterMenu, DMenu)
char *mEnterString; public:
FString mEnterString;
unsigned int mEnterSize; unsigned int mEnterSize;
unsigned int mEnterPos; unsigned int mEnterPos;
int mSizeMode; // 1: size is length in chars. 2: also check string width int mSizeMode; // 1: size is length in chars. 2: also check string width
@ -460,17 +461,15 @@ class DTextEnterMenu : public DMenu
// [TP] // [TP]
bool AllowColors; bool AllowColors;
public:
// [TP] Added allowcolors // [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 (); void Drawer ();
bool MenuEvent (int mkey, bool fromcontroller); bool MenuEvent (int mkey, bool fromcontroller);
bool Responder(event_t *ev); bool Responder(event_t *ev);
//bool TranslateKeyboardEvents();
bool MouseEvent(int type, int x, int y); bool MouseEvent(int type, int x, int y);
FString GetText();
}; };

View file

@ -61,6 +61,8 @@ static const char InputGridChars[INPUTGRID_WIDTH * INPUTGRID_HEIGHT] =
CVAR(Bool, m_showinputgrid, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) 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 // [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) : DMenu(parent)
{ {
mEnterString = textbuffer; mEnterString = textbuffer;
mEnterSize = maxlen; mEnterSize = maxlen < 0 ? UINT_MAX : unsigned(maxlen);
mEnterPos = (unsigned)strlen(textbuffer);
mSizeMode = sizemode; mSizeMode = sizemode;
mInputGridOkay = showgrid || m_showinputgrid; mInputGridOkay = showgrid || m_showinputgrid;
if (mEnterPos > 0) if (mEnterString.IsNotEmpty())
{ {
InputGridX = INPUTGRID_WIDTH - 1; InputGridX = INPUTGRID_WIDTH - 1;
InputGridY = INPUTGRID_HEIGHT - 1; InputGridY = INPUTGRID_HEIGHT - 1;
@ -96,12 +97,17 @@ DTextEnterMenu::DTextEnterMenu(DMenu *parent, char *textbuffer, int maxlen, int
// //
//============================================================================= //=============================================================================
/* FString DTextEnterMenu::GetText()
bool DTextEnterMenu::TranslateKeyboardEvents()
{ {
return mInputGridOkay; return mEnterString;
} }
*/
//=============================================================================
//
//
//
//=============================================================================
//============================================================================= //=============================================================================
// //
@ -117,21 +123,19 @@ bool DTextEnterMenu::Responder(event_t *ev)
if (ev->subtype == EV_GUI_Char) if (ev->subtype == EV_GUI_Char)
{ {
mInputGridOkay = false; mInputGridOkay = false;
if (mEnterPos < mEnterSize && if (mEnterString.Len() < mEnterSize &&
(mSizeMode == 2/*entering player name*/ || (size_t)SmallFont->StringWidth(mEnterString) < (mEnterSize-1)*8)) (mSizeMode == 2/*entering player name*/ || (size_t)SmallFont->StringWidth(mEnterString) < (mEnterSize-1)*8))
{ {
mEnterString[mEnterPos] = (char)ev->data1; mEnterString.AppendFormat("%c", (char)ev->data1);
mEnterString[++mEnterPos] = 0;
} }
return true; return true;
} }
char ch = (char)ev->data1; char ch = (char)ev->data1;
if ((ev->subtype == EV_GUI_KeyDown || ev->subtype == EV_GUI_KeyRepeat) && ch == '\b') if ((ev->subtype == EV_GUI_KeyDown || ev->subtype == EV_GUI_KeyRepeat) && ch == '\b')
{ {
if (mEnterPos > 0) if (mEnterString.IsNotEmpty())
{ {
mEnterPos--; mEnterString.Truncate(mEnterString.Len() - 1);
mEnterString[mEnterPos] = 0;
} }
} }
else if (ev->subtype == EV_GUI_KeyDown) else if (ev->subtype == EV_GUI_KeyDown)
@ -145,15 +149,15 @@ bool DTextEnterMenu::Responder(event_t *ev)
} }
else if (ch == '\r') else if (ch == '\r')
{ {
if (mEnterString[0]) if (mEnterString.IsNotEmpty())
{ {
// [TP] If we allow color codes, colorize the string now. // [TP] If we allow color codes, colorize the string now.
if (AllowColors) if (AllowColors)
strbin(mEnterString); mEnterString = strbin1(mEnterString);
DMenu *parent = mParentMenu; DMenu *parent = mParentMenu;
Close();
parent->CallMenuEvent(MKEY_Input, false); parent->CallMenuEvent(MKEY_Input, false);
Close();
return true; return true;
} }
} }
@ -247,9 +251,9 @@ bool DTextEnterMenu::MenuEvent (int key, bool fromcontroller)
return true; return true;
case MKEY_Clear: case MKEY_Clear:
if (mEnterPos > 0) if (mEnterString.IsNotEmpty())
{ {
mEnterString[--mEnterPos] = 0; mEnterString.Truncate(mEnterString.Len() - 1);
} }
return true; return true;
@ -260,7 +264,7 @@ bool DTextEnterMenu::MenuEvent (int key, bool fromcontroller)
ch = InputGridChars[InputGridX + InputGridY * INPUTGRID_WIDTH]; ch = InputGridChars[InputGridX + InputGridY * INPUTGRID_WIDTH];
if (ch == 0) // end if (ch == 0) // end
{ {
if (mEnterString[0] != '\0') if (mEnterString.IsNotEmpty())
{ {
DMenu *parent = mParentMenu; DMenu *parent = mParentMenu;
Close(); Close();
@ -270,16 +274,15 @@ bool DTextEnterMenu::MenuEvent (int key, bool fromcontroller)
} }
else if (ch == '\b') // bs 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)) (mSizeMode == 2/*entering player name*/ || (size_t)SmallFont->StringWidth(mEnterString) < (mEnterSize-1)*8))
{ {
mEnterString[mEnterPos] = ch; mEnterString += char(ch);
mEnterString[++mEnterPos] = 0;
} }
} }
return true; return true;
@ -371,3 +374,23 @@ void DTextEnterMenu::Drawer ()
} }
Super::Drawer(); Super::Drawer();
} }
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());
}

View file

@ -112,7 +112,7 @@ public:
} }
else else
{ // Move hanging ( characters to the new line { // Move hanging ( characters to the new line
Str.Truncate(long(Str.Len() - ConsecOpens)); Str.Truncate(Str.Len() - ConsecOpens);
NestDepth -= ConsecOpens; NestDepth -= ConsecOpens;
} }
Str << '\n'; Str << '\n';

View file

@ -2598,7 +2598,7 @@ void ZCCCompiler::CompileStates()
statename << FName(part->Id) << '.'; statename << FName(part->Id) << '.';
part = static_cast<decltype(part)>(part->SiblingNext); part = static_cast<decltype(part)>(part->SiblingNext);
} while (part != sg->Label); } 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) if (sg->Offset != nullptr)
{ {
int offset = IntConstFromNode(sg->Offset, c->Type()); int offset = IntConstFromNode(sg->Offset, c->Type());

View file

@ -368,15 +368,15 @@ FString &FString::CopyCStrPart(const char *tail, size_t tailLen)
return *this; return *this;
} }
void FString::Truncate(long newlen) void FString::Truncate(size_t newlen)
{ {
if (newlen <= 0) if (newlen == 0)
{ {
Data()->Release(); Data()->Release();
NullString.RefCount++; NullString.RefCount++;
Chars = &NullString.Nothing[0]; Chars = &NullString.Nothing[0];
} }
else if (newlen < (long)Len()) else if (newlen < Len())
{ {
ReallocBuffer (newlen); ReallocBuffer (newlen);
Chars[newlen] = '\0'; Chars[newlen] = '\0';

View file

@ -301,7 +301,7 @@ public:
bool IsEmpty() const { return Len() == 0; } bool IsEmpty() const { return Len() == 0; }
bool IsNotEmpty() 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); void Remove(size_t index, size_t remlen);
int Compare (const FString &other) const { return strcmp (Chars, other.Chars); } int Compare (const FString &other) const { return strcmp (Chars, other.Chars); }

View file

@ -14,6 +14,7 @@
#include "zscript/menu/joystickmenu.txt" #include "zscript/menu/joystickmenu.txt"
#include "zscript/menu/playerdisplay.txt" #include "zscript/menu/playerdisplay.txt"
#include "zscript/menu/playermenu.txt" #include "zscript/menu/playermenu.txt"
#include "zscript/menu/textentermenu.txt"
#include "zscript/inventory/inventory.txt" #include "zscript/inventory/inventory.txt"
#include "zscript/inventory/inv_misc.txt" #include "zscript/inventory/inv_misc.txt"

View file

@ -1083,22 +1083,24 @@ class OptionMenuFieldBase : OptionMenuItem
class OptionMenuItemTextField : OptionMenuFieldBase class OptionMenuItemTextField : OptionMenuFieldBase
{ {
TextEnterMenu mEnter;
OptionMenuItemTextField Init (String label, Name command, CVar graycheck = null) OptionMenuItemTextField Init (String label, Name command, CVar graycheck = null)
{ {
Super.Init(label, command, graycheck); Super.Init(label, command, graycheck);
mEntering = false; mEnter = null;
return self; return self;
} }
override String Represent() override String Represent()
{ {
if (mEntering) return mEditName .. SmallFont.GetCursor(); if (mEnter) return mEnter.GetText() .. SmallFont.GetCursor();
else return GetCVarString(); else return GetCVarString();
} }
override int Draw(OptionMenuDescriptor desc, int y, int indent, bool selected) 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. // reposition the text so that the cursor is visible when in entering mode.
String text = Represent(); String text = Represent();
@ -1114,30 +1116,23 @@ class OptionMenuItemTextField : OptionMenuFieldBase
if (mkey == Menu.MKEY_Enter) if (mkey == Menu.MKEY_Enter)
{ {
Menu.MenuSound("menu/choose"); Menu.MenuSound("menu/choose");
mEditName = GetCVarString(); mEnter = TextEnterMenu.Open(Menu.GetCurrentMenu(), GetCVarString(), -1, 2, fromcontroller);
mEntering = true;
//Menu* input = new DTextEnterMenu (Menu.CurrentMenu, mEditName, sizeof mEditName, 2, fromcontroller);
//M_ActivateMenu(input);
return true; return true;
} }
else if (mkey == Menu.MKEY_Input) else if (mkey == Menu.MKEY_Input)
{ {
if (mCVar) mCVar.SetString(mEditName); if (mCVar) mCVar.SetString(mEnter.GetText());
mEntering = false; mEnter = null;
return true; return true;
} }
else if (mkey == Menu.MKEY_Abort) else if (mkey == Menu.MKEY_Abort)
{ {
mEntering = false; mEnter = null;
return true; return true;
} }
return Super.MenuEvent(mkey, fromcontroller); return Super.MenuEvent(mkey, fromcontroller);
} }
bool mEntering;
String mEditName;
} }

View file

@ -46,8 +46,7 @@ class ListMenuItemPlayerNameBox : ListMenuItemSelectable
int mFontColor; int mFontColor;
int mFrameSize; int mFrameSize;
String mPlayerName; String mPlayerName;
String mEditName; TextEnterMenu mEnter;
bool mEntering;
//============================================================================= //=============================================================================
// //
@ -63,7 +62,7 @@ class ListMenuItemPlayerNameBox : ListMenuItemSelectable
mFontColor = desc.mFontColor; mFontColor = desc.mFontColor;
mFrameSize = frameofs; mFrameSize = frameofs;
mPlayerName = ""; mPlayerName = "";
mEntering = false; mEnter = null;
} }
//============================================================================= //=============================================================================
@ -80,7 +79,7 @@ class ListMenuItemPlayerNameBox : ListMenuItemSelectable
mFontColor = color; mFontColor = color;
mFrameSize = frameofs; mFrameSize = frameofs;
mPlayerName = ""; mPlayerName = "";
mEntering = false; mEnter = null;
} }
//============================================================================= //=============================================================================
@ -164,13 +163,13 @@ class ListMenuItemPlayerNameBox : ListMenuItemSelectable
// Draw player name box // Draw player name box
int x = mXpos + mFont.StringWidth(text) + 16 + mFrameSize; int x = mXpos + mFont.StringWidth(text) + 16 + mFrameSize;
DrawBorder (x, mYpos - mFrameSize, MAXPLAYERNAME+1); DrawBorder (x, mYpos - mFrameSize, MAXPLAYERNAME+1);
if (!mEntering) if (!mEnter)
{ {
screen.DrawText (SmallFont, Font.CR_UNTRANSLATED, x + mFrameSize, mYpos, mPlayerName, DTA_Clean, true); screen.DrawText (SmallFont, Font.CR_UNTRANSLATED, x + mFrameSize, mYpos, mPlayerName, DTA_Clean, true);
} }
else 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); 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) if (mkey == Menu.MKEY_Enter)
{ {
Menu.MenuSound ("menu/choose"); Menu.MenuSound ("menu/choose");
mEditName = mPlayerName; mEnter = TextEnterMenu.Open(Menu.GetCurrentMenu(), mPlayerName, MAXPLAYERNAME, 2, fromcontroller);
mEntering = true;
//DMenu *input = new DTextEnterMenu(Menu.CurrentMenu, mEditName, MAXPLAYERNAME, 2, fromcontroller);
//M_ActivateMenu(input);
return true; return true;
} }
else if (mkey == Menu.MKEY_Input) else if (mkey == Menu.MKEY_Input)
{ {
mPlayerName = mEditName; mPlayerName = mEnter.GetText();
mEntering = false; mEnter = null;
return true; return true;
} }
else if (mkey == Menu.MKEY_Abort) else if (mkey == Menu.MKEY_Abort)
{ {
mEntering = false; mEnter = null;
return true; return true;
} }
return false; return false;

View file

@ -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;
}
}