ugui.c3l/src/main.c3

378 lines
10 KiB
Plaintext
Raw Normal View History

2024-10-29 22:45:47 +01:00
import std::io;
import vtree;
import cache;
import ugui;
2025-01-30 19:38:53 +01:00
import raylib5::rl;
2024-12-01 00:25:43 +01:00
import std::time;
import std::collections::ringbuffer;
2024-12-16 17:06:46 +01:00
import std::core::string;
2024-12-01 00:25:43 +01:00
2025-05-05 16:23:26 +02:00
alias Times = ringbuffer::RingBuffer{time::NanoDuration[128]};
2024-12-01 00:25:43 +01:00
fn void Times.print_stats(&times)
{
time::NanoDuration min, max, avg, x;
min = times.get(0);
for (usz i = 0; i < times.written; i++) {
x = times.get(i);
if (x < min) { min = x; }
if (x > max) { max = x; }
avg += x;
}
avg = (NanoDuration)((ulong)avg/128.0);
io::printfn("min=%s, max=%s, avg=%s", min, max, avg);
}
2024-10-29 22:45:47 +01:00
2024-12-16 17:06:46 +01:00
struct TimeStats {
time::NanoDuration min, max, avg;
}
fn TimeStats Times.get_stats(&times)
{
time::NanoDuration min, max, avg, x;
min = times.get(0);
for (usz i = 0; i < times.written; i++) {
x = times.get(i);
if (x < min) { min = x; }
if (x > max) { max = x; }
avg += x;
}
avg = (NanoDuration)((ulong)avg/128.0);
2025-05-05 16:23:26 +02:00
return {.min = min, .max = max, .avg = avg};
2024-12-16 17:06:46 +01:00
}
2025-02-04 22:40:44 +01:00
const ZString MSDF_FS = `
#version 330
in vec2 fragTexCoord;
out vec4 fragColor;
uniform sampler2D texture0;
uniform vec4 colDiffuse;
const float pxRange = 4.0f;
float screenPxRange() {
vec2 unitRange = vec2(pxRange)/vec2(textureSize(texture0, 0));
vec2 screenTexSize = vec2(1.0)/fwidth(fragTexCoord);
return max(0.5*dot(unitRange, screenTexSize), 1.0);
}
float median(float r, float g, float b) {
return max(min(r, g), min(max(r, g), b));
}
void main() {
vec3 msd = texture(texture0, fragTexCoord).rgb;
float sd = median(msd.r, msd.g, msd.b);
float screenPxDistance = screenPxRange()*(sd - 0.5);
float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0);
fragColor = colDiffuse * opacity;
}
`;
2024-12-16 13:54:53 +01:00
const ZString FONT_FS = `
#version 330
// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// Output fragment color
out vec4 finalColor;
void main()
{
vec4 alpha = texture(texture0, fragTexCoord);
finalColor = colDiffuse*fragColor;
finalColor.a *= alpha.r;
}
`;
2025-02-06 23:44:03 +01:00
// SDF based rect rendering
const ZString RECT_FS = `
#version 330
in vec2 fragTexCoord;
in vec4 fragColor;
out vec4 finalColor;
uniform float radius;
uniform float border;
uniform vec2 size;
void main()
{
}
`;
macro rl::Color ugui::Color.conv(color)
{
2025-05-05 16:23:26 +02:00
return {.r = color.r, .g = color.g, .b = color.b, .a = color.a};
}
2025-02-06 23:44:03 +01:00
macro rl::Rectangle Rect.conv(rect)
{
2025-05-05 16:23:26 +02:00
return {.x = rect.x, .y = rect.y, .width = rect.w, .height = rect.h};
}
2024-10-29 22:45:47 +01:00
fn int main(String[] args)
{
2024-11-17 21:36:46 +01:00
ugui::Ctx ui;
ui.init()!!;
2025-01-30 18:36:47 +01:00
defer ui.free();
2024-12-17 11:26:59 +01:00
ui.load_font("font1", "resources/hack-nerd.ttf", 16)!!;
2025-02-06 23:51:15 +01:00
ui.sprite_atlas_create("icons", AtlasType.ATLAS_R8G8B8A8, 512, 512)!!;
ui.import_sprite_file_qoi("tux", "resources/tux.qoi")!!;
2025-02-04 22:40:44 +01:00
ui.import_sprite_file_qoi("tick", "resources/tick_sdf.qoi", SpriteType.SPRITE_MSDF)!!;
2024-10-29 22:45:47 +01:00
short width = 800;
short height = 450;
2025-01-30 19:38:53 +01:00
rl::setConfigFlags(rl::FLAG_WINDOW_RESIZABLE);
rl::initWindow(width, height, "Ugui Test");
2024-11-17 21:36:46 +01:00
ui.input_window_size(width, height)!!;
2025-01-30 19:38:53 +01:00
rl::setTargetFPS(60);
rl::enableEventWaiting();
2024-10-29 22:45:47 +01:00
isz frame;
2024-12-01 00:25:43 +01:00
bool toggle = true;
time::Clock clock;
Times ui_times;
Times draw_times;
// font stuff
2025-01-30 19:38:53 +01:00
rl::Shader font_shader = rl::loadShaderFromMemory(null, FONT_FS);
2024-12-16 17:06:46 +01:00
rl::Image font_atlas;
rl::Texture2D font_texture;
ugui::Id font_id = ui.get_font_id("font1");
2025-02-04 22:40:44 +01:00
// sprite stuff
rl::Shader msdf_shader = rl::loadShaderFromMemory(null, MSDF_FS);
rl::Image sprite_atlas;
rl::Texture2D sprite_texture;
ugui::Id sprite_id = ui.get_sprite_atlas_id("icons");
2024-10-29 22:45:47 +01:00
// Main loop
2025-01-30 19:38:53 +01:00
while (!rl::windowShouldClose()) {
2024-12-01 00:25:43 +01:00
clock.mark();
2024-12-13 14:42:15 +01:00
2025-01-30 18:36:47 +01:00
ugui::ModKeys mod;
2025-01-30 19:38:53 +01:00
mod.rctrl = rl::isKeyDown(rl::KEY_RIGHT_CONTROL);
mod.lctrl = rl::isKeyDown(rl::KEY_LEFT_CONTROL);
2025-01-30 18:36:47 +01:00
ui.input_mod_keys(mod);
2025-05-05 16:23:26 +02:00
/*
2025-01-30 19:38:53 +01:00
for (rl::KeyboardKey key; (key = (KeyboardKey)rl::getKeyPressed()) != 0;) {
ZString kname = rl::getKeyName(key);
if (kname == null) continue;
ui.input_text_unicode(kname.str_view());
2024-12-25 12:30:35 +01:00
}
2025-05-05 16:23:26 +02:00
*/
2025-01-30 19:38:53 +01:00
2024-10-31 00:04:49 +01:00
/* Start Input Handling */
2025-01-30 19:38:53 +01:00
if (rl::isWindowResized()) {
width = (short)rl::getScreenWidth();
height = (short)rl::getScreenHeight();
2024-11-17 21:36:46 +01:00
ui.input_window_size(width, height)!!;
2024-10-29 22:45:47 +01:00
}
2025-01-30 19:38:53 +01:00
ui.input_changefocus(rl::isWindowFocused());
2024-10-29 22:45:47 +01:00
2025-01-30 19:38:53 +01:00
rl::Vector2 mpos = rl::getMousePosition();
2024-11-17 21:36:46 +01:00
ui.input_mouse_abs((short)mpos.x, (short)mpos.y);
2025-01-30 19:38:53 +01:00
rl::Vector2 mwheel = rl::getMouseWheelMoveV();
2024-12-25 12:30:35 +01:00
ui.input_mouse_wheel((short)mwheel.x, (short)mwheel.y);
ugui::MouseButtons buttons = {
2025-01-30 19:38:53 +01:00
.btn_left = rl::isMouseButtonDown(rl::MouseButton.LEFT),
.btn_right = rl::isMouseButtonDown(rl::MouseButton.RIGHT),
.btn_middle = rl::isMouseButtonDown(rl::MouseButton.MIDDLE),
2024-12-25 12:30:35 +01:00
};
2024-11-17 21:36:46 +01:00
ui.input_mouse_button(buttons);
2024-10-31 00:04:49 +01:00
/* End Input Handling */
2024-10-29 22:45:47 +01:00
2024-10-31 00:04:49 +01:00
/* Start UI Handling */
2024-11-17 21:36:46 +01:00
ui.frame_begin()!!;
2024-10-29 22:45:47 +01:00
2025-01-30 18:36:47 +01:00
if (ui.check_key_combo(ugui::KMOD_CTRL, "q")) break;
2025-05-05 16:23:26 +02:00
ui.div_begin("main", {.w=-100})!!;
{
2024-12-20 01:45:10 +01:00
ui.layout_set_column()!!;
2025-05-05 16:23:26 +02:00
if (ui.button("button0", {0,0,30,30}, toggle)!!.mouse_press) {
2024-10-31 00:04:49 +01:00
io::printn("press button0");
2024-12-01 00:25:43 +01:00
toggle = !toggle;
2024-10-29 22:45:47 +01:00
}
2024-11-17 21:36:46 +01:00
//ui.layout_next_column()!!;
2025-05-05 16:23:26 +02:00
if (ui.button("button1", {0,0,30,30})!!.mouse_press) {
2024-10-31 00:04:49 +01:00
io::printn("press button1");
}
2024-11-17 21:36:46 +01:00
//ui.layout_next_column()!!;
2025-05-05 16:23:26 +02:00
if (ui.button("button2", {0,0,30,30})!!.mouse_release) {
2024-12-19 15:24:39 +01:00
io::printn("release button2");
2024-10-31 00:04:49 +01:00
}
2024-12-20 19:50:58 +01:00
ui.layout_set_row()!!;
ui.layout_next_row()!!;
static float rf, gf, bf, af;
2025-05-05 16:23:26 +02:00
ui.slider_ver("slider_r", {0,0,30,100}, &rf)!!;
ui.slider_ver("slider_g", {0,0,30,100}, &gf)!!;
ui.slider_ver("slider_b", {0,0,30,100}, &bf)!!;
ui.slider_ver("slider_a", {0,0,30,100}, &af)!!;
2024-12-20 19:50:58 +01:00
ui.layout_next_column()!!;
2025-01-30 18:36:47 +01:00
ui.text_unbounded("text1", "Ciao Mamma\nAbilità ⚡\n'\udb80\udd2c'")!!;
2024-12-20 19:50:58 +01:00
ui.layout_next_column()!!;
ui.button_label("Continua!")!!;
2025-01-31 12:20:19 +01:00
ui.layout_next_row()!!;
static bool check;
2025-05-05 16:23:26 +02:00
ui.checkbox("check1", "", {}, &check, "tick")!!;
ui.toggle("toggle1", "", {}, &toggle)!!;
/*
ui.layout_set_column()!!;
ui.button_label(" A ")!!;
ui.button_label(" B ")!!;
ui.layout_next_column()!!;
ui.button_label(" C ")!!;
ui.button_label(" D ")!!;
ui.layout_next_row()!!;
ui.button_label(" E ")!!;
*/
2025-05-05 16:23:26 +02:00
};
2025-01-30 18:36:47 +01:00
ui.draw_sprite("sprite1", "tux")!!;
2024-11-17 21:36:46 +01:00
ui.div_end()!!;
2024-12-01 00:25:43 +01:00
2024-12-18 14:58:40 +01:00
ui.div_begin("second", ugui::DIV_FILL, scroll_x: true, scroll_y: true)!!;
2025-05-05 16:23:26 +02:00
{
2024-11-17 21:36:46 +01:00
ui.layout_set_column()!!;
2024-12-18 14:58:40 +01:00
static float slider2 = 0.5;
2025-05-05 16:23:26 +02:00
if (ui.slider_ver("slider", {0,0,30,100}, &slider2)!!.update) {
2024-12-18 14:58:40 +01:00
io::printfn("other slider: %f", slider2);
2024-11-07 18:35:20 +01:00
}
2025-05-05 16:23:26 +02:00
ui.button("button0", {0,0,50,50})!!;
ui.button("button1", {0,0,50,50})!!;
ui.button("button2", {0,0,50,50})!!;
ui.button("button3", {0,0,50,50})!!;
2024-12-17 11:26:59 +01:00
if (toggle) {
2025-05-05 16:23:26 +02:00
ui.button("button4", {0,0,50,50})!!;
ui.button("button5", {0,0,50,50})!!;
ui.button("button6", {0,0,50,50})!!;
ui.button("button7", {0,0,50,50})!!;
2024-12-17 11:26:59 +01:00
}
2024-12-18 14:58:40 +01:00
ui.layout_next_column()!!;
ui.layout_set_row()!!;
static float f1, f2;
2025-05-05 16:23:26 +02:00
ui.slider_hor("hs1", {0,0,100,30}, &f1)!!;
ui.slider_hor("hs2", {0,0,100,30}, &f2)!!;
};
2024-11-17 21:36:46 +01:00
ui.div_end()!!;
2024-12-16 17:06:46 +01:00
// Timings counter
TimeStats dts = draw_times.get_stats();
TimeStats uts = ui_times.get_stats();
ui.layout_set_floating()!!;
2025-05-05 16:23:26 +02:00
ui.div_begin("fps", {0, ui.height-100, -300, 100})!!;
{
2024-12-25 12:30:35 +01:00
ui.layout_set_column()!!;
ui.text_unbounded("draw times", string::tformat("ui avg: %s\ndraw avg: %s\nTOT: %s", uts.avg, dts.avg, uts.avg+dts.avg))!!;
ui.text_unbounded("ui text input", (String)ui.input.keyboard.text[..])!!;
2025-05-05 16:23:26 +02:00
};
2024-12-16 17:06:46 +01:00
ui.div_end()!!;
2024-11-17 21:36:46 +01:00
ui.frame_end()!!;
2024-10-31 00:04:49 +01:00
/* End UI Handling */
2024-12-01 00:25:43 +01:00
ui_times.push(clock.mark());
2024-12-17 11:26:59 +01:00
//ui_times.print_stats();
2024-10-29 22:45:47 +01:00
2024-10-31 00:04:49 +01:00
/* Start UI Drawing */
2025-01-30 19:38:53 +01:00
rl::beginDrawing();
2024-10-29 22:45:47 +01:00
// ClearBackground(BLACK);
2024-11-17 21:36:46 +01:00
for (Cmd* cmd; (cmd = ui.cmd_queue.dequeue() ?? null) != null;) {
2024-10-29 22:45:47 +01:00
switch (cmd.type) {
2025-02-06 23:44:03 +01:00
case CmdType.CMD_RECT:
Rect r = cmd.rect.rect;
2024-12-16 17:06:46 +01:00
float rad = cmd.rect.radius;
2024-12-13 14:42:15 +01:00
// for some weird-ass reason the straight forward inverse formula does not work
float roundness = r.w > r.h ? (2.1f*rad)/(float)r.h : (2.1f*rad)/(float)r.w;
2025-01-30 19:38:53 +01:00
rl::drawRectangleRounded(cmd.rect.rect.conv(), roundness, 0, cmd.rect.color.conv());
2025-02-06 23:44:03 +01:00
case CmdType.CMD_UPDATE_ATLAS:
if (cmd.update_atlas.id == font_id) {
//rl::unload_image(font_atlas);
font_atlas.data = cmd.update_atlas.raw_buffer;
font_atlas.width = cmd.update_atlas.width;
font_atlas.height = cmd.update_atlas.height;
font_atlas.mipmaps = 1;
2025-01-30 19:38:53 +01:00
font_atlas.format = rl::PixelFormat.UNCOMPRESSED_GRAYSCALE;
if (rl::isTextureValid(font_texture)) {
rl::unloadTexture(font_texture);
}
2025-01-30 19:38:53 +01:00
font_texture = rl::loadTextureFromImage(font_atlas);
} else if (cmd.update_atlas.id == sprite_id) {
sprite_atlas.data = cmd.update_atlas.raw_buffer;
sprite_atlas.width = cmd.update_atlas.width;
sprite_atlas.height = cmd.update_atlas.height;
sprite_atlas.mipmaps = 1;
2025-01-30 19:38:53 +01:00
sprite_atlas.format = rl::PixelFormat.UNCOMPRESSED_R8G8B8A8;
if (rl::isTextureValid(sprite_texture)) {
rl::unloadTexture(sprite_texture);
}
2025-01-30 19:38:53 +01:00
sprite_texture = rl::loadTextureFromImage(sprite_atlas);
2024-12-11 01:14:14 +01:00
}
2025-02-06 23:44:03 +01:00
case CmdType.CMD_SPRITE:
if (cmd.sprite.texture_id == font_id) {
rl::Vector2 position = {
.x = cmd.sprite.rect.x,
.y = cmd.sprite.rect.y,
};
2025-01-30 19:38:53 +01:00
rl::beginShaderMode(font_shader);
rl::drawTextureRec(font_texture, cmd.sprite.texture_rect.conv(), position, cmd.sprite.hue.conv());
rl::endShaderMode();
} else if (cmd.sprite.texture_id == sprite_id) {
2025-02-04 22:40:44 +01:00
// FIXME: THIS CODE IS SHIT, REAL DOO DOO
2025-05-05 16:23:26 +02:00
{
2025-02-04 22:40:44 +01:00
if (cmd.sprite.type == SpriteType.SPRITE_MSDF) {
rl::beginShaderMode(msdf_shader);
defer rl::endShaderMode();
2025-05-05 16:23:26 +02:00
rl::drawTexturePro(sprite_texture, cmd.sprite.texture_rect.conv(), cmd.sprite.rect.conv(), {0.0f, 0.0f}, 0.0f, cmd.sprite.hue.conv());
2025-02-04 22:40:44 +01:00
} else {
2025-05-05 16:23:26 +02:00
rl::drawTexturePro(sprite_texture, cmd.sprite.texture_rect.conv(), cmd.sprite.rect.conv(), {0.0f, 0.0f}, 0.0f, cmd.sprite.hue.conv());
2025-02-04 22:40:44 +01:00
}
2025-05-05 16:23:26 +02:00
};
} else {
io::printfn("unknown texture id: %d", cmd.sprite.texture_id);
}
2025-02-06 23:44:03 +01:00
case CmdType.CMD_SCISSOR:
2024-12-15 22:29:07 +01:00
if (cmd.scissor.rect.w == 0 && cmd.scissor.rect.h == 0) {
2025-01-30 19:38:53 +01:00
rl::endScissorMode();
2024-12-15 22:29:07 +01:00
} else {
2025-01-30 19:38:53 +01:00
rl::beginScissorMode(cmd.scissor.rect.x, cmd.scissor.rect.y, cmd.scissor.rect.w, cmd.scissor.rect.h);
2024-12-15 22:29:07 +01:00
}
2024-10-29 22:45:47 +01:00
default:
2024-12-11 01:14:14 +01:00
io::printfn("Unknown cmd type: %s", cmd.type);
2024-10-29 22:45:47 +01:00
}
}
2024-12-01 00:25:43 +01:00
draw_times.push(clock.mark());
2024-12-17 11:26:59 +01:00
//draw_times.print_stats();
2025-01-30 19:38:53 +01:00
rl::endDrawing();
2024-12-01 00:25:43 +01:00
/* End Drawing */
2024-10-29 22:45:47 +01:00
}
2025-01-30 19:38:53 +01:00
rl::closeWindow();
2024-10-29 22:45:47 +01:00
return 0;
}