From d50f021f0cc66e14053f910b8906ea005993a888 Mon Sep 17 00:00:00 2001 From: FourteenBrush <74827262+FourteenBrush@users.noreply.github.com> Date: Sat, 31 Aug 2024 20:59:11 +0200 Subject: [PATCH] Cleanup raylib renderer --- .../clay_renderer_raylib.odin | 295 +++++++++--------- 1 file changed, 141 insertions(+), 154 deletions(-) diff --git a/bindings/odin/examples/clay-official-website/clay_renderer_raylib.odin b/bindings/odin/examples/clay-official-website/clay_renderer_raylib.odin index 257b8ca..ad3985a 100644 --- a/bindings/odin/examples/clay-official-website/clay_renderer_raylib.odin +++ b/bindings/odin/examples/clay-official-website/clay_renderer_raylib.odin @@ -2,6 +2,7 @@ package main import clay "../../clay-odin" import "core:math" +import "core:strings" import "vendor:raylib" RaylibFont :: struct { @@ -22,16 +23,16 @@ measureText :: proc "c" (text: ^clay.String, config: ^clay.TextElementConfig) -> maxTextWidth: f32 = 0 lineTextWidth: f32 = 0 - textHeight: f32 = cast(f32)config.fontSize + textHeight := cast(f32)config.fontSize fontToUse := raylibFonts[config.fontId].font - for i := 0; i < cast(int)text.length; i += 1 { + for i in 0.. } } - maxTextWidth = math.max(maxTextWidth, lineTextWidth) + maxTextWidth = max(maxTextWidth, lineTextWidth) textSize.width = maxTextWidth / 2 textSize.height = textHeight @@ -48,168 +49,154 @@ measureText :: proc "c" (text: ^clay.String, config: ^clay.TextElementConfig) -> } clayRaylibRender :: proc(renderCommands: ^clay.ClayArray(clay.RenderCommand)) { - for i := 0; i < cast(int)renderCommands.length; i += 1 { + for i in 0.. 0) { - radius: f32 = (config.cornerRadius.topLeft * 2) / (boundingBox.width > boundingBox.height ? boundingBox.height : boundingBox.width) - raylib.DrawRectangleRounded( - raylib.Rectangle{boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height}, - radius, - 8, - clayColorToRaylibColor(config.color), - ) - } else { - raylib.DrawRectangle( - cast(i32)boundingBox.x, - cast(i32)boundingBox.y, - cast(i32)boundingBox.width, - cast(i32)boundingBox.height, - clayColorToRaylibColor(config.color), - ) - } + config: ^clay.RectangleElementConfig = renderCommand.config.rectangleElementConfig + if (config.cornerRadius.topLeft > 0) { + radius: f32 = (config.cornerRadius.topLeft * 2) / min(boundingBox.width, boundingBox.height) + raylib.DrawRectangleRounded( + raylib.Rectangle{boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height}, + radius, + 8, + clayColorToRaylibColor(config.color), + ) + } else { + raylib.DrawRectangle( + cast(i32)boundingBox.x, + cast(i32)boundingBox.y, + cast(i32)boundingBox.width, + cast(i32)boundingBox.height, + clayColorToRaylibColor(config.color), + ) } case clay.RenderCommandType.Border: - { - config := renderCommand.config.borderElementConfig - // Left border - if (config.left.width > 0) { - raylib.DrawRectangle( - cast(i32)math.round(boundingBox.x), - cast(i32)math.round(boundingBox.y + config.cornerRadius.topLeft), - cast(i32)config.left.width, - cast(i32)math.round(boundingBox.height - config.cornerRadius.topLeft - config.cornerRadius.bottomLeft), - clayColorToRaylibColor(config.left.color), - ) - } - // Right border - if (config.right.width > 0) { - raylib.DrawRectangle( - cast(i32)math.round(boundingBox.x + boundingBox.width - cast(f32)config.right.width), - cast(i32)math.round(boundingBox.y + config.cornerRadius.topRight), - cast(i32)config.right.width, - cast(i32)math.round(boundingBox.height - config.cornerRadius.topRight - config.cornerRadius.bottomRight), - clayColorToRaylibColor(config.right.color), - ) - } - // Top border - if (config.top.width > 0) { - raylib.DrawRectangle( - cast(i32)math.round(boundingBox.x + config.cornerRadius.topLeft), - cast(i32)math.round(boundingBox.y), - cast(i32)math.round(boundingBox.width - config.cornerRadius.topLeft - config.cornerRadius.topRight), - cast(i32)config.top.width, - clayColorToRaylibColor(config.top.color), - ) - } - // Bottom border - if (config.bottom.width > 0) { - raylib.DrawRectangle( - cast(i32)math.round(boundingBox.x + config.cornerRadius.bottomLeft), - cast(i32)math.round(boundingBox.y + boundingBox.height - cast(f32)config.bottom.width), - cast(i32)math.round(boundingBox.width - config.cornerRadius.bottomLeft - config.cornerRadius.bottomRight), - cast(i32)config.bottom.width, - clayColorToRaylibColor(config.bottom.color), - ) - } - if (config.cornerRadius.topLeft > 0) { - raylib.DrawRing( - raylib.Vector2{math.round(boundingBox.x + config.cornerRadius.topLeft), math.round(boundingBox.y + config.cornerRadius.topLeft)}, - math.round(config.cornerRadius.topLeft - cast(f32)config.top.width), - config.cornerRadius.topLeft, - 180, - 270, - 10, - clayColorToRaylibColor(config.top.color), - ) - } - if (config.cornerRadius.topRight > 0) { - raylib.DrawRing( - raylib.Vector2{math.round(boundingBox.x + boundingBox.width - config.cornerRadius.topRight), math.round(boundingBox.y + config.cornerRadius.topRight)}, - math.round(config.cornerRadius.topRight - cast(f32)config.top.width), - config.cornerRadius.topRight, - 270, - 360, - 10, - clayColorToRaylibColor(config.top.color), - ) - } - if (config.cornerRadius.bottomLeft > 0) { - raylib.DrawRing( - raylib.Vector2 { - math.round(boundingBox.x + config.cornerRadius.bottomLeft), - math.round(boundingBox.y + boundingBox.height - config.cornerRadius.bottomLeft), - }, - math.round(config.cornerRadius.bottomLeft - cast(f32)config.top.width), - config.cornerRadius.bottomLeft, - 90, - 180, - 10, - clayColorToRaylibColor(config.bottom.color), - ) - } - if (config.cornerRadius.bottomRight > 0) { - raylib.DrawRing( - raylib.Vector2 { - math.round(boundingBox.x + boundingBox.width - config.cornerRadius.bottomRight), - math.round(boundingBox.y + boundingBox.height - config.cornerRadius.bottomRight), - }, - math.round(config.cornerRadius.bottomRight - cast(f32)config.bottom.width), - config.cornerRadius.bottomRight, - 0.1, - 90, - 10, - clayColorToRaylibColor(config.bottom.color), - ) - } + config := renderCommand.config.borderElementConfig + // Left border + if (config.left.width > 0) { + raylib.DrawRectangle( + cast(i32)math.round(boundingBox.x), + cast(i32)math.round(boundingBox.y + config.cornerRadius.topLeft), + cast(i32)config.left.width, + cast(i32)math.round(boundingBox.height - config.cornerRadius.topLeft - config.cornerRadius.bottomLeft), + clayColorToRaylibColor(config.left.color), + ) + } + // Right border + if (config.right.width > 0) { + raylib.DrawRectangle( + cast(i32)math.round(boundingBox.x + boundingBox.width - cast(f32)config.right.width), + cast(i32)math.round(boundingBox.y + config.cornerRadius.topRight), + cast(i32)config.right.width, + cast(i32)math.round(boundingBox.height - config.cornerRadius.topRight - config.cornerRadius.bottomRight), + clayColorToRaylibColor(config.right.color), + ) + } + // Top border + if (config.top.width > 0) { + raylib.DrawRectangle( + cast(i32)math.round(boundingBox.x + config.cornerRadius.topLeft), + cast(i32)math.round(boundingBox.y), + cast(i32)math.round(boundingBox.width - config.cornerRadius.topLeft - config.cornerRadius.topRight), + cast(i32)config.top.width, + clayColorToRaylibColor(config.top.color), + ) + } + // Bottom border + if (config.bottom.width > 0) { + raylib.DrawRectangle( + cast(i32)math.round(boundingBox.x + config.cornerRadius.bottomLeft), + cast(i32)math.round(boundingBox.y + boundingBox.height - cast(f32)config.bottom.width), + cast(i32)math.round(boundingBox.width - config.cornerRadius.bottomLeft - config.cornerRadius.bottomRight), + cast(i32)config.bottom.width, + clayColorToRaylibColor(config.bottom.color), + ) + } + if (config.cornerRadius.topLeft > 0) { + raylib.DrawRing( + raylib.Vector2{math.round(boundingBox.x + config.cornerRadius.topLeft), math.round(boundingBox.y + config.cornerRadius.topLeft)}, + math.round(config.cornerRadius.topLeft - cast(f32)config.top.width), + config.cornerRadius.topLeft, + 180, + 270, + 10, + clayColorToRaylibColor(config.top.color), + ) + } + if (config.cornerRadius.topRight > 0) { + raylib.DrawRing( + raylib.Vector2{math.round(boundingBox.x + boundingBox.width - config.cornerRadius.topRight), math.round(boundingBox.y + config.cornerRadius.topRight)}, + math.round(config.cornerRadius.topRight - cast(f32)config.top.width), + config.cornerRadius.topRight, + 270, + 360, + 10, + clayColorToRaylibColor(config.top.color), + ) + } + if (config.cornerRadius.bottomLeft > 0) { + raylib.DrawRing( + raylib.Vector2 { + math.round(boundingBox.x + config.cornerRadius.bottomLeft), + math.round(boundingBox.y + boundingBox.height - config.cornerRadius.bottomLeft), + }, + math.round(config.cornerRadius.bottomLeft - cast(f32)config.top.width), + config.cornerRadius.bottomLeft, + 90, + 180, + 10, + clayColorToRaylibColor(config.bottom.color), + ) + } + if (config.cornerRadius.bottomRight > 0) { + raylib.DrawRing( + raylib.Vector2 { + math.round(boundingBox.x + boundingBox.width - config.cornerRadius.bottomRight), + math.round(boundingBox.y + boundingBox.height - config.cornerRadius.bottomRight), + }, + math.round(config.cornerRadius.bottomRight - cast(f32)config.bottom.width), + config.cornerRadius.bottomRight, + 0.1, + 90, + 10, + clayColorToRaylibColor(config.bottom.color), + ) } case clay.RenderCommandType.Custom: - { - // Implement custom element rendering here - } + // Implement custom element rendering here } } }