Skip to content

Commit 964238a

Browse files
committed
Make script editor use new input system
The old input system still exists for one-line inputs, but the script editor really needs the upgrade. This commit fixes a few bugs with the input system, and makes the script editor use it.
1 parent 8bf5657 commit 964238a

File tree

6 files changed

+124
-92
lines changed

6 files changed

+124
-92
lines changed

desktop_version/src/Editor.cpp

Lines changed: 81 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "Music.h"
2222
#include "Screen.h"
2323
#include "Script.h"
24+
#include "TextInput.h"
2425
#include "UTF8.h"
2526
#include "UtilityClass.h"
2627
#include "VFormat.h"
@@ -103,8 +104,6 @@ void editorclass::reset(void)
103104

104105
clear_script_buffer();
105106

106-
script_cursor_x = 0;
107-
script_cursor_y = 0;
108107
script_offset = 0;
109108
lines_visible = 25;
110109
current_script = "null";
@@ -1509,6 +1508,7 @@ void editorrender(void)
15091508

15101509
// Draw text
15111510
int font_height = font::height(PR_FONT_LEVEL);
1511+
15121512
for (int i = 0; i < ed.lines_visible; i++)
15131513
{
15141514
if (i + ed.script_offset < (int) ed.script_buffer.size())
@@ -1517,10 +1517,77 @@ void editorrender(void)
15171517
}
15181518
}
15191519

1520+
// Draw selection
1521+
1522+
if (TextInput::selecting) {
1523+
int x = TextInput::cursor_select_pos.x;
1524+
int y = TextInput::cursor_select_pos.y;
1525+
int w = TextInput::cursor_pos.x - x;
1526+
int h = TextInput::cursor_pos.y - y;
1527+
1528+
if (h == 0)
1529+
{
1530+
Selection_Rect rect = TextInput::reorder_selection_positions();
1531+
char* offset_x = UTF8_substr(ed.script_buffer[rect.y].c_str(), 0, rect.x);
1532+
char* cut_string = UTF8_substr(ed.script_buffer[rect.y].c_str(), rect.x, rect.x2);
1533+
1534+
graphics.fill_rect(16 + font::len(PR_FONT_LEVEL, offset_x), 20 + (y - ed.script_offset) * font_height, font::len(PR_FONT_LEVEL,cut_string), font_height, graphics.getRGB(123, 111, 218));
1535+
font::print(PR_FONT_LEVEL | PR_CJK_LOW, 16 + font::len(PR_FONT_LEVEL, offset_x), 20 + (rect.y2 - ed.script_offset) * font_height, cut_string, 61, 48, 162);
1536+
1537+
SDL_free(offset_x);
1538+
SDL_free(cut_string);
1539+
}
1540+
else
1541+
{
1542+
Selection_Rect rect = TextInput::reorder_selection_positions();
1543+
1544+
const char* line = ed.script_buffer[rect.y].c_str();
1545+
1546+
char* offset_x = UTF8_substr(line, 0, rect.x);
1547+
char* selection_w = UTF8_substr(line, rect.x, UTF8_total_codepoints(line));
1548+
graphics.fill_rect(16 + font::len(PR_FONT_LEVEL, offset_x), 20 + (rect.y - ed.script_offset) * font_height, SDL_max(font::len(PR_FONT_LEVEL, selection_w), 1), font_height, graphics.getRGB(123, 111, 218));
1549+
font::print(PR_FONT_LEVEL | PR_CJK_LOW, 16 + font::len(PR_FONT_LEVEL, offset_x), 20 + (rect.y - ed.script_offset) * font_height, selection_w, 61, 48, 162);
1550+
SDL_free(offset_x);
1551+
SDL_free(selection_w);
1552+
for (int i = 1; i < rect.y2 - rect.y; i++)
1553+
{
1554+
if (rect.y + i - ed.script_offset < ed.lines_visible)
1555+
{
1556+
graphics.fill_rect(16, 20 + (rect.y + i - ed.script_offset) * font_height, SDL_max(font::len(PR_FONT_LEVEL, ed.script_buffer[rect.y + i].c_str()), 1), font_height, graphics.getRGB(123, 111, 218));
1557+
font::print(PR_FONT_LEVEL | PR_CJK_LOW, 16, 20 + (rect.y + i - ed.script_offset) * font_height, ed.script_buffer[rect.y + i].c_str(), 61, 48, 162);
1558+
}
1559+
}
1560+
1561+
if (rect.y2 - ed.script_offset < ed.lines_visible)
1562+
{
1563+
const char* line_2 = ed.script_buffer[rect.y2].c_str();
1564+
1565+
selection_w = UTF8_substr(line_2, 0, rect.x2);
1566+
graphics.fill_rect(16, 20 + (rect.y2 - ed.script_offset) * font_height, SDL_max(font::len(PR_FONT_LEVEL, selection_w), 1), font_height, graphics.getRGB(123, 111, 218));
1567+
font::print(PR_FONT_LEVEL | PR_CJK_LOW, 16, 20 + (rect.y2 - ed.script_offset) * font_height, selection_w, 61, 48, 162);
1568+
SDL_free(offset_x);
1569+
SDL_free(selection_w);
1570+
}
1571+
}
1572+
}
1573+
15201574
// Draw cursor
1521-
if (ed.entframe < 2)
1575+
if (TextInput::flash_timer < 15)
15221576
{
1523-
font::print(PR_FONT_LEVEL | PR_CJK_LOW, 16 + font::len(PR_FONT_LEVEL, ed.script_buffer[ed.script_cursor_y].c_str()), 20 + ((ed.script_cursor_y - ed.script_offset) * font_height), "_", 123, 111, 218);
1577+
char* substr = UTF8_substr(ed.script_buffer[TextInput::cursor_pos.y].c_str(), 0, TextInput::cursor_pos.x);
1578+
1579+
if (TextInput::cursor_pos.x < (int)ed.script_buffer[TextInput::cursor_pos.y].size() || TextInput::selecting)
1580+
{
1581+
graphics.set_color(123, 111, 218);
1582+
int x = 16 + font::len(PR_FONT_LEVEL, substr);
1583+
int y = 20 + ((TextInput::cursor_pos.y - ed.script_offset) * font_height);
1584+
SDL_RenderDrawLine(gameScreen.m_renderer, x, y, x, y + font_height - 1);
1585+
}
1586+
else
1587+
{
1588+
font::print(PR_FONT_LEVEL | PR_CJK_LOW, 16 + font::len(PR_FONT_LEVEL, substr), 20 + ((TextInput::cursor_pos.y - ed.script_offset) * font_height), "_", 123, 111, 218);
1589+
}
1590+
SDL_free(substr);
15241591
}
15251592
break;
15261593
}
@@ -1832,6 +1899,8 @@ void editorlogic(void)
18321899
ed.entframedelay = 8;
18331900
}
18341901

