fix syntax

This commit is contained in:
2026-02-17 09:15:15 -06:00
parent f310c18b84
commit 4e1b63fd0e
52 changed files with 2169 additions and 1754 deletions

137
clay.cm
View File

@@ -38,13 +38,13 @@ var base_config = {
}
function normalize_color(c, fallback) {
fallback = fallback || {r:1, g:1, b:1, a:1}
if (!c) return {r:fallback.r, g:fallback.g, b:fallback.b, a:fallback.a}
var fb = fallback || {r:1, g:1, b:1, a:1}
if (!c) return {r:fb.r, g:fb.g, b:fb.b, a:fb.a}
return {
r: c.r != null ? c.r : fallback.r,
g: c.g != null ? c.g : fallback.g,
b: c.b != null ? c.b : fallback.b,
a: c.a != null ? c.a : fallback.a
r: c.r != null ? c.r : fb.r,
g: c.g != null ? c.g : fb.g,
b: c.b != null ? c.b : fb.b,
a: c.a != null ? c.a : fb.a
}
}
@@ -59,8 +59,8 @@ function normalize_spacing(s) {
}
// Tree building state
var root_item
var tree_root
var root_item = null
var tree_root = null
var config_stack = []
// Rewriting state management for cleaner recursion
@@ -89,11 +89,11 @@ clay.layout = function(fn, size) {
return build_drawables(root_node, size.height)
}
function build_drawables(node, root_height, parent_abs_x, parent_abs_y, parent_scissor, parent_layer) {
parent_abs_x = parent_abs_x || 0
parent_abs_y = parent_abs_y || 0
parent_layer = parent_layer || 0 // UI usually on top, but let's start at 0
function build_drawables(node, root_height, parent, parent_scissor) {
var p_abs_x = (parent && parent.x) || 0
var p_abs_y = (parent && parent.y) || 0
var p_layer = (parent && parent.layer) || 0
var rect = lay_ctx.get_rect(node.id)
// Calculate absolute world Y for this node (bottom-up layout to top-down render)
@@ -108,22 +108,23 @@ function build_drawables(node, root_height, parent_abs_x, parent_abs_y, parent_s
// Scissor
var current_scissor = parent_scissor
var sx = vis_x
var sy = vis_y
var sw = rect.width
var sh = rect.height
var clip_right = null
var clip_bottom = null
if (node.config.clipped) {
var sx = vis_x
var sy = vis_y
var sw = rect.width
var sh = rect.height
// Intersect with parent
if (parent_scissor) {
sx = max(sx, parent_scissor.x)
sy = max(sy, parent_scissor.y)
var right = min(vis_x + sw, parent_scissor.x + parent_scissor.width)
var bottom = min(vis_y + sh, parent_scissor.y + parent_scissor.height)
sw = max(0, right - sx)
sh = max(0, bottom - sy)
clip_right = min(vis_x + sw, parent_scissor.x + parent_scissor.width)
clip_bottom = min(vis_y + sh, parent_scissor.y + parent_scissor.height)
sw = max(0, clip_right - sx)
sh = max(0, clip_bottom - sy)
}
current_scissor = {x: sx, y: sy, width: sw, height: sh}
}
@@ -138,7 +139,7 @@ function build_drawables(node, root_height, parent_abs_x, parent_abs_y, parent_s
height: rect.height,
slice: node.config.slice,
color: node.config.background_color || {r:1, g:1, b:1, a:1},
layer: parent_layer - 0.1, // slightly behind content
layer: p_layer - 0.1, // slightly behind content
scissor: current_scissor
})
} else {
@@ -149,7 +150,7 @@ function build_drawables(node, root_height, parent_abs_x, parent_abs_y, parent_s
width: rect.width,
height: rect.height,
color: node.config.background_color || {r:1, g:1, b:1, a:1},
layer: parent_layer - 0.1,
layer: p_layer - 0.1,
scissor: current_scissor
})
}
@@ -160,7 +161,7 @@ function build_drawables(node, root_height, parent_abs_x, parent_abs_y, parent_s
width: rect.width,
height: rect.height,
color: node.config.background_color,
layer: parent_layer - 0.1,
layer: p_layer - 0.1,
scissor: current_scissor
})
}
@@ -174,7 +175,7 @@ function build_drawables(node, root_height, parent_abs_x, parent_abs_y, parent_s
width: rect.width,
height: rect.height,
color: node.config.color,
layer: parent_layer,
layer: p_layer,
scissor: current_scissor
})
}
@@ -196,14 +197,14 @@ function build_drawables(node, root_height, parent_abs_x, parent_abs_y, parent_s
// `abs_y = root_height - (rect.y + rect.height)` -> Top edge of element.
// Text usually wants baseline.
// If we put it at `vis_y + rect.height`, that's bottom of element.
layer: parent_layer,
layer: p_layer,
scissor: current_scissor
})
}
// Children
arrfor(node.children, function(child) {
drawables = array(drawables, build_drawables(child, root_height, vis_x, vis_y, current_scissor, parent_layer + 0.01))
drawables = array(drawables, build_drawables(child, root_height, {x: vis_x, y: vis_y, layer: p_layer + 0.01}, current_scissor))
})
return drawables
@@ -235,8 +236,8 @@ function push_node(configs, contain_mode) {
lay_ctx.set_contain(item, config.contain)
lay_ctx.set_behave(item, config.behave)
if (config.size) {
var s = config.size
var s = config.size
if (s) {
if (is_array(s)) s = {width: s[0], height: s[1]}
lay_ctx.set_size(item, s)
}
@@ -262,44 +263,44 @@ function pop_node() {
// Generic container
clay.container = function(configs, fn) {
if (is_function(configs)) { fn = configs; configs = {} }
if (!is_array(configs)) configs = [configs]
push_node(configs, null)
if (fn) fn()
var _fn = is_function(configs) ? configs : fn
var _configs = is_function(configs) ? [{}] : (is_array(configs) ? configs : [configs])
push_node(_configs, null)
if (_fn) _fn()
pop_node()
}
// Stacks
clay.vstack = function(configs, fn) {
if (is_function(configs)) { fn = configs; configs = {} }
if (!is_array(configs)) configs = [configs]
var _fn = is_function(configs) ? configs : fn
var _configs = is_function(configs) ? [{}] : (is_array(configs) ? configs : [configs])
var c = layout.contain.column
push_node(configs, c)
if (fn) fn()
push_node(_configs, c)
if (_fn) _fn()
pop_node()
}
clay.hstack = function(configs, fn) {
if (is_function(configs)) { fn = configs; configs = {} }
if (!is_array(configs)) configs = [configs]
var _fn = is_function(configs) ? configs : fn
var _configs = is_function(configs) ? [{}] : (is_array(configs) ? configs : [configs])
var c = layout.contain.row
push_node(configs, c)
if (fn) fn()
push_node(_configs, c)
if (_fn) _fn()
pop_node()
}
clay.zstack = function(configs, fn) {
if (is_function(configs)) { fn = configs; configs = {} }
if (!is_array(configs)) configs = [configs]
var c = layout.contain.layout
push_node(configs, c)
if (fn) fn()
var _fn = is_function(configs) ? configs : fn
var _configs = is_function(configs) ? [{}] : (is_array(configs) ? configs : [configs])
var c = layout.contain.layout
push_node(_configs, c)
if (_fn) _fn()
pop_node()
}
@@ -308,12 +309,12 @@ clay.image = function(path, configs) {
var img = graphics.texture(path)
var c = [{image: path}]
var final_config = process_configs(configs)
if (!final_config.size && !final_config.behave)
if (!final_config.size && !final_config.behave)
c.size = {width: img.width, height: img.height}
if (!is_array(configs)) configs = [configs]
push_node(array(c, configs), null)
var _configs = is_array(configs) ? configs : [configs]
push_node(array(c, _configs), null)
pop_node()
}
@@ -321,18 +322,18 @@ clay.text = function(str, configs) {
var c = [{text: str}]
var final_config = process_configs(configs)
if (!final_config.size && !final_config.behave) {
c.size = {width: 100, height: 20}
c.size = {width: 100, height: 20}
}
if (!is_array(configs)) configs = [configs]
push_node(array(c, configs), null)
var _configs = is_array(configs) ? configs : [configs]
push_node(array(c, _configs), null)
pop_node()
}
clay.rectangle = function(configs) {
if (!is_array(configs)) configs = [configs]
push_node(configs, null)
var _configs = is_array(configs) ? configs : [configs]
push_node(_configs, null)
pop_node()
}
@@ -341,10 +342,10 @@ clay.button = function(str, action, configs) {
padding: 10,
background_color: {r:0.3, g:0.3, b:0.4, a:1}
}]
if (!is_array(configs)) configs = [configs]
clay.zstack(array(btn_config, configs), function() {
var _configs = is_array(configs) ? configs : [configs]
clay.zstack(array(btn_config, _configs), function() {
clay.text(str, {color: {r:1,g:1,b:1,a:1}})
})
}