diff --git a/src/ui/ui.c b/src/ui/ui.c new file mode 100644 index 0000000..6a0ae2a --- /dev/null +++ b/src/ui/ui.c @@ -0,0 +1,82 @@ +#include "ui.h" +#include "input.h" +#include "layers.h" +#include "SDL2/SDL_render.h" +#include "SDL2/SDL_ttf.h" + +#define UI_NUM_WINDOWS 16 + +style_t ui_style = {}; + +typedef struct window_t { + SDL_FRect rect; + int dragged; + int confirmed; +} window_t; + +struct { + depth_t depth; + uintptr_t selected; + int hold_selection; + + window_t* window; + float width, height; + + struct { + char input_buffer[999]; + char* cursor; + } input; +} _state; + +window_t _windows[UI_NUM_WINDOWS]; +window_t* _windows_endptr = _windows; + +int ui_button(const char* label) { + SDL_FRect r = _state.window->rect; + + int w, h; + TTF_SizeText(ui_style.text.font, label, &w, &h); + SDL_FRect btn = { + _state.window->rect.x, _state.window->rect.y + _state.window->rect.h, + w, h + }; + + float mx, my; + mouse_screen_position(&mx, &my); + draw_text(label, btn, ui_style.text, _state.depth-1); + mx -= btn.x; my -= btn.y; + if(mx >= 0 && mx < btn.w && my >= 0 && my < btn.h) { + return 1; + } else { + return 0; + } +} + +void ui_init() { +} + +void ui_begin_frame() { + if(!_state.hold_selection) { + _state.selected = 0x0; + } + _state.window = NULL; + _state.depth = RLAYER_UI; +} + +void ui_window_begin(const char* name, float* x, float* y) { + for(window_t* itr = _windows; itr != _windows_endptr; ++itr) { + if(itr->rect.x == *x && itr->rect.y == *y) { + itr->confirmed = 1; + _state.window = itr; + } + } + if(_state.window == NULL) { + *_windows_endptr = (window_t){ + .rect = (SDL_FRect){*x, *y, 0.0, 0.0}, + .dragged = 0, .confirmed = 0 + }; + } +} + +void ui_window_end() { +} diff --git a/src/ui/ui.h b/src/ui/ui.h new file mode 100644 index 0000000..516f0e4 --- /dev/null +++ b/src/ui/ui.h @@ -0,0 +1,35 @@ +#ifndef _ui_h +#define _ui_h + +#include "render.h" +#include "SDL2/SDL_ttf.h" + +typedef struct style_t { + struct { + nineslice_t button; + nineslice_t active; + } button; + struct { + nineslice_t input; + nineslice_t active; + } input; + nineslice_t window_style; + + float spacing; + text_style_t text; +} style_t; + +extern style_t ui_style; + +extern void ui_init(); + +extern void ui_begin_frame(); +extern void ui_end_frame(); + +extern void ui_window_begin(const char* name, float* x, float* y); +extern void ui_window_end(); + +extern int ui_button(const char* label); +extern int ui_input(char* buffer, int max_size); + +#endif /* _ui_h */