Cleaned up code, renamed variables, added comments. Switched over all existing text boxes to new code

This commit is contained in:
Duncan Frost 2014-10-16 20:57:53 +01:00
parent edbfb43251
commit 1c62a0c29d
8 changed files with 67 additions and 25 deletions

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

@ -510,7 +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, uint16 title, uint16 description, rct_string_id string_id, uint32 args);
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();
@ -541,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
@ -580,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

@ -42,7 +42,7 @@ unsigned char *gKeysPressed;
unsigned int gLastKeyPressed;
char* gTextInput;
int gTextInputLength;
int text_input_max_length;
int gTextInputMaxLength;
int gTextInputCursorPosition = 0;
static void osinterface_create_window();
@ -86,7 +86,7 @@ int osinterface_scancode_to_rct_keycode(int sdl_key){
void osinterface_start_text_input(char* buffer, int max_length){
SDL_StartTextInput();
text_input_max_length = max_length - 1;
gTextInputMaxLength = max_length - 1;
gTextInput = buffer;
gTextInputCursorPosition = strnlen(gTextInput, max_length);
gTextInputLength = gTextInputCursorPosition;
@ -424,16 +424,22 @@ 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){
if (gTextInputCursorPosition != text_input_max_length)
memmove(gTextInput + gTextInputCursorPosition - 1, gTextInput + gTextInputCursorPosition, text_input_max_length - gTextInputCursorPosition - 1);
// 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_DELETE && gTextInputLength > 0 && gTextInput && gTextInputCursorPosition != gTextInputLength){
memmove(gTextInput + gTextInputCursorPosition, gTextInput + gTextInputCursorPosition + 1, text_input_max_length - gTextInputCursorPosition - 1);
gTextInput[text_input_max_length - 1] = '\0';
memmove(gTextInput + gTextInputCursorPosition, gTextInput + gTextInputCursorPosition + 1, gTextInputMaxLength - gTextInputCursorPosition - 1);
gTextInput[gTextInputMaxLength - 1] = '\0';
gTextInputLength--;
}
if (e.key.keysym.sym == SDLK_LEFT && gTextInput){
@ -464,19 +470,22 @@ void osinterface_process_messages()
break;
case SDL_TEXTINPUT:
if (gTextInputLength < text_input_max_length && gTextInput){
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, text_input_max_length - gTextInputCursorPosition - 1);
memmove(gTextInput + gTextInputCursorPosition + 1, gTextInput + gTextInputCursorPosition, gTextInputMaxLength - gTextInputCursorPosition - 1);
gTextInput[gTextInputCursorPosition] = new_char;
gTextInputLength++;
}
else gTextInput[gTextInputLength++] = new_char;
gTextInputCursorPosition++;
}
break;

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, uint32), 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

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

View File

@ -18,6 +18,13 @@
* 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"
@ -86,15 +93,16 @@ static void* window_text_input_events[] = {
window_text_input_emptysub
};
int string_description;
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, uint16 title, uint16 description, rct_string_id string_id, uint32 args){
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,
@ -108,12 +116,23 @@ void window_text_input_open(rct_window* call_w, int call_widget, uint16 title, u
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);
format_string(text_input, string_id, &args);
string_description = description;
// 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);
@ -137,12 +156,16 @@ static void window_text_input_mouseup(){
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);
@ -162,7 +185,7 @@ static void window_text_input_paint(){
int y = w->y + 25;
gfx_draw_string_centred(dpi, string_description, w->x + WW / 2, y, w->colours[1], 0);
gfx_draw_string_centred(dpi, input_text_description, w->x + WW / 2, y, w->colours[1], 0);
y += 25;
@ -171,17 +194,22 @@ static void window_text_input_paint(){
y += 1;
gfx_draw_string(dpi, text_input, w->colours[2], w->x + 12, y);
char temp_string[32] = { 0 };
// 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]);
@ -199,6 +227,8 @@ static void window_text_input_text(int key, rct_window* w){
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);
}
@ -209,16 +239,20 @@ static void window_text_input_text(int key, rct_window* 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();
}