[platform/canvas]

- Changed canvas coordinate system to match UI coord system (origin at top left, y axis down)
- Changed mouse coordinate system to match UI and canvas
- No need to pass width/height to ui_begin_frame() anymore
This commit is contained in:
Martin Fouilleul 2023-03-08 13:19:18 +01:00
parent 7b2ef080e8
commit 1212d714d5
12 changed files with 34 additions and 59 deletions

View File

@ -51,8 +51,8 @@ int main()
str8 path1 = mp_app_get_resource_path(mem_scratch(), "../resources/triceratops.png"); str8 path1 = mp_app_get_resource_path(mem_scratch(), "../resources/triceratops.png");
str8 path2 = mp_app_get_resource_path(mem_scratch(), "../resources/Top512.png"); str8 path2 = mp_app_get_resource_path(mem_scratch(), "../resources/Top512.png");
mg_image_region image1 = mg_image_atlas_alloc_from_file(atlas, atlasImage, path1, true); mg_image_region image1 = mg_image_atlas_alloc_from_file(atlas, atlasImage, path1, false);
mg_image_region image2 = mg_image_atlas_alloc_from_file(atlas, atlasImage, path2, true); mg_image_region image2 = mg_image_atlas_alloc_from_file(atlas, atlasImage, path2, false);
// start app // start app
mp_window_bring_to_front(window); mp_window_bring_to_front(window);

View File