1902+
TextInput::flash_timer = (TextInput::flash_timer + 1) % 30;
1903+
18351904
ed.old_note_timer = ed.note_timer;
18361905
ed.note_timer = SDL_max(ed.note_timer - 1, 0);
18371906

@@ -2301,8 +2370,6 @@ static void editormenuactionpress(void)
23012370
ed.script_list_offset = 0;
23022371
ed.selected_script = 0;
23032372

2304-
ed.script_cursor_y = 0;
2305-
ed.script_cursor_x = 0;
23062373
ed.script_offset = 0;
23072374
ed.lines_visible = 200 / font::height(PR_FONT_LEVEL);
23082375
break;
@@ -3217,17 +3284,13 @@ void editorinput(void)
32173284
{
32183285
game.mapheld = true;
32193286
ed.substate = EditorSubState_SCRIPTS_EDIT;
3220-
key.enabletextentry();
3221-
key.keybuffer = "";
3222-
ed.current_text_ptr = &(key.keybuffer);
3287+
32233288
ed.current_script = script.customscripts[(script.customscripts.size() - 1) - ed.selected_script].name;
32243289
ed.load_script_in_editor(ed.current_script);
32253290

3226-
ed.script_cursor_y = ed.script_buffer.size() - 1;
3227-
ed.script_offset = SDL_max(ed.script_cursor_y - (ed.lines_visible - SCRIPT_LINE_PADDING), 0);
3291+
ed.script_offset = 0;
32283292

3229-
key.keybuffer = ed.script_buffer[ed.script_cursor_y];
3230-
ed.script_cursor_x = UTF8_total_codepoints(ed.script_buffer[ed.script_cursor_y].c_str());
3293+
TextInput::attach_input(&ed.script_buffer);
32313294

32323295
music.playef(11);
32333296
}
@@ -3244,41 +3307,11 @@ void editorinput(void)
32443307

32453308
// Alright, now re-add the script.
32463309
ed.create_script(ed.current_script, ed.script_buffer);
3310+
TextInput::detach_input();
32473311
}
32483312

32493313
if (ed.keydelay > 0) ed.keydelay--;
32503314

