updated tiled layout API to be easier to use
This commit is contained in:
parent
e50fec3e12
commit
536c81f193
12
Readme.md
12
Readme.md
|
@ -324,16 +324,18 @@ struct gui_panel panel;
|
|||
struct gui_input input = {0};
|
||||
gui_panel_init(&panel, 0, 0, 0, 0, 0, &config, &buffer);
|
||||
|
||||
struct gui_layout tiled;
|
||||
struct gui_layout_config ratio = {...};
|
||||
gui_layout_init(&tiled, &ratio, window_width, window_height);
|
||||
gui_layout_slot(&tiled, GUI_SLOT_LEFT, GUI_LAYOUT_VERTICAL, 1);
|
||||
|
||||
while (1) {
|
||||
gui_input_begin(&input);
|
||||
/* record input */
|
||||
gui_input_end(&input);
|
||||
|
||||
/* setup layout */
|
||||
struct gui_layout tiled;
|
||||
gui_layout_begin(&tiled, 0, window_width, window_height);
|
||||
gui_layout_slot(&tiled, GUI_SLOT_LEFT, 1.0f, GUI_LAYOUT_VERTICAL, 1);
|
||||
gui_layout_end(&tiled);
|
||||
|
||||
/* GUI */
|
||||
struct gui_panel_layout layout;
|
||||
gui_panel_begin_tiled(&layout, &panel, &tiled, GUI_SLOT_LEFT, 0, "Demo", &input);
|
||||
gui_panel_row(&layout, 30, 1);
|
||||
|
|
85
gui.c
85
gui.c
|
@ -1091,11 +1091,12 @@ gui_slider(struct gui_command_buffer *out, gui_float x, gui_float y, gui_float w
|
|||
}
|
||||
|
||||
{
|
||||
/* draw slider with background and circle cursor*/
|
||||
/* NOTE: this is a shitty hack since I am to stupid for math */
|
||||
gui_float c_pos = (slider_value <= slider_min) ? cursor.x:
|
||||
(slider_value >= slider_max) ? ((bar.x + bar.w) - cursor.h) :
|
||||
cursor.x + (cursor.w/2) - cursor.h/2;
|
||||
|
||||
/* draw slider with background and circle cursor*/
|
||||
gui_command_buffer_push_rect(out, bar.x, bar.y, bar.w, bar.h,0, s->bar);
|
||||
gui_command_buffer_push_circle(out,c_pos,cursor.y,cursor.h,cursor.h,s->border);
|
||||
gui_command_buffer_push_circle(out,c_pos + 1,cursor.y+1,cursor.h-2,cursor.h-2,s->fg);
|
||||
|
@ -3440,64 +3441,23 @@ gui_stack_pop(struct gui_stack *stack, struct gui_panel*panel)
|
|||
*
|
||||
* ===============================================================
|
||||
*/
|
||||
void
|
||||
gui_layout_init(struct gui_layout *layout, const struct gui_layout_config *config,
|
||||
gui_flags flags, gui_size width, gui_size height)
|
||||
{
|
||||
gui_float left, right;
|
||||
gui_float centerh, centerv;
|
||||
gui_float bottom, top;
|
||||
|
||||
void
|
||||
gui_layout_begin(struct gui_layout *layout, gui_size width, gui_size height, gui_flags flags)
|
||||
{
|
||||
GUI_ASSERT(layout);
|
||||
GUI_ASSERT(config);
|
||||
if (!layout || !config) return;
|
||||
if (!layout) return;
|
||||
|
||||
gui_zero(layout, sizeof(*layout));
|
||||
layout->flags = flags;
|
||||
layout->width = width;
|
||||
layout->height = height;
|
||||
|
||||
left = GUI_SATURATE(config->left);
|
||||
right = GUI_SATURATE(config->right);
|
||||
centerh = GUI_SATURATE(config->centerh);
|
||||
centerv = GUI_SATURATE(config->centerv);
|
||||
bottom = GUI_SATURATE(config->bottom);
|
||||
top = GUI_SATURATE(config->top);
|
||||
|
||||
layout->slots[GUI_SLOT_TOP].ratio = gui_vec2(1.0f, top);
|
||||
layout->slots[GUI_SLOT_LEFT].ratio = gui_vec2(left, centerv);
|
||||
layout->slots[GUI_SLOT_BOTTOM].ratio = gui_vec2(1.0f, bottom);
|
||||
layout->slots[GUI_SLOT_CENTER].ratio = gui_vec2(centerh, centerv);
|
||||
layout->slots[GUI_SLOT_RIGHT].ratio = gui_vec2(right, centerv);
|
||||
|
||||
layout->slots[GUI_SLOT_TOP].offset = gui_vec2(0.0f, 0.0f);
|
||||
layout->slots[GUI_SLOT_LEFT].offset = gui_vec2(0.0f, top);
|
||||
layout->slots[GUI_SLOT_BOTTOM].offset = gui_vec2(0.0f, top + centerv);
|
||||
layout->slots[GUI_SLOT_CENTER].offset = gui_vec2(left, top);
|
||||
layout->slots[GUI_SLOT_RIGHT].offset = gui_vec2(left + centerh, top);
|
||||
}
|
||||
|
||||
void
|
||||
gui_layout_set_size(struct gui_layout *layout, gui_size width, gui_size height)
|
||||
{
|
||||
GUI_ASSERT(layout);
|
||||
if (!layout) return;
|
||||
layout->width = width;
|
||||
layout->height = height;
|
||||
}
|
||||
|
||||
void
|
||||
gui_layout_set_state(struct gui_layout *layout, gui_uint state)
|
||||
{
|
||||
GUI_ASSERT(layout);
|
||||
if (!layout) return;
|
||||
if (state) layout->flags |= GUI_LAYOUT_INACTIVE;
|
||||
else layout->flags &= (gui_flags)~GUI_LAYOUT_INACTIVE;
|
||||
}
|
||||
|
||||
void
|
||||
gui_layout_slot(struct gui_layout *layout, enum gui_layout_slot_index slot,
|
||||
enum gui_layout_format format, gui_size count)
|
||||
gui_float ratio, enum gui_layout_format format, gui_size count)
|
||||
{
|
||||
GUI_ASSERT(layout);
|
||||
GUI_ASSERT(count);
|
||||
|
@ -3505,5 +3465,36 @@ gui_layout_slot(struct gui_layout *layout, enum gui_layout_slot_index slot,
|
|||
if (!layout || !count) return;
|
||||
layout->slots[slot].capacity = count;
|
||||
layout->slots[slot].format = format;
|
||||
layout->slots[slot].value = GUI_SATURATE(ratio);
|
||||
}
|
||||
|
||||
void
|
||||
gui_layout_end(struct gui_layout *layout)
|
||||
{
|
||||
struct gui_layout_slot *top, *bottom;
|
||||
struct gui_layout_slot *left, *right;
|
||||
struct gui_layout_slot *center;
|
||||
gui_float centerh, centerv;
|
||||
|
||||
top = &layout->slots[GUI_SLOT_TOP];
|
||||
bottom = &layout->slots[GUI_SLOT_BOTTOM];
|
||||
left = &layout->slots[GUI_SLOT_LEFT];
|
||||
right = &layout->slots[GUI_SLOT_RIGHT];
|
||||
center = &layout->slots[GUI_SLOT_CENTER];
|
||||
|
||||
centerh = MAX(0.0f, 1.0f - (left->value + right->value));
|
||||
centerv = MAX(0.0f, 1.0f - (top->value + bottom->value));
|
||||
layout->slots[GUI_SLOT_CENTER].ratio = gui_vec2(centerh, centerv);
|
||||
|
||||
layout->slots[GUI_SLOT_TOP].ratio = gui_vec2(1.0f, top->value);
|
||||
layout->slots[GUI_SLOT_LEFT].ratio = gui_vec2(left->value, centerv);
|
||||
layout->slots[GUI_SLOT_BOTTOM].ratio = gui_vec2(1.0f, bottom->value);
|
||||
layout->slots[GUI_SLOT_RIGHT].ratio = gui_vec2(right->value, centerv);
|
||||
|
||||
layout->slots[GUI_SLOT_TOP].offset = gui_vec2(0.0f, 0.0f);
|
||||
layout->slots[GUI_SLOT_LEFT].offset = gui_vec2(0.0f, top->value);
|
||||
layout->slots[GUI_SLOT_BOTTOM].offset = gui_vec2(0.0f, top->value + centerv);
|
||||
layout->slots[GUI_SLOT_RIGHT].offset = gui_vec2(left->value + centerh, top->value);
|
||||
layout->slots[GUI_SLOT_CENTER].offset = gui_vec2(left->value, top->value);
|
||||
}
|
||||
|
||||
|
|
39
gui.h
39
gui.h
|
@ -2090,7 +2090,7 @@ void gui_stack_pop(struct gui_stack*, struct gui_panel*);
|
|||
| Top |
|
||||
-----------------------------
|
||||
| | | |
|
||||
| left | center | right |
|
||||
| Left | Center | Right |
|
||||
| | | |
|
||||
-----------------------------
|
||||
| Bottom |
|
||||
|
@ -2112,25 +2112,13 @@ enum gui_layout_format {
|
|||
/* panels in slots are added top to bottom */
|
||||
};
|
||||
|
||||
struct gui_layout_config {
|
||||
/* every value is in percent (0.0f - 1.0f) */
|
||||
gui_float left;
|
||||
/* horizontal window ratio left slot */
|
||||
gui_float right;
|
||||
/* horizontal window ratio right slot */
|
||||
gui_float centerh;
|
||||
/* horizontal window ratio center slot */
|
||||
gui_float centerv;
|
||||
/* vertical window ratio in center slot */
|
||||
gui_float bottom;
|
||||
/* vertical window ratio in bottom slot */
|
||||
gui_float top;
|
||||
/* vertical window ratio in top slot */
|
||||
};
|
||||
|
||||
struct gui_layout_slot {
|
||||
gui_float scaler_width;
|
||||
/* width of the scaling line between slots */
|
||||
gui_size capacity;
|
||||
/* number of panels inside the slot */
|
||||
gui_float value;
|
||||
/* temporary storage for the layout build up process */
|
||||
struct gui_vec2 ratio;
|
||||
/* horizontal and vertical window ratio */
|
||||
struct gui_vec2 offset;
|
||||
|
@ -2139,11 +2127,6 @@ struct gui_layout_slot {
|
|||
/* panel filling layout */
|
||||
};
|
||||
|
||||
enum gui_layout_state {
|
||||
GUI_LAYOUT_DEACTIVATED,
|
||||
GUI_LAYOUT_ACTIVATED
|
||||
};
|
||||
|
||||
enum gui_layout_flags {
|
||||
GUI_LAYOUT_INACTIVE = 0x01,
|
||||
/* tiled layout is inactive and cannot be updated by the user */
|
||||
|
@ -2162,16 +2145,10 @@ struct gui_layout {
|
|||
/* each slot inside the panel layout */
|
||||
};
|
||||
|
||||
void gui_layout_init(struct gui_layout*, const struct gui_layout_config*,
|
||||
gui_flags, gui_size width, gui_size height);
|
||||
/* initializes the layout with given slot ratio and size */
|
||||
void gui_layout_set_size(struct gui_layout*, gui_size width, gui_size height);
|
||||
/* updates the size of the complete layout */
|
||||
void gui_layout_set_state(struct gui_layout*, gui_uint state);
|
||||
/* updates the state of the layout */
|
||||
void gui_layout_begin(struct gui_layout*, gui_size width, gui_size height, gui_flags);
|
||||
void gui_layout_slot(struct gui_layout*, enum gui_layout_slot_index,
|
||||
enum gui_layout_format, gui_size panel_count);
|
||||
/* activates a layout slot with number of panels and filling format*/
|
||||
gui_float ratio, enum gui_layout_format, gui_size panel_count);
|
||||
void gui_layout_end(struct gui_layout*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue