#include "State_Options.h" #include "state.h" #include "globals.h" #include "render.h" char *opt_text_name[] = { "Width: ", "Height: ", "vsync(0 or 1): " }; char *opt_button_name[] = { "Cancel", "Apply" }; int openOptionsState() { report(DEBUG, "OptionsState", "opened"); int offset_x = 256, offset_y = 256; int i; for (i = 0; i < OPT_TEXT_COUNT; i++) { opt_text[i] = newTextt(&g_large_glyphs, ui_colors, opt_text_name[i]); opt_text[i]->box.x = offset_x; opt_text[i]->box.y = offset_y; opt_input[i] = newTextInput(&g_large_glyphs, button_colors, 128); opt_input[i]->box.x = opt_text[i]->box.x + opt_text[i]->box.w; opt_input[i]->box.y = offset_y; offset_y += opt_text[i]->box.h + 8; } opt_input_a = NULL; setTextInputText(opt_input[OPT_TEXT_WIDTH], "%d", g_v_width); setTextInputText(opt_input[OPT_TEXT_HEIGHT], "%d", g_v_height); setTextInputText(opt_input[OPT_TEXT_VSYNC], "%d", g_v_vsync); for (i = 0; i < OPT_BUTTON_COUNT; i++) { opt_button[i] = newButton(&g_large_glyphs, button_colors, opt_button_name[i]); opt_button[i]->box.x = offset_x; opt_button[i]->box.y = offset_y; offset_x += opt_button[i]->box.w + 8; } opt_button_a = NULL; return 0; } int closeOptionsState() { report(DEBUG, "OptionsState", "closed"); int i; for (i = 0; i < OPT_TEXT_COUNT; i++) { freeTextt(opt_text[i]); } for (i = 0; i < OPT_BUTTON_COUNT; i++) { freeButton(opt_button[i]); } return 0; } int handleOptionsState(SDL_Event event) { int i; switch(event.type) { case SDL_KEYDOWN: if (opt_input_a != NULL) { if (event.key.keysym.sym == SDLK_LEFT) { if (opt_input_a->cursor > 0) { opt_input_a->cursor--; } } else if (event.key.keysym.sym == SDLK_RIGHT) { if (opt_input_a->cursor < strlen(opt_input_a->text)) { opt_input_a->cursor++; } } else if (event.key.keysym.sym == SDLK_BACKSPACE) { if (opt_input_a->cursor != 0) { deleteTextInput(opt_input_a, opt_input_a->cursor-1, 1); opt_input_a->cursor--; } } } break; case SDL_KEYUP: if (event.key.keysym.sym == SDLK_ESCAPE) { popState(g_state_manager); } break; case SDL_MOUSEBUTTONDOWN: if (opt_input_a != NULL) { opt_input_a->flags &= ~UI_ACTIVE; opt_input_a = NULL; SDL_StopTextInput(); } for (i = 0; i < OPT_BUTTON_COUNT; i++) { if (inBox(opt_button[i]->box, event.motion.x, event.motion.y)) { opt_button_a = opt_button[i]; opt_button_a->flags |= UI_ACTIVE; break; } } for (i = 0; i < OPT_TEXT_COUNT; i++) { if (inBox(opt_input[i]->box, event.motion.x, event.motion.y)) { opt_input_a = opt_input[i]; opt_input_a->flags |= UI_ACTIVE; SDL_Rect rect = { opt_input_a->box.x, opt_input_a->box.y, opt_input_a->box.w, opt_input_a->box.h }; SDL_SetTextInputRect(&rect); SDL_StartTextInput(); break; } } break; case SDL_MOUSEBUTTONUP: if (opt_button_a != NULL) { if (inBox(opt_button_a->box, event.motion.x, event.motion.y)) { if (opt_button_a == opt_button[OPT_BUTTON_CANCEL]) { popState(g_state_manager); } else if (opt_button_a == opt_button[OPT_BUTTON_APPLY]) { g_v_width = atoi(opt_input[OPT_TEXT_WIDTH]->text); g_v_height = atoi(opt_input[OPT_TEXT_HEIGHT]->text); g_v_vsync = atoi(opt_input[OPT_TEXT_VSYNC]->text); reinitDisplay(); } } opt_button_a->flags &= ~UI_ACTIVE; opt_button_a = NULL; } break; case SDL_TEXTINPUT: if (opt_input_a != NULL) { insertTextInput(opt_input_a, event.text.text); } break; } return 1; } int processOptionsState() { return 0; } int renderOptionsState() { SDL_Color bg = { 16, 16, 16, 200 }; renderSQuad(0, 0, g_v_width, g_v_height, bg); int i; for (i = 0; i < OPT_TEXT_COUNT; i++) { renderTextt(opt_text[i]); renderTextInput(opt_input[i]); } for (i = 0; i < OPT_BUTTON_COUNT; i++) { renderButton(opt_button[i]); } return 0; }