Files
kryon/runtime/toggle.kry
T

158 lines
5.5 KiB
Plaintext

#module "toggle"
#import "button"
#import "control_props"
#import "drawing_props"
#import "material"
#import "style"
#import "surface"
ToggleSpec :: struct {
bounds: Rectangle
checked: bool
enabled: bool
hovered: bool
pressed: bool
focused: bool
has_labels: bool
off_width: i32
on_width: i32
font: i32
scale: float
track: StyleFrame
active: StyleFrame
thumb: StyleFrame
}
TogglePaint :: struct {
bounds: Rectangle
track_bounds: Rectangle
focus_bounds: Rectangle
active_bounds: Rectangle
off_label_bounds: Rectangle
on_label_bounds: Rectangle
track: StyleFrame
active: StyleFrame
thumb: StyleFrame
show_focus: bool
has_labels: bool
off_label_color: u32
on_label_color: u32
thumb_x: float
thumb_y: float
thumb_radius: float
thumb_shadow_color: u32
thumb_fill_color: u32
thumb_edge_color: u32
thumb_highlight_color: u32
thumb_glow_color: u32
}
ToggleMinimumWidth :: (has_labels: bool, off_width: i32, on_width: i32,
scale: float) -> i32 #export {
if scale <= 0.0 { scale = 1.0 }
if !has_labels { return (i32)(54.0 * scale) }
widest: i32 = off_width
if on_width > widest { widest = on_width }
return (widest + (i32)(16.0 * scale)) * 2 + (i32)(6.0 * scale)
}
ToggleMinimumHeight :: (scale: float) -> i32 #export {
if scale <= 0.0 { scale = 1.0 }
return (i32)(34.0 * scale)
}
TogglePaintFor :: (spec: ToggleSpec) -> TogglePaint #export {
paint: TogglePaint
scale: float = spec.scale
if scale <= 0.0 { scale = 1.0 }
bounds: Rectangle = spec.bounds
min_w: float = (float)ToggleMinimumWidth(spec.has_labels, spec.off_width,
spec.on_width, scale)
min_h: float = (float)ToggleMinimumHeight(scale)
if bounds.width < min_w { bounds.width = min_w }
if bounds.height < min_h { bounds.height = min_h }
track_w: float = 54.0 * scale
track_h: float = 32.0 * scale
if spec.has_labels {
track_w = bounds.width
track_h = bounds.height
}
if track_w > bounds.width { track_w = bounds.width }
if track_h > bounds.height { track_h = bounds.height }
track: Rectangle
track.x = bounds.x + (bounds.width - track_w) * 0.5
track.y = bounds.y + (bounds.height - track_h) * 0.5
track.width = track_w
track.height = track_h
paint.track = spec.track
paint.active = spec.active
paint.thumb = spec.thumb
focus_gap: float = 4.0 * scale
paint.focus_bounds.x = track.x - focus_gap
paint.focus_bounds.y = track.y - focus_gap
paint.focus_bounds.width = track.width + focus_gap * 2.0
paint.focus_bounds.height = track.height + focus_gap * 2.0
paint.show_focus = spec.focused
paint.bounds = bounds
paint.track_bounds = track
paint.has_labels = spec.has_labels
paint.off_label_color = Opacity(spec.track.value.foreground,
spec.track.value.opacity)
paint.on_label_color = Opacity(spec.track.value.foreground,
spec.track.value.opacity)
if spec.has_labels {
active_w: float = (track.width - 6.0 * scale) * 0.5
active_x: float = track.x + 3.0 * scale
if spec.checked { active_x = track.x + track.width - active_w - 3.0 * scale }
paint.active_bounds.x = active_x
paint.active_bounds.y = track.y + 3.0 * scale
paint.active_bounds.width = active_w
paint.active_bounds.height = track.height - 6.0 * scale
text_y: float = bounds.y + (bounds.height - (float)spec.font) * 0.5
paint.off_label_bounds.x = bounds.x + bounds.width * 0.25 - (float)spec.off_width * 0.5
paint.off_label_bounds.y = text_y
paint.off_label_bounds.width = (float)spec.off_width
paint.off_label_bounds.height = (float)spec.font
paint.on_label_bounds.x = bounds.x + bounds.width * 0.75 - (float)spec.on_width * 0.5
paint.on_label_bounds.y = text_y
paint.on_label_bounds.width = (float)spec.on_width
paint.on_label_bounds.height = (float)spec.font
if spec.checked {
paint.on_label_color = Opacity(paint.active.value.foreground,
paint.active.value.opacity)
}
else {
paint.off_label_color = Opacity(paint.active.value.foreground,
paint.active.value.opacity)
}
} else {
thumb_size: float = 20.0 * scale
if spec.checked { thumb_size = 24.0 * scale }
if spec.pressed { thumb_size += 2.0 * scale }
max_thumb: float = track.height - 6.0 * scale
if thumb_size > max_thumb { thumb_size = max_thumb }
thumb_x: float = track.x + 4.0 * scale
if spec.checked { thumb_x = track.x + track.width - thumb_size - 4.0 * scale }
thumb_y: float = track.y + (track.height - thumb_size) * 0.5
paint.thumb_radius = thumb_size * 0.5
paint.thumb_x = thumb_x + paint.thumb_radius
paint.thumb_y = thumb_y + paint.thumb_radius
thumb_opacity: float = paint.thumb.value.opacity
paint.thumb_fill_color = Opacity(paint.thumb.value.background,
thumb_opacity)
paint.thumb_edge_color = Opacity(paint.thumb.value.border,
thumb_opacity)
paint.thumb_glow_color = Opacity(paint.thumb.value.focus,
0.36 * thumb_opacity)
paint.thumb_shadow_color = Opacity(paint.thumb.value.border,
(spec.enabled ? 0.36 : 0.18) * thumb_opacity)
paint.thumb_highlight_color = Opacity(paint.thumb.value.foreground,
(spec.enabled ? 0.23 : 0.09) * thumb_opacity)
}
return paint
}