1
1
Fork 0
Browse Source

improve textbox

master
alistair 3 years ago
parent
commit
feb4e4a48e
  1. 56
      src/game.c
  2. 10
      src/game.h

56
src/game.c

@ -38,8 +38,6 @@ struct draw_watcher draw_watch = {};
struct textbox_info textboxes[1]; struct textbox_info textboxes[1];
struct ui_state gameui = {}; struct ui_state gameui = {};
struct timespec last_tick; struct timespec last_tick;
@ -1647,6 +1645,7 @@ GlobWorld create_world() {
void startgame(SDL_Renderer * ren) { void startgame(SDL_Renderer * ren) {
get_input_map(); get_input_map();
textboxes[TB_LEVEL_CHOOSER].id = TB_LEVEL_CHOOSER;
textboxes[TB_LEVEL_CHOOSER].close_callback = new_level_tb_close_callback; textboxes[TB_LEVEL_CHOOSER].close_callback = new_level_tb_close_callback;
debug_ren = ren; debug_ren = ren;
@ -1706,6 +1705,29 @@ void handle_input_event(SDL_Event event) {
static bool mouse_down = false; static bool mouse_down = false;
switch (event.type) { switch (event.type) {
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (gameui.currently_bound_textbox) {
if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE
|| event.key.keysym.scancode == SDL_SCANCODE_DELETE) {
delete_from_textbox(gameui.currently_bound_textbox);
} else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN
|| event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
if (gameui.currently_bound_textbox->close_callback) {
gameui.currently_bound_textbox->close_callback(
gameui.currently_bound_textbox, NULL);
}
gameui.currently_bound_textbox = NULL;
}
else if (event.key.keysym.sym >= 32
&& event.key.keysym.sym <= 126) {
write_to_textbox(gameui.currently_bound_textbox,
(char)event.key.keysym.sym);
}
// dont do anything else when the textbox is bound;
break;
}
#ifdef DEBUGMODE #ifdef DEBUGMODE
if (sc == input_map.player_up) { if (sc == input_map.player_up) {
walk_player(0, -1); walk_player(0, -1);
@ -1725,6 +1747,12 @@ void handle_input_event(SDL_Event event) {
if (in_game) if (in_game)
game_paused = !game_paused; game_paused = !game_paused;
} }
if (sc == input_map.goto_level) {
gameui.currently_bound_textbox = textboxes + TB_LEVEL_CHOOSER;
};
break; break;
case SDL_KEYUP: case SDL_KEYUP:
if (event.key.keysym.scancode == input_map.player_rope) { if (event.key.keysym.scancode == input_map.player_rope) {
@ -1745,30 +1773,6 @@ void handle_input_event(SDL_Event event) {
stop_pull_rope(); stop_pull_rope();
} }
if (gameui.currently_bound_textbox) {
if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE
|| event.key.keysym.scancode == SDL_SCANCODE_DELETE) {
delete_from_textbox(gameui.currently_bound_textbox);
} else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN) {
if (gameui.currently_bound_textbox->close_callback) {
gameui.currently_bound_textbox->close_callback(
gameui.currently_bound_textbox, NULL);
}
gameui.currently_bound_textbox = NULL;
}
else {
write_to_textbox(gameui.currently_bound_textbox,
(char)event.key.keysym.sym);
}
}
if (sc == input_map.goto_level) {
gameui.level_chooser = !gameui.level_chooser;
if (gameui.level_chooser) {
gameui.currently_bound_textbox = textboxes + TB_LEVEL_CHOOSER;
}
};
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
add_rope(event.button.x, event.button.y); add_rope(event.button.x, event.button.y);

10
src/game.h

@ -22,22 +22,22 @@ struct draw_watcher {
struct ui_state { struct ui_state {
struct textbox_info *currently_bound_textbox; struct textbox_info *currently_bound_textbox;
bool level_chooser;
int goto_new_level; int goto_new_level;
}; };
enum TextBoxId {
TB_LEVEL_CHOOSER = 0
};
struct textbox_info { struct textbox_info {
enum TextBoxId id;
bool capture_input_text_field; bool capture_input_text_field;
char text_input[32]; char text_input[32];
int text_input_bufpos; int text_input_bufpos;
void (*close_callback)(struct textbox_info *textbox, void*callback); void (*close_callback)(struct textbox_info *textbox, void*callback);
}; };
enum TextBoxId {
TB_LEVEL_CHOOSER = 0
};
extern struct draw_watcher draw_watch; extern struct draw_watcher draw_watch;
extern struct textbox_info* texboxes; extern struct textbox_info* texboxes;
extern struct ui_state gameui; extern struct ui_state gameui;

Loading…
Cancel
Save