[ui, canvas]

- Allow querying the size of the canvas's underlying surface
- Set size of ui root to canvas' size
This commit is contained in:
Martin Fouilleul 2023-03-08 15:03:35 +01:00
parent 1212d714d5
commit 873d4ab6db
5 changed files with 43 additions and 7 deletions

View File

@ -255,7 +255,7 @@ int main()
.font = font, .font = font,
.fontSize = 32}; .fontSize = 32};
ui_flags defaultFlags = UI_FLAG_DRAW_BORDER; ui_flags defaultFlags = UI_FLAG_DRAW_BORDER | UI_FLAG_DRAW_BACKGROUND;
ui_box* root = 0; ui_box* root = 0;
ui_frame() ui_frame()
@ -294,6 +294,9 @@ int main()
&(ui_style){.bgColor = {0.5, 1, 0.5, 1}}, &(ui_style){.bgColor = {0.5, 1, 0.5, 1}},
UI_STYLE_BG_COLOR); UI_STYLE_BG_COLOR);
ui_style_next(&(ui_style){.bgColor = {0.7, 0.7, 0.7, 1}}, UI_STYLE_BG_COLOR);
ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1}, .size.height = {UI_SIZE_PARENT, 1}}, UI_STYLE_SIZE);
ui_container("a", defaultFlags) ui_container("a", defaultFlags)
{ {
ui_pattern pattern = {0}; ui_pattern pattern = {0};
@ -337,6 +340,14 @@ int main()
static f32 slider3 = 0; static f32 slider3 = 0;
ui_slider("slider3", 0.3, &slider3); ui_slider("slider3", 0.3, &slider3);
ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 20},
.size.height = {UI_SIZE_PIXELS, 200}},
UI_STYLE_SIZE);
static f32 slider4 = 0;
ui_slider("slider4", 0.3, &slider4);
} }
} }
ui_tag_next("foo"); ui_tag_next("foo");

View File

@ -212,6 +212,7 @@ typedef struct mg_canvas_data
u32 vertexCount; u32 vertexCount;
u32 indexCount; u32 indexCount;
mg_surface surface;
mg_canvas_backend* backend; mg_canvas_backend* backend;
} mg_canvas_data; } mg_canvas_data;
@ -2734,6 +2735,7 @@ mg_canvas mg_canvas_create(mg_surface surface)
memset(canvas, 0, sizeof(mg_canvas_data)); memset(canvas, 0, sizeof(mg_canvas_data));
} }
canvas->surface = surface;
canvas->backend = backend; canvas->backend = backend;
canvas->attributes.color = (mg_color){0, 0, 0, 1}; canvas->attributes.color = (mg_color){0, 0, 0, 1};
@ -2780,6 +2782,17 @@ mg_canvas mg_canvas_set_current(mg_canvas canvas)
return(old); return(old);
} }
vec2 mg_canvas_size(void)
{
vec2 res = {0};
if(__mgCurrentCanvas)
{
mp_rect frame = mg_surface_get_frame(__mgCurrentCanvas->surface);
res = (vec2){frame.w, frame.h};
}
return(res);
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -169,15 +169,16 @@ typedef struct mg_text_extents
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
//NOTE(martin): graphics canvas //NOTE(martin): graphics canvas
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
MP_API mg_canvas mg_canvas_nil(); MP_API mg_canvas mg_canvas_nil(void);
MP_API bool mg_canvas_is_nil(mg_canvas canvas); MP_API bool mg_canvas_is_nil(mg_canvas canvas);
MP_API mg_canvas mg_canvas_create(mg_surface surface); MP_API mg_canvas mg_canvas_create(mg_surface surface);
MP_API void mg_canvas_destroy(mg_canvas canvas); MP_API void mg_canvas_destroy(mg_canvas canvas);
MP_API mg_canvas mg_canvas_set_current(mg_canvas canvas); MP_API mg_canvas mg_canvas_set_current(mg_canvas canvas);
MP_API void mg_flush(); MP_API void mg_flush(void);
MP_API vec2 mg_canvas_size(void);
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
//NOTE(martin): fonts //NOTE(martin): fonts
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------

View File

@ -1296,17 +1296,25 @@ void ui_begin_frame()
ui->clipStack = 0; ui->clipStack = 0;
ui->z = 0; ui->z = 0;
vec2 size = mg_canvas_size();
ui_style defaultStyle = {0}; ui_style defaultStyle = {0};
defaultStyle.size.s[UI_AXIS_X] = (ui_size){UI_SIZE_CHILDREN}; defaultStyle.size.s[UI_AXIS_X] = (ui_size){UI_SIZE_PIXELS, size.x};
defaultStyle.size.s[UI_AXIS_Y] = (ui_size){UI_SIZE_CHILDREN}; defaultStyle.size.s[UI_AXIS_Y] = (ui_size){UI_SIZE_PIXELS, size.y};
ui->root = ui_box_begin("_root_", 0); ui->root = ui_box_begin("_root_", 0);
*ui->root->targetStyle = defaultStyle; *ui->root->targetStyle = defaultStyle;
ui_style_next(&(ui_style){.layout = {UI_AXIS_Y, UI_ALIGN_START, UI_ALIGN_START}, ui_style_mask contentStyleMask = UI_STYLE_SIZE
| UI_STYLE_LAYOUT
| UI_STYLE_FLOAT;
ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 1},
.size.height = {UI_SIZE_PARENT, 1},
.layout = {UI_AXIS_Y, UI_ALIGN_START, UI_ALIGN_START},
.floating = {true, true}, .floating = {true, true},
.floatTarget = {0, 0}}, .floatTarget = {0, 0}},
UI_STYLE_LAYOUT | UI_STYLE_FLOAT_X | UI_STYLE_FLOAT_Y); contentStyleMask);
ui_box* contents = ui_box_make("_contents_", 0); ui_box* contents = ui_box_make("_contents_", 0);

View File

@ -168,6 +168,9 @@ enum
| UI_STYLE_LAYOUT_MARGIN_X | UI_STYLE_LAYOUT_MARGIN_X
| UI_STYLE_LAYOUT_MARGIN_Y, | UI_STYLE_LAYOUT_MARGIN_Y,
UI_STYLE_FLOAT = UI_STYLE_FLOAT_X
| UI_STYLE_FLOAT_Y,
UI_STYLE_MASK_INHERITED = UI_STYLE_COLOR UI_STYLE_MASK_INHERITED = UI_STYLE_COLOR
| UI_STYLE_FONT | UI_STYLE_FONT
| UI_STYLE_FONT_SIZE | UI_STYLE_FONT_SIZE