3251-
if (up_pressed && ed.keydelay <= 0)
3252-
{
3253-
ed.keydelay = 3;
3254-
ed.script_cursor_y = SDL_max(0, ed.script_cursor_y - 1);
3255-
3256-
key.keybuffer = ed.script_buffer[ed.script_cursor_y];
3257-
}
3258-
3259-
if (down_pressed && ed.keydelay <= 0)
3260-
{
3261-
ed.keydelay = 3;
3262-
ed.script_cursor_y = SDL_min((int) ed.script_buffer.size() - 1, ed.script_cursor_y + 1);
3263-
3264-
key.keybuffer = ed.script_buffer[ed.script_cursor_y];
3265-
}
3266-
3267-
if (key.linealreadyemptykludge)
3268-
{
3269-
ed.keydelay = 6;
3270-
key.linealreadyemptykludge = false;
3271-
}
3272-
3273-
if (key.pressedbackspace && ed.script_buffer[ed.script_cursor_y] == "" && ed.keydelay <= 0)
3274-
{
3275-
//Remove this line completely
3276-
ed.remove_line(ed.script_cursor_y);
3277-
ed.script_cursor_y = SDL_max(0, ed.script_cursor_y - 1);
3278-
key.keybuffer = ed.script_buffer[ed.script_cursor_y];
3279-
ed.keydelay = 6;
3280-
}
3281-
32823315
/* Remove all pipes, they are the line separator in the XML
32833316
* When this loop reaches the end, it wraps to SIZE_MAX; SIZE_MAX + 1 is 0 */
32843317
{size_t i; for (i = key.keybuffer.length() - 1; i + 1 > 0; --i)
@@ -3289,38 +3322,14 @@ void editorinput(void)
32893322
}
32903323
}}
32913324

3292-
ed.script_buffer[ed.script_cursor_y] = key.keybuffer;
3293-
ed.script_cursor_x = UTF8_total_codepoints(ed.script_buffer[ed.script_cursor_y].c_str());
3294-
3295-
if (enter_pressed)
3296-
{
3297-
//Continue to next line
3298-
if (ed.script_cursor_y >= (int)ed.script_buffer.size()) //we're on the last line
3299-
{
3300-
ed.script_cursor_y++;
3301-
3302-
key.keybuffer = ed.script_buffer[ed.script_cursor_y];
3303-
ed.script_cursor_x = UTF8_total_codepoints(ed.script_buffer[ed.script_cursor_y].c_str());
3304-
}
3305-
else
3306-
{
3307-
//We're not, insert a line instead
3308-
ed.script_cursor_y++;
3309-
3310-
ed.insert_line(ed.script_cursor_y);
3311-
key.keybuffer = "";
3312-
ed.script_cursor_x = 0;
3313-
}
3314-
}
3315-
3316-
if (ed.script_cursor_y < ed.script_offset + SCRIPT_LINE_PADDING)
3325+
if (TextInput::cursor_pos.y < ed.script_offset + SCRIPT_LINE_PADDING)
33173326
{
3318-
ed.script_offset = SDL_max(0, ed.script_cursor_y - SCRIPT_LINE_PADDING);
3327+
ed.script_offset = SDL_max(0, TextInput::cursor_pos.y - SCRIPT_LINE_PADDING);
33193328
}
33203329

3321-
if (ed.script_cursor_y > ed.script_offset + ed.lines_visible - SCRIPT_LINE_PADDING)
3330+
if (TextInput::cursor_pos.y > ed.script_offset + ed.lines_visible - SCRIPT_LINE_PADDING)
33223331
{
3323-
ed.script_offset = SDL_min((int) ed.script_buffer.size() - ed.lines_visible + SCRIPT_LINE_PADDING, ed.script_cursor_y - ed.lines_visible + SCRIPT_LINE_PADDING);
3332+
ed.script_offset = SDL_min((int) ed.script_buffer.size() - ed.lines_visible + SCRIPT_LINE_PADDING, TextInput::cursor_pos.y - ed.lines_visible + SCRIPT_LINE_PADDING);
33243333
}
33253334

33263335
break;

desktop_version/src/Editor.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ class editorclass
229229

230230
std::vector<std::string> script_buffer;
231231
std::string current_script;
232-
int script_cursor_x, script_cursor_y;
233232
int script_offset;
234233
int lines_visible;
235234

desktop_version/src/KeyPoll.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void KeyPoll::disabletextentry(void)
6767

6868
bool KeyPoll::textentry(void)
6969
{
70-
return SDL_IsTextInputActive() == SDL_TRUE;
70+
return SDL_IsTextInputActive() == SDL_TRUE && !TextInput::taking_input;
7171
}
7272

7373
void KeyPoll::toggleFullscreen(void)

desktop_version/src/TextInput.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ namespace TextInput {
2828
current_text = text;
2929
selecting = false;
3030
flash_timer = 0;
31+
SDL_StartTextInput();
3132

3233
send_cursor_to_end();
3334
}
3435

3536
void detach_input(void) {
3637
taking_input = false;
38+
SDL_StopTextInput();
3739
}
3840

