Portable bundles draw band charts through their host
A bundle that shows values against a good range could not draw a band chart, because the chart measures and paints with Kryon's native modules. The chart's records move to band_chart_props, a bundle built with KRYON_HOSTED_UI submits the chart as BandChartHostedHost, and a host answers it with BandChartHostCall from band_chart_host, which decodes the records with a codec generated on top of Kryon's widget codec. The check builds the bundles as ZX, which the toolchain now runs.
This commit is contained in:
1 parent
4b0e25bd2d
commit
2e06dfe503
13 files changed
+796
-235
No files matched your search
@@ -21,11 +21,23 @@ BINDS := \
|
||||
--bind kryon/raster:RasterLine=$(HOST):RasterLine \
|
||||
--bind kryon/paint_queue:RasterImage=$(HOST):RasterImage
|
||||
|
||||
# The band chart's host test runs natively, as a host does, and paints
|
||||
# through its own counting provider.
|
||||
HOSTED_HOST := band_chart_host_provider
|
||||
HOSTED_BINDS := \
|
||||
--bind kryon/font_metrics:MeasureGlyphWidth=$(HOSTED_HOST):MeasureGlyphWidth \
|
||||
--bind kryon/raster_text:RasterText=$(HOSTED_HOST):RasterText \
|
||||
--bind kryon/raster_text:RasterTextClipped=$(HOSTED_HOST):RasterTextClipped \
|
||||
--bind kryon/raster_shape:RasterRoundedRectangle=$(HOSTED_HOST):RasterRoundedRectangle \
|
||||
--bind kryon/raster_shape:RasterRoundedRectangleOutline=$(HOSTED_HOST):RasterRoundedRectangleOutline \
|
||||
--bind kryon/raster:RasterLine=$(HOSTED_HOST):RasterLine \
|
||||
--bind kryon/paint_queue:RasterImage=$(HOSTED_HOST):RasterImage
|
||||
|
||||
check:
|
||||
$(ZIRAN) tool kryon check
|
||||
$(ZIRAN) bundle --project $(BINDS) --entry plot_widget_behavior:main \
|
||||
-o build/plot-widget.zib tests/plot_widget_behavior.zi
|
||||
test "$$($(ZIRAN) run --project build/plot-widget.zib)" = 0
|
||||
-o build/plot-widget.zx tests/plot_widget_behavior.zi
|
||||
test "$$($(ZIRAN) run --project build/plot-widget.zx)" = 0
|
||||
@if command -v go >/dev/null; then \
|
||||
rm -rf build/plot-widget-go && \
|
||||
$(ZIRAN) build --project --target=go --pkg main --exe $(BINDS) \
|
||||
@@ -34,6 +46,19 @@ check:
|
||||
cd build/plot-widget-go && env -u DISPLAY -u WAYLAND_DISPLAY \
|
||||
GO111MODULE=off go run *.go ; \
|
||||
fi
|
||||
python3 scripts/generate-codec.py --check
|
||||
$(ZIRAN) bundle --project --define KRYON_HOSTED_UI \
|
||||
--bind band_chart:BandChartHostedHost=band_chart_hosted_provider:HostedBandChart \
|
||||
--entry band_chart_hosted:main -o build/band-chart-hosted.zx tests/band_chart_hosted.zi
|
||||
test "$$($(ZIRAN) run --project build/band-chart-hosted.zx)" = 0
|
||||
rm -rf build/band-chart-host-c
|
||||
mkdir -p build/band-chart-host-c
|
||||
$(ZIRAN) build --project --target=c $(HOSTED_BINDS) \
|
||||
--entry band_chart_host_behavior:main -o build/band-chart-host-c \
|
||||
tests/band_chart_host_behavior.zi
|
||||
$(CC) -std=c11 -I$(ZIRAN_INCLUDE) -Ibuild/band-chart-host-c \
|
||||
build/band-chart-host-c/*.c -lm -o build/band-chart-host
|
||||
build/band-chart-host
|
||||
mkdir -p build/indicator-test-c
|
||||
$(ZIRAN) build --project --target=c --entry indicator_test:main \
|
||||
-o build/indicator-test-c \
|
||||
|
||||
@@ -44,3 +44,15 @@ to a Ziran package manifest. Other packages can use the dependency directly.
|
||||
Public module names use `UpperCamelCase` (`Kryon`, `PlotWidget`, `Plot`).
|
||||
Lowercase names such as `plot_widget` are implementation modules; the
|
||||
lowercase `plot` export remains available for existing consumers.
|
||||
|
||||
## Band charts in portable bundles
|
||||
|
||||
`BandChart(session, props, points, bands)` draws values over a row of slots
|
||||
against the range that counts as good. A portable bundle built with
|
||||
`KRYON_HOSTED_UI` sends the chart to its host as `BandChartHostedHost`. The
|
||||
host answers it with `BandChartHostCall(session, module, function, arguments,
|
||||
count, result)` from `band_chart_host`, beside Kryon's `WidgetHostCall`, and
|
||||
allows it where `BandChartHostSupports(module, function)` says so. The
|
||||
records are in `band_chart_props`, and `scripts/generate-codec.py` generates
|
||||
their codec on top of Kryon's widget codec. A chart takes at most 366 points
|
||||
and 366 bands.
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate src/band_chart_codec.zi with Kryon's widget codec generator.
|
||||
|
||||
A portable bundle submits a band chart's records to its host, which decodes
|
||||
them with this codec on top of Kryon's own widget codec. Pass --check to
|
||||
verify the file is current."""
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
KRYON = Path(subprocess.run(['ziran', 'pkg', 'path', 'kryon'], cwd=ROOT, check=True,
|
||||
capture_output=True, text=True).stdout.strip())
|
||||
command = [sys.executable, str(KRYON / 'scripts/generate-widget-codecs.py'),
|
||||
'--output', str(ROOT / 'src/band_chart_codec.zi'),
|
||||
'--uses', str(KRYON / 'src/backend/widget_codec.zi'),
|
||||
'--source', str(KRYON / 'src/ui/geometry.zi'),
|
||||
'--source', str(KRYON / 'src/ui/drawing_props.zi'),
|
||||
'--source', str(ROOT / 'src/band_chart_props.zi'),
|
||||
'--generated-by', 'scripts/generate-codec.py']
|
||||
for root in ('BandChartProps', 'BandChartPoint', 'BandChartBand', 'BandChartResult'):
|
||||
command += ['--root', root]
|
||||
for line in ('"std/bundle_host"', '"std/byte_text_linux"', '"kryon/hosted_values"',
|
||||
'"kryon/widget_codec"', '"kryon/geometry"', '"kryon/drawing_props"',
|
||||
'"band_chart_props"'):
|
||||
command += ['--import', line]
|
||||
command += sys.argv[1:]
|
||||
sys.exit(subprocess.run(command).returncode)
|
||||
@@ -3,3 +3,4 @@ using UI :: #import "kryon/Kryon";
|
||||
using Candle :: #import "candle_chart";
|
||||
using Line :: #import "line_chart";
|
||||
using Band :: #import "band_chart";
|
||||
using BandProps :: #import "band_chart_props";
|
||||
+201
-233
@@ -1,252 +1,220 @@
|
||||
// Values over a row of slots, such as the last 30 days, against a band of
|
||||
// good values that can change from one slot on. Each point is good, not, or
|
||||
// unjudged; the point under the pointer, or tapped last, shows its label.
|
||||
using UI :: #import "kryon/Kryon";
|
||||
using Widgets :: #import "kryon/Widgets";
|
||||
#import "kryon/font_metrics"
|
||||
#import "kryon/tree_input"
|
||||
|
||||
BandChartPoint :: struct {
|
||||
// 0 is the leftmost slot.
|
||||
slot: s32
|
||||
value: float32
|
||||
// 1 good, 0 not good, -1 not judged.
|
||||
good: s32
|
||||
// Shown while the point is hovered or after it is tapped, such as
|
||||
// "12 Oct · 07:40 · good: before 08:00".
|
||||
label: string
|
||||
}
|
||||
|
||||
// The good values from slot from on, until the next band starts. A bound
|
||||
// that is not set is open. A wrapping band is good above low or below
|
||||
// high, as a time of day between 22:00 and 01:00.
|
||||
BandChartBand :: struct {
|
||||
from: s32
|
||||
low: float32
|
||||
high: float32
|
||||
has_low: bool
|
||||
has_high: bool
|
||||
wraps: bool
|
||||
}
|
||||
|
||||
BandChartProps :: struct {
|
||||
key: u64
|
||||
bounds: Rectangle
|
||||
semantic_label: string
|
||||
slots: s32
|
||||
min_value: float32
|
||||
max_value: float32
|
||||
// Bars from min_value, or dots.
|
||||
bars: bool
|
||||
// The scale's top and bottom values as the caller writes them.
|
||||
top_label: string
|
||||
bottom_label: string
|
||||
font: s32
|
||||
good_color: Color
|
||||
other_color: Color
|
||||
band_color: Color
|
||||
grid_color: Color
|
||||
text_color: Color
|
||||
label_fill: Color
|
||||
}
|
||||
|
||||
BandChartResult :: struct {
|
||||
// The point whose label shows, or -1.
|
||||
shown: s32
|
||||
}
|
||||
|
||||
BandChartAxisWidth :: (props: BandChartProps) -> float32 {
|
||||
widest: s32 = GlyphWidth(props.top_label, props.font, "")
|
||||
bottom: s32 = GlyphWidth(props.bottom_label, props.font, "")
|
||||
if bottom > widest { widest = bottom }
|
||||
return cast(float32)widest + 12.0
|
||||
}
|
||||
|
||||
// The plot's area: right of the scale labels, with room for dots at the
|
||||
// top and bottom.
|
||||
#program_export
|
||||
BandChartPlotBounds :: (props: BandChartProps) -> Rectangle {
|
||||
axis: float32 = BandChartAxisWidth(props)
|
||||
return Rectangle.{props.bounds.x + axis, props.bounds.y + 10.0,
|
||||
props.bounds.width - axis - 8.0, props.bounds.height - 20.0}
|
||||
}
|
||||
|
||||
#program_export
|
||||
BandChartY :: (plot: Rectangle, props: BandChartProps, value: float32) -> float32 {
|
||||
span: float32 = props.max_value - props.min_value
|
||||
if span <= 0.0 { span = 1.0 }
|
||||
fraction: float32 = (value - props.min_value) / span
|
||||
if fraction < 0.0 { fraction = 0.0 }
|
||||
if fraction > 1.0 { fraction = 1.0 }
|
||||
return plot.y + plot.height * (1.0 - fraction)
|
||||
}
|
||||
|
||||
#program_export
|
||||
BandChartSlotX :: (plot: Rectangle, props: BandChartProps, slot: s32) -> float32 {
|
||||
slots: s32 = props.slots
|
||||
if slots < 1 { slots = 1 }
|
||||
return plot.x + plot.width * (cast(float32)slot + 0.5) / cast(float32)slots
|
||||
}
|
||||
|
||||
// The point nearest to x and y, preferring the pointer's own slot, or -1.
|
||||
#program_export
|
||||
BandChartNearest :: (props: BandChartProps, points: []BandChartPoint,
|
||||
x: float32, y: float32) -> s32 {
|
||||
plot: Rectangle = BandChartPlotBounds(props)
|
||||
nearest: s32 = -1
|
||||
best: float32 = 0.0
|
||||
index: s32 = 0
|
||||
while index < cast(s32)points.count {
|
||||
dx: float32 = BandChartSlotX(plot, props, points[index].slot) - x
|
||||
dy: float32 = BandChartY(plot, props, points[index].value) - y
|
||||
if props.bars { dy = 0.0 }
|
||||
distance: float32 = dx * dx * 4.0 + dy * dy
|
||||
if nearest < 0 || distance < best {
|
||||
nearest = index
|
||||
best = distance
|
||||
}
|
||||
index += 1
|
||||
#if #defined(KRYON_HOSTED_UI) {
|
||||
// A portable bundle submits the chart to its host, which draws it with
|
||||
// this widget (band_chart_host.zi).
|
||||
#import "kryon/session"
|
||||
#import "band_chart_props"
|
||||
host_api :: #system_library "host_api";
|
||||
BandChartHostedHost :: (session: Session, props: BandChartProps, points: []BandChartPoint,
|
||||
bands: []BandChartBand) -> BandChartResult #foreign host_api;
|
||||
#program_export
|
||||
BandChart :: (session: Session, props: BandChartProps, points: []BandChartPoint,
|
||||
bands: []BandChartBand) -> BandChartResult {
|
||||
return BandChartHostedHost(session, props, points, bands)
|
||||
}
|
||||
return nearest
|
||||
}
|
||||
} else {
|
||||
using UI :: #import "kryon/Kryon";
|
||||
using Widgets :: #import "kryon/Widgets";
|
||||
#import "kryon/font_metrics"
|
||||
#import "kryon/tree_input"
|
||||
|
||||
// The bands' good areas, and a line where a later band starts.
|
||||
BandChartBands :: (session: Session, props: BandChartProps, plot: Rectangle,
|
||||
bands: []BandChartBand) {
|
||||
slots: s32 = props.slots
|
||||
if slots < 1 { slots = 1 }
|
||||
slot_w: float32 = plot.width / cast(float32)slots
|
||||
index: s32 = 0
|
||||
while index < cast(s32)bands.count {
|
||||
band: BandChartBand = bands[index]
|
||||
start: s32 = band.from
|
||||
if start < 0 { start = 0 }
|
||||
end: s32 = slots
|
||||
if index + 1 < cast(s32)bands.count && bands[index + 1].from < end {
|
||||
end = bands[index + 1].from
|
||||
#import "band_chart_props"
|
||||
|
||||
BandChartAxisWidth :: (props: BandChartProps) -> float32 {
|
||||
widest: s32 = GlyphWidth(props.top_label, props.font, "")
|
||||
bottom: s32 = GlyphWidth(props.bottom_label, props.font, "")
|
||||
if bottom > widest { widest = bottom }
|
||||
return cast(float32)widest + 12.0
|
||||
}
|
||||
|
||||
// The plot's area: right of the scale labels, with room for dots at the
|
||||
// top and bottom.
|
||||
#program_export
|
||||
BandChartPlotBounds :: (props: BandChartProps) -> Rectangle {
|
||||
axis: float32 = BandChartAxisWidth(props)
|
||||
return Rectangle.{props.bounds.x + axis, props.bounds.y + 10.0,
|
||||
props.bounds.width - axis - 8.0, props.bounds.height - 20.0}
|
||||
}
|
||||
|
||||
#program_export
|
||||
BandChartY :: (plot: Rectangle, props: BandChartProps, value: float32) -> float32 {
|
||||
span: float32 = props.max_value - props.min_value
|
||||
if span <= 0.0 { span = 1.0 }
|
||||
fraction: float32 = (value - props.min_value) / span
|
||||
if fraction < 0.0 { fraction = 0.0 }
|
||||
if fraction > 1.0 { fraction = 1.0 }
|
||||
return plot.y + plot.height * (1.0 - fraction)
|
||||
}
|
||||
|
||||
#program_export
|
||||
BandChartSlotX :: (plot: Rectangle, props: BandChartProps, slot: s32) -> float32 {
|
||||
slots: s32 = props.slots
|
||||
if slots < 1 { slots = 1 }
|
||||
return plot.x + plot.width * (cast(float32)slot + 0.5) / cast(float32)slots
|
||||
}
|
||||
|
||||
// The point nearest to x and y, preferring the pointer's own slot, or -1.
|
||||
#program_export
|
||||
BandChartNearest :: (props: BandChartProps, points: []BandChartPoint,
|
||||
x: float32, y: float32) -> s32 {
|
||||
plot: Rectangle = BandChartPlotBounds(props)
|
||||
nearest: s32 = -1
|
||||
best: float32 = 0.0
|
||||
index: s32 = 0
|
||||
while index < cast(s32)points.count {
|
||||
dx: float32 = BandChartSlotX(plot, props, points[index].slot) - x
|
||||
dy: float32 = BandChartY(plot, props, points[index].value) - y
|
||||
if props.bars { dy = 0.0 }
|
||||
distance: float32 = dx * dx * 4.0 + dy * dy
|
||||
if nearest < 0 || distance < best {
|
||||
nearest = index
|
||||
best = distance
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
if end > start && (band.has_low || band.has_high) {
|
||||
left: float32 = plot.x + slot_w * cast(float32)start
|
||||
width: float32 = slot_w * cast(float32)(end - start)
|
||||
top: float32 = plot.y
|
||||
bottom: float32 = plot.y + plot.height
|
||||
if band.wraps {
|
||||
low_y: float32 = BandChartY(plot, props, band.low)
|
||||
high_y: float32 = BandChartY(plot, props, band.high)
|
||||
PaintRoundedFill(session, -1, Rectangle.{left, top, width,
|
||||
low_y - top}, 0.0, 1, props.band_color)
|
||||
PaintRoundedFill(session, -1, Rectangle.{left, high_y, width,
|
||||
bottom - high_y}, 0.0, 1, props.band_color)
|
||||
} else {
|
||||
if band.has_high { top = BandChartY(plot, props, band.high) }
|
||||
if band.has_low { bottom = BandChartY(plot, props, band.low) }
|
||||
if bottom > top {
|
||||
return nearest
|
||||
}
|
||||
|
||||
// The bands' good areas, and a line where a later band starts.
|
||||
BandChartBands :: (session: Session, props: BandChartProps, plot: Rectangle,
|
||||
bands: []BandChartBand) {
|
||||
slots: s32 = props.slots
|
||||
if slots < 1 { slots = 1 }
|
||||
slot_w: float32 = plot.width / cast(float32)slots
|
||||
index: s32 = 0
|
||||
while index < cast(s32)bands.count {
|
||||
band: BandChartBand = bands[index]
|
||||
start: s32 = band.from
|
||||
if start < 0 { start = 0 }
|
||||
end: s32 = slots
|
||||
if index + 1 < cast(s32)bands.count && bands[index + 1].from < end {
|
||||
end = bands[index + 1].from
|
||||
}
|
||||
if end > start && (band.has_low || band.has_high) {
|
||||
left: float32 = plot.x + slot_w * cast(float32)start
|
||||
width: float32 = slot_w * cast(float32)(end - start)
|
||||
top: float32 = plot.y
|
||||
bottom: float32 = plot.y + plot.height
|
||||
if band.wraps {
|
||||
low_y: float32 = BandChartY(plot, props, band.low)
|
||||
high_y: float32 = BandChartY(plot, props, band.high)
|
||||
PaintRoundedFill(session, -1, Rectangle.{left, top, width,
|
||||
bottom - top}, 0.0, 1, props.band_color)
|
||||
low_y - top}, 0.0, 1, props.band_color)
|
||||
PaintRoundedFill(session, -1, Rectangle.{left, high_y, width,
|
||||
bottom - high_y}, 0.0, 1, props.band_color)
|
||||
} else {
|
||||
if band.has_high { top = BandChartY(plot, props, band.high) }
|
||||
if band.has_low { bottom = BandChartY(plot, props, band.low) }
|
||||
if bottom > top {
|
||||
PaintRoundedFill(session, -1, Rectangle.{left, top, width,
|
||||
bottom - top}, 0.0, 1, props.band_color)
|
||||
}
|
||||
}
|
||||
}
|
||||
if index > 0 && start > 0 && start < slots {
|
||||
PaintLine(session, -1, Rectangle.{plot.x + slot_w * cast(float32)start,
|
||||
plot.y, 0.0, plot.height}, props.grid_color)
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
if index > 0 && start > 0 && start < slots {
|
||||
PaintLine(session, -1, Rectangle.{plot.x + slot_w * cast(float32)start,
|
||||
plot.y, 0.0, plot.height}, props.grid_color)
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
}
|
||||
|
||||
// The shown point's label in a box above it, kept inside the chart.
|
||||
BandChartLabel :: (session: Session, props: BandChartProps, plot: Rectangle,
|
||||
point: BandChartPoint) {
|
||||
if point.label == "" { return }
|
||||
x: float32 = BandChartSlotX(plot, props, point.slot)
|
||||
y: float32 = BandChartY(plot, props, point.value)
|
||||
PaintLine(session, -1, Rectangle.{x, plot.y, 0.0, plot.height}, props.grid_color)
|
||||
text_w: float32 = cast(float32)GlyphWidth(point.label, props.font, "")
|
||||
box_w: float32 = text_w + 16.0
|
||||
box_h: float32 = cast(float32)props.font + 12.0
|
||||
box_x: float32 = x - box_w * 0.5
|
||||
if box_x < props.bounds.x { box_x = props.bounds.x }
|
||||
if box_x + box_w > props.bounds.x + props.bounds.width {
|
||||
box_x = props.bounds.x + props.bounds.width - box_w
|
||||
}
|
||||
box_y: float32 = y - box_h - 10.0
|
||||
if box_y < props.bounds.y { box_y = y + 10.0 }
|
||||
if box_y + box_h > props.bounds.y + props.bounds.height {
|
||||
box_y = props.bounds.y + props.bounds.height - box_h
|
||||
}
|
||||
PaintRoundedFill(session, -1, Rectangle.{box_x, box_y, box_w, box_h},
|
||||
6.0, 8, props.label_fill)
|
||||
PaintClippedLabel(session, -1, point.label, cast(s32)(box_x + 8.0),
|
||||
cast(s32)(box_y + 6.0), props.font, props.text_color, props.bounds)
|
||||
}
|
||||
|
||||
// Draws the chart and returns the point whose label shows: the one under
|
||||
// the pointer, or on a touch screen the one tapped last.
|
||||
#program_export
|
||||
BandChart :: (session: Session, props: BandChartProps, points: []BandChartPoint,
|
||||
bands: []BandChartBand) -> BandChartResult {
|
||||
result: BandChartResult
|
||||
result.shown = -1
|
||||
plot: Rectangle = BandChartPlotBounds(props)
|
||||
if plot.width <= 0.0 || plot.height <= 0.0 { return result }
|
||||
area: PressProps
|
||||
area.key = props.key
|
||||
area.bounds = props.bounds
|
||||
area.label = props.semantic_label
|
||||
pressed: PressResult = Press(session, area)
|
||||
BandChartBands(session, props, plot, bands)
|
||||
PaintLine(session, -1, Rectangle.{plot.x, plot.y + plot.height,
|
||||
plot.width, 0.0}, props.grid_color)
|
||||
PaintClippedLabel(session, -1, props.top_label, cast(s32)props.bounds.x,
|
||||
cast(s32)(plot.y - cast(float32)props.font * 0.5), props.font,
|
||||
props.text_color, props.bounds)
|
||||
PaintClippedLabel(session, -1, props.bottom_label, cast(s32)props.bounds.x,
|
||||
cast(s32)(plot.y + plot.height - cast(float32)props.font * 0.5), props.font,
|
||||
props.text_color, props.bounds)
|
||||
slots: s32 = props.slots
|
||||
if slots < 1 { slots = 1 }
|
||||
slot_w: float32 = plot.width / cast(float32)slots
|
||||
// Bars of one slot share it side by side.
|
||||
per_slot: [366]s32
|
||||
placed: [366]s32
|
||||
for point: points {
|
||||
if point.slot >= 0 && point.slot < 366 { per_slot[point.slot] += 1 }
|
||||
}
|
||||
for point: points {
|
||||
if point.slot < 0 || point.slot >= slots || point.slot >= 366 { continue }
|
||||
color: Color = ifx point.good == 0 then props.other_color else props.good_color
|
||||
// The shown point's label in a box above it, kept inside the chart.
|
||||
BandChartLabel :: (session: Session, props: BandChartProps, plot: Rectangle,
|
||||
point: BandChartPoint) {
|
||||
if point.label == "" { return }
|
||||
x: float32 = BandChartSlotX(plot, props, point.slot)
|
||||
y: float32 = BandChartY(plot, props, point.value)
|
||||
if props.bars {
|
||||
share: float32 = slot_w / cast(float32)per_slot[point.slot]
|
||||
left: float32 = plot.x + slot_w * cast(float32)point.slot +
|
||||
share * cast(float32)placed[point.slot] + 1.0
|
||||
width: float32 = share - 2.0
|
||||
if width < 1.0 { width = 1.0 }
|
||||
height: float32 = plot.y + plot.height - y
|
||||
if height < 2.0 { height = 2.0 }
|
||||
PaintRoundedFill(session, -1, Rectangle.{left, plot.y + plot.height - height,
|
||||
width, height}, 0.0, 1, color)
|
||||
placed[point.slot] += 1
|
||||
} else {
|
||||
dot: float32 = 8.0
|
||||
PaintRoundedFill(session, -1, Rectangle.{x - dot * 0.5, y - dot * 0.5,
|
||||
dot, dot}, dot * 0.5, 12, color)
|
||||
PaintLine(session, -1, Rectangle.{x, plot.y, 0.0, plot.height}, props.grid_color)
|
||||
text_w: float32 = cast(float32)GlyphWidth(point.label, props.font, "")
|
||||
box_w: float32 = text_w + 16.0
|
||||
box_h: float32 = cast(float32)props.font + 12.0
|
||||
box_x: float32 = x - box_w * 0.5
|
||||
if box_x < props.bounds.x { box_x = props.bounds.x }
|
||||
if box_x + box_w > props.bounds.x + props.bounds.width {
|
||||
box_x = props.bounds.x + props.bounds.width - box_w
|
||||
}
|
||||
}
|
||||
if pressed.node >= 0 && (TreeHoveredAt(session, pressed.node) || pressed.down ||
|
||||
pressed.released) {
|
||||
pointer: PointerFrame = PointerSample(session)
|
||||
x: float32 = pointer.x
|
||||
y: float32 = pointer.y
|
||||
if pressed.down || pressed.released {
|
||||
x = pressed.x
|
||||
y = pressed.y
|
||||
box_y: float32 = y - box_h - 10.0
|
||||
if box_y < props.bounds.y { box_y = y + 10.0 }
|
||||
if box_y + box_h > props.bounds.y + props.bounds.height {
|
||||
box_y = props.bounds.y + props.bounds.height - box_h
|
||||
}
|
||||
result.shown = BandChartNearest(props, points, x, y)
|
||||
PaintRoundedFill(session, -1, Rectangle.{box_x, box_y, box_w, box_h},
|
||||
6.0, 8, props.label_fill)
|
||||
PaintClippedLabel(session, -1, point.label, cast(s32)(box_x + 8.0),
|
||||
cast(s32)(box_y + 6.0), props.font, props.text_color, props.bounds)
|
||||
}
|
||||
if result.shown >= 0 {
|
||||
BandChartLabel(session, props, plot, points[result.shown])
|
||||
|
||||
// Draws the chart and returns the point whose label shows: the one under
|
||||
// the pointer, or on a touch screen the one tapped last.
|
||||
#program_export
|
||||
BandChart :: (session: Session, props: BandChartProps, points: []BandChartPoint,
|
||||
bands: []BandChartBand) -> BandChartResult {
|
||||
result: BandChartResult
|
||||
result.shown = -1
|
||||
plot: Rectangle = BandChartPlotBounds(props)
|
||||
if plot.width <= 0.0 || plot.height <= 0.0 { return result }
|
||||
area: PressProps
|
||||
area.key = props.key
|
||||
area.bounds = props.bounds
|
||||
area.label = props.semantic_label
|
||||
pressed: PressResult = Press(session, area)
|
||||
BandChartBands(session, props, plot, bands)
|
||||
PaintLine(session, -1, Rectangle.{plot.x, plot.y + plot.height,
|
||||
plot.width, 0.0}, props.grid_color)
|
||||
PaintClippedLabel(session, -1, props.top_label, cast(s32)props.bounds.x,
|
||||
cast(s32)(plot.y - cast(float32)props.font * 0.5), props.font,
|
||||
props.text_color, props.bounds)
|
||||
PaintClippedLabel(session, -1, props.bottom_label, cast(s32)props.bounds.x,
|
||||
cast(s32)(plot.y + plot.height - cast(float32)props.font * 0.5), props.font,
|
||||
props.text_color, props.bounds)
|
||||
slots: s32 = props.slots
|
||||
if slots < 1 { slots = 1 }
|
||||
slot_w: float32 = plot.width / cast(float32)slots
|
||||
// Bars of one slot share it side by side.
|
||||
per_slot: [366]s32
|
||||
placed: [366]s32
|
||||
for point: points {
|
||||
if point.slot >= 0 && point.slot < 366 { per_slot[point.slot] += 1 }
|
||||
}
|
||||
for point: points {
|
||||
if point.slot < 0 || point.slot >= slots || point.slot >= 366 { continue }
|
||||
color: Color = ifx point.good == 0 then props.other_color else props.good_color
|
||||
x: float32 = BandChartSlotX(plot, props, point.slot)
|
||||
y: float32 = BandChartY(plot, props, point.value)
|
||||
if props.bars {
|
||||
share: float32 = slot_w / cast(float32)per_slot[point.slot]
|
||||
left: float32 = plot.x + slot_w * cast(float32)point.slot +
|
||||
share * cast(float32)placed[point.slot] + 1.0
|
||||
width: float32 = share - 2.0
|
||||
if width < 1.0 { width = 1.0 }
|
||||
height: float32 = plot.y + plot.height - y
|
||||
if height < 2.0 { height = 2.0 }
|
||||
PaintRoundedFill(session, -1, Rectangle.{left, plot.y + plot.height - height,
|
||||
width, height}, 0.0, 1, color)
|
||||
placed[point.slot] += 1
|
||||
} else {
|
||||
dot: float32 = 8.0
|
||||
PaintRoundedFill(session, -1, Rectangle.{x - dot * 0.5, y - dot * 0.5,
|
||||
dot, dot}, dot * 0.5, 12, color)
|
||||
}
|
||||
}
|
||||
if pressed.node >= 0 && (TreeHoveredAt(session, pressed.node) || pressed.down ||
|
||||
pressed.released) {
|
||||
pointer: PointerFrame = PointerSample(session)
|
||||
x: float32 = pointer.x
|
||||
y: float32 = pointer.y
|
||||
if pressed.down || pressed.released {
|
||||
x = pressed.x
|
||||
y = pressed.y
|
||||
}
|
||||
result.shown = BandChartNearest(props, points, x, y)
|
||||
}
|
||||
if result.shown >= 0 {
|
||||
BandChartLabel(session, props, plot, points[result.shown])
|
||||
}
|
||||
return result
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
// Generated by scripts/generate-codec.py; bounded public widget values.
|
||||
#import "std/bundle_host"
|
||||
#import "std/byte_text_linux"
|
||||
#import "kryon/hosted_values"
|
||||
#import "kryon/widget_codec"
|
||||
#import "kryon/geometry"
|
||||
#import "kryon/drawing_props"
|
||||
#import "band_chart_props"
|
||||
using HostValueKind;
|
||||
|
||||
WidgetBandChartPropsCodec :: struct {
|
||||
fields: [16]HostField
|
||||
bounds: WidgetRectangleCodec
|
||||
good_color: WidgetColorCodec
|
||||
other_color: WidgetColorCodec
|
||||
band_color: WidgetColorCodec
|
||||
grid_color: WidgetColorCodec
|
||||
text_color: WidgetColorCodec
|
||||
label_fill: WidgetColorCodec
|
||||
}
|
||||
|
||||
WidgetEncodeBandChartProps :: (data: BandChartProps, codec: *WidgetBandChartPropsCodec) -> HostValue {
|
||||
value := HostRecordValue("BandChartProps", codec.fields[:])
|
||||
codec.fields[0] = HostField.{.name = HostTextData("key"), .value = HostScalar("u64", cast(s64)data.key)}
|
||||
codec.fields[1].name = HostTextData("bounds")
|
||||
codec.fields[1].value = WidgetEncodeRectangle(data.bounds, *codec.bounds)
|
||||
codec.fields[2] = HostField.{.name = HostTextData("semantic_label"), .value = HostValue.{.kind = cast(s32)HostString, .type_name = HostTextData("string"), .data = HostTextData(data.semantic_label), .length = cast(usize)data.semantic_label.count}}
|
||||
codec.fields[3] = HostField.{.name = HostTextData("slots"), .value = HostScalar("s32", cast(s64)data.slots)}
|
||||
codec.fields[4] = HostField.{.name = HostTextData("min_value"), .value = HostValue.{.kind = cast(s32)HostReal, .type_name = HostTextData("float32"), .real = cast(float64)data.min_value}}
|
||||
codec.fields[5] = HostField.{.name = HostTextData("max_value"), .value = HostValue.{.kind = cast(s32)HostReal, .type_name = HostTextData("float32"), .real = cast(float64)data.max_value}}
|
||||
codec.fields[6] = HostField.{.name = HostTextData("bars"), .value = HostScalar("bool", ifx data.bars then 1 else 0)}
|
||||
codec.fields[7] = HostField.{.name = HostTextData("top_label"), .value = HostValue.{.kind = cast(s32)HostString, .type_name = HostTextData("string"), .data = HostTextData(data.top_label), .length = cast(usize)data.top_label.count}}
|
||||
codec.fields[8] = HostField.{.name = HostTextData("bottom_label"), .value = HostValue.{.kind = cast(s32)HostString, .type_name = HostTextData("string"), .data = HostTextData(data.bottom_label), .length = cast(usize)data.bottom_label.count}}
|
||||
codec.fields[9] = HostField.{.name = HostTextData("font"), .value = HostScalar("s32", cast(s64)data.font)}
|
||||
codec.fields[10].name = HostTextData("good_color")
|
||||
codec.fields[10].value = WidgetEncodeColor(data.good_color, *codec.good_color)
|
||||
codec.fields[11].name = HostTextData("other_color")
|
||||
codec.fields[11].value = WidgetEncodeColor(data.other_color, *codec.other_color)
|
||||
codec.fields[12].name = HostTextData("band_color")
|
||||
codec.fields[12].value = WidgetEncodeColor(data.band_color, *codec.band_color)
|
||||
codec.fields[13].name = HostTextData("grid_color")
|
||||
codec.fields[13].value = WidgetEncodeColor(data.grid_color, *codec.grid_color)
|
||||
codec.fields[14].name = HostTextData("text_color")
|
||||
codec.fields[14].value = WidgetEncodeColor(data.text_color, *codec.text_color)
|
||||
codec.fields[15].name = HostTextData("label_fill")
|
||||
codec.fields[15].value = WidgetEncodeColor(data.label_fill, *codec.label_fill)
|
||||
return value
|
||||
}
|
||||
|
||||
WidgetDecodeBandChartProps :: (value: HostValue, codec: *WidgetBandChartPropsCodec) -> BandChartProps {
|
||||
data: BandChartProps
|
||||
zero: HostValue
|
||||
data.key = cast(u64)HostFieldValue(*value, 0, "key", *zero).bits
|
||||
data.bounds = WidgetDecodeRectangle(HostFieldValue(*value, 1, "bounds", *zero).*, *codec.bounds)
|
||||
data.semantic_label = HostValueText(HostFieldValue(*value, 2, "semantic_label", *zero).*)
|
||||
data.slots = cast(s32)HostFieldValue(*value, 3, "slots", *zero).integer
|
||||
data.min_value = cast(float32)HostFieldValue(*value, 4, "min_value", *zero).real
|
||||
data.max_value = cast(float32)HostFieldValue(*value, 5, "max_value", *zero).real
|
||||
data.bars = HostFieldValue(*value, 6, "bars", *zero).integer != 0
|
||||
data.top_label = HostValueText(HostFieldValue(*value, 7, "top_label", *zero).*)
|
||||
data.bottom_label = HostValueText(HostFieldValue(*value, 8, "bottom_label", *zero).*)
|
||||
data.font = cast(s32)HostFieldValue(*value, 9, "font", *zero).integer
|
||||
data.good_color = WidgetDecodeColor(HostFieldValue(*value, 10, "good_color", *zero).*, *codec.good_color)
|
||||
data.other_color = WidgetDecodeColor(HostFieldValue(*value, 11, "other_color", *zero).*, *codec.other_color)
|
||||
data.band_color = WidgetDecodeColor(HostFieldValue(*value, 12, "band_color", *zero).*, *codec.band_color)
|
||||
data.grid_color = WidgetDecodeColor(HostFieldValue(*value, 13, "grid_color", *zero).*, *codec.grid_color)
|
||||
data.text_color = WidgetDecodeColor(HostFieldValue(*value, 14, "text_color", *zero).*, *codec.text_color)
|
||||
data.label_fill = WidgetDecodeColor(HostFieldValue(*value, 15, "label_fill", *zero).*, *codec.label_fill)
|
||||
return data
|
||||
}
|
||||
|
||||
WidgetValidBandChartProps :: (value: HostValue) -> bool {
|
||||
if !HostRecordValid(*value, "BandChartProps") { return false }
|
||||
if !HostFieldScalarValid(*value, 0, "key", "u64", cast(s32)HostUnsigned) { return false }
|
||||
if !HostFieldRecordValid(*value, 1, "bounds", WidgetValidRectangle) { return false }
|
||||
if !HostFieldScalarValid(*value, 2, "semantic_label", "string", cast(s32)HostString) { return false }
|
||||
if !HostFieldScalarValid(*value, 3, "slots", "s32", cast(s32)HostInteger) { return false }
|
||||
if !HostFieldScalarValid(*value, 4, "min_value", "float32", cast(s32)HostReal) { return false }
|
||||
if !HostFieldScalarValid(*value, 5, "max_value", "float32", cast(s32)HostReal) { return false }
|
||||
if !HostFieldScalarValid(*value, 6, "bars", "bool", cast(s32)HostInteger) { return false }
|
||||
if !HostFieldScalarValid(*value, 7, "top_label", "string", cast(s32)HostString) { return false }
|
||||
if !HostFieldScalarValid(*value, 8, "bottom_label", "string", cast(s32)HostString) { return false }
|
||||
if !HostFieldScalarValid(*value, 9, "font", "s32", cast(s32)HostInteger) { return false }
|
||||
if !HostFieldRecordValid(*value, 10, "good_color", WidgetValidColor) { return false }
|
||||
if !HostFieldRecordValid(*value, 11, "other_color", WidgetValidColor) { return false }
|
||||
if !HostFieldRecordValid(*value, 12, "band_color", WidgetValidColor) { return false }
|
||||
if !HostFieldRecordValid(*value, 13, "grid_color", WidgetValidColor) { return false }
|
||||
if !HostFieldRecordValid(*value, 14, "text_color", WidgetValidColor) { return false }
|
||||
if !HostFieldRecordValid(*value, 15, "label_fill", WidgetValidColor) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
WidgetBandChartPointCodec :: struct {
|
||||
fields: [4]HostField
|
||||
}
|
||||
|
||||
WidgetEncodeBandChartPoint :: (data: BandChartPoint, codec: *WidgetBandChartPointCodec) -> HostValue {
|
||||
value := HostRecordValue("BandChartPoint", codec.fields[:])
|
||||
codec.fields[0] = HostField.{.name = HostTextData("slot"), .value = HostScalar("s32", cast(s64)data.slot)}
|
||||
codec.fields[1] = HostField.{.name = HostTextData("value"), .value = HostValue.{.kind = cast(s32)HostReal, .type_name = HostTextData("float32"), .real = cast(float64)data.value}}
|
||||
codec.fields[2] = HostField.{.name = HostTextData("good"), .value = HostScalar("s32", cast(s64)data.good)}
|
||||
codec.fields[3] = HostField.{.name = HostTextData("label"), .value = HostValue.{.kind = cast(s32)HostString, .type_name = HostTextData("string"), .data = HostTextData(data.label), .length = cast(usize)data.label.count}}
|
||||
return value
|
||||
}
|
||||
|
||||
WidgetDecodeBandChartPoint :: (value: HostValue, codec: *WidgetBandChartPointCodec) -> BandChartPoint {
|
||||
data: BandChartPoint
|
||||
unused codec
|
||||
zero: HostValue
|
||||
data.slot = cast(s32)HostFieldValue(*value, 0, "slot", *zero).integer
|
||||
data.value = cast(float32)HostFieldValue(*value, 1, "value", *zero).real
|
||||
data.good = cast(s32)HostFieldValue(*value, 2, "good", *zero).integer
|
||||
data.label = HostValueText(HostFieldValue(*value, 3, "label", *zero).*)
|
||||
return data
|
||||
}
|
||||
|
||||
WidgetValidBandChartPoint :: (value: HostValue) -> bool {
|
||||
if !HostRecordValid(*value, "BandChartPoint") { return false }
|
||||
if !HostFieldScalarValid(*value, 0, "slot", "s32", cast(s32)HostInteger) { return false }
|
||||
if !HostFieldScalarValid(*value, 1, "value", "float32", cast(s32)HostReal) { return false }
|
||||
if !HostFieldScalarValid(*value, 2, "good", "s32", cast(s32)HostInteger) { return false }
|
||||
if !HostFieldScalarValid(*value, 3, "label", "string", cast(s32)HostString) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
WidgetBandChartBandCodec :: struct {
|
||||
fields: [6]HostField
|
||||
}
|
||||
|
||||
WidgetEncodeBandChartBand :: (data: BandChartBand, codec: *WidgetBandChartBandCodec) -> HostValue {
|
||||
value := HostRecordValue("BandChartBand", codec.fields[:])
|
||||
codec.fields[0] = HostField.{.name = HostTextData("from"), .value = HostScalar("s32", cast(s64)data.from)}
|
||||
codec.fields[1] = HostField.{.name = HostTextData("low"), .value = HostValue.{.kind = cast(s32)HostReal, .type_name = HostTextData("float32"), .real = cast(float64)data.low}}
|
||||
codec.fields[2] = HostField.{.name = HostTextData("high"), .value = HostValue.{.kind = cast(s32)HostReal, .type_name = HostTextData("float32"), .real = cast(float64)data.high}}
|
||||
codec.fields[3] = HostField.{.name = HostTextData("has_low"), .value = HostScalar("bool", ifx data.has_low then 1 else 0)}
|
||||
codec.fields[4] = HostField.{.name = HostTextData("has_high"), .value = HostScalar("bool", ifx data.has_high then 1 else 0)}
|
||||
codec.fields[5] = HostField.{.name = HostTextData("wraps"), .value = HostScalar("bool", ifx data.wraps then 1 else 0)}
|
||||
return value
|
||||
}
|
||||
|
||||
WidgetDecodeBandChartBand :: (value: HostValue, codec: *WidgetBandChartBandCodec) -> BandChartBand {
|
||||
data: BandChartBand
|
||||
unused codec
|
||||
zero: HostValue
|
||||
data.from = cast(s32)HostFieldValue(*value, 0, "from", *zero).integer
|
||||
data.low = cast(float32)HostFieldValue(*value, 1, "low", *zero).real
|
||||
data.high = cast(float32)HostFieldValue(*value, 2, "high", *zero).real
|
||||
data.has_low = HostFieldValue(*value, 3, "has_low", *zero).integer != 0
|
||||
data.has_high = HostFieldValue(*value, 4, "has_high", *zero).integer != 0
|
||||
data.wraps = HostFieldValue(*value, 5, "wraps", *zero).integer != 0
|
||||
return data
|
||||
}
|
||||
|
||||
WidgetValidBandChartBand :: (value: HostValue) -> bool {
|
||||
if !HostRecordValid(*value, "BandChartBand") { return false }
|
||||
if !HostFieldScalarValid(*value, 0, "from", "s32", cast(s32)HostInteger) { return false }
|
||||
if !HostFieldScalarValid(*value, 1, "low", "float32", cast(s32)HostReal) { return false }
|
||||
if !HostFieldScalarValid(*value, 2, "high", "float32", cast(s32)HostReal) { return false }
|
||||
if !HostFieldScalarValid(*value, 3, "has_low", "bool", cast(s32)HostInteger) { return false }
|
||||
if !HostFieldScalarValid(*value, 4, "has_high", "bool", cast(s32)HostInteger) { return false }
|
||||
if !HostFieldScalarValid(*value, 5, "wraps", "bool", cast(s32)HostInteger) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
WidgetBandChartResultCodec :: struct {
|
||||
fields: [1]HostField
|
||||
}
|
||||
|
||||
WidgetEncodeBandChartResult :: (data: BandChartResult, codec: *WidgetBandChartResultCodec) -> HostValue {
|
||||
value := HostRecordValue("BandChartResult", codec.fields[:])
|
||||
codec.fields[0] = HostField.{.name = HostTextData("shown"), .value = HostScalar("s32", cast(s64)data.shown)}
|
||||
return value
|
||||
}
|
||||
|
||||
WidgetDecodeBandChartResult :: (value: HostValue, codec: *WidgetBandChartResultCodec) -> BandChartResult {
|
||||
data: BandChartResult
|
||||
unused codec
|
||||
zero: HostValue
|
||||
data.shown = cast(s32)HostFieldValue(*value, 0, "shown", *zero).integer
|
||||
return data
|
||||
}
|
||||
|
||||
WidgetValidBandChartResult :: (value: HostValue) -> bool {
|
||||
if !HostRecordValid(*value, "BandChartResult") { return false }
|
||||
if !HostFieldScalarValid(*value, 0, "shown", "s32", cast(s32)HostInteger) { return false }
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// Draws the band charts a portable bundle asks for. A host that runs such a
|
||||
// bundle answers BandChartHostedHost with BandChartHostCall, beside Kryon's
|
||||
// WidgetHostCall, while the bundle draws its frame in the host's session.
|
||||
#import "std/bundle_host"
|
||||
#import "std/byte_text_linux"
|
||||
#import "kryon/session"
|
||||
#import "kryon/hosted_values"
|
||||
#import "kryon/widget_codec"
|
||||
#import "band_chart"
|
||||
#import "band_chart_props"
|
||||
#import "band_chart_codec"
|
||||
|
||||
using HostValueKind;
|
||||
|
||||
// The most points and bands one chart takes, a year of days each.
|
||||
BAND_CHART_HOST_LIMIT :: 366
|
||||
|
||||
band_chart_host_props_codec: WidgetBandChartPropsCodec;
|
||||
band_chart_host_result_codec: WidgetBandChartResultCodec;
|
||||
band_chart_host_session_codec: WidgetSessionCodec;
|
||||
band_chart_host_points: [BAND_CHART_HOST_LIMIT]BandChartPoint;
|
||||
band_chart_host_point_codecs: [BAND_CHART_HOST_LIMIT]WidgetBandChartPointCodec;
|
||||
band_chart_host_bands: [BAND_CHART_HOST_LIMIT]BandChartBand;
|
||||
band_chart_host_band_codecs: [BAND_CHART_HOST_LIMIT]WidgetBandChartBandCodec;
|
||||
|
||||
// The module a bundle names the chart by: band_chart, or with the package's
|
||||
// name in front when it imports plot/band_chart.
|
||||
BandChartHostModule :: (module: string) -> bool {
|
||||
return module == "band_chart" || module == "plot_band_chart" ||
|
||||
module == "plot/band_chart"
|
||||
}
|
||||
|
||||
// The calls BandChartHostCall answers, for checking a bundle's capabilities
|
||||
// before it runs.
|
||||
BandChartHostSupports :: (module: string, function: string) -> bool {
|
||||
return BandChartHostModule(module) && function == "BandChartHostedHost"
|
||||
}
|
||||
|
||||
// Answers a bundle's BandChart in session, the frame the host draws; 0 for a
|
||||
// call it does not answer or refuses.
|
||||
BandChartHostCall :: (session: Session, module: *u8, function: *u8,
|
||||
arguments: *HostValue, count: s32, result: *HostValue) -> s32 {
|
||||
if !BandChartHostSupports(TextFromCString(module), TextFromCString(function)) ||
|
||||
count != 4 || !WidgetValidSession(arguments[0]) ||
|
||||
!WidgetValidBandChartProps(arguments[1]) {
|
||||
return 0
|
||||
}
|
||||
asked := WidgetDecodeSession(arguments[0], *band_chart_host_session_codec)
|
||||
if asked.slot != session.slot || asked.epoch != session.epoch {
|
||||
return 0
|
||||
}
|
||||
points := arguments[2]
|
||||
bands := arguments[3]
|
||||
if points.kind != cast(s32)HostSlice || TextFromCString(points.type_name) != "[]BandChartPoint" ||
|
||||
points.length > cast(usize)BAND_CHART_HOST_LIMIT ||
|
||||
(points.length > cast(usize)0 && points.elements == null) {
|
||||
return 0
|
||||
}
|
||||
if bands.kind != cast(s32)HostSlice || TextFromCString(bands.type_name) != "[]BandChartBand" ||
|
||||
bands.length > cast(usize)BAND_CHART_HOST_LIMIT ||
|
||||
(bands.length > cast(usize)0 && bands.elements == null) {
|
||||
return 0
|
||||
}
|
||||
index: usize = 0
|
||||
while index < points.length {
|
||||
if !WidgetValidBandChartPoint(points.elements[index]) {
|
||||
return 0
|
||||
}
|
||||
band_chart_host_points[index] = WidgetDecodeBandChartPoint(points.elements[index],
|
||||
*band_chart_host_point_codecs[index])
|
||||
index += cast(usize)1
|
||||
}
|
||||
index = 0
|
||||
while index < bands.length {
|
||||
if !WidgetValidBandChartBand(bands.elements[index]) {
|
||||
return 0
|
||||
}
|
||||
band_chart_host_bands[index] = WidgetDecodeBandChartBand(bands.elements[index],
|
||||
*band_chart_host_band_codecs[index])
|
||||
index += cast(usize)1
|
||||
}
|
||||
shown := BandChart(session, WidgetDecodeBandChartProps(arguments[1],
|
||||
*band_chart_host_props_codec),
|
||||
band_chart_host_points[:cast(s64)points.length], band_chart_host_bands[:cast(s64)bands.length])
|
||||
result.* = WidgetEncodeBandChartResult(shown, *band_chart_host_result_codec)
|
||||
return 1
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// The records a band chart takes and answers, apart from its drawing, so a
|
||||
// portable bundle and its host share them.
|
||||
#import "kryon/geometry"
|
||||
#import "kryon/drawing_props"
|
||||
|
||||
BandChartPoint :: struct {
|
||||
// 0 is the leftmost slot.
|
||||
slot: s32
|
||||
value: float32
|
||||
// 1 good, 0 not good, -1 not judged.
|
||||
good: s32
|
||||
// Shown while the point is hovered or after it is tapped, such as
|
||||
// "12 Oct · 07:40 · good: before 08:00".
|
||||
label: string
|
||||
}
|
||||
|
||||
// The good values from slot from on, until the next band starts. A bound
|
||||
// that is not set is open. A wrapping band is good above low or below
|
||||
// high, as a time of day between 22:00 and 01:00.
|
||||
BandChartBand :: struct {
|
||||
from: s32
|
||||
low: float32
|
||||
high: float32
|
||||
has_low: bool
|
||||
has_high: bool
|
||||
wraps: bool
|
||||
}
|
||||
|
||||
BandChartProps :: struct {
|
||||
key: u64
|
||||
bounds: Rectangle
|
||||
semantic_label: string
|
||||
slots: s32
|
||||
min_value: float32
|
||||
max_value: float32
|
||||
// Bars from min_value, or dots.
|
||||
bars: bool
|
||||
// The scale's top and bottom values as the caller writes them.
|
||||
top_label: string
|
||||
bottom_label: string
|
||||
font: s32
|
||||
good_color: Color
|
||||
other_color: Color
|
||||
band_color: Color
|
||||
grid_color: Color
|
||||
text_color: Color
|
||||
label_fill: Color
|
||||
}
|
||||
|
||||
BandChartResult :: struct {
|
||||
// The point whose label shows, or -1.
|
||||
shown: s32
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// A portable bundle's band chart draws through its host: the host decodes
|
||||
// the chart's props, points and bands and draws it in its own frame, and
|
||||
// refuses another session, too many points or another module's call.
|
||||
#import "std/bundle_host"
|
||||
#import "std/c_string"
|
||||
#import "kryon/session"
|
||||
#import "kryon/geometry"
|
||||
#import "kryon/drawing_props"
|
||||
#import "kryon/tree"
|
||||
#import "kryon/Frame"
|
||||
#import "kryon/paint_queue"
|
||||
#import "kryon/hosted_values"
|
||||
#import "kryon/widget_codec"
|
||||
#import "band_chart_props"
|
||||
#import "band_chart_codec"
|
||||
#import "band_chart_host"
|
||||
#import "band_chart_host_provider"
|
||||
|
||||
using HostValueKind;
|
||||
|
||||
session_codec: WidgetSessionCodec;
|
||||
props_codec: WidgetBandChartPropsCodec;
|
||||
result_codec: WidgetBandChartResultCodec;
|
||||
point_codecs: [400]WidgetBandChartPointCodec;
|
||||
point_values: [400]HostValue;
|
||||
band_codecs: [2]WidgetBandChartBandCodec;
|
||||
band_values: [2]HostValue;
|
||||
module_name: [64]u8;
|
||||
function_name: [64]u8;
|
||||
|
||||
Call :: (session: Session, module: string, function: string, arguments: []HostValue,
|
||||
result: *HostValue) -> s32 {
|
||||
unused CopyCString(module, module_name[:])
|
||||
unused CopyCString(function, function_name[:])
|
||||
return BandChartHostCall(session, *module_name[0], *function_name[0], *arguments[0],
|
||||
cast(s32)arguments.count, result)
|
||||
}
|
||||
|
||||
#program_export
|
||||
main :: () -> s32 {
|
||||
if !BandChartHostSupports("plot_band_chart", "BandChartHostedHost") ||
|
||||
BandChartHostSupports("band_chart", "BandChartNearest") {
|
||||
return 1
|
||||
}
|
||||
session := SessionOpen()
|
||||
bounds := Rectangle.{0.0, 0.0, 300.0, 120.0}
|
||||
BeginFrame(session, cast(u64)1, bounds)
|
||||
for index: cast(s32)0..399 {
|
||||
point := BandChartPoint.{.slot = index % 30, .value = cast(float32)(index % 7),
|
||||
.good = index % 2, .label = "Day"}
|
||||
point_values[index] = WidgetEncodeBandChartPoint(point, *point_codecs[index])
|
||||
}
|
||||
band_values[0] = WidgetEncodeBandChartBand(BandChartBand.{.low = 2.0, .has_low = true},
|
||||
*band_codecs[0])
|
||||
arguments: [4]HostValue
|
||||
arguments[0] = WidgetEncodeSession(session, *session_codec)
|
||||
arguments[1] = WidgetEncodeBandChartProps(BandChartProps.{.key = cast(u64)7,
|
||||
.bounds = bounds, .slots = 30, .min_value = 0.0, .max_value = 6.0, .bars = true,
|
||||
.top_label = "6", .bottom_label = "0", .font = 12,
|
||||
.good_color = Color.{0, 128, 0, 255}, .band_color = Color.{0, 128, 0, 40},
|
||||
.grid_color = Color.{0, 0, 0, 64}, .text_color = Color.{0, 0, 0, 255}},
|
||||
*props_codec)
|
||||
arguments[2] = HostValue.{.kind = cast(s32)HostSlice, .type_name = HostTextData("[]BandChartPoint"),
|
||||
.length = cast(usize)30, .elements = *point_values[0]}
|
||||
arguments[3] = HostValue.{.kind = cast(s32)HostSlice, .type_name = HostTextData("[]BandChartBand"),
|
||||
.length = cast(usize)1, .elements = *band_values[0]}
|
||||
result: HostValue
|
||||
before := PendingPaintCount(session)
|
||||
if Call(session, "plot_band_chart", "BandChartHostedHost", arguments[:], *result) != 1 ||
|
||||
!WidgetValidBandChartResult(result) || PendingPaintCount(session) <= before + 30 {
|
||||
return 2
|
||||
}
|
||||
// Another session, another module and too many points are refused.
|
||||
other := SessionOpen()
|
||||
if Call(other, "plot_band_chart", "BandChartHostedHost", arguments[:], *result) != 0 {
|
||||
return 3
|
||||
}
|
||||
if Call(session, "band_chart_widget", "BandChartHostedHost", arguments[:], *result) != 0 {
|
||||
return 4
|
||||
}
|
||||
arguments[2].length = cast(usize)367
|
||||
if Call(session, "band_chart", "BandChartHostedHost", arguments[:], *result) != 0 {
|
||||
return 5
|
||||
}
|
||||
arguments[2].length = cast(usize)30
|
||||
arguments[3].type_name = HostTextData("[]BandChartPoint")
|
||||
if Call(session, "band_chart", "BandChartHostedHost", arguments[:], *result) != 0 {
|
||||
return 6
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// What the band chart host test's frame paints, counted: text widths of
|
||||
// eight units a byte, and every shape and label the chart queues.
|
||||
#import "kryon/drawing_props"
|
||||
#import "kryon/geometry"
|
||||
|
||||
band_chart_host_shapes: s32;
|
||||
band_chart_host_labels: s32;
|
||||
|
||||
#program_export
|
||||
MeasureGlyphWidth :: (value: string, font: s32, typeface: string) -> s32 {
|
||||
unused font
|
||||
unused typeface
|
||||
return cast(s32)value.count * 8
|
||||
}
|
||||
|
||||
#program_export
|
||||
RasterRoundedRectangle :: (bounds: Rectangle, radius: float32, segments: s32, color: Color) {
|
||||
unused bounds
|
||||
unused radius
|
||||
unused segments
|
||||
unused color
|
||||
band_chart_host_shapes += 1
|
||||
}
|
||||
|
||||
#program_export
|
||||
RasterRoundedRectangleOutline :: (bounds: Rectangle, radius: float32, segments: s32,
|
||||
thickness: float32, color: Color) {
|
||||
unused bounds
|
||||
unused radius
|
||||
unused segments
|
||||
unused thickness
|
||||
unused color
|
||||
}
|
||||
|
||||
#program_export
|
||||
RasterLine :: (bounds: Rectangle, color: Color) {
|
||||
unused bounds
|
||||
unused color
|
||||
}
|
||||
|
||||
#program_export
|
||||
RasterText :: (text: string, x: s32, y: s32, font: s32, color: Color) {
|
||||
unused text
|
||||
unused x
|
||||
unused y
|
||||
unused font
|
||||
unused color
|
||||
band_chart_host_labels += 1
|
||||
}
|
||||
|
||||
#program_export
|
||||
RasterTextClipped :: (text: string, x: s32, y: s32, font: s32, color: Color, clip: Rectangle) {
|
||||
unused text
|
||||
unused x
|
||||
unused y
|
||||
unused font
|
||||
unused color
|
||||
unused clip
|
||||
band_chart_host_labels += 1
|
||||
}
|
||||
|
||||
#program_export
|
||||
RasterImage :: (path: string, texture_id: u32, source: Rectangle, destination: Rectangle,
|
||||
clip: Rectangle, origin: Vector2, rotation: float32, radius: float32, tint: Color) {
|
||||
unused path
|
||||
unused texture_id
|
||||
unused source
|
||||
unused destination
|
||||
unused clip
|
||||
unused origin
|
||||
unused rotation
|
||||
unused radius
|
||||
unused tint
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// A portable bundle's BandChart forwards its props, points and bands to the
|
||||
// host, and returns the point the host says shows its label.
|
||||
#import "kryon/session"
|
||||
#import "band_chart"
|
||||
#import "band_chart_props"
|
||||
#import "band_chart_hosted_provider"
|
||||
|
||||
#program_export
|
||||
main :: () -> s32 {
|
||||
session := Session.{.slot = 4, .epoch = cast(u64)12}
|
||||
points: [2]BandChartPoint = .[BandChartPoint.{.slot = 0, .value = 1.0, .good = 1,
|
||||
.label = "One"}, BandChartPoint.{.slot = 1, .value = 2.0}]
|
||||
bands: [1]BandChartBand = .[BandChartBand.{.low = 1.0, .has_low = true}]
|
||||
shown := BandChart(session, BandChartProps.{.slots = 7, .max_value = 2.0}, points[:],
|
||||
bands[:])
|
||||
if shown.shown != 1 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// The host side of the hosted band chart test: it checks what the bundle
|
||||
// sent and answers with the second point.
|
||||
#import "kryon/session"
|
||||
#import "band_chart_props"
|
||||
|
||||
#program_export "BandChartHostedHost"
|
||||
HostedBandChart :: (session: Session, props: BandChartProps, points: []BandChartPoint,
|
||||
bands: []BandChartBand) -> BandChartResult {
|
||||
if session.slot != 4 || props.slots != 7 || points.count != 2 ||
|
||||
points[0].label != "One" || bands.count != 1 || !bands[0].has_low {
|
||||
return BandChartResult.{.shown = -1}
|
||||
}
|
||||
return BandChartResult.{.shown = 1}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ TestSession :: () -> Session {
|
||||
#import "plot_props"
|
||||
#import "plot_widget"
|
||||
#import "band_chart"
|
||||
#import "band_chart_props"
|
||||
#import "kryon/tree"
|
||||
#import "kryon/widget_kind"
|
||||
|
||||
|
||||
Reference in new issue
Block a user