Compare commits

...

6 Commits

13 changed files with 159 additions and 87 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.o *.o
*.a
build/* build/*
**/.ccls-cache **/.ccls-cache

6
TODO
View File

@ -6,6 +6,8 @@
[ ] Write a README.md [ ] Write a README.md
[ ] Use an arena allocator for cache [ ] Use an arena allocator for cache
[ ] Do not redraw if there was no update (no layout and no draw) [ ] Do not redraw if there was no update (no layout and no draw)
[ ] Better handling of the active and focused widgets, try
to maintain focus until mouse release (fix scroll bars)
## Commands ## Commands
[x] rect commads should have: [x] rect commads should have:
@ -19,11 +21,11 @@
## Fonts ## Fonts
[ ] Fix the missing alpha channel [ ] Fix the missing alpha channel
[ ] Fix the allignment [x] Fix the alignment
## Raylib ## Raylib
[ ] Implement type (Rect, Color, Point) conversion functions between rl:: and ugui:: [ ] Implement type (Rect, Color, Point) conversion functions between rl:: and ugui::
[ ] Implement pixel radius rounding for border radius [x] Implement pixel radius rounding for border radius
## Widgets ## Widgets
[ ] Dynamic text box to implement an fps counter [ ] Dynamic text box to implement an fps counter

View File

@ -0,0 +1,3 @@
all:
make -C thirdparty/libgrapheme
cp thirdparty/libgrapheme/libgrapheme.a linux-x64/libgrapheme.a

View File

@ -0,0 +1,46 @@
module grapheme;
const uint GRAPHEME_INVALID_CODEPOINT UINT32_C = 0xFFFD;
enum BidirectionalDirection {
GRAPHEME_BIDIRECTIONAL_DIRECTION_NEUTRAL,
GRAPHEME_BIDIRECTIONAL_DIRECTION_LTR,
GRAPHEME_BIDIRECTIONAL_DIRECTION_RTL,
}
fn isz bidirectional_get_line_embedding_levels(uint *, isz, ichar *, isz) @extern("grapheme_bidirectional_get_line_embedding_levels");
fn isz bidirectional_preprocess_paragraph(uint *, isz, BidirectionalDirection, uint *, isz, BidirectionalDirection *) @extern("grapheme_bidirectional_preprocess_paragraph");
fn isz bidirectional_reorder_line(uint *, uint *, isz, uint *, isz) @extern("grapheme_bidirectional_reorder_line");
fn isz decode_utf8(char *, isz, uint *) @extern("grapheme_decode_utf8");
fn isz encode_utf8(uint, char *, isz) @extern("grapheme_encode_utf8");
fn bool is_character_break(uint, uint, ushort *) @extern("grapheme_is_character_break");
fn bool is_lowercase(uint *, isz, isz *) @extern("grapheme_is_lowercase");
fn bool is_titlecase(uint *, isz, isz *) @extern("grapheme_is_titlecase");
fn bool is_uppercase(uint *, isz, isz *) @extern("grapheme_is_uppercase");
fn bool is_lowercase_utf8(char *, isz, isz *) @extern("grapheme_is_lowercase_utf8");
fn bool is_titlecase_utf8(char *, isz, isz *) @extern("grapheme_is_titlecase_utf8");
fn bool is_uppercase_utf8(char *, isz, isz *) @extern("grapheme_is_uppercase_utf8");
fn isz next_character_break(uint *, isz) @extern("grapheme_next_character_break");
fn isz next_line_break(uint *, isz) @extern("grapheme_next_line_break");
fn isz next_sentence_break(uint *, isz) @extern("grapheme_next_sentence_break");
fn isz next_word_break(uint *, isz) @extern("grapheme_next_word_break");
fn isz next_character_break_utf8(char *, isz) @extern("grapheme_next_character_break_utf8");
fn isz next_line_break_utf8(char *, isz) @extern("grapheme_next_line_break_utf8");
fn isz next_sentence_break_utf8(char *, isz) @extern("grapheme_next_sentence_break_utf8");
fn isz next_word_break_utf8(char *, isz) @extern("grapheme_next_word_break_utf8");
fn isz to_lowercase(uint *, isz, uint *, isz) @extern("grapheme_to_lowercase");
fn isz to_titlecase(uint *, isz, uint *, isz) @extern("grapheme_to_titlecase");
fn isz to_uppercase(uint *, isz, uint *, isz) @extern("grapheme_to_uppercase");
fn isz to_lowercase_utf8(char *, isz, char *, isz) @extern("grapheme_to_lowercase_utf8");
fn isz to_titlecase_utf8(char *, isz, char *, isz) @extern("grapheme_to_titlecase_utf8");
fn isz to_uppercase_utf8(char *, isz, char *, isz) @extern("grapheme_to_uppercase_utf8");