3941
void send_cursor_to_end(void)
4042
{
41-
cursor_pos.x = current_text->size() - 1;
42-
cursor_pos.y = current_text->at(cursor_pos.x).size();
43+
cursor_pos.y = current_text->size() - 1;
44+
cursor_pos.x = UTF8_total_codepoints(current_text->at(cursor_pos.y).c_str());
4345
cursor_x_tallest = cursor_pos.x;
4446
}
4547

@@ -48,8 +50,8 @@ namespace TextInput {
4850
std::string output; // We'll need the output later
4951
while (std::getline(string_stream, output)) { // Iterate through all lines,
5052
output.erase(std::remove(output.begin(), output.end(), '\r'), output.end()); // Strip \r... dammit Windows.
51-
(*current_text)[cursor_pos.y].insert(cursor_pos.x, output); // Insert the current line of text into our text
52-
cursor_pos.x += output.length(); // Update cursor position
53+
current_text->at(cursor_pos.y).insert(cursor_pos.x, output); // Insert the current line of text into our text
54+
cursor_pos.x += UTF8_total_codepoints(output.c_str()); // Update cursor position
5355
if (!string_stream.eof()) { // If we haven't hit the end of the file,
5456
insert_newline(); // Insert a newline
5557
}
@@ -180,7 +182,7 @@ namespace TextInput {
180182
Selection_Rect rect = reorder_selection_positions();
181183

182184
if (rect.y == rect.y2) {
183-
return UTF8_substr(current_text->at(rect.y).c_str(), rect.x, rect.x2 - rect.x);
185+
return UTF8_substr(current_text->at(rect.y).c_str(), rect.x, rect.x2);
184186
}
185187

186188
char* select_part_first_line = UTF8_substr(current_text->at(rect.y).c_str(), rect.x, UTF8_total_codepoints(current_text->at(rect.y).c_str()) - rect.x);
@@ -244,9 +246,9 @@ namespace TextInput {
244246
return;
245247
}
246248
// Get the rest of the last line
247-
char* rest_of_string = UTF8_substr(current_text->at(y2).c_str(), x2, UTF8_total_codepoints(current_text->at(y2).c_str()) - x2);
249+
char* rest_of_string = UTF8_substr(current_text->at(y2).c_str(), x2, UTF8_total_codepoints(current_text->at(y2).c_str()));
248250

249-
for (int i = y2; i >= y; i--)
251+
for (int i = y2; i > y; i--)
250252
{
251253
if (cursor_pos.y >= i)
252254
{
@@ -262,10 +264,11 @@ namespace TextInput {
262264
}
263265

264266
// Erase from the start of the selection to the end
265-
char* erased = UTF8_erase(current_text->at(y).c_str(), x, x2 - x);
267+
char* erased = UTF8_erase(current_text->at(y).c_str(), x, UTF8_total_codepoints(current_text->at(y).c_str()));
266268
current_text->at(y) = erased;
267269
// Add the rest of the last line to the end of the first line
268270
current_text->at(y) += rest_of_string;
271+
cursor_pos.x = x;
269272
SDL_free(erased);
270273
SDL_free(rest_of_string);
271274
}
@@ -310,7 +313,7 @@ namespace TextInput {
310313
else
311314
{
312315
// Erase the character before the cursor
313-
char* erased = UTF8_erase(current_text->at(cursor_pos.y).c_str(), cursor_pos.x - 1, 1);
316+
char* erased = UTF8_erase(current_text->at(cursor_pos.y).c_str(), cursor_pos.x - 1, cursor_pos.x);
314317
current_text->at(cursor_pos.y) = erased;
315318
SDL_free(erased);
316319

@@ -324,6 +327,9 @@ namespace TextInput {
324327
if (!taking_input) return;
325328

326329
if (e.type == SDL_KEYDOWN) {
330+
// Show cursor!!
331+
flash_timer = 0;
332+
327333
// Handle backspace
328334
if (e.key.keysym.sym == SDLK_BACKSPACE)
329335
{
@@ -374,6 +380,9 @@ namespace TextInput {
374380
//Special text input event
375381
else if (e.type == SDL_TEXTINPUT)
376382
{
383+
// Show cursor!!
384+
flash_timer = 0;
385+
377386
//Append character(s)
378387
if (selecting) {
379388
remove_selection();

desktop_version/src/TextInput.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ struct Selection_Rect
1111

1212
namespace TextInput
1313
{
14+
extern bool taking_input;
15+
extern bool selecting;
16+
extern int flash_timer;
17+
extern SDL_Point cursor_pos;
18+
extern SDL_Point cursor_select_pos;
19+
1420
void send_cursor_to_end(void);
1521
void insert_newline(void);
1622
Selection_Rect reorder_selection_positions(void);
1723
void handle_events(SDL_Event e);
24+
void attach_input(std::vector<std::string>* text);
25+
void detach_input(void);
1826
}
1927

2028
#endif /* TEXTINPUT_H */

0 commit comments

Comments
 (0)