Skip to content

Commit

Permalink
Move everything else to the new textinput system
Browse files Browse the repository at this point in the history
This commit makes the system support single-line text input, and moves
every remaining text input to it. This disables selection and cursor
movement for single line input, since drawing that seems a bit hard
right now.
  • Loading branch information
AllyTally committed Apr 3, 2023
1 parent 68ea2b1 commit bf547ca
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 226 deletions.
117 changes: 48 additions & 69 deletions desktop_version/src/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,33 +246,36 @@ static void editormenurender(int tr, int tg, int tb)
break;
case Menu::ed_desc:
{
const std::string input_text = key.keybuffer + ((ed.entframe < 2) ? "_" : " ");
const char* cursor = (TextInput::flash_timer < 15) ? "_" : " ";

bool title_is_gettext;
std::string title = translate_title(cl.title, &title_is_gettext);

if (ed.current_text_mode == TEXT_TITLE)
{
font::print(PR_2X | PR_CEN | PR_FONT_LEVEL, -1, 35, input_text, tr, tg, tb);
}
else
{
bool title_is_gettext;
std::string title = translate_title(cl.title, &title_is_gettext);
font::print(PR_2X | PR_CEN | (title_is_gettext ? PR_FONT_INTERFACE : PR_FONT_LEVEL), -1, 35, title, tr, tg, tb);
title += cursor;
}

font::print(PR_2X | PR_CEN | (title_is_gettext ? PR_FONT_INTERFACE : PR_FONT_LEVEL), -1, 35, title, tr, tg, tb);

bool creator_is_gettext = false;
std::string creator = (ed.current_text_mode == TEXT_CREATOR) ? input_text : translate_creator(cl.creator, &creator_is_gettext);
std::string creator = translate_creator(cl.creator, &creator_is_gettext);
if (ed.current_text_mode == TEXT_CREATOR)
{
creator += cursor;
}

int sp = SDL_max(10, font::height(PR_FONT_LEVEL));
graphics.print_level_creator((creator_is_gettext ? PR_FONT_INTERFACE : PR_FONT_LEVEL), 60, creator, tr, tg, tb);

font::print(PR_CEN | PR_FONT_LEVEL, -1, 60 + sp, (ed.current_text_mode == TEXT_WEBSITE) ? input_text : cl.website, tr, tg, tb);
font::print(PR_CEN | PR_FONT_LEVEL, -1, 60 + sp * 3, (ed.current_text_mode == TEXT_DESC1) ? input_text : cl.Desc1, tr, tg, tb);
font::print(PR_CEN | PR_FONT_LEVEL, -1, 60 + sp * 4, (ed.current_text_mode == TEXT_DESC2) ? input_text : cl.Desc2, tr, tg, tb);
font::print(PR_CEN | PR_FONT_LEVEL, -1, 60 + sp, cl.website + ((ed.current_text_mode == TEXT_WEBSITE) ? cursor : ""), tr, tg, tb);
font::print(PR_CEN | PR_FONT_LEVEL, -1, 60 + sp * 3, cl.Desc1 + ((ed.current_text_mode == TEXT_DESC1) ? cursor : ""), tr, tg, tb);
font::print(PR_CEN | PR_FONT_LEVEL, -1, 60 + sp * 4, cl.Desc2 + ((ed.current_text_mode == TEXT_DESC2) ? cursor : ""), tr, tg, tb);


if (ed.current_text_mode == TEXT_DESC3)
{
font::print(PR_CEN | PR_FONT_LEVEL, -1, 60 + sp * 5, input_text, tr, tg, tb);
font::print(PR_CEN | PR_FONT_LEVEL, -1, 60 + sp * 5, cl.Desc3 + cursor, tr, tg, tb);
}
else if (sp <= 10)
{
Expand Down Expand Up @@ -1492,16 +1495,10 @@ void editorrender(void)
graphics.fill_rect(0, 238 - textheight, 320, 240, graphics.getRGB(32, 32, 32));
graphics.fill_rect(0, 239 - textheight, 320, 240, graphics.getRGB(0, 0, 0));
font::print_wrap(0, 4, 240 - textheight, wrapped.c_str(), 255, 255, 255, 8, 312);
std::string input = key.keybuffer;
if (ed.entframe < 2)
{
input += "_";
}
else
{
input += " ";
}
font::print(PR_CEN | PR_FONT_LEVEL | PR_CJK_HIGH, -1, 232, input, 196, 196, 255 - help.glow);

const char* cursor = (TextInput::flash_timer < 15) ? "_" : " ";

font::print(PR_CEN | PR_FONT_LEVEL | PR_CJK_HIGH, -1, 232, *ed.current_text_ptr + cursor, 196, 196, 255 - help.glow);
break;
}
case EditorSubState_DRAW_WARPTOKEN:
Expand Down Expand Up @@ -1593,7 +1590,7 @@ void editorrender(void)
const int w = TextInput::cursor_pos.x - x;
const int h = TextInput::cursor_pos.y - y;

const Selection_Rect rect = TextInput::reorder_selection_positions();
const SelectionRect rect = TextInput::reorder_selection_positions();

if (h == 0)
{
Expand Down Expand Up @@ -1812,14 +1809,12 @@ static void input_submitted(void)
{
extern editorclass ed;

*ed.current_text_ptr = key.keybuffer;

ed.help_open = false;
ed.shiftkey = false;

bool reset_text_mode = true;

key.disabletextentry();
TextInput::detach_input();

ed.substate = EditorSubState_MAIN;

Expand Down Expand Up @@ -1906,42 +1901,33 @@ static void input_submitted(void)
}
break;
case TEXT_TITLE:
cl.title = key.keybuffer;
if (cl.title == "")
{
cl.title = "Untitled Level";
}
break;
case TEXT_CREATOR:
cl.creator = key.keybuffer;
if (cl.creator == "")
{
cl.creator = "Unknown";
}
break;
case TEXT_WEBSITE:
cl.website = key.keybuffer;
break;
case TEXT_DESC1:
cl.Desc1 = key.keybuffer;
ed.current_text_mode = TEXT_DESC2;
ed.substate = EditorSubState_MENU_INPUT;
reset_text_mode = false;
key.enabletextentry();
ed.current_text_ptr = &(key.keybuffer);
key.keybuffer = cl.Desc2;

TextInput::attach_input(&cl.Desc2);
break;
case TEXT_DESC2:
cl.Desc2 = key.keybuffer;

if (font::height(PR_FONT_LEVEL) <= 10)
{
ed.current_text_mode = TEXT_DESC3;
key.enabletextentry();
TextInput::attach_input(&cl.Desc3);
ed.substate = EditorSubState_MENU_INPUT;
reset_text_mode = false;
ed.current_text_ptr = &(key.keybuffer);
key.keybuffer = cl.Desc3;
}
else
{
Expand All @@ -1950,7 +1936,6 @@ static void input_submitted(void)

break;
case TEXT_DESC3:
cl.Desc3 = key.keybuffer;
break;
default:
break;
Expand Down Expand Up @@ -2427,17 +2412,14 @@ static void editormenuactionpress(void)

ed.current_text_mode = TEXT_TITLE;
ed.substate = EditorSubState_MENU_INPUT;
key.enabletextentry();
ed.current_text_ptr = &(key.keybuffer);

if (title_is_gettext)
{
key.keybuffer = "";
}
else
{
key.keybuffer = cl.title;
cl.title = "";
}

TextInput::attach_input(&cl.title);

break;
}
case 1:
Expand All @@ -2447,31 +2429,25 @@ static void editormenuactionpress(void)

ed.current_text_mode = TEXT_CREATOR;
ed.substate = EditorSubState_MENU_INPUT;
key.enabletextentry();
ed.current_text_ptr = &(key.keybuffer);

if (creator_is_gettext)
{
key.keybuffer = "";
}
else
{
key.keybuffer = cl.creator;
cl.creator = "";
}

TextInput::attach_input(&cl.creator);
break;
}
case 2:
ed.current_text_mode = TEXT_DESC1;
ed.substate = EditorSubState_MENU_INPUT;
key.enabletextentry();
ed.current_text_ptr = &(key.keybuffer);
key.keybuffer = cl.Desc1;

TextInput::attach_input(&cl.Desc1);
break;
case 3:
ed.current_text_mode = TEXT_WEBSITE;
ed.substate = EditorSubState_MENU_INPUT;
key.enabletextentry();
ed.current_text_ptr = &(key.keybuffer);
key.keybuffer=cl.website;
TextInput::attach_input(&cl.website);
break;
case 4:
game.createmenu(Menu::ed_font);
Expand Down Expand Up @@ -2901,20 +2877,22 @@ void editorclass::get_input_line(const enum TextMode mode, const std::string& pr
{
state = EditorState_DRAW;
substate = EditorSubState_DRAW_INPUT;
current_text_mode = mode;
current_text_ptr = ptr;
current_text_desc = prompt;
key.enabletextentry();

if (ptr)
{
key.keybuffer = *ptr;
TextInput::attach_input(ptr);
current_text_ptr = ptr;
}
else
{
key.keybuffer = "";
current_text_ptr = &(key.keybuffer);
TextInput::attach_input(&key.keybuffer);
current_text_ptr = &key.keybuffer;
}

current_text_mode = mode;
current_text_desc = prompt;

old_entity_text = key.keybuffer;
}

Expand Down Expand Up @@ -3247,7 +3225,8 @@ void editorinput(void)
if (escape_pressed)
{
// Cancel it, and remove the enemy it's tied to if necessary
key.disabletextentry();
TextInput::detach_input();

if (ed.current_text_mode >= FIRST_ENTTEXT && ed.current_text_mode <= LAST_ENTTEXT)
{
*ed.current_text_ptr = ed.old_entity_text;
Expand Down Expand Up @@ -3360,11 +3339,11 @@ void editorinput(void)
break;

case EditorSubState_MENU_INPUT:
if (escape_pressed && key.textentry())
if (escape_pressed && TextInput::taking_input)
{
ed.substate = EditorSubState_MAIN;
key.disabletextentry();
ed.current_text_mode = TEXT_NONE;
TextInput::detach_input();

music.playef(11);
}
Expand Down
1 change: 0 additions & 1 deletion desktop_version/src/Editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ class editorclass
int note_timer;
int old_note_timer;
std::string note;
std::string keybuffer;
std::string filename;
std::string loaded_filepath;

Expand Down
60 changes: 0 additions & 60 deletions desktop_version/src/KeyPoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,12 @@ KeyPoll::KeyPoll(void)
leftbutton=0; rightbutton=0; middlebutton=0;
mx=0; my=0;
resetWindow = 0;
pressedbackspace=false;

linealreadyemptykludge = false;

isActive = true;
}

void KeyPoll::enabletextentry(void)
{
keybuffer="";
SDL_StartTextInput();
}

void KeyPoll::disabletextentry(void)
{
SDL_StopTextInput();
}

bool KeyPoll::textentry(void)
{
return SDL_IsTextInputActive() == SDL_TRUE && !TextInput::taking_input;
}

void KeyPoll::toggleFullscreen(void)
{
gameScreen.toggleFullScreen();
Expand Down Expand Up @@ -152,11 +135,6 @@ void KeyPoll::Poll(void)
{
keymap[evt.key.keysym.sym] = true;

if (evt.key.keysym.sym == SDLK_BACKSPACE)
{
pressedbackspace = true;
}

#ifdef __APPLE__ /* OSX prefers the command keys over the alt keys. -flibit */
altpressed = keymap[SDLK_LGUI] || keymap[SDLK_RGUI];
#else
Expand All @@ -178,50 +156,12 @@ void KeyPoll::Poll(void)
}

BUTTONGLYPHS_keyboard_set_active(true);

if (textentry())
{
if (evt.key.keysym.sym == SDLK_BACKSPACE && !keybuffer.empty())
{
keybuffer.erase(UTF8_backspace(keybuffer.c_str(), keybuffer.length()));
if (keybuffer.empty())
{
linealreadyemptykludge = true;
}
}
else if ( evt.key.keysym.sym == SDLK_v &&
keymap[SDLK_LCTRL] )
{
char* text = SDL_GetClipboardText();
if (text != NULL)
{
keybuffer += text;
VVV_free(text);
}
}
else if ( evt.key.keysym.sym == SDLK_x &&
keymap[SDLK_LCTRL] )
{
if (SDL_SetClipboardText(keybuffer.c_str()) == 0)
{
keybuffer = "";
}
}
}
break;
}
case SDL_KEYUP:
keymap[evt.key.keysym.sym] = false;
if (evt.key.keysym.sym == SDLK_BACKSPACE)
{
pressedbackspace = false;
}
break;
case SDL_TEXTINPUT:
if (!altpressed)
{
keybuffer += evt.text.text;
}
break;

/* Mouse Input */
Expand Down
6 changes: 0 additions & 6 deletions desktop_version/src/KeyPoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ class KeyPoll

KeyPoll(void);

void enabletextentry(void);

void disabletextentry(void);

void Poll(void);

bool isDown(SDL_Keycode key);
Expand All @@ -65,8 +61,6 @@ class KeyPoll
int leftbutton, rightbutton, middlebutton;
int mx, my;

bool textentry(void);
bool pressedbackspace;
std::string keybuffer;

bool linealreadyemptykludge;
Expand Down
Loading

0 comments on commit bf547ca

Please sign in to comment.