From 9d5d8ba4eaea5c0ec9e4e24cedad51fa727bf2c3 Mon Sep 17 00:00:00 2001 From: jesse Date: Sat, 31 May 2025 16:31:10 -0700 Subject: [PATCH] made type Rect and added rect_cut procedures --- math/module.jai | 34 ++++++++++++++++++++++++++++++++- math/shape.jai | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 math/shape.jai diff --git a/math/module.jai b/math/module.jai index b1d50e2..c81a075 100644 --- a/math/module.jai +++ b/math/module.jai @@ -1,8 +1,12 @@ #module_parameters( UNITS: enum { radians; degrees; turns; } = .turns, + RECT_TYPE: Type = float, + // RECT_METHOD: enum { dimension; absolute; } = absolute, // Note(Jesse): Maybe at a later point we can do this RUN_TESTS := false ); +#assert meta.type_is_scalar(RECT_TYPE); + // @todo(judah): dumb we can't use meta.range_for here. U8_Min, U8_Max :: #run meta.lo_for(u8), #run meta.hi_for(u8); @@ -18,10 +22,11 @@ S64_Min, S64_Max :: #run meta.lo_for(s64), #run meta.hi_for(s64); F32_Min, F32_Max :: #run meta.lo_for(float32), #run meta.hi_for(float32); F64_Min, F64_Max :: #run meta.lo_for(float64), #run meta.hi_for(float64); +#load "common.jai"; #load "vec.jai"; #load "mat.jai"; #load "ease.jai"; -#load "common.jai"; +#load "shape.jai"; #scope_module; @@ -35,6 +40,7 @@ F64_Min, F64_Max :: #run meta.lo_for(float64), #run meta.hi_for(float64); mat2_tests(); mat4_tests(); quat_tests(); + rect_cut_tests(); } #scope_file; @@ -338,6 +344,32 @@ quat_tests :: () { } } +rect_cut_tests :: () { + r := make_rect(0, 0, 1920, 1080); + menu_ribbon := cut_top(*r, 28); + basic.print("menu_ribbon: %\n", menu_ribbon); + basic.print("r': %\n", r); + + status_bar := cut_bottom(*r, 16); + basic.print("status_bar: %\n", status_bar); + basic.print("r': %\n", r); + + character_preview_panel := cut_right(*r, r.width/3); + basic.print("character_preview_panel: %\n", character_preview_panel); + basic.print("r': %\n", r); + + text_box := cut_top(*r, r.height/2); + basic.print("text_box: %\n", text_box); + basic.print("r': %\n", r); + + dialogue_choices := cut_left(*r, r.width/2); + basic.print("dialogue_choices: %\n", dialogue_choices); + basic.print("r': %\n", r); + + dialogue_history := r; + basic.print("dialogue_history: %\n", dialogue_history); +} + dot_print :: (fmt: string, args: ..Any, width: int = 30) { dots := "................................................................."; str := basic.tprint(fmt, args); diff --git a/math/shape.jai b/math/shape.jai new file mode 100644 index 0000000..e6fde84 --- /dev/null +++ b/math/shape.jai @@ -0,0 +1,51 @@ + +Rect :: #type,distinct Vec(4, RECT_TYPE); + +make_rect :: (x: RECT_TYPE, y: RECT_TYPE, w: RECT_TYPE, h: RECT_TYPE) -> Rect { + r: Rect = ---; + r.x = x; + r.y = y; + r.width = w; + r.height = h; + return r; +} + +cut_left :: (rect: *Rect, want: RECT_TYPE) -> Rect { + amnt := basic.min(want, rect.width); + r := rect.*; + r.width = amnt; + rect.x += amnt; + rect.width -= amnt; + return r; +} + +cut_right :: (rect: *Rect, want: RECT_TYPE) -> Rect { + amnt := basic.min(want, rect.width); + r := rect.*; + r.x += rect.width - amnt; + r.width = amnt; + rect.width -= r.width; + return r; +} + +cut_top :: (rect: *Rect, want: RECT_TYPE) -> Rect { + amnt := basic.min(rect.height, want); + r := rect.*; + r.height -= amnt; + rect.y += amnt; + rect.height -= amnt; + return r; +} + +cut_bottom :: (rect: *Rect, want: RECT_TYPE) -> Rect { + amnt := basic.min(want, rect.height); + r := rect.*; + r.y += r.height - amnt; + r.height = amnt; + rect.height -= amnt; + return r; +} + +#scope_file + +basic :: #import "Basic";