Files
kryon/runtime/progress.kry
T

116 lines
3.4 KiB
Plaintext

#module "progress"
#import "control_props"
#import "drawing_props"
#import "style"
ProgressLayout :: struct {
ratio: float
fill_bounds: Rectangle
label_x: float
label_on_fill: bool
}
ProgressPaint :: struct {
layout: ProgressLayout
track: StyleFrame
fill: StyleFrame
label: StyleFrame
track_color: u32
fill_color: u32
border_color: u32
label_color: u32
filled_label_color: u32
radius: float
border_width: float
}
ProgressMetric :: (fields: u32, field: u32, value: float,
fallback: float, scale: float) -> float {
if (fields & field) == (u32)0 || value < 0.0 { value = fallback }
return value * scale
}
ProgressRatio :: (min: i32, max: i32, value: i32) -> float #export {
if max <= min {
max = min + 1
}
ratio: float = (float)(value - min) / (float)(max - min)
if ratio < 0.0 {
return 0.0
}
if ratio > 1.0 {
return 1.0
}
return ratio
}
ProgressFillBounds :: (bounds: Rectangle, ratio: float) -> Rectangle #export {
fill: Rectangle = bounds
if ratio < 0.0 {
ratio = 0.0
}
if ratio > 1.0 {
ratio = 1.0
}
fill.width = bounds.width * ratio
return fill
}
ProgressLabelX :: (bounds: Rectangle, fill_width: float,
label_width: float, padding: float) -> float #export {
x: float = bounds.x + (bounds.width - label_width) * 0.5
fill_end: float = bounds.x + fill_width
empty_width: float = bounds.width - fill_width
if empty_width >= label_width + padding * 2.0 {
return fill_end + padding
}
if fill_width >= label_width + padding * 2.0 {
return fill_end - label_width - padding
}
return x
}
ProgressLabelOnFill :: (fill_width: float, label_width: float,
padding: float) -> bool #export {
return fill_width >= label_width + padding * 2.0
}
ProgressLayoutFor :: (bounds: Rectangle, min: i32, max: i32, value: i32,
label_width: float, padding: float) -> ProgressLayout #export {
layout: ProgressLayout
layout.ratio = ProgressRatio(min, max, value)
layout.fill_bounds = ProgressFillBounds(bounds, layout.ratio)
layout.label_x = ProgressLabelX(bounds, layout.fill_bounds.width,
label_width, padding)
layout.label_on_fill = ProgressLabelOnFill(layout.fill_bounds.width,
label_width, padding)
return layout
}
ProgressLabelPaddingForStyle :: (label: StyleFrame, scale: float) -> float #export {
if scale <= 0.0 { scale = 1.0 }
return ProgressMetric(label.value.fields, (u32)StylePaddingX,
label.value.padding_x, 6.0, scale)
}
ProgressPaintFor :: (bounds: Rectangle, min: i32, max: i32, value: i32,
label_width: float, scale: float, track: StyleFrame, fill: StyleFrame,
label: StyleFrame) -> ProgressPaint #export {
paint: ProgressPaint
if scale <= 0.0 { scale = 1.0 }
padding: float = ProgressLabelPaddingForStyle(label, scale)
paint.layout = ProgressLayoutFor(bounds, min, max, value, label_width, padding)
paint.track = track
paint.fill = fill
paint.label = label
paint.track_color = track.value.background
paint.fill_color = fill.value.background
paint.border_color = track.value.border
paint.label_color = label.value.foreground
paint.filled_label_color = fill.value.foreground
paint.radius = track.value.radius * scale
paint.border_width = track.value.border_width * scale
if paint.border_width < 0.0 { paint.border_width = 0.0 }
return paint
}