From ee976ace83ec1f6a9408bdaebb90754834863d16 Mon Sep 17 00:00:00 2001 From: hgn Date: Mon, 30 Sep 2024 17:42:37 +0100 Subject: [PATCH] keep panels relative to corners --- vg_engine.c | 3 +++ vg_magi.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++--- vg_magi.h | 3 +++ 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/vg_engine.c b/vg_engine.c index 920bdd8..98a4344 100644 --- a/vg_engine.c +++ b/vg_engine.c @@ -244,6 +244,9 @@ static void _vg_process_events(void) } else { + + i32 delta[2] = { w - vg.window_x, h - vg.window_y }; + _vg_magi_area_change( delta ); vg.window_x = w; vg.window_y = h; diff --git a/vg_magi.c b/vg_magi.c index 6d1abb5..15ad090 100644 --- a/vg_magi.c +++ b/vg_magi.c @@ -19,6 +19,7 @@ struct vg_magi_panel *_vg_magi_open( ui_px w, ui_px h, u32 flags ) panel->close_cb = NULL; panel->min_w = w; panel->min_h = h; + panel->corner = 0; if( flags & VG_MAGI_PERSISTENT ) strcpy( panel->cmd, vg_console.input ); @@ -26,6 +27,26 @@ struct vg_magi_panel *_vg_magi_open( ui_px w, ui_px h, u32 flags ) return panel; } +void _vg_magi_area_change( i32 d[2] ) +{ + for( u32 i=0; i<_vg_magi.panel_count; i ++ ) + { + struct vg_magi_panel *panel = &_vg_magi.panels[ i ]; + if( panel->corner & 0x1 ) + panel->rect[0] += d[0]; + if( panel->corner & 0x2 ) + panel->rect[1] += d[1]; + } +} + +static void vg_magi_getcorner( ui_rect rect, u32 id, ui_px corner[2] ) +{ + corner[0] = rect[0]; + corner[1] = rect[1]; + if( id&0x1 ) corner[0] += rect[2]; + if( id&0x2 ) corner[1] += rect[3]; +} + void _vg_magi_render( ui_context *ctx ) { if( _vg_magi.panel_count == 0 ) return; @@ -64,6 +85,26 @@ void _vg_magi_render( ui_context *ctx ) { ptop->rect[0] = _vg_magi.drag_original[0] + ctx->mouse_delta[0]; ptop->rect[1] = _vg_magi.drag_original[1] + ctx->mouse_delta[1]; + + ui_rect vp = { 0,0, ctx->area[0],ctx->area[1] }; + + f32 min2 = 9999999.9f; + for( u32 i=0; i<4; i ++ ) + { + ui_px c0[2], c1[2]; + vg_magi_getcorner( vp, i, c0 ); + vg_magi_getcorner( ptop->rect, i, c1 ); + + f32 dx = c0[0]-c1[0], + dy = c0[1]-c1[1], + d2 = dx*dx + dy*dy; + + if( d2 < min2 ) + { + min2 = d2; + ptop->corner = i; + } + } } else if( _vg_magi.mode == k_magi_mode_resize ) { @@ -222,11 +263,17 @@ void vg_magi_save(void) if( magi->flags & VG_MAGI_PERSISTENT ) { + ui_rect vp = {0,0,vg.window_x,vg.window_y}; + ui_px c[2], p[2]; + vg_magi_getcorner( vp, magi->corner, c ); + p[0] = magi->rect[0] - c[0]; + p[1] = magi->rect[1] - c[1]; + fprintf( fp, "%s\n", magi->cmd ); - fprintf( fp, "magi_pos %d %d %d %d %d\n", - magi->rect[0], magi->rect[1], magi->rect[2], + fprintf( fp, "magi_pos %d %d %d %d %d %d\n", + p[0], p[1], magi->rect[2], magi->minimized? magi->sh: magi->rect[3], - (i32)magi->minimized ); + (i32)magi->minimized, magi->corner ); } } @@ -262,6 +309,16 @@ static int cmd_vg_magi_dim( int argc, const char *argv[] ) } } + if( argc >= 6 ) + { + ui_rect vp = {0,0,vg.window_x,vg.window_y}; + ui_px c[2]; + magi->corner = atoi( argv[5] ); + vg_magi_getcorner( vp, magi->corner, c ); + magi->rect[0] += c[0]; + magi->rect[1] += c[1]; + } + return 1; } diff --git a/vg_magi.h b/vg_magi.h index 3d480a0..0290914 100644 --- a/vg_magi.h +++ b/vg_magi.h @@ -15,6 +15,8 @@ struct vg_magi ui_rect rect; ui_px sh; + u32 corner; /* which corner of screen relative to? TL, TR, BL, BR */ + u32 flags; void *data; @@ -47,3 +49,4 @@ void _vg_magi_render( ui_context *ctx ); void vg_magi_init(void); void vg_magi_save(void); void vg_magi_restore(void); +void _vg_magi_area_change( i32 d[2] ); -- 2.25.1