Merge pull request #521 from duncanspumpkin/text_input

Text input window
This commit is contained in:
Ted John 2014-10-16 22:35:51 +01:00
commit 51b7dba3ac
12 changed files with 360 additions and 11 deletions

View File

@ -128,6 +128,7 @@
<ClCompile Include="..\src\windows\staff_fire_prompt.c" />
<ClCompile Include="..\src\windows\staff_list.c" />
<ClCompile Include="..\src\windows\staff.c" />
<ClCompile Include="..\src\windows\text_input.c" />
<ClCompile Include="..\src\windows\title_exit.c" />
<ClCompile Include="..\src\windows\title_logo.c" />
<ClCompile Include="..\src\windows\title_menu.c" />

View File

@ -425,6 +425,9 @@
<ClCompile Include="..\src\windows\viewport.c">
<Filter>Source\Windows</Filter>
</ClCompile>
<ClCompile Include="..\src\windows\text_input.c">
<Filter>Source\Windows</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\management\award.h">

View File

@ -1222,6 +1222,11 @@ void game_handle_key_scroll()
if (mainWindow->viewport == NULL)
return;
rct_window *textWindow;
textWindow = window_find_by_class(WC_TEXTINPUT);
if (textWindow) return;
scrollX = 0;
scrollY = 0;
@ -1623,8 +1628,16 @@ void game_handle_keyboard_input()
set_shortcut(key);
else if (RCT2_GLOBAL(RCT2_ADDRESS_ON_TUTORIAL, uint8) == 1)
tutorial_stop();
else
handle_shortcut(key);
else{
w = window_find_by_class(WC_TEXTINPUT);
if (w != NULL){
((void(*)(int, rct_window*))w->event_handlers[WE_TEXT_INPUT])(key,w);
}
else{
handle_shortcut(key);
}
}
}
if (RCT2_GLOBAL(RCT2_ADDRESS_ON_TUTORIAL, uint8) == 0)

View File

