Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/console/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ void console_tick(game_state *gs) {
con->y_pos = 0;
}
}
console_release_keypress();
}

void console_add_cmd(const char *name, command_func func, const char *doc) {
Expand All @@ -372,6 +373,20 @@ void console_remove_cmd(const char *name) {
hashmap_del_str(&con->cmds, name);
}

void console_release_keypress(void) {
if(con->keypress_time > 0) {
con->keypress_time--;
return;
}
if(con->keypress_scancode != SDL_SCANCODE_UNKNOWN) {
Uint8 *state = (Uint8 *)SDL_GetKeyboardState(NULL);
state[con->keypress_scancode] = 0;

con->keypress_time = 0;
con->keypress_scancode = SDL_SCANCODE_UNKNOWN;
}
}

bool console_window_is_open(void) {
return con->is_open;
}
Expand Down
3 changes: 3 additions & 0 deletions src/console/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ void console_tick(game_state *gs);
void console_add_cmd(const char *name, command_func func, const char *doc);
void console_remove_cmd(const char *name);

// Potentially release a key that has been pressed by the keypress command.
void console_release_keypress(void);

void console_output_add(const char *text);
void console_output_addline(const char *text);

Expand Down
39 changes: 39 additions & 0 deletions src/console/console_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "console/console_type.h"
#include "formats/error.h"
#include "formats/rec_assertion.h"
#include "game/gui/component.h"
#include "game/gui/sizer.h"
#include "game/scenes/arena.h"
#include "game/scenes/mechlab.h"
#include "resources/ids.h"
Expand Down Expand Up @@ -273,6 +275,41 @@ int console_cmd_god(game_state *gs, int argc, char **argv) {
return 0;
}

int console_find_by_text(game_state *gs, int argc, char **argv) {
scene *sc = game_state_get_scene(gs);
if(sc->id != SCENE_MECHLAB) {
log_debug("Wrong scene for find_by_text");
return 1;
}
gui_frame *frame = mechlab_get_frame(game_state_get_scene(gs));
// component *c = component_find_text(gui_frame_get_root(frame), "NEW");
component *c = gui_frame_find_text(frame, "NEW");
if(c == NULL) {
log_debug("Could not find the component");
return 1;
}
component_action(c, ACT_PUNCH);

return 0;
}

int console_keypress(game_state *gs, int argc, char **argv) {
SDL_Event sdlevent;
memset(&sdlevent, 0, sizeof(sdlevent));
sdlevent.type = SDL_KEYDOWN;
sdlevent.key.keysym.sym = SDLK_DOWN;
sdlevent.key.keysym.scancode = SDL_SCANCODE_DOWN;
SDL_PushEvent(&sdlevent);

Uint8 *state = (Uint8 *)SDL_GetKeyboardState(NULL);
state[SDL_SCANCODE_DOWN] = 1;

con->keypress_time = 20;
con->keypress_scancode = SDL_SCANCODE_DOWN;

return 0;
}

int console_kreissack(game_state *gs, int argc, char **argv) {
game_player *p1 = game_state_get_player(gs, 0);
p1->sp_wins = (2046 ^ (2 << p1->pilot->pilot_id));
Expand Down Expand Up @@ -472,6 +509,8 @@ void console_init_cmd(void) {
console_add_cmd("stun", &console_cmd_stun, "Stun the other player");
console_add_cmd("rein", &console_cmd_rein, "R-E-I-N!");
console_add_cmd("god", &console_cmd_god, "Enable god mode");
console_add_cmd("keypress", &console_keypress, "Push a keypress into the input queue");
console_add_cmd("find_by_text", &console_find_by_text, "Move the hand to this button");
console_add_cmd("kreissack", &console_kreissack, "Fight Kreissack");
console_add_cmd("ez-destruct", &console_cmd_ez_destruct, "Punch = destruction, kick = scrap");
console_add_cmd("warp", &console_toggle_warp, "Toggle warp speed");
Expand Down
2 changes: 2 additions & 0 deletions src/console/console_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ typedef struct console {
int y_pos;
hashmap cmds; // string -> command
text *text;
int keypress_time;
int keypress_scancode;
} console;

typedef struct command {
Expand Down
14 changes: 14 additions & 0 deletions src/game/gui/component.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ component *component_find(component *c, int id) {
return c->find(c, id);
}

component *component_find_text(component *c, const char *text) {
log_debug("component_find_text");
if(c->find_text != NULL) {
log_debug("component_find_text: callback found");
return c->find_text(c, text);
}
log_debug("component_find_text: NULL");
return NULL;
}

void component_set_obj(component *c, void *obj) {
c->obj = obj;
}
Expand Down Expand Up @@ -162,6 +172,10 @@ void component_set_find_cb(component *c, component_find_cb cb) {
c->find = cb;
}

void component_set_find_text_cb(component *c, component_find_text_cb cb) {
c->find_text = cb;
}

void component_set_init_cb(component *c, component_init_cb cb) {
c->init = cb;
}
Expand Down
10 changes: 7 additions & 3 deletions src/game/gui/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef void (*component_tick_cb)(component *c);
typedef void (*component_free_cb)(component *c);
typedef void (*component_init_cb)(component *c, const gui_theme *theme);
typedef component *(*component_find_cb)(component *c, int id);
typedef component *(*component_find_text_cb)(component *c, const char *text);

/*! \brief Basic GUI object
*
Expand Down Expand Up @@ -70,8 +71,9 @@ struct component {
component_tick_cb tick; ///< Tick function callback. This is called periodically.
component_free_cb free; ///< Free function callback. Any component callbacks should be done here.
component_find_cb find; ///< Should only be set by widget and sizer. Used to look up widgets by ID.
component_init_cb init; ///< Initialization function callback. This is called right before layout function. This
///< should be used to prerender elements, decide size hints, etc.
component_find_text_cb find_text; ///< ...
component_init_cb init; ///< Initialization function callback. This is called right before layout function. This
///< should be used to prerender elements, decide size hints, etc.

component *parent; ///< Parent component. For widgets, usually a sizer. NULL for root component.
};
Expand Down Expand Up @@ -106,8 +108,9 @@ void component_set_help_text(component *c, const char *text);
void component_set_theme(component *c, const gui_theme *theme);
const gui_theme *component_get_theme(component *c);

// ID lookup stuff
// lookup stuff
component *component_find(component *c, int id);
component *component_find_text(component *c, const char *text);

// Basic component callbacks
void component_set_obj(component *c, void *obj);
Expand All @@ -121,5 +124,6 @@ void component_set_init_cb(component *c, component_init_cb cb);
void component_set_tick_cb(component *c, component_tick_cb cb);
void component_set_free_cb(component *c, component_free_cb cb);
void component_set_find_cb(component *c, component_find_cb cb);
void component_set_find_text_cb(component *c, component_find_text_cb cb);

#endif // COMPONENT_H
9 changes: 9 additions & 0 deletions src/game/gui/gui_frame.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "game/gui/gui_frame.h"
#include "utils/allocator.h"
#include "utils/log.h"

typedef struct gui_frame {
int x;
Expand Down Expand Up @@ -47,6 +48,14 @@ component *gui_frame_find(gui_frame *frame, int id) {
return NULL;
}

component *gui_frame_find_text(gui_frame *frame, const char *text) {
log_debug("gui_frame_find_text");
if(frame->root_node) {
return component_find_text(frame->root_node, text);
}
return NULL;
}

component *gui_frame_get_root(const gui_frame *frame) {
return frame->root_node;
}
Expand Down
1 change: 1 addition & 0 deletions src/game/gui/gui_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ component *gui_frame_get_root(const gui_frame *frame);
void gui_frame_free(gui_frame *frame);

component *gui_frame_find(gui_frame *frame, int id);
component *gui_frame_find_text(gui_frame *frame, const char *text);

void gui_frame_get_measurements(const gui_frame *frame, int *x, int *y, int *w, int *h);

Expand Down
12 changes: 12 additions & 0 deletions src/game/gui/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,17 @@ static component *menu_find(component *c, int id) {
return NULL;
}

static component *menu_find_text(component *c, const char *text) {
log_debug("menu_find_text");
menu *m = sizer_get_obj(c);
if(m->submenu) {
log_debug("menu_find_text: calling for submenu");
return component_find_text(m->submenu, text);
}
log_debug("menu_find_text: returning NULL");
return NULL;
}

void menu_set_margin_top(component *c, int margin) {
menu *m = sizer_get_obj(c);
m->margin_top = margin;
Expand Down Expand Up @@ -467,6 +478,7 @@ component *menu_create(void) {
sizer_set_layout_cb(c, menu_layout);
sizer_set_free_cb(c, menu_free);
sizer_set_find_cb(c, menu_find);
sizer_set_find_text_cb(c, menu_find_text);

return c;
}
33 changes: 33 additions & 0 deletions src/game/gui/sizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct sizer {
sizer_tick_cb tick;
sizer_free_cb free;
sizer_find_cb find;
sizer_find_text_cb find_text;
sizer_init_cb init;
} sizer;

Expand Down Expand Up @@ -109,6 +110,12 @@ void sizer_set_find_cb(component *c, sizer_find_cb cb) {
local->find = cb;
}

void sizer_set_find_text_cb(component *c, sizer_find_text_cb cb) {
assert(c->header == SIZER_MAGIC);
sizer *local = component_get_obj(c);
local->find_text = cb;
}

void sizer_set_init_cb(component *c, sizer_init_cb cb) {
assert(c->header == SIZER_MAGIC);
sizer *local = component_get_obj(c);
Expand Down Expand Up @@ -243,6 +250,31 @@ static component *sizer_find(component *c, int id) {
return NULL;
}

static component *sizer_find_text(component *c, const char *text) {
assert(c->header == SIZER_MAGIC);
sizer *local = component_get_obj(c);

iterator it;
component **tmp;
vector_iter_begin(&local->objs, &it);
foreach(it, tmp) {
// Find out if the component is what we're looking for.
// If it is, return pointer.
component *out = component_find_text(*tmp, text);
if(out != NULL) {
return out;
}
}

// If find callback is set, try to use it.
if(local->find_text) {
return local->find_text(c, text);
}

// If requested ID was not found in any of the internal components/widgets, return NULL.
return NULL;
}

component *sizer_create(void) {
component *c = component_create(SIZER_MAGIC);

Expand All @@ -257,6 +289,7 @@ component *sizer_create(void) {
component_set_layout_cb(c, sizer_layout);
component_set_free_cb(c, sizer_free);
component_set_find_cb(c, sizer_find);
component_set_find_text_cb(c, sizer_find_text);
component_set_init_cb(c, sizer_init);

return c;
Expand Down
2 changes: 2 additions & 0 deletions src/game/gui/sizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef void (*sizer_tick_cb)(component *c);
typedef void (*sizer_init_cb)(component *c, const gui_theme *theme);
typedef void (*sizer_free_cb)(component *c);
typedef component *(*sizer_find_cb)(component *c, int id);
typedef component *(*sizer_find_text_cb)(component *c, const char *text);

typedef struct sizer sizer;

Expand All @@ -36,6 +37,7 @@ void sizer_set_tick_cb(component *c, sizer_tick_cb cb);
void sizer_set_free_cb(component *c, sizer_free_cb cb);
void sizer_set_init_cb(component *c, sizer_init_cb cb);
void sizer_set_find_cb(component *c, sizer_find_cb cb);
void sizer_set_find_text_cb(component *c, sizer_find_text_cb cb);

void sizer_attach(component *c, component *nc);

Expand Down
12 changes: 12 additions & 0 deletions src/game/gui/spritebutton.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef struct spritebutton {
spritebutton_click_cb click_cb;
spritebutton_tick_cb tick_cb;
spritebutton_focus_cb focus_cb;
spritebutton_find_text_cb find_text_cb;
bool free_userdata;
void *userdata;
} spritebutton;
Expand Down Expand Up @@ -71,6 +72,15 @@ static void spritebutton_tick(component *c) {
}
}

static component *spritebutton_find_text(component *c, const char *text) {
log_debug("spritebutton_find_text");
spritebutton *b = widget_get_obj(c);
if(strcmp(text_c(b->text), text) == 0) {
return c;
}
return NULL;
}

static void spritebutton_focus(component *c, bool focused) {
spritebutton *b = widget_get_obj(c);
if(b->focus_cb) {
Expand Down Expand Up @@ -139,6 +149,8 @@ component *spritebutton_create(const char *text, const surface *img, bool disabl
widget_set_tick_cb(c, spritebutton_tick);
widget_set_free_cb(c, spritebutton_free);
widget_set_layout_cb(c, spritebutton_layout);
log_debug("widget_set_find_text_cb(c, spritebutton_find_text): %s", text_c(b->text));
widget_set_find_text_cb(c, spritebutton_find_text);
return c;
}

Expand Down
1 change: 1 addition & 0 deletions src/game/gui/spritebutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
typedef void (*spritebutton_click_cb)(component *c, void *userdata);
typedef void (*spritebutton_tick_cb)(component *c, void *userdata);
typedef void (*spritebutton_focus_cb)(component *c, bool focused, void *userdata);
typedef component *(*spritebutton_find_text_cb)(component *c, const char *text);

component *spritebutton_create(const char *text, const surface *img, bool disabled, spritebutton_click_cb cb,
void *userdata);
Expand Down
Loading