2017-02-18 20:25:19 +00:00
|
|
|
/*
|
|
|
|
** menuinput.cpp
|
|
|
|
** The string input code
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2001-2010 Randy Heit
|
|
|
|
** Copyright 2010-2017 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
2017-02-12 20:45:37 +00:00
|
|
|
|
2017-02-18 20:25:19 +00:00
|
|
|
|
|
|
|
class TextEnterMenu : Menu
|
2017-02-12 20:45:37 +00:00
|
|
|
{
|
2017-02-18 19:12:06 +00:00
|
|
|
const INPUTGRID_WIDTH = 13;
|
|
|
|
const INPUTGRID_HEIGHT = 5;
|
|
|
|
|
|
|
|
const Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-=.,!?@'\":;[]()<>^#$%&*/_ \b";
|
|
|
|
|
2017-02-18 20:25:19 +00:00
|
|
|
String mEnterString;
|
|
|
|
int mEnterSize;
|
|
|
|
int mEnterPos;
|
|
|
|
bool mInputGridOkay;
|
|
|
|
int InputGridX;
|
|
|
|
int InputGridY;
|
2019-02-18 23:08:23 +00:00
|
|
|
int CursorSize;
|
2017-02-18 20:25:19 +00:00
|
|
|
bool AllowColors;
|
2019-02-16 20:29:46 +00:00
|
|
|
Font displayFont;
|
2017-02-12 20:45:37 +00:00
|
|
|
|
2017-02-18 20:18:23 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
// [TP] Added allowcolors
|
2019-02-18 23:08:23 +00:00
|
|
|
private void Init(Menu parent, Font dpf, String textbuffer, int maxlen, bool showgrid, bool allowcolors)
|
2017-02-18 20:18:23 +00:00
|
|
|
{
|
|
|
|
Super.init(parent);
|
|
|
|
mEnterString = textbuffer;
|
2019-02-18 23:08:23 +00:00
|
|
|
mEnterSize = maxlen;
|
2017-09-10 11:03:41 +00:00
|
|
|
mInputGridOkay = (showgrid && (m_showinputgrid == 0)) || (m_showinputgrid >= 1);
|
2017-02-18 20:18:23 +00:00
|
|
|
if (mEnterString.Length() > 0)
|
|
|
|
{
|
|
|
|
InputGridX = INPUTGRID_WIDTH - 1;
|
|
|
|
InputGridY = INPUTGRID_HEIGHT - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If we are naming a new save, don't start the cursor on "end".
|
|
|
|
InputGridX = 0;
|
|
|
|
InputGridY = 0;
|
|
|
|
}
|
|
|
|
AllowColors = allowcolors; // [TP]
|
2019-02-18 23:08:23 +00:00
|
|
|
displayFont = dpf;
|
|
|
|
CursorSize = displayFont.StringWidth(displayFont.GetCursor());
|
2017-02-18 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 23:08:23 +00:00
|
|
|
// This had to be deprecated because the unit for maxlen is 8 pixels.
|
|
|
|
deprecated("3.8") static TextEnterMenu Open(Menu parent, String textbuffer, int maxlen, int sizemode, bool showgrid = false, bool allowcolors = false)
|
2017-02-18 20:18:23 +00:00
|
|
|
{
|
|
|
|
let me = new("TextEnterMenu");
|
2019-03-17 15:58:10 +00:00
|
|
|
me.Init(parent, Menu.OptionFont(), textbuffer, maxlen*8, showgrid, allowcolors);
|
2017-02-18 20:18:23 +00:00
|
|
|
return me;
|
|
|
|
}
|
|
|
|
|
2019-02-18 23:08:23 +00:00
|
|
|
static TextEnterMenu OpenTextEnter(Menu parent, Font displayfnt, String textbuffer, int maxlen, bool showgrid = false, bool allowcolors = false)
|
|
|
|
{
|
|
|
|
let me = new("TextEnterMenu");
|
|
|
|
me.Init(parent, displayfnt, textbuffer, maxlen, showgrid, allowcolors);
|
|
|
|
return me;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-18 20:18:23 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 19:12:06 +00:00
|
|
|
String GetText()
|
|
|
|
{
|
|
|
|
return mEnterString;
|
|
|
|
}
|
|
|
|
|
2017-02-12 20:45:37 +00:00
|
|
|
override bool TranslateKeyboardEvents()
|
|
|
|
{
|
|
|
|
return mInputGridOkay;
|
|
|
|
}
|
2017-02-18 19:12:06 +00:00
|
|
|
|
2017-02-18 20:18:23 +00:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-03-06 21:27:51 +00:00
|
|
|
override bool OnUIEvent(UIEvent ev)
|
2017-02-18 20:18:23 +00:00
|
|
|
{
|
2017-03-06 21:27:51 +00:00
|
|
|
// Save game and player name string input
|
|
|
|
if (ev.Type == UIEvent.Type_Char)
|
2017-02-18 20:18:23 +00:00
|
|
|
{
|
2017-03-06 21:27:51 +00:00
|
|
|
mInputGridOkay = false;
|
2019-02-18 23:08:23 +00:00
|
|
|
AppendChar(ev.KeyChar);
|
2017-03-06 21:27:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int ch = ev.KeyChar;
|
|
|
|
if ((ev.Type == UIEvent.Type_KeyDown || ev.Type == UIEvent.Type_KeyRepeat) && ch == 8)
|
|
|
|
{
|
|
|
|
if (mEnterString.Length() > 0)
|
2017-02-18 20:18:23 +00:00
|
|
|
{
|
2019-02-16 20:29:46 +00:00
|
|
|
mEnterString.DeleteLastCharacter();
|
2017-02-18 20:18:23 +00:00
|
|
|
}
|
2017-03-06 21:27:51 +00:00
|
|
|
}
|
|
|
|
else if (ev.Type == UIEvent.Type_KeyDown)
|
|
|
|
{
|
|
|
|
if (ch == UIEvent.Key_ESCAPE)
|
2017-02-18 20:18:23 +00:00
|
|
|
{
|
2017-03-06 21:27:51 +00:00
|
|
|
Menu parent = mParentMenu;
|
|
|
|
Close();
|
|
|
|
parent.MenuEvent(MKEY_Abort, false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ch == 13)
|
|
|
|
{
|
|
|
|
if (mEnterString.Length() > 0)
|
2017-02-18 20:18:23 +00:00
|
|
|
{
|
2017-03-06 21:27:51 +00:00
|
|
|
// [TP] If we allow color codes, colorize the string now.
|
|
|
|
if (AllowColors)
|
|
|
|
mEnterString = mEnterString.Filter();
|
|
|
|
|
2017-02-18 20:18:23 +00:00
|
|
|
Menu parent = mParentMenu;
|
2017-03-06 21:27:51 +00:00
|
|
|
parent.MenuEvent(MKEY_Input, false);
|
2017-02-18 20:18:23 +00:00
|
|
|
Close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-06 21:27:51 +00:00
|
|
|
if (ev.Type == UIEvent.Type_KeyDown || ev.Type == UIEvent.Type_KeyRepeat)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return Super.OnUIEvent(ev);
|
2017-02-18 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
override bool MouseEvent(int type, int x, int y)
|
|
|
|
{
|
2019-03-31 08:06:39 +00:00
|
|
|
int cell_width = 18 * CleanXfac_1;
|
|
|
|
int cell_height = 16 * CleanYfac_1;
|
2017-02-19 21:08:22 +00:00
|
|
|
int screen_y = screen.GetHeight() - INPUTGRID_HEIGHT * cell_height;
|
|
|
|
int screen_x = (screen.GetWidth() - INPUTGRID_WIDTH * cell_width) / 2;
|
2017-02-18 20:18:23 +00:00
|
|
|
|
2017-02-19 21:08:22 +00:00
|
|
|
if (x >= screen_x && x < screen_x + INPUTGRID_WIDTH * cell_width && y >= screen_y)
|
|
|
|
{
|
|
|
|
InputGridX = (x - screen_x) / cell_width;
|
|
|
|
InputGridY = (y - screen_y) / cell_height;
|
|
|
|
if (type == MOUSE_Release)
|
2017-02-18 20:18:23 +00:00
|
|
|
{
|
2017-02-19 21:08:22 +00:00
|
|
|
if (MenuEvent(MKEY_Enter, true))
|
2017-02-18 20:18:23 +00:00
|
|
|
{
|
2017-02-19 21:08:22 +00:00
|
|
|
MenuSound("menu/choose");
|
|
|
|
if (m_use_mouse == 2) InputGridX = InputGridY = -1;
|
2017-02-18 20:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-19 21:08:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
InputGridX = InputGridY = -1;
|
2017-02-18 20:18:23 +00:00
|
|
|
}
|
|
|
|
return Super.MouseEvent(type, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2019-02-18 23:08:23 +00:00
|
|
|
private void AppendChar(int ch)
|
|
|
|
{
|
2019-02-18 23:27:47 +00:00
|
|
|
String newstring = String.Format("%s%c%s", mEnterString, ch, displayFont.GetCursor());
|
2019-02-18 23:08:23 +00:00
|
|
|
if (mEnterSize < 0 || displayFont.StringWidth(newstring) < mEnterSize)
|
|
|
|
{
|
|
|
|
mEnterString.AppendCharacter(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-18 20:18:23 +00:00
|
|
|
override bool MenuEvent (int key, bool fromcontroller)
|
|
|
|
{
|
|
|
|
String InputGridChars = Chars;
|
|
|
|
if (key == MKEY_Back)
|
|
|
|
{
|
|
|
|
mParentMenu.MenuEvent(MKEY_Abort, false);
|
|
|
|
return Super.MenuEvent(key, fromcontroller);
|
|
|
|
}
|
|
|
|
if (fromcontroller)
|
|
|
|
{
|
|
|
|
mInputGridOkay = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInputGridOkay)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
if (InputGridX == -1 || InputGridY == -1)
|
|
|
|
{
|
|
|
|
InputGridX = InputGridY = 0;
|
|
|
|
}
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case MKEY_Down:
|
|
|
|
InputGridY = (InputGridY + 1) % INPUTGRID_HEIGHT;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case MKEY_Up:
|
|
|
|
InputGridY = (InputGridY + INPUTGRID_HEIGHT - 1) % INPUTGRID_HEIGHT;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case MKEY_Right:
|
|
|
|
InputGridX = (InputGridX + 1) % INPUTGRID_WIDTH;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case MKEY_Left:
|
|
|
|
InputGridX = (InputGridX + INPUTGRID_WIDTH - 1) % INPUTGRID_WIDTH;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case MKEY_Clear:
|
|
|
|
if (mEnterString.Length() > 0)
|
|
|
|
{
|
2019-03-31 07:47:43 +00:00
|
|
|
mEnterString.DeleteLastCharacter();
|
2017-02-18 20:18:23 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case MKEY_Enter:
|
|
|
|
if (mInputGridOkay)
|
|
|
|
{
|
2019-04-13 07:31:36 +00:00
|
|
|
int ch = InputGridChars.ByteAt(InputGridX + InputGridY * INPUTGRID_WIDTH);
|
2017-02-18 20:18:23 +00:00
|
|
|
if (ch == 0) // end
|
|
|
|
{
|
|
|
|
if (mEnterString.Length() > 0)
|
|
|
|
{
|
|
|
|
Menu parent = mParentMenu;
|
|
|
|
parent.MenuEvent(MKEY_Input, false);
|
2017-02-19 21:08:22 +00:00
|
|
|
Close();
|
2017-02-18 20:18:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ch == 8) // bs
|
|
|
|
{
|
|
|
|
if (mEnterString.Length() > 0)
|
|
|
|
{
|
2019-02-18 23:08:23 +00:00
|
|
|
mEnterString.DeleteLastCharacter();
|
2017-02-18 20:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-18 23:08:23 +00:00
|
|
|
else
|
2017-02-18 20:18:23 +00:00
|
|
|
{
|
2019-02-18 23:08:23 +00:00
|
|
|
AppendChar(ch);
|
2017-02-18 20:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
2019-03-31 07:47:43 +00:00
|
|
|
break;
|
2017-02-18 20:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-18 19:12:06 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
override void Drawer ()
|
|
|
|
{
|
|
|
|
mParentMenu.Drawer();
|
|
|
|
if (mInputGridOkay)
|
|
|
|
{
|
|
|
|
String InputGridChars = Chars;
|
2019-03-31 08:06:39 +00:00
|
|
|
int cell_width = 18 * CleanXfac_1;
|
|
|
|
int cell_height = 16 * CleanYfac_1;
|
|
|
|
int top_padding = cell_height / 2 - displayFont.GetHeight() * CleanYfac_1 / 2;
|
2017-02-18 19:12:06 +00:00
|
|
|
|
|
|
|
// Darken the background behind the character grid.
|
|
|
|
screen.Dim(0, 0.8, 0, screen.GetHeight() - INPUTGRID_HEIGHT * cell_height, screen.GetWidth(), INPUTGRID_HEIGHT * cell_height);
|
|
|
|
|
|
|
|
if (InputGridX >= 0 && InputGridY >= 0)
|
|
|
|
{
|
|
|
|
// Highlight the background behind the selected character.
|
|
|
|
screen.Dim(Color(255,248,220), 0.6,
|
|
|
|
InputGridX * cell_width - INPUTGRID_WIDTH * cell_width / 2 + screen.GetWidth() / 2,
|
|
|
|
InputGridY * cell_height - INPUTGRID_HEIGHT * cell_height + screen.GetHeight(),
|
|
|
|
cell_width, cell_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int y = 0; y < INPUTGRID_HEIGHT; ++y)
|
|
|
|
{
|
|
|
|
int yy = y * cell_height - INPUTGRID_HEIGHT * cell_height + screen.GetHeight();
|
|
|
|
for (int x = 0; x < INPUTGRID_WIDTH; ++x)
|
|
|
|
{
|
|
|
|
int xx = x * cell_width - INPUTGRID_WIDTH * cell_width / 2 + screen.GetWidth() / 2;
|
2019-04-13 07:31:36 +00:00
|
|
|
int ch = InputGridChars.ByteAt(y * INPUTGRID_WIDTH + x);
|
2019-02-16 20:29:46 +00:00
|
|
|
int width = displayFont.GetCharWidth(ch);
|
2017-02-18 19:12:06 +00:00
|
|
|
|
|
|
|
// The highlighted character is yellow; the rest are dark gray.
|
|
|
|
int colr = (x == InputGridX && y == InputGridY) ? Font.CR_YELLOW : Font.CR_DARKGRAY;
|
|
|
|
Color palcolor = (x == InputGridX && y == InputGridY) ? Color(160, 120, 0) : Color(120, 120, 120);
|
|
|
|
|
|
|
|
if (ch > 32)
|
|
|
|
{
|
|
|
|
// Draw a normal character.
|
2019-03-31 08:06:39 +00:00
|
|
|
screen.DrawChar(displayFont, colr, xx + cell_width/2 - width*CleanXfac_1/2, yy + top_padding, ch, DTA_CleanNoMove_1, true);
|
2017-02-18 19:12:06 +00:00
|
|
|
}
|
|
|
|
else if (ch == 32)
|
|
|
|
{
|
|
|
|
// Draw the space as a box outline. We also draw it 50% wider than it really is.
|
2019-03-31 08:06:39 +00:00
|
|
|
int x1 = xx + cell_width/2 - width * CleanXfac_1 * 3 / 4;
|
|
|
|
int x2 = x1 + width * 3 * CleanXfac_1 / 2;
|
2017-02-18 19:12:06 +00:00
|
|
|
int y1 = yy + top_padding;
|
2019-03-31 08:06:39 +00:00
|
|
|
int y2 = y1 + displayFont.GetHeight() * CleanYfac_1;
|
|
|
|
screen.Clear(x1, y1, x2, y1+CleanYfac_1, palcolor); // top
|
|
|
|
screen.Clear(x1, y2, x2, y2+CleanYfac_1, palcolor); // bottom
|
|
|
|
screen.Clear(x1, y1+CleanYfac_1, x1+CleanXfac_1, y2, palcolor); // left
|
|
|
|
screen.Clear(x2-CleanXfac_1, y1+CleanYfac_1, x2, y2, palcolor); // right
|
2017-02-18 19:12:06 +00:00
|
|
|
}
|
|
|
|
else if (ch == 8 || ch == 0)
|
|
|
|
{
|
|
|
|
// Draw the backspace and end "characters".
|
2021-09-09 12:00:00 +00:00
|
|
|
String str = ch == 8 ? "←" : "↲";
|
2019-02-16 20:29:46 +00:00
|
|
|
screen.DrawText(displayFont, colr,
|
2019-03-31 08:06:39 +00:00
|
|
|
xx + cell_width/2 - displayFont.StringWidth(str)*CleanXfac_1/2,
|
|
|
|
yy + top_padding, str, DTA_CleanNoMove_1, true);
|
2017-02-18 19:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Super.Drawer();
|
|
|
|
}
|
|
|
|
|
2019-02-18 23:08:23 +00:00
|
|
|
}
|