Files
plot/src/plot_widget.zi
T
waozi a99a18598a Own PlotWidget and import Kryon by package-qualified paths
The plot surface moves here from Kryon, which no longer ships it.
Plot exports PlotWidget for apps that draw their own series next to the
Plot charts, and every Kryon import names its package (kryon/Kryon,
kryon/session, ...). The widget's behavior test runs as a portable
bundle and in Go, binding Kryon's host capabilities by import path.
2026-09-29 19:50:01 -03:00

189 lines
7.4 KiB
Ziran

#import "kryon/session"
#import "kryon/control_props"
#import "kryon/drawing_props"
#import "kryon/font_metrics"
#import "kryon/geometry"
#import "kryon/paint_queue"
#import "plot"
#import "plot_props"
#import "kryon/style"
#import "kryon/style_sheet"
#import "kryon/surface"
#import "kryon/tree"
#import "kryon/widget_kind"
using PlotMode;
using WidgetKind;
using ButtonTone;
using ButtonEmphasis;
using ControlSize;
using ButtonState;
using StyleField;
PlotFaces :: struct {
plot: StyleFrame
mark: StyleFrame
}
#program_export
DefaultPlotFaces :: () -> PlotFaces {
faces: PlotFaces
faces.plot.value.fields = cast(u32)StyleBackground |
cast(u32)StyleForeground | cast(u32)StyleBorder |
cast(u32)StyleBorderWidth | cast(u32)StyleRadius |
cast(u32)StyleFontSize | cast(u32)StylePaddingX |
cast(u32)StylePaddingY | cast(u32)StyleOpacity
faces.plot.value.background = cast(u32)0xf5f5f5ff
faces.plot.value.foreground = cast(u32)0x171717ff
faces.plot.value.border = cast(u32)0x737373ff
faces.plot.value.border_width = 1.0
faces.plot.value.radius = 4.0
faces.plot.value.font_size = 14.0
faces.plot.value.padding_x = 6.0
faces.plot.value.padding_y = 4.0
faces.plot.value.opacity = 1.0
faces.mark.value.fields = cast(u32)StyleBackground |
cast(u32)StyleOpacity
faces.mark.value.background = cast(u32)0x3b609bff
faces.mark.value.opacity = 1.0
return faces
}
#program_export
PlotFacesFor :: (props: PlotProps,
defaults: PlotFaces) -> PlotFaces {
faces: PlotFaces = defaults
plot_facts: StyleFacts = StyleControlRoleFacts(StyleKindPlot(),
props.id, props.class_name, StyleAny(),
cast(s32)ButtonToneNeutral, cast(s32)ButtonEmphasisFilled,
cast(s32)ControlSizeMedium, cast(s32)ButtonStateNormal)
mark_facts: StyleFacts = StyleControlRoleFacts(StyleKindPlotMark(),
props.id, props.class_name, StyleAny(),
cast(s32)ButtonToneAccent, cast(s32)ButtonEmphasisFilled,
cast(s32)ControlSizeMedium, cast(s32)ButtonStateNormal)
faces.plot.value = ResolveInstalledStyleRules(defaults.plot.value,
plot_facts, cast(s32)ButtonStateNormal)
faces.mark.value = ResolveInstalledStyleRules(defaults.mark.value,
mark_facts, cast(s32)ButtonStateNormal)
return faces
}
#program_export
PlotObservedRange :: (values: []float32,
offset: s32, props: PlotProps) -> PlotRange {
offset = PlotOffset((cast(s32)values.count), offset)
observed_min: float32 = 0.0
observed_max: float32 = 0.0
if (cast(s32)values.count) > 0 {
observed_min = values[offset]
observed_max = observed_min
index: s32 = 1
while index < (cast(s32)values.count) {
value: float32 = values[(offset + index) % (cast(s32)values.count)]
if value < observed_min { observed_min = value }
if value > observed_max { observed_max = value }
index += 1
}
}
return PlotRangeFor(props.scale_min, props.scale_max,
observed_min, observed_max)
}
// Values are borrowed for this frame. The retained node keeps only its key,
// bounds, and semantic label; the caller supplies the next frame's values.
#program_export
Plot :: (session: Session, props: PlotProps, values: []float32) -> s32 {
bounds: Rectangle = props.bounds
if bounds.width <= 0.0 || bounds.height <= 0.0 { return -1 }
key: u64 = props.key
if key == cast(u64)0 && props.id > 0 { key = cast(u64)cast(u32)props.id }
if key == cast(u64)0 { key = TreeNextKey(session) }
node: s32 = -1
if TreeIsBuilding(session) {
node = TreeSubmitCurrent(session, key, WidgetKindPlot, bounds)
if node < 0 { return -1 }
semantic_label: string = props.label
if props.semantic_label != "" {
semantic_label = props.semantic_label
}
TreeSetLabel(session, node, semantic_label)
bounds = TreeSubmittedNodeAt(session, node).bounds
}
faces: PlotFaces = PlotFacesFor(props,
DefaultPlotFaces())
opacity: float32 = StyleOpacityValue(faces.plot.value.fields,
faces.plot.value.opacity)
PaintRoundedFill(session, node, bounds, faces.plot.value.radius, 12,
ColorFromPacked(Opacity(faces.plot.value.background, opacity)))
if faces.plot.value.border_width > 0.0 {
PaintRoundedOutline(session, node, bounds, faces.plot.value.radius, 12,
faces.plot.value.border_width,
ColorFromPacked(Opacity(faces.plot.value.border, opacity)))
}
if (cast(s32)values.count) > 0 {
offset: s32 = PlotOffset((cast(s32)values.count), props.offset)
value_range: PlotRange = PlotObservedRange(values, offset, props)
mark_color: Color = ColorFromPacked(Opacity(
faces.mark.value.background,
StyleOpacityValue(faces.mark.value.fields,
faces.mark.value.opacity)))
index: s32 = 0
if props.mode == cast(PlotMode)PlotBars {
while index < (cast(s32)values.count) {
mark: PlotMark = PlotHistogramBar(bounds, index,
(cast(s32)values.count), values[(offset + index) % (cast(s32)values.count)],
value_range, faces.mark)
if mark.bounds.width > 0.0 && mark.bounds.height > 0.0 {
PaintRoundedFill(session, node, mark.bounds, 0.0, 0, mark_color)
}
index += 1
}
} else if (cast(s32)values.count) == 1 {
mark: PlotMark = PlotSingleLine(bounds, values[offset],
value_range, faces.mark)
PaintLine(session, node, mark.bounds, mark_color)
} else {
index = 1
while index < (cast(s32)values.count) {
a: float32 = values[(offset + index - 1) % (cast(s32)values.count)]
b: float32 = values[(offset + index) % (cast(s32)values.count)]
mark: PlotMark = PlotLineSegment(bounds, index,
(cast(s32)values.count), a, b, value_range, faces.mark)
PaintLine(session, node, mark.bounds, mark_color)
index += 1
}
}
}
if props.label != "" || props.overlay != "" {
font: s32 = ResolveFont(0,
StyleFontValue(faces.plot.value.fields,
faces.plot.value.font_size), 14)
label_width: float32 = 0.0
overlay_width: float32 = 0.0
if props.label != "" {
label_width = cast(float32)GlyphWidth(props.label, font,
faces.plot.value.typeface)
}
if props.overlay != "" {
overlay_width = cast(float32)GlyphWidth(props.overlay, font,
faces.plot.value.typeface)
}
paint: PlotTextPaint = PlotTextPaintFor(bounds, label_width,
overlay_width, props.scale, faces.plot,
props.label != "", props.overlay != "")
text_color: Color = ColorFromPacked(Opacity(paint.text_color,
opacity))
if paint.show_label {
PaintClippedLabel(session, node, props.label,
cast(s32)paint.label_bounds.x,
cast(s32)paint.label_bounds.y, font, text_color, bounds)
}
if paint.show_overlay {
PaintClippedLabel(session, node, props.overlay,
cast(s32)paint.overlay_bounds.x,
cast(s32)paint.overlay_bounds.y, font, text_color, bounds)
}
}
return node
}