View File

@ -0,0 +1,9 @@
{
"provides": "grapheme",
"targets": {
"linux-x64": {
"dependencies": [],
"linked-libraries": ["grapheme", "c"]
}
}
}

View File

@ -0,0 +1,44 @@
{
// Language version of C3.
"langrev": "1",
// Warnings used for all targets.
"warnings": ["no-unused"],
// Directories where C3 library files may be found.
"dependency-search-paths": [".."],
// Libraries to use for all targets.
"dependencies": ["grapheme"],
// Authors, optionally with email.
"authors": ["Alessandro Mauri <alemauri001@gmail.com"],
// Version using semantic versioning.
"version": "0.1.0",
// Sources compiled for all targets.
"sources": [],
// C sources if the project also compiles C sources
// relative to the project file.
// "c-sources": [ "csource/**" ],
// Output location, relative to project file.
"output": "build",
// Architecture and OS target.
// You can use 'c3c --list-targets' to list all valid targets.
// "target": "windows-x64",
"features": [
// See rcore.c3
//"SUPPORT_INTERNAL_MEMORY_MANAGEMENT",
//"SUPPORT_STANDARD_FILEIO",
//"SUPPORT_FILE_SYSTEM_FUNCTIONS",
//"SUPPORT_DATA_ENCODER",
// See text.c3
//"SUPPORT_TEXT_CODEPOINTS_MANAGEMENT",
//"SUPPORT_TEXT_C_STRING_MANAGEMENT",
//"SUPPORT_RANDOM_GENERATION",
//"SUPPORT_RAYGUI",
//"RAYGUI_NO_ICONS",
//"RAYGUI_CUSTOM_ICONS",
],
// Global settings.
// CPU name, used for optimizations in the LLVM backend.
"cpu": "generic",
// Optimization: "O0", "O1", "O2", "O3", "O4", "O5", "Os", "Oz".
"opt": "O0"
// See resources/examples/project_all_settings.json and 'c3c --list-project-properties' to see more properties.
}

@ -0,0 +1 @@
Subproject commit 65b354f0fcb1d925f4340dbb4415ea06e8af2bec

View File

@ -27,7 +27,7 @@ fn int main(String[] args)
{ {
ugui::Ctx ui; ugui::Ctx ui;
ui.init()!!; ui.init()!!;
ui.font.load("/usr/share/fonts/TTF/FreeSans.ttf", 16, scale: 1.5)!!; ui.font.load("/usr/share/fonts/TTF/Hack-Regular.ttf", 16, scale: 1.5)!!;
short width = 800; short width = 800;
short height = 450; short height = 450;
@ -46,7 +46,7 @@ fn int main(String[] args)
// Main loop // Main loop
while (!rl::window_should_close()) { while (!rl::window_should_close()) {
clock.mark(); clock.mark();
/* Start Input Handling */ /* Start Input Handling */
if (rl::is_window_resized()) { if (rl::is_window_resized()) {
width = (short)rl::get_screen_width(); width = (short)rl::get_screen_width();
@ -94,7 +94,7 @@ fn int main(String[] args)
io::printfn("slider: %f", e.slider.value); io::printfn("slider: %f", e.slider.value);
} }
ui.text_unbounded("text1", "Ciao Mamma\n Sono a Casa")!!; ui.text_unbounded("text1", "Ciao Mamma\nSono a Casa")!!;
|}; |};
ui.div_end()!!; ui.div_end()!!;
@ -146,9 +146,10 @@ fn int main(String[] args)
.height = cmd.rect.rect.h, .height = cmd.rect.rect.h,
.width = cmd.rect.rect.w, .width = cmd.rect.rect.w,
}; };
// TODO: find a way to do real pixel-perfec rounding float rad = cmd.rect.radius;
float round = cmd.rect.radius ? 0.2 : 0; // for some weird-ass reason the straight forward inverse formula does not work
rl::draw_rectangle_rounded(r, round, 2, c); float roundness = r.width > r.height ? (2.1*rad)/r.height : (2.1*rad)/r.width;
rl::draw_rectangle_rounded(r, roundness, 0, c);
case ugui::CmdType.CMD_UPDATE_ATLAS: case ugui::CmdType.CMD_UPDATE_ATLAS:
rl::unload_image(font_atlas); rl::unload_image(font_atlas);
font_atlas.data = cmd.update_atlas.raw_buffer; font_atlas.data = cmd.update_atlas.raw_buffer;
@ -173,7 +174,7 @@ fn int main(String[] args)
.x = cmd.sprite.rect.x, .x = cmd.sprite.rect.x,
.y = cmd.sprite.rect.y, .y = cmd.sprite.rect.y,
}; };
rl::draw_texture_rec(font_texture, source, position, rl::WHITE); rl::draw_texture_rec(font_texture, source, position, rl::WHITE);
//rl::draw_rectangle(cmd.sprite.rect.x, //rl::draw_rectangle(cmd.sprite.rect.x,
// cmd.sprite.rect.y, // cmd.sprite.rect.y,

