From 5a89e9ec7d5f71337af4ebf094ba3d37ab2b9d11 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Sat, 14 Dec 2024 13:29:45 +0100 Subject: [PATCH] fixed formatting fuckups offered by the zed team --- src/cache.c3 | 60 +++++++++++++++++++++++----------------------- src/main.c3 | 22 ++++++++--------- src/ugui_cmd.c3 | 20 ++++++++-------- src/ugui_data.c3 | 4 ++-- src/ugui_div.c3 | 6 ++--- src/ugui_impl.c3 | 12 +++++----- src/ugui_input.c3 | 34 +++++--------------------- src/ugui_layout.c3 | 18 +++++++------- src/ugui_slider.c3 | 4 ++-- src/ugui_text.c3 | 2 +- src/vtree.c3 | 30 +++++++++++------------ 11 files changed, 95 insertions(+), 117 deletions(-) diff --git a/src/cache.c3 b/src/cache.c3 index 2b1b5f2..5b37d29 100644 --- a/src/cache.c3 +++ b/src/cache.c3 @@ -24,10 +24,10 @@ def IdTableEntry = map::Entry() @private; const usz CACHE_NCYCLES = (usz)(SIZE * 2.0/3.0); struct Cache { - BitArr present, used; - IdTable table; - Value[] pool; - usz cycle_count; + BitArr present, used; + IdTable table; + Value[] pool; + usz cycle_count; } // Every CACHE_CYCLES operations mark as not-present the unused elements @@ -44,32 +44,32 @@ macro Cache.cycle(&cache) @private { fn void! Cache.init(&cache) { - cache.table.new_init(capacity: SIZE); - // FIXME: this shit is SLOW - foreach (idx, bit : cache.used) { cache.used[idx] = false; } - foreach (idx, bit : cache.present) { cache.present[idx] = false; } - cache.pool = mem::new_array(Value, SIZE); + cache.table.new_init(capacity: SIZE); + // FIXME: this shit is SLOW + foreach (idx, bit : cache.used) { cache.used[idx] = false; } + foreach (idx, bit : cache.present) { cache.present[idx] = false; } + cache.pool = mem::new_array(Value, SIZE); } fn void Cache.free(&cache) { - (void)cache.table.free(); - (void)mem::free(cache.pool); + (void)cache.table.free(); + (void)mem::free(cache.pool); } fn Value*! Cache.search(&cache, Key id) { - // get_entry() faults on miss + // get_entry() faults on miss IdTableEntry* entry = cache.table.get_entry(id)!; /* MISS */ if (entry.key != id) { - return SearchResult.MISSING?; + return SearchResult.MISSING?; } /* MISS, the data is not valid (not present) */ if (!cache.present[entry.value]) { - // if the data is not present but it is still in the table, remove it + // if the data is not present but it is still in the table, remove it cache.table.remove(id)!; return SearchResult.MISSING?; } @@ -83,8 +83,8 @@ fn Value*! Cache.search(&cache, Key id) /* If there is no free space left then just return the first position */ fn usz Cache.get_free_spot(&cache) @private { - const BITS = $typeof(cache.present.data[0]).sizeof*8; - foreach (idx, d: cache.present.data) { + const BITS = $typeof(cache.present.data[0]).sizeof*8; + foreach (idx, d: cache.present.data) { if (d.clz() != BITS) { return idx*BITS + BITS-d.clz(); } @@ -94,7 +94,7 @@ fn usz Cache.get_free_spot(&cache) @private fn Value*! Cache.insert_at(&cache, Value *g, Key id, usz index) @private { - // TODO: verify index, g and id + // TODO: verify index, g and id Value* spot; /* Set used and present */ @@ -117,17 +117,17 @@ fn Value*! Cache.insert_new(&cache, Value* g, Key id) fn Value*! Cache.get_or_insert(&cache, Value* g, Key id, bool *is_new = null) { - Value*! c = cache.search(id); - if (catch e = c) { - if (e != SearchResult.MISSING) { - return e?; - } else { - // if the element is new (inserted) set the is_new flag - if (is_new) *is_new = true; - return cache.insert_new(g, id); - } - } else { - if (is_new) *is_new = false; - return c; - } + Value*! c = cache.search(id); + if (catch e = c) { + if (e != SearchResult.MISSING) { + return e?; + } else { + // if the element is new (inserted) set the is_new flag + if (is_new) *is_new = true; + return cache.insert_new(g, id); + } + } else { + if (is_new) *is_new = false; + return c; + } } diff --git a/src/main.c3 b/src/main.c3 index cb37cbf..d9bef14 100644 --- a/src/main.c3 +++ b/src/main.c3 @@ -27,7 +27,7 @@ fn int main(String[] args) { ugui::Ctx ui; ui.init()!!; - ui.font.load("/usr/share/fonts/NerdFonts/ttf/HackNerdFont-Regular.ttf", 16, scale: 1.5)!!; + ui.font.load("/usr/share/fonts/TTF/HackNerdFontMono-Regular.ttf", 16)!!; short width = 800; short height = 450; @@ -203,19 +203,19 @@ fn int main(String[] args) /* fn void! test_vtree() @test { - vtree::VTree() vt; - vt.init(10)!!; - defer vt.free(); + vtree::VTree() vt; + vt.init(10)!!; + defer vt.free(); - assert(vt.size() == 10, "Size is incorrect"); + assert(vt.size() == 10, "Size is incorrect"); - isz ref = vt.add("Ciao Mamma", 0)!!; - String s = vt.get(ref)!!; - assert(s == "Ciao Mamma", "String is incorrect"); - isz par = vt.parentof(0)!!; - assert(ref == par, "Not Root"); + isz ref = vt.add("Ciao Mamma", 0)!!; + String s = vt.get(ref)!!; + assert(s == "Ciao Mamma", "String is incorrect"); + isz par = vt.parentof(0)!!; + assert(ref == par, "Not Root"); - vt.print(); + vt.print(); } def StrCache = cache::Cache(); diff --git a/src/ugui_cmd.c3 b/src/ugui_cmd.c3 index d882121..13dfb3e 100644 --- a/src/ugui_cmd.c3 +++ b/src/ugui_cmd.c3 @@ -43,7 +43,7 @@ fn void! Ctx.push_rect(&ctx, Rect rect, Color color, bool do_border = false, boo // TODO: add texture id fn void! Ctx.push_sprite(&ctx, Rect bounds, Rect texture) { - Cmd cmd = { + Cmd cmd = { .type = CMD_SPRITE, .sprite.rect = bounds, .sprite.texture_rect = texture, @@ -56,7 +56,7 @@ fn void! Ctx.push_string(&ctx, Rect bounds, String text) if (text.len == 0) { return; } - + short baseline = (short)ctx.font.ascender; short line_height = (short)ctx.font.ascender - (short)ctx.font.descender; short line_gap = (short)ctx.font.linegap; @@ -74,16 +74,16 @@ fn void! Ctx.push_string(&ctx, Rect bounds, String text) if (!ascii::is_cntrl((char)cp)) { gp = ctx.font.get_glyph(cp)!; Rect gb = { - .x = orig.x + line_len + gp.ox, - .y = orig.y + gp.oy + baseline, - .w = gp.w, - .h = gp.h, + .x = orig.x + line_len + gp.ox, + .y = orig.y + gp.oy + baseline, + .w = gp.w, + .h = gp.h, }; Rect gt = { - .x = gp.u, - .y = gp.v, - .w = gp.w, - .h = gp.h, + .x = gp.u, + .y = gp.v, + .w = gp.w, + .h = gp.h, }; if (rect_collision(gb, bounds)) { ctx.push_sprite(gb, gt)!; diff --git a/src/ugui_data.c3 b/src/ugui_data.c3 index bcfad9c..03b80da 100644 --- a/src/ugui_data.c3 +++ b/src/ugui_data.c3 @@ -71,8 +71,8 @@ struct Div { // slider element struct Slider { - float value; - Rect handle; + float value; + Rect handle; } struct Text { diff --git a/src/ugui_div.c3 b/src/ugui_div.c3 index 833b6ec..657babe 100644 --- a/src/ugui_div.c3 +++ b/src/ugui_div.c3 @@ -12,7 +12,7 @@ fn void! Ctx.div_begin(&ctx, String label, Rect size) ctx.active_div = div_node; // 1. Fill the element fields - c_elem.type = ETYPE_DIV; + c_elem.type = ETYPE_DIV; // do layout and update flags only if the element was updated if (c_elem.flags.is_new || parent.flags.updated) { @@ -31,8 +31,8 @@ fn void! Ctx.div_begin(&ctx, String label, Rect size) c_elem.flags.updated = true; // 4. Fill the div fields c_elem.div.origin_c = Point{ - .x = c_elem.bounds.x, - .y = c_elem.bounds.y, + .x = c_elem.bounds.x, + .y = c_elem.bounds.y, }; c_elem.div.origin_r = c_elem.div.origin_c; c_elem.div.layout = parent.div.layout; diff --git a/src/ugui_impl.c3 b/src/ugui_impl.c3 index be172c1..e49c8d4 100644 --- a/src/ugui_impl.c3 +++ b/src/ugui_impl.c3 @@ -130,12 +130,12 @@ fn void! Ctx.frame_end(&ctx) // draw mouse position $if 1: Cmd cmd = { - .type = CMD_RECT, - .rect.rect = { - .x = ctx.input.mouse.pos.x - 2, - .y = ctx.input.mouse.pos.y - 2, - .w = 4, - .h = 4, + .type = CMD_RECT, + .rect.rect = { + .x = ctx.input.mouse.pos.x - 2, + .y = ctx.input.mouse.pos.y - 2, + .w = 4, + .h = 4, }, .rect.color = uint_to_rgba(0xff00ffff) }; diff --git a/src/ugui_input.c3 b/src/ugui_input.c3 index fba777b..1b49ae5 100644 --- a/src/ugui_input.c3 +++ b/src/ugui_input.c3 @@ -41,20 +41,9 @@ bitstruct MouseButtons : uint { bool btn_5 : 4; } -macro Ctx.mouse_pressed(&ctx) -{ - return ctx.input.mouse.updated & ctx.input.mouse.down; -} - -macro Ctx.mouse_released(&ctx) -{ - return ctx.input.mouse.updated & ~ctx.input.mouse.down; -} - -macro Ctx.mouse_down(&ctx) -{ - return ctx.input.mouse.down; -} +macro Ctx.mouse_pressed(&ctx) => ctx.input.mouse.updated & ctx.input.mouse.down; +macro Ctx.mouse_released(&ctx) => ctx.input.mouse.updated & ~ctx.input.mouse.down; +macro Ctx.mouse_down(&ctx) => ctx.input.mouse.down; const MouseButtons BTN_NONE = (MouseButtons)0u; const MouseButtons BTN_ANY = (MouseButtons)(uint.max); @@ -66,20 +55,9 @@ const MouseButtons BTN_5 = {.btn_5 = true}; // FIXME: hthis compairson could be done with a cast using MouseButtons.inner // property but I could not figure out how -macro Ctx.is_mouse_pressed(&ctx, MouseButtons btn) -{ - return (ctx.mouse_pressed() & btn) != BTN_NONE; -} - -macro Ctx.is_mouse_released(&ctx, MouseButtons btn) -{ - return (ctx.mouse_released() & btn) != BTN_NONE; -} - -macro Ctx.is_mouse_down(&ctx, MouseButtons btn) -{ - return (ctx.mouse_down() & btn) != BTN_NONE; -} +macro Ctx.is_mouse_pressed(&ctx, MouseButtons btn) => (ctx.mouse_pressed() & btn) != BTN_NONE; +macro Ctx.is_mouse_released(&ctx, MouseButtons btn) => (ctx.mouse_released() & btn) != BTN_NONE; +macro Ctx.is_mouse_down(&ctx, MouseButtons btn) => (ctx.mouse_down() & btn) != BTN_NONE; macro ElemEvents Ctx.get_elem_events(&ctx, Elem *elem) { diff --git a/src/ugui_layout.c3 b/src/ugui_layout.c3 index d403051..5b56913 100644 --- a/src/ugui_layout.c3 +++ b/src/ugui_layout.c3 @@ -51,8 +51,8 @@ fn void! Ctx.layout_next_row(&ctx) } parent.div.origin_r = Point{ - .x = parent.bounds.x, - .y = parent.div.origin_c.y, + .x = parent.bounds.x, + .y = parent.div.origin_c.y, }; parent.div.origin_c = parent.div.origin_r; } @@ -68,8 +68,8 @@ fn void! Ctx.layout_next_column(&ctx) } parent.div.origin_c = Point{ - .x = parent.div.origin_r.x, - .y = parent.bounds.y, + .x = parent.div.origin_r.x, + .y = parent.bounds.y, }; parent.div.origin_r = parent.div.origin_c; } @@ -84,7 +84,7 @@ fn void! Ctx.layout_next_column(&ctx) *> fn Rect Ctx.position_element(&ctx, Elem *parent, Rect rect, bool style = false) { - Rect placement; + Rect placement; Point origin; Div* div = &parent.div; @@ -128,12 +128,12 @@ fn Rect Ctx.position_element(&ctx, Elem *parent, Rect rect, bool style = false) // 3. Update the origins of the parent div.origin_r = Point{ - .x = pl_corner.x, - .y = origin.y, + .x = pl_corner.x, + .y = origin.y, }; div.origin_c = Point{ - .x = origin.x, - .y = pl_corner.y, + .x = origin.x, + .y = pl_corner.y, }; // 4. Calculate the "scrolled" view diff --git a/src/ugui_slider.c3 b/src/ugui_slider.c3 index ddabc4e..59ef66b 100644 --- a/src/ugui_slider.c3 +++ b/src/ugui_slider.c3 @@ -33,7 +33,7 @@ fn ElemEvents! Ctx.slider_hor(&ctx, String label, Rect size) c_elem.events = ctx.get_elem_events(c_elem); if (parent.flags.has_focus && c_elem.events.mouse_hover) { - if (point_in_rect(ctx.input.mouse.pos, c_elem.slider.handle) && c_elem.events.mouse_hold) { + if (point_in_rect(ctx.input.mouse.pos, c_elem.slider.handle) && c_elem.events.mouse_hold) { short x = (short)clamp(ctx.input.mouse.pos.x - c_elem.slider.handle.w/2, c_elem.bounds.x, c_elem.bounds.x + c_elem.bounds.w - c_elem.slider.handle.w); float v = (float)(c_elem.slider.handle.x-c_elem.bounds.x) / (float)(c_elem.bounds.w-c_elem.slider.handle.w); c_elem.slider.handle.x = x; @@ -89,7 +89,7 @@ fn ElemEvents! Ctx.slider_ver(&ctx, String label, Rect size) c_elem.events = ctx.get_elem_events(c_elem); if (parent.flags.has_focus && c_elem.events.mouse_hover) { - if (point_in_rect(ctx.input.mouse.pos, c_elem.slider.handle) && c_elem.events.mouse_hold) { + if (point_in_rect(ctx.input.mouse.pos, c_elem.slider.handle) && c_elem.events.mouse_hold) { short y = (short)clamp(ctx.input.mouse.pos.y - c_elem.slider.handle.h/2, c_elem.bounds.y, c_elem.bounds.y + c_elem.bounds.h - c_elem.slider.handle.h); float v = (float)(c_elem.slider.handle.y-c_elem.bounds.y) / (float)(c_elem.bounds.h-c_elem.slider.handle.h); c_elem.slider.handle.y = y; diff --git a/src/ugui_text.c3 b/src/ugui_text.c3 index cd5ba97..e39c82a 100644 --- a/src/ugui_text.c3 +++ b/src/ugui_text.c3 @@ -62,7 +62,7 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text) // 1. Fill the element fields // this resets the flags - c_elem.type = ETYPE_TEXT; + c_elem.type = ETYPE_TEXT; short baseline = (short)ctx.font.ascender; short line_height = (short)ctx.font.ascender - (short)ctx.font.descender; diff --git a/src/vtree.c3 b/src/vtree.c3 index 8e5d7b1..42b5d19 100644 --- a/src/vtree.c3 +++ b/src/vtree.c3 @@ -10,11 +10,11 @@ struct VTree { } fault VTreeError { - CANNOT_SHRINK, - INVALID_REFERENCE, - TREE_FULL, - REFERENCE_NOT_PRESENT, - INVALID_ARGUMENT, + CANNOT_SHRINK, + INVALID_REFERENCE, + TREE_FULL, + REFERENCE_NOT_PRESENT, + INVALID_ARGUMENT, } macro VTree.ref_is_valid(&tree, isz ref) { return (ref >= 0 && ref < tree.refs.len); } @@ -116,8 +116,8 @@ fn void! VTree.resize(&tree, usz newsize) defer catch { (void)mem::free(tree.ordered_refs); } if (newsize > tree.size()) { - tree.vector[old_size..newsize-1] = @zero(); - tree.refs[old_size..newsize-1] = -1; + tree.vector[old_size..newsize-1] = @zero(); + tree.refs[old_size..newsize-1] = -1; } } @@ -131,13 +131,13 @@ fn isz! VTree.add(&tree, ElemType elem, isz parent) // no space left if (tree.elements >= tree.size()) { - return VTreeError.TREE_FULL?; + return VTreeError.TREE_FULL?; } // check if the parent exists // if there are no elements in the tree the first add will set the root if (!tree.ref_is_present(parent) && tree.elements != 0) { - return VTreeError.REFERENCE_NOT_PRESENT?; + return VTreeError.REFERENCE_NOT_PRESENT?; } // get the first free spot @@ -164,7 +164,7 @@ fn isz! VTree.add(&tree, ElemType elem, isz parent) // returns the number of pruned elements fn usz! VTree.prune(&tree, isz ref) { - if (!tree.ref_is_valid(ref)) { + if (!tree.ref_is_valid(ref)) { return VTreeError.INVALID_REFERENCE?; } @@ -189,7 +189,7 @@ fn usz! VTree.prune(&tree, isz ref) // find the size of the subtree starting from ref fn usz! VTree.subtree_size(&tree, isz ref) { - if (!tree.ref_is_valid(ref)) { + if (!tree.ref_is_valid(ref)) { return VTreeError.INVALID_REFERENCE?; } @@ -250,7 +250,7 @@ fn isz! VTree.children_it(&tree, isz parent, isz *cursor) fn isz! VTree.level_order_it(&tree, isz ref, isz *cursor) { if (cursor == null) { - return VTreeError.INVALID_ARGUMENT?; + return VTreeError.INVALID_ARGUMENT?; } isz[] queue = tree.ordered_refs; @@ -301,7 +301,7 @@ fn isz! VTree.parentof(&tree, isz ref) } if (!tree.ref_is_present(ref)) { - return VTreeError.REFERENCE_NOT_PRESENT?; + return VTreeError.REFERENCE_NOT_PRESENT?; } return tree.refs[ref]; @@ -309,12 +309,12 @@ fn isz! VTree.parentof(&tree, isz ref) fn ElemType! VTree.get(&tree, isz ref) { - if (!tree.ref_is_valid(ref)) { + if (!tree.ref_is_valid(ref)) { return VTreeError.INVALID_REFERENCE?; } if (!tree.ref_is_present(ref)) { - return VTreeError.REFERENCE_NOT_PRESENT?; + return VTreeError.REFERENCE_NOT_PRESENT?; } return tree.vector[ref];