@ -1219,6 +1219,7 @@ void window_zoom_out(rct_window *w)
/**
*
* rct2: 0x006EE308
* DEPRECIATED please use the new text_input window.
*/
void window_show_textinput(rct_window *w, int widgetIndex, uint16 title, uint16 text, int value)
{

View File

@ -391,7 +391,8 @@ enum {
WC_MANAGE_TRACK_DESIGN = 89,
WC_CHEATS = 110,
WC_RESEARCH = 111,
WC_VIEWPORT = 112
WC_VIEWPORT = 112,
WC_TEXTINPUT = 113
} WINDOW_CLASS;
enum PROMPT_MODE {
@ -509,6 +510,7 @@ void window_music_credits_open();
void window_publisher_credits_open();
void window_track_manage_open();
void window_viewport_open();
void window_text_input_open(rct_window* call_w, int call_widget, rct_string_id title, rct_string_id description, rct_string_id existing_text, uint32 existing_args);
void window_guest_list_init_vars_a();
void window_guest_list_init_vars_b();
@ -539,9 +541,9 @@ void sub_6EA73F();
__asm mov widgetIndex, dx \
__asm mov w, esi
#define window_text_input_get_registers(w, widgetIndex, _cl, text) \
#define window_text_input_get_registers(w, widgetIndex, result, text) \
__asm mov widgetIndex, dx \
__asm mov _cl, cl \
__asm mov result, cl \
__asm mov w, esi \
__asm mov text, edi
@ -578,8 +580,8 @@ void sub_6EA73F();
__asm__ ( "mov %["#widgetIndex"], dx " : [widgetIndex] "+m" (widgetIndex) ); \
__asm__ ( "mov %["#w"], esi " : [w] "+m" (w) );
#define window_text_input_get_registers(w, widgetIndex, _cl, text) \
__asm__ ( "mov %[_cl], cl " : [_cl] "+m" (_cl) ); \
#define window_text_input_get_registers(w, widgetIndex, result, text) \
__asm__ ( "mov %[_cl], cl " : [_cl] "+m" (result) ); \
__asm__ ( "mov %[widgetIndex], dx " : [widgetIndex] "+m" (widgetIndex) ); \
__asm__ ( "mov %[w], esi " : [w] "+m" (w) ); \
__asm__ ( "mov %[text], edi " : [text] "+m" (text) );

View File

@ -40,6 +40,10 @@ openrct2_cursor gCursorState;
const unsigned char *gKeysState;
unsigned char *gKeysPressed;
unsigned int gLastKeyPressed;
char* gTextInput;
int gTextInputLength;
int gTextInputMaxLength;
int gTextInputCursorPosition = 0;
static void osinterface_create_window();
static void osinterface_close_window();
@ -80,6 +84,19 @@ int osinterface_scancode_to_rct_keycode(int sdl_key){
return keycode;
}
void osinterface_start_text_input(char* buffer, int max_length){
SDL_StartTextInput();
gTextInputMaxLength = max_length - 1;
gTextInput = buffer;
gTextInputCursorPosition = strnlen(gTextInput, max_length);
gTextInputLength = gTextInputCursorPosition;
}
void osinterface_stop_text_input(){
SDL_StopTextInput();
gTextInput = NULL;
}
/**
* This is not quite the same as the below function as we don't want to
* derfererence the cursor before the function.
@ -407,6 +424,36 @@ void osinterface_process_messages()
//calling it here will save screenshots even while in main menu
screenshot_check();
}
// Text input
// If backspace and we have input text with a cursor position none zero
if (e.key.keysym.sym == SDLK_BACKSPACE && gTextInputLength > 0 && gTextInput && gTextInputCursorPosition){
// When at max length don't shift the data left
// as it would buffer overflow.
if (gTextInputCursorPosition != gTextInputMaxLength)
memmove(gTextInput + gTextInputCursorPosition - 1, gTextInput + gTextInputCursorPosition, gTextInputMaxLength - gTextInputCursorPosition - 1);
gTextInput[gTextInputLength - 1] = '\0';
gTextInputCursorPosition--;
gTextInputLength--;
}
if (e.key.keysym.sym == SDLK_END){
gTextInputCursorPosition = gTextInputLength;
}
if (e.key.keysym.sym == SDLK_HOME){
gTextInputCursorPosition = 0;
}
if (e.key.keysym.sym == SDLK_DELETE && gTextInputLength > 0 && gTextInput && gTextInputCursorPosition != gTextInputLength){
memmove(gTextInput + gTextInputCursorPosition, gTextInput + gTextInputCursorPosition + 1, gTextInputMaxLength - gTextInputCursorPosition - 1);
gTextInput[gTextInputMaxLength - 1] = '\0';
gTextInputLength--;
}
if (e.key.keysym.sym == SDLK_LEFT && gTextInput){
if (gTextInputCursorPosition) gTextInputCursorPosition--;
}
else if (e.key.keysym.sym == SDLK_RIGHT && gTextInput){
if (gTextInputCursorPosition < gTextInputLength) gTextInputCursorPosition++;
}
break;
case SDL_MULTIGESTURE:
if (e.mgesture.numFingers == 2) {
@ -427,6 +474,27 @@ void osinterface_process_messages()
}
}
break;
case SDL_TEXTINPUT:
if (gTextInputLength < gTextInputMaxLength && gTextInput){
// Convert the utf-8 code into rct ascii
char new_char;
if (!(e.text.text[0] & 0x80))
new_char = *e.text.text;
else if (!(e.text.text[0] & 0x20))
new_char = ((e.text.text[0] & 0x1F) << 6) | (e.text.text[1] & 0x3F);
// If inserting in center of string make space for new letter
if (gTextInputLength > gTextInputCursorPosition){
memmove(gTextInput + gTextInputCursorPosition + 1, gTextInput + gTextInputCursorPosition, gTextInputMaxLength - gTextInputCursorPosition - 1);
gTextInput[gTextInputCursorPosition] = new_char;
gTextInputLength++;
}
else gTextInput[gTextInputLength++] = new_char;
gTextInputCursorPosition++;
}
break;
default:
break;
}

View File

@ -83,7 +83,10 @@ extern openrct2_cursor gCursorState;
extern const unsigned char *gKeysState;
extern unsigned char *gKeysPressed;
extern unsigned int gLastKeyPressed;
extern int gTextInputCursorPosition;
void osinterface_start_text_input(char* buffer, int max_length);
void osinterface_stop_text_input();
void osinterface_init();
void osinterface_process_messages();
void osinterface_draw();

View File

@ -631,7 +631,7 @@ void window_guest_overview_mouse_up(){
RCT2_CALLPROC_X(0x0069A42F, 0, 0, 0, 0, (int)peep, 0, 0);
break;
case WIDX_RENAME:
window_show_textinput(w, (int)widgetIndex, 0x5AC, 0x5AD, peep->name_string_idx);
window_text_input_open(w, widgetIndex, 0x5AC, 0x5AD, peep->name_string_idx, peep->id);
break;
case WIDX_LOCATE:
window_scroll_to_viewport(w);

View File

@ -694,7 +694,7 @@ static void window_park_entrance_mouseup()
break;
case WIDX_RENAME:
RCT2_GLOBAL(0x013CE962, uint32) = RCT2_GLOBAL(0x013573D8, uint32);
window_show_textinput(w, WIDX_RENAME, STR_PARK_NAME, STR_ENTER_PARK_NAME, RCT2_GLOBAL(0x013573D4, uint32));
window_text_input_open(w, WIDX_RENAME, STR_PARK_NAME, STR_ENTER_PARK_NAME, RCT2_GLOBAL(0x013573D4, rct_string_id), 0);
break;
}
}

View File

@ -1401,7 +1401,7 @@ static void window_ride_rename(rct_window *w)
ride = GET_RIDE(w->number);
RCT2_GLOBAL(0x013CE962, uint32) = ride->name_arguments;
window_show_textinput(w, WIDX_RENAME, STR_RIDE_ATTRACTION_NAME, STR_ENTER_NEW_NAME_FOR_THIS_RIDE_ATTRACTION, ride->name);
window_text_input_open(w, WIDX_RENAME, STR_RIDE_ATTRACTION_NAME, STR_ENTER_NEW_NAME_FOR_THIS_RIDE_ATTRACTION, ride->name, ride->name_arguments);
}
/**

View File

@ -469,7 +469,7 @@ void window_staff_overview_mouseup()
window_staff_fire_prompt_open(peep);
break;
case WIDX_RENAME:
window_show_textinput(w, (int)widgetIndex, 2977, 2978, peep->name_string_idx);
window_text_input_open(w, widgetIndex, 2977, 2978, peep->name_string_idx, peep->id);
break;
}
}

258
src/windows/text_input.c Normal file
View File

@ -0,0 +1,258 @@
/*****************************************************************************
* Copyright (c) 2014 Ted John, Duncan Frost
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* This file is part of OpenRCT2.
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
/**
* Text Input Window
*
* This is a new window created to replace the windows dialog box
* that is used for inputing new text for ride names and peep names.
*/
#include "../addresses.h"
#include "../config.h"
#include "../platform/osinterface.h"
#include "../interface/window.h"
#include "../interface/widget.h"
#include "../localisation/localisation.h"
#define WW 250
#define WH 90
#define MAX_TEXTINPUT 32
enum WINDOW_TEXT_INPUT_WIDGET_IDX {
WIDX_BACKGROUND,
WIDX_TITLE,
WIDX_CLOSE,
WIDX_CANCEL,
WIDX_OKAY
};
// 0x9DE4E0
static rct_widget window_text_input_widgets[] = {
{ WWT_FRAME, 1, 0, WW - 1, 0, WH - 1, STR_NONE, STR_NONE },
{ WWT_CAPTION, 1, 1, WW - 2, 1, 14, STR_OPTIONS, STR_WINDOW_TITLE_TIP },
{ WWT_CLOSEBOX, 1, WW - 13, WW - 3, 2, 13, STR_CLOSE_X, STR_CLOSE_WINDOW_TIP },
{ WWT_DROPDOWN_BUTTON, 1, WW - 80, WW - 10, WH - 21, WH - 10, STR_CANCEL, STR_NONE },
{ WWT_DROPDOWN_BUTTON, 1, 10, 80, WH - 21, WH - 10, STR_OK, STR_NONE },
{ WIDGETS_END }
};
static void window_text_input_emptysub(){}
static void window_text_input_mouseup();
static void window_text_input_paint();
static void window_text_input_text(int key, rct_window* w);
static void window_text_input_update(rct_window* w);
static void window_text_input_close();
//0x9A3F7C
static void* window_text_input_events[] = {
window_text_input_close,
window_text_input_mouseup,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_update,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_text,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_emptysub,
window_text_input_paint,
window_text_input_emptysub
};
int input_text_description;
char text_input[MAX_TEXTINPUT] = { 0 };
rct_windowclass calling_class = 0;
rct_windownumber calling_number = 0;
int calling_widget = 0;
void window_text_input_open(rct_window* call_w, int call_widget, rct_string_id title, rct_string_id description, rct_string_id existing_text, uint32 existing_args){
window_close_by_class(WC_TEXTINPUT);
// Window will be in the center of the screen
rct_window* w = window_create(
(RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_WIDTH, sint16) / 2) - WW / 2,
(RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_HEIGHT, sint16) / 2) - WH / 2,
WW,
WH,
(uint32*)window_text_input_events,
WC_TEXTINPUT,
0);
w->widgets = window_text_input_widgets;
w->enabled_widgets = (1 << WIDX_CLOSE) | (1<<WIDX_CANCEL) | (1<<WIDX_OKAY);
window_text_input_widgets[WIDX_TITLE].image = title;
// Clear the text input buffer
memset(text_input, 0, MAX_TEXTINPUT);
// Enter in the the text input buffer any existing
// text.
format_string(text_input, existing_text, &existing_args);
// This is the text displayed above the input box
input_text_description = description;
// Save calling window details so that the information
// can be passed back to the correct window & widget
calling_class = call_w->classification;
calling_number = call_w->number;
calling_widget = call_widget;
osinterface_start_text_input(text_input, MAX_TEXTINPUT);
window_init_scroll_widgets(w);
w->colours[0] = call_w->colours[0];
w->colours[1] = call_w->colours[1];
w->colours[2] = call_w->colours[2];
}
/**
*
*/
static void window_text_input_mouseup(){
short widgetIndex;
rct_window *w;
rct_window *calling_w;
window_widget_get_registers(w, widgetIndex);
calling_w = window_find_by_number(calling_class, calling_number);
switch (widgetIndex){
case WIDX_CANCEL:
case WIDX_CLOSE:
osinterface_stop_text_input();
// Pass back the text that has been entered.
// ecx when zero means text input failed
if (calling_w != NULL)
RCT2_CALLPROC_X(calling_w->event_handlers[WE_TEXT_INPUT], 0, 0, 0, calling_widget, (int)calling_w, (int)text_input, 0);
window_close(w);
break;
case WIDX_OKAY:
osinterface_stop_text_input();
// Pass back the text that has been entered.
// ecx when none zero means text input success
if (calling_w != NULL)
RCT2_CALLPROC_X(calling_w->event_handlers[WE_TEXT_INPUT], 0, 0, 1, calling_widget, (int)calling_w, (int)text_input, 0);
window_close(w);
}
}
/**
*
*/
static void window_text_input_paint(){
rct_window *w;
rct_drawpixelinfo *dpi;
window_paint_get_registers(w, dpi);
window_draw_widgets(w, dpi);
int y = w->y + 25;
gfx_draw_string_centred(dpi, input_text_description, w->x + WW / 2, y, w->colours[1], 0);
y += 25;
gfx_fill_rect_inset(dpi, w->x + 10, y, w->x + WW - 10, y + 12, w->colours[1], 0x60);
y += 1;
gfx_draw_string(dpi, text_input, w->colours[1], w->x + 12, y);
// Make a copy of the string for measuring the width.
char temp_string[32] = { 0 };
memcpy(temp_string, text_input, gTextInputCursorPosition);
int x = w->x + 13 + gfx_get_string_width(temp_string);
int width = 6;
if ((uint32)gTextInputCursorPosition < strlen(text_input)){
// Make a new 1 character wide string for measuring the width
// of the character that the cursor is under.
temp_string[1] = '\0';
temp_string[0] = text_input[gTextInputCursorPosition];
width = max(gfx_get_string_width(temp_string) - 2, 4);
}
// Draw the cursor
y += 9;
if (w->frame_no > 15){
gfx_fill_rect(dpi, x, y, x + width, y, w->colours[1]);
}
}
static void window_text_input_text(int key, rct_window* w){
int text = key;
char new_char = osinterface_scancode_to_rct_keycode(0xFF&key);
// If the return button is pressed stop text input
if (new_char == '\r'){
osinterface_stop_text_input();
window_close(w);
rct_window* calling_w = window_find_by_number(calling_class, calling_number);
// Pass back the text that has been entered.
// ecx when none zero means text input success
if (calling_w)
RCT2_CALLPROC_X(calling_w->event_handlers[WE_TEXT_INPUT], 0, 0, 1, calling_widget, (int)calling_w, (int)text_input, 0);
}
window_invalidate(w);
}
void window_text_input_update(rct_window* w)
{
rct_window* calling_w = window_find_by_number(calling_class, calling_number);
// If the calling window is closed then close the text
// input window.
if (!calling_w){
window_close(w);
}
// Used to blink the cursor.
w->frame_no++;
if (w->frame_no > 30) w->frame_no = 0;
window_invalidate(w);
}
static void window_text_input_close(){
// Make sure that we take it out of the text input
// mode otherwise problems may occur.
osinterface_stop_text_input();
}