started on ui library

pull/3/head
Sara 2023-05-07 16:11:17 +02:00
parent 2a2ea2c784
commit bb6dec40b6
2 changed files with 117 additions and 0 deletions

82
src/ui/ui.c Normal file
View File

@ -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() {
}

35
src/ui/ui.h Normal file
View File

@ -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 */