View File

@ -4,6 +4,11 @@ module ugui;
// "rect" is the bounding box of the element, which includes the border and the padding (so not just the content) // "rect" is the bounding box of the element, which includes the border and the padding (so not just the content)
fn void! Ctx.push_rect(&ctx, Rect rect, Color color, bool do_border = false, bool do_padding = false, bool do_radius = false) fn void! Ctx.push_rect(&ctx, Rect rect, Color color, bool do_border = false, bool do_padding = false, bool do_radius = false)
{ {
// FIXME: this should be culled higher up, maybe
if (rect.w <= 0 || rect.h <= 0) {
return;
}
Rect border = ctx.style.border; Rect border = ctx.style.border;
Rect padding = ctx.style.padding; Rect padding = ctx.style.padding;
ushort radius = ctx.style.radius; ushort radius = ctx.style.radius;
@ -18,7 +23,7 @@ fn void! Ctx.push_rect(&ctx, Rect rect, Color color, bool do_border = false, boo
}; };
ctx.cmd_queue.enqueue(&cmd)!; ctx.cmd_queue.enqueue(&cmd)!;
} }
Cmd cmd = { Cmd cmd = {
.type = CMD_RECT, .type = CMD_RECT,
.rect.rect = { .rect.rect = {
@ -32,3 +37,14 @@ fn void! Ctx.push_rect(&ctx, Rect rect, Color color, bool do_border = false, boo
}; };
ctx.cmd_queue.enqueue(&cmd)!; ctx.cmd_queue.enqueue(&cmd)!;
} }
// TODO: add texture id
fn void! Ctx.push_sprite(&ctx, Rect bounds, Rect texture)
{
Cmd cmd = {
.type = CMD_SPRITE,
.sprite.rect = bounds,
.sprite.texture_rect = texture,
};
ctx.cmd_queue.enqueue(&cmd)!;
}

View File

@ -39,14 +39,7 @@ fn void! Ctx.div_begin(&ctx, String label, Rect size)
} }
// Add the background to the draw stack // Add the background to the draw stack
Cmd cmd = { ctx.push_rect(c_elem.bounds, c_elem.div.color_bg)!;
.type = CMD_RECT,
.rect = {
.rect = c_elem.bounds,
.color = c_elem.div.color_bg,
},
};
ctx.cmd_queue.enqueue(&cmd)!;
// TODO: check active // TODO: check active
// TODO: check resizeable // TODO: check resizeable
@ -95,17 +88,8 @@ fn void! Ctx.div_begin(&ctx, String label, Rect size)
c_elem.div.origin_r = c_elem.div.origin_c; c_elem.div.origin_r = c_elem.div.origin_c;
} }
Cmd scrl = { ctx.push_rect(vslider, uint_to_rgba(0x999999ff))!;
.type = CMD_RECT, ctx.push_rect(vhandle, uint_to_rgba(0x9999ffff))!;
.rect.rect = vslider,
.rect.color = uint_to_rgba(0x999999ff),
};
ctx.cmd_queue.enqueue(&scrl)!;
scrl.rect.rect = vhandle;
scrl.rect.color = uint_to_rgba(0x9999ffff);
ctx.cmd_queue.enqueue(&scrl)!;
} else { } else {
c_elem.div.scroll.on_y = false; c_elem.div.scroll.on_y = false;
} }

View File

@ -56,7 +56,7 @@ fn void! Ctx.init(&ctx)
ctx.style.margin = Rect{2, 2, 2, 2}; ctx.style.margin = Rect{2, 2, 2, 2};
ctx.style.border = Rect{2, 2, 2, 2}; ctx.style.border = Rect{2, 2, 2, 2};
ctx.style.padding = Rect{1, 1, 1, 1}; ctx.style.padding = Rect{1, 1, 1, 1};
ctx.style.radius = 4; ctx.style.radius = 5;
ctx.style.bgcolor = uint_to_rgba(0x282828ff); ctx.style.bgcolor = uint_to_rgba(0x282828ff);
ctx.style.fgcolor = uint_to_rgba(0xfbf1c7ff); ctx.style.fgcolor = uint_to_rgba(0xfbf1c7ff);
ctx.style.brcolor = uint_to_rgba(0xd79921ff); ctx.style.brcolor = uint_to_rgba(0xd79921ff);

