- 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();
if (holdstring.Len() > 0 && holdstring[holdstring.Len()-1] == '\\')
{
holdstring.Truncate((long)holdstring.Len()-1);
holdstring.Truncate(holdstring.Len()-1);
Line2 = igets ();
}
else

View file

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

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

View file

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

View file

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

View file

@ -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;
@ -371,3 +374,23 @@ void DTextEnterMenu::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
{ // Move hanging ( characters to the new line
Str.Truncate(long(Str.Len() - ConsecOpens));
Str.Truncate(Str.Len() - ConsecOpens);
NestDepth -= ConsecOpens;
}
Str << '\n';

View file

@ -2598,7 +2598,7 @@ void ZCCCompiler::CompileStates()
statename << FName(part->Id) << '.';
part = static_cast<decltype(part)>(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());

View file

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

View file

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

View file

@ -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"

View file

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

View file

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

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