diff --git a/examples/fabric/src/fabric.c b/examples/fabric/src/fabric.c index fc8d4470..a828bc64 100644 --- a/examples/fabric/src/fabric.c +++ b/examples/fabric/src/fabric.c @@ -88,12 +88,6 @@ void addPt(double xp, double yp) points = newPoints; } -void Fabric_resize(double width, double height) -{ - canvasWidth = width; - canvasHeight = height; -} - ///---OBJECTS---/// // gets distance between two points (pythogorian theorum) @@ -311,7 +305,7 @@ void Fabric_getSpans(Span **outSpans, int *count) void Fabric_getPoints(Point **outPoints, int *count) { *outPoints = points; - *count = spanCount; + *count = pointCount; } /** @@ -319,15 +313,18 @@ void Fabric_getPoints(Point **outPoints, int *count) * * @param fw fabric width (as percentage of canvas width) * @param fh fabric height (as percentage of canvas height) + * @param world_width world width of the simulation + * @param world_height world height of the simulation */ -void Fabric_init(int fw, int fh, int canvaswidth, int canvasHeight) +void Fabric_init(int fw, int fh, int world_width, int world_height) { int i, j, x, y; int htc = fw; // fabric horizontal thread count int vtc = fh; // fabric vertical thread count srand((unsigned)time(NULL)); - Fabric_resize(canvaswidth, canvasHeight); + canvasWidth = world_width; + canvasHeight = world_height; grabRadius = canvasWidth / 25; // creates points diff --git a/examples/fabric/src/fabric.h b/examples/fabric/src/fabric.h index e493dd96..f82ea305 100644 --- a/examples/fabric/src/fabric.h +++ b/examples/fabric/src/fabric.h @@ -1,5 +1,10 @@ #include +// The fabric simulation always runs in this fixed coordinate space. The +// viewport scales it to fit the window like CSS `object-fit: contain`. +#define FABRIC_WORLD_WIDTH 800 +#define FABRIC_WORLD_HEIGHT 600 + typedef struct PointRec_ { double cx; double cy; @@ -22,8 +27,6 @@ void Fabric_getSpans(Span **outSpans, int *count); void Fabric_getPoints(Point **outPoints, int *count); -void Fabric_resize(double width, double height); - void Fabric_update(void); // grabs fabric on click diff --git a/examples/fabric/src/main.c b/examples/fabric/src/main.c index 4e07e010..05227e62 100644 --- a/examples/fabric/src/main.c +++ b/examples/fabric/src/main.c @@ -6,18 +6,50 @@ #include "fabric.h" #define M_PI 3.1415926 -#define FABRIC_WIDTH 800 -#define FABRIC_HEIGHT 600 typedef struct ui_fabric { int timer; } ui_fabric_t; +typedef struct ui_fabric_viewport { + double scale; + double offset_x; + double offset_y; +} ui_fabric_viewport_t; + bool displayPoints = false; bool displaySpans = true; ui_widget_prototype_t *ui_fabric_proto; +// Computes the viewport that fits the fixed-size fabric world into the +// widget content box, like CSS `object-fit: contain`. +static void ui_fabric_get_viewport(ui_widget_t *w, ui_fabric_viewport_t *vp) +{ + double scale_x = w->content_box.width / FABRIC_WORLD_WIDTH; + double scale_y = w->content_box.height / FABRIC_WORLD_HEIGHT; + + vp->scale = scale_x < scale_y ? scale_x : scale_y; + if (vp->scale <= 0) { + vp->scale = 1; + } + vp->offset_x = + (w->content_box.width - FABRIC_WORLD_WIDTH * vp->scale) / 2; + vp->offset_y = + (w->content_box.height - FABRIC_WORLD_HEIGHT * vp->scale) / 2; +} + +// Maps a point in the root (or window) coordinate space to the fabric world. +static void ui_fabric_window_to_world(ui_widget_t *w, float x, float y, + double *world_x, double *world_y) +{ + ui_fabric_viewport_t vp; + + ui_fabric_get_viewport(w, &vp); + *world_x = (x - w->content_box.x - vp.offset_x) / vp.scale; + *world_y = (y - w->content_box.y - vp.offset_y) / vp.scale; +} + #ifdef HAS_CAIRO // displays points void render_points(cairo_t *cr) @@ -58,6 +90,8 @@ void render_spans(cairo_t *cr) void ui_fabric_on_frame(ui_widget_t *w) { #ifdef HAS_CAIRO + float scale; + ui_fabric_viewport_t vp; ui_canvas_context_t *ctx = ui_canvas_get_context(w); cairo_surface_t *surface = cairo_image_surface_create_for_data( ctx->buffer.bytes, CAIRO_FORMAT_RGB24, ctx->buffer.width, @@ -68,6 +102,20 @@ void ui_fabric_on_frame(ui_widget_t *w) // clears previous frame ctx->fill_color = pd_color(255, 255, 255, 255); ctx->fill_rect(ctx, 0, 0, ctx->width, ctx->height); + // First map logical pixels to buffer pixels, then map the fabric + // world to the contain viewport inside the content box. The buffer + // is created with the window DPI, which may differ from the global + // metrics read during the timer callback, so derive the ratio from + // the buffer and content box instead of using ctx->scale. + if (w->content_box.width > 0) { + scale = (float)(ctx->buffer.width / w->content_box.width); + } else { + scale = ctx->scale > 0 ? ctx->scale : 1.0f; + } + ui_fabric_get_viewport(w, &vp); + cairo_scale(cr, scale, scale); + cairo_translate(cr, vp.offset_x, vp.offset_y); + cairo_scale(cr, vp.scale, vp.scale); if (displayPoints) { render_points(cr); } @@ -83,19 +131,19 @@ void ui_fabric_on_frame(ui_widget_t *w) static void ui_fabric_on_mousedown(ui_widget_t *w, ui_event_t *e, void *arg) { - float x, y; + double x, y; - ui_widget_get_offset(w, NULL, &x, &y); - Fabric_grab(e->mouse.x - x, e->mouse.y - y); + ui_fabric_window_to_world(w, e->mouse.x, e->mouse.y, &x, &y); + Fabric_grab(x, y); ui_widget_set_mouse_capture(w); } static void ui_fabric_on_mousemove(ui_widget_t *w, ui_event_t *e, void *arg) { - float x, y; + double x, y; - ui_widget_get_offset(w, NULL, &x, &y); - Fabric_move(e->mouse.x - x, e->mouse.y - y); + ui_fabric_window_to_world(w, e->mouse.x, e->mouse.y, &x, &y); + Fabric_move(x, y); } static void ui_fabric_on_mouseup(ui_widget_t *w, ui_event_t *e, void *arg) @@ -104,6 +152,13 @@ static void ui_fabric_on_mouseup(ui_widget_t *w, ui_event_t *e, void *arg) ui_widget_release_mouse_capture(w); } +static void ui_fabric_on_resize(ui_widget_t *w, float width, float height) +{ + // The fabric keeps its fixed world size; only the canvas buffer needs + // to be rebuilt for the new physical size. + ui_fabric_proto->proto->resize(w, width, height); +} + void ui_fabric_on_init(ui_widget_t *w) { ui_fabric_t *data; @@ -129,20 +184,19 @@ void ui_register_fabric(void) ui_fabric_proto = ui_create_widget_prototype("fabric", "canvas"); ui_fabric_proto->init = ui_fabric_on_init; ui_fabric_proto->destroy = ui_fabric_on_destroy; + ui_fabric_proto->resize = ui_fabric_on_resize; } const char *app_css = "\ root {\ display: flex;\ - align-items: center;\ - justify-content: center;\ background: #f2f4f5;\ - width: 800px;\ - height: 600px;\ + width: 100%;\ + height: 100%;\ }\ fabric {\ - width: 800px;\ - height: 600px;\ + width: 100%;\ + height: 100%;\ border: 1px solid #697b8c;\ box-sizing: border-box;\ box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);\ @@ -154,11 +208,13 @@ int main(int argc, char **argv) lcui_window_t *window; lcui_init(); - Fabric_init(66, 55, 800, 600); + Fabric_init(66, 55, FABRIC_WORLD_WIDTH, FABRIC_WORLD_HEIGHT); ui_register_fabric(); ui_load_css_string(app_css, __FILE__); ui_widget_set_title(ui_root(), L"Fabric"); ui_root_append(ui_create_widget("fabric")); + opt.width = FABRIC_WORLD_WIDTH; + opt.height = FABRIC_WORLD_HEIGHT; window = lcui_window_create_for_widget(ui_root(), &opt); lcui_window_show(window); return lcui_main();