View File

@ -42,27 +42,11 @@ fn ElemEvents! Ctx.slider_hor(&ctx, String label, Rect size)
} }
} }
// Draw the button // Draw the slider background and handle
Color bg_color = uint_to_rgba(0x0000ffff); Color bg_color = uint_to_rgba(0x0000ffff);
Color handle_color = uint_to_rgba(0x0ff000ff); Color handle_color = uint_to_rgba(0x0ff000ff);
ctx.push_rect(c_elem.bounds, bg_color)!;
Cmd cmd = { ctx.push_rect(c_elem.slider.handle, handle_color)!;
.type = CMD_RECT,
.rect = {
.rect = c_elem.bounds,
.color = bg_color,
},
};
ctx.cmd_queue.enqueue(&cmd)!;
cmd = Cmd{
.type = CMD_RECT,
.rect = {
.rect = c_elem.slider.handle,
.color = handle_color,
},
};
ctx.cmd_queue.enqueue(&cmd)!;
return c_elem.events; return c_elem.events;
} }
@ -114,27 +98,11 @@ fn ElemEvents! Ctx.slider_ver(&ctx, String label, Rect size)
} }
} }
// Draw the button // Draw the slider background and handle
Color bg_color = uint_to_rgba(0x0000ffff); Color bg_color = uint_to_rgba(0x0000ffff);
Color handle_color = uint_to_rgba(0x0ff000ff); Color handle_color = uint_to_rgba(0x0ff000ff);
ctx.push_rect(c_elem.bounds, bg_color)!;
Cmd cmd = { ctx.push_rect(c_elem.slider.handle, handle_color)!;
.type = CMD_RECT,
.rect = {
.rect = c_elem.bounds,
.color = bg_color,
},
};
ctx.cmd_queue.enqueue(&cmd)!;
cmd = Cmd{
.type = CMD_RECT,
.rect = {
.rect = c_elem.slider.handle,
.color = handle_color,
},
};
ctx.cmd_queue.enqueue(&cmd)!;
return c_elem.events; return c_elem.events;
} }

View File

@ -21,11 +21,11 @@ fn Rect! Ctx.get_text_bounds(&ctx, String text, bool* update_atlas)
if (n) { *update_atlas = true; } if (n) { *update_atlas = true; }
} else { } else {
text_bounds.h += line_height + line_gap; text_bounds.h += line_height + line_gap;
if (line_len > text_bounds.w) {
text_bounds.w = line_len;
}
line_len = 0; line_len = 0;
} }
if (line_len > text_bounds.w) {
text_bounds.w = line_len;
}
} }
return text_bounds; return text_bounds;
@ -78,7 +78,7 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text)
.rect.color = uint_to_rgba(0x000000ff), .rect.color = uint_to_rgba(0x000000ff),
}; };
ctx.cmd_queue.enqueue(&bounds)!; ctx.cmd_queue.enqueue(&bounds)!;
Point orig = { Point orig = {
.x = c_elem.bounds.x, .x = c_elem.bounds.x,
.y = c_elem.bounds.y, .y = c_elem.bounds.y,
@ -89,23 +89,20 @@ fn void! Ctx.text_unbounded(&ctx, String label, String text)
Codepoint cp = (Codepoint)c; Codepoint cp = (Codepoint)c;
if (cp != '\n') { if (cp != '\n') {
gp = ctx.font.get_glyph(cp)!; gp = ctx.font.get_glyph(cp)!;
Cmd cmd = { Rect gb = {
.type = CMD_SPRITE, .x = orig.x + line_len + gp.ox,
.sprite.rect = { .y = orig.y + gp.oy + baseline,
.x = orig.x + line_len + gp.ox, .w = gp.w,
.y = orig.y + gp.oy + baseline, .h = gp.h,
.w = gp.w,
.h = gp.h,
},
.sprite.texture_rect = {
.x = gp.u,
.y = gp.v,
.w = gp.w,
.h = gp.h,
},
}; };
Rect gt = {
.x = gp.u,
.y = gp.v,
.w = gp.w,
.h = gp.h,
};
ctx.push_sprite(gb, gt)!;
line_len += gp.adv; line_len += gp.adv;
ctx.cmd_queue.enqueue(&cmd)!;
} else { } else {
orig.y += line_height + line_gap; orig.y += line_height + line_gap;
line_len = 0; line_len = 0;