@ -123,14 +123,14 @@ int main()
{ {
if(y + 200 < contentRect.h) if(y + 200 < contentRect.h)
{ {
y+=5; y-=5;
} }
} }
else if(event.key.code == MP_KEY_DOWN) else if(event.key.code == MP_KEY_DOWN)
{ {
if(y - 200 > 0) if(y - 200 > 0)
{ {
y-=5; y+=5;
} }
} }
//*/ //*/
@ -180,19 +180,19 @@ int main()
mg_set_color_rgba(0, 0, 0, 1); mg_set_color_rgba(0, 0, 0, 1);
mg_set_width(20); mg_set_width(20);
mg_move_to(x-100, y-100); mg_move_to(x-100, y+100);
mg_cubic_to(x-50, y-150+frown, x+50, y-150+frown, x+100, y-100); mg_cubic_to(x-50, y+150+frown, x+50, y+150+frown, x+100, y+100);
mg_stroke(); mg_stroke();
// eyes // eyes
mg_ellipse_fill(x-70, y+50, 30, 50); mg_ellipse_fill(x-70, y-50, 30, 50);
mg_ellipse_fill(x+70, y+50, 30, 50); mg_ellipse_fill(x+70, y-50, 30, 50);
// text // text
mg_set_color_rgba(0, 0, 1, 1); mg_set_color_rgba(0, 0, 1, 1);
mg_set_font(font); mg_set_font(font);
mg_set_font_size(12); mg_set_font_size(12);
mg_move_to(50, 50); mg_move_to(50, 600-50);
str8 text = str8_pushf(mem_scratch(), str8 text = str8_pushf(mem_scratch(),
"Milepost vector graphics test program (frame time = %fs, fps = %f)...", "Milepost vector graphics test program (frame time = %fs, fps = %f)...",

View File

@ -43,11 +43,11 @@ int main()
//NOTE: create image //NOTE: create image
str8 imagePath = mp_app_get_resource_path(mem_scratch(), "../resources/triceratops.png"); str8 imagePath = mp_app_get_resource_path(mem_scratch(), "../resources/triceratops.png");
mg_image image = mg_image_create_from_file(imagePath, true); mg_image image = mg_image_create_from_file(imagePath, false);
vec2 imageSize = mg_image_size(image); vec2 imageSize = mg_image_size(image);
str8 imagePath2 = mp_app_get_resource_path(mem_scratch(), "../resources/Top512.png"); str8 imagePath2 = mp_app_get_resource_path(mem_scratch(), "../resources/Top512.png");
mg_image image2 = mg_image_create_from_file(imagePath2, true); mg_image image2 = mg_image_create_from_file(imagePath2, false);
vec2 imageSize2 = mg_image_size(image2); vec2 imageSize2 = mg_image_size(image2);
// start app // start app

View File

@ -140,11 +140,8 @@ int main()
f32 zoom = 1; f32 zoom = 1;
f32 startX = 10; f32 startX = 10;
f32 startY = (contentRect.h - lineHeight - 10); f32 startY = 10 + lineHeight;
/*
f32 startX = -100;
f32 startY = -100;
*/
while(!mp_should_quit()) while(!mp_should_quit())
{ {
f64 startFrameTime = mp_get_time(MP_CLOCK_MONOTONIC); f64 startFrameTime = mp_get_time(MP_CLOCK_MONOTONIC);
@ -167,7 +164,7 @@ int main()
if(event.key.action == MP_KEY_PRESS) if(event.key.action == MP_KEY_PRESS)
{ {
tracked = true; tracked = true;
vec2 mousePos = mp_input_mouse_position(); vec2 mousePos = mp_mouse_position();
trackPoint.x = mousePos.x/zoom - startX; trackPoint.x = mousePos.x/zoom - startX;
trackPoint.y = mousePos.y/zoom - startY; trackPoint.y = mousePos.y/zoom - startY;
} }
@ -180,7 +177,7 @@ int main()
case MP_EVENT_MOUSE_WHEEL: case MP_EVENT_MOUSE_WHEEL:
{ {
vec2 mousePos = mp_input_mouse_position(); vec2 mousePos = mp_mouse_position();
f32 trackX = mousePos.x/zoom - startX; f32 trackX = mousePos.x/zoom - startX;
f32 trackY = mousePos.y/zoom - startY; f32 trackY = mousePos.y/zoom - startY;
@ -198,7 +195,7 @@ int main()
if(tracked) if(tracked)
{ {
vec2 mousePos = mp_input_mouse_position(); vec2 mousePos = mp_mouse_position();
startX = mousePos.x/zoom - trackPoint.x; startX = mousePos.x/zoom - trackPoint.x;
startY = mousePos.y/zoom - trackPoint.y; startY = mousePos.y/zoom - trackPoint.y;
} }
@ -249,7 +246,7 @@ int main()
mg_glyph_outlines((str32){subIndex, glyphs}); mg_glyph_outlines((str32){subIndex, glyphs});
mg_fill(); mg_fill();
textY -= lineHeight; textY += lineHeight;
mg_move_to(textX, textY); mg_move_to(textX, textY);
startIndex++; startIndex++;
@ -261,7 +258,7 @@ int main()
mg_set_color_rgba(0, 0, 1, 1); mg_set_color_rgba(0, 0, 1, 1);
mg_set_font(font); mg_set_font(font);
mg_set_font_size(14); mg_set_font_size(14);
mg_move_to(10, 10 + lineHeight); mg_move_to(10, contentRect.h - 10 - lineHeight);
str8 text = str8_pushf(mem_scratch(), str8 text = str8_pushf(mem_scratch(),
"Test program: %i glyphs, frame time = %fs, fps = %f", "Test program: %i glyphs, frame time = %fs, fps = %f",

View File

@ -258,7 +258,7 @@ int main()
ui_flags defaultFlags = UI_FLAG_DRAW_BORDER; ui_flags defaultFlags = UI_FLAG_DRAW_BORDER;
ui_box* root = 0; ui_box* root = 0;
ui_frame(800, 610) ui_frame()
{ {
root = ui_box_top(); root = ui_box_top();

View File

@ -3348,7 +3348,7 @@ mp_rect mg_glyph_outlines_from_font_data(mg_font_data* fontData, str32 glyphIndi
f32 xOffset = canvas->subPathLastPoint.x; f32 xOffset = canvas->subPathLastPoint.x;
f32 yOffset = canvas->subPathLastPoint.y; f32 yOffset = canvas->subPathLastPoint.y;
f32 flip = canvas->textFlip ? -1 : 1; f32 flip = canvas->textFlip ? 1 : -1;
if(!glyphIndex || glyphIndex >= fontData->glyphCount) if(!glyphIndex || glyphIndex >= fontData->glyphCount)
{ {

View File

@ -16,7 +16,7 @@ vertex vs_out VertexShader(ushort vid [[vertex_id]])
{ {
vs_out out; vs_out out;
out.uv = float2((vid << 1) & 2, vid & 2); out.uv = float2((vid << 1) & 2, vid & 2);
out.pos = float4(out.uv * float2(2, 2) + float2(-1, -1), 0, 1); out.pos = float4(out.uv * float2(2, -2) + float2(-1, 1), 0, 1);
return(out); return(out);
} }

View File

@ -871,12 +871,12 @@ static void mp_process_mouse_button(NSEvent* nsEvent, mp_window_data* window, mp
event.type = MP_EVENT_MOUSE_MOVE; event.type = MP_EVENT_MOUSE_MOVE;
event.window = mp_window_handle_from_ptr(window); event.window = mp_window_handle_from_ptr(window);
event.move.x = p.x; event.move.x = p.x;
event.move.y = p.y; event.move.y = frame.size.height - p.y;
event.move.deltaX = [nsEvent deltaX]; event.move.deltaX = [nsEvent deltaX];
event.move.deltaY = -[nsEvent deltaY]; event.move.deltaY = [nsEvent deltaY];
event.move.mods = mp_convert_osx_mods([nsEvent modifierFlags]); event.move.mods = mp_convert_osx_mods([nsEvent modifierFlags]);
mp_update_mouse_move(p.x, p.y, event.move.deltaX, event.move.deltaY); mp_update_mouse_move(event.move.x, event.move.y, event.move.deltaX, event.move.deltaY);
mp_queue_event(&event); mp_queue_event(&event);
} }
@ -891,7 +891,7 @@ static void mp_process_mouse_button(NSEvent* nsEvent, mp_window_data* window, mp
event.move.x = 0; event.move.x = 0;
event.move.y = 0; event.move.y = 0;
event.move.deltaX = [nsEvent scrollingDeltaX]*factor; event.move.deltaX = [nsEvent scrollingDeltaX]*factor;
event.move.deltaY = [nsEvent scrollingDeltaY]*factor; event.move.deltaY = -[nsEvent scrollingDeltaY]*factor;
event.move.mods = mp_convert_osx_mods([nsEvent modifierFlags]); event.move.mods = mp_convert_osx_mods([nsEvent modifierFlags]);
mp_update_mouse_wheel(event.move.deltaX, event.move.deltaY); mp_update_mouse_wheel(event.move.deltaX, event.move.deltaY);

View File

@ -66,9 +66,6 @@ typedef struct ui_context
{ {
bool init; bool init;
f32 width;
f32 height;
u64 frameCounter; u64 frameCounter;
f64 frameTime; f64 frameTime;
f64 lastFrameDuration; f64 lastFrameDuration;
@ -384,10 +381,7 @@ bool ui_box_hovering(ui_box* box, vec2 p)
vec2 ui_mouse_position(void) vec2 ui_mouse_position(void)
{ {
ui_context* ui = ui_get_context();
vec2 mousePos = mp_mouse_position(); vec2 mousePos = mp_mouse_position();
mousePos.y = ui->height - mousePos.y;
return(mousePos); return(mousePos);
} }
@ -395,7 +389,6 @@ vec2 ui_mouse_delta(void)
{ {
ui_context* ui = ui_get_context(); ui_context* ui = ui_get_context();
vec2 delta = mp_mouse_delta(); vec2 delta = mp_mouse_delta();
delta.y *= -1.;
return(delta); return(delta);
} }
@ -403,7 +396,6 @@ vec2 ui_mouse_wheel(void)
{ {
ui_context* ui = ui_get_context(); ui_context* ui = ui_get_context();
vec2 delta = mp_mouse_wheel(); vec2 delta = mp_mouse_wheel();
delta.y *= -1.;
return(delta); return(delta);
} }
@ -1278,33 +1270,24 @@ void ui_draw()
ui_context* ui = ui_get_context(); ui_context* ui = ui_get_context();
//NOTE: draw //NOTE: draw
mg_mat2x3 transform = {1, 0, 0,
0, -1, ui->height};
bool oldTextFlip = mg_get_text_flip(); bool oldTextFlip = mg_get_text_flip();
mg_set_text_flip(true); mg_set_text_flip(false);
mg_matrix_push(transform);
ui_draw_box(ui->root); ui_draw_box(ui->root);
mg_matrix_pop();
mg_set_text_flip(oldTextFlip); mg_set_text_flip(oldTextFlip);
//TODO: restore flip??
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// frame begin/end // frame begin/end
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void ui_begin_frame(u32 width, u32 height) void ui_begin_frame()
{ {
ui_context* ui = ui_get_context(); ui_context* ui = ui_get_context();
mem_arena_clear(&ui->frameArena); mem_arena_clear(&ui->frameArena);
ui->width = width;
ui->height = height;
ui->frameCounter++; ui->frameCounter++;
f64 time = mp_get_time(MP_CLOCK_MONOTONIC); f64 time = mp_get_time(MP_CLOCK_MONOTONIC);
ui->lastFrameDuration = time - ui->frameTime; ui->lastFrameDuration = time - ui->frameTime;
@ -1314,8 +1297,8 @@ void ui_begin_frame(u32 width, u32 height)
ui->z = 0; ui->z = 0;
ui_style defaultStyle = {0}; ui_style defaultStyle = {0};
defaultStyle.size.s[UI_AXIS_X] = (ui_size){UI_SIZE_PIXELS, width, 0}; defaultStyle.size.s[UI_AXIS_X] = (ui_size){UI_SIZE_CHILDREN};
defaultStyle.size.s[UI_AXIS_Y] = (ui_size){UI_SIZE_PIXELS, height, 0}; defaultStyle.size.s[UI_AXIS_Y] = (ui_size){UI_SIZE_CHILDREN};
ui->root = ui_box_begin("_root_", 0); ui->root = ui_box_begin("_root_", 0);
*ui->root->targetStyle = defaultStyle; *ui->root->targetStyle = defaultStyle;

View File

@ -316,11 +316,11 @@ void ui_init(void);
ui_context* ui_get_context(void); ui_context* ui_get_context(void);
void ui_set_context(ui_context* context); void ui_set_context(ui_context* context);
void ui_begin_frame(u32 width, u32 height); void ui_begin_frame(void);
void ui_end_frame(void); void ui_end_frame(void);
void ui_draw(void); void ui_draw(void);
#define ui_frame(width, height) defer_loop(ui_begin_frame(width, height), ui_end_frame()) #define ui_frame() defer_loop(ui_begin_frame(), ui_end_frame())
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
// Box keys // Box keys

View File

@ -23,13 +23,7 @@ extern "C" {
const decltype( ((type *)0)->member ) *__mptr = (ptr); \ const decltype( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - OFFSET_OF_CONTAINER(type,member) );}) (type *)( (char *)__mptr - OFFSET_OF_CONTAINER(type,member) );})
#else #else
/*
#define CONTAINER_OF(ptr, type, member) ({ \
const char *__mptr = (char*)(ptr); \
(type *)(__mptr - OFFSET_OF_CONTAINER(type,member) );})
*/
#define CONTAINER_OF(ptr, type, member) (type *)((char*)(ptr) - OFFSET_OF_CONTAINER(type,member)) #define CONTAINER_OF(ptr, type, member) (type *)((char*)(ptr) - OFFSET_OF_CONTAINER(type,member))
#endif #endif
//------------------------------------------------------------------------- //-------------------------------------------------------------------------

View File

@ -9,6 +9,7 @@
#ifndef __STRINGS_H_ #ifndef __STRINGS_H_
#define __STRINGS_H_ #define __STRINGS_H_
#include<string.h>
#include"typedefs.h" #include"typedefs.h"
#include"lists.h" #include"lists.h"
#include"memory.h" #include"memory.h"