use crate::data::IdxSpan;
use crate::geom::{PointPx, Px, RectPx, Rgba, SizePx, Span};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Invalidation {
#[default]
None,
Cursor,
Light,
Data,
Full,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct FontId(pub u16);
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct FontStyleRef {
pub font: FontId,
pub size: f32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TextAlign {
#[default]
Left,
Center,
Right,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LabelBg {
pub color: Rgba,
pub padding: Px,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TextRun {
pub pos: PointPx,
pub text: Span,
pub style: FontStyleRef,
pub color: Rgba,
pub align: TextAlign,
pub bg: Option<LabelBg>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RectInst {
pub rect: RectPx,
pub color: Rgba,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LineDash {
#[default]
Solid,
Dashed,
Dotted,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct StrokeStyle {
pub width: Px,
pub color: Rgba,
pub dash: LineDash,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LineInst {
pub from: PointPx,
pub to: PointPx,
pub stroke: StrokeStyle,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct IconInst {
pub pos: PointPx,
pub size: Px,
pub icon: u16,
pub color: Rgba,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SeriesKind {
Candlestick,
OhlcBar,
HeikinAshi,
Line,
Area,
Baseline,
Histogram,
Points,
Band,
Volume,
Custom,
Kagi,
PointFigure,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct SeriesStyleRef(pub u32);
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SeriesStyle {
pub up: Rgba,
pub down: Rgba,
pub up_wick: Rgba,
pub down_wick: Rgba,
pub up_border: Rgba,
pub down_border: Rgba,
pub line_width: Px,
pub line_dash: LineDash,
pub step: bool,
pub fill_top: Rgba,
pub fill_bottom: Rgba,
pub base_value: f64,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct XTransform {
pub base: i64,
pub step: f32,
pub offset: f32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum PriceMapping {
#[default]
Linear,
Log,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct YTransform {
pub a: f32,
pub b: f32,
pub view_ref: f64,
pub mapping: PriceMapping,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SeriesDraw {
pub series: crate::data::SeriesId,
pub generation: u64,
pub style: SeriesStyleRef,
pub kind: SeriesKind,
pub lod_level: u8,
pub index_range: IdxSpan,
pub x_transform: XTransform,
pub y_transform: YTransform,
pub clip: RectPx,
pub pair: Option<crate::data::SeriesId>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CustomDraw {
pub series: crate::data::SeriesId,
pub generation: u64,
pub kind: &'static str,
pub points: IdxSpan,
pub x_transform: XTransform,
pub y_transform: YTransform,
pub style: SeriesStyleRef,
pub params: [f32; 8],
pub clip: RectPx,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DrawCmd {
PushClip(RectPx),
PopClip,
Series(SeriesDraw),
Rects(Span),
Lines(Span),
Polyline {
points: Span,
style: StrokeStyle,
},
Polygon {
points: Span,
fill: Rgba,
},
Text(Span),
Icon(Span),
Custom(CustomDraw),
}
#[derive(Clone, Debug, Default)]
pub struct FramePools {
pub rects: Vec<RectInst>,
pub lines: Vec<LineInst>,
pub points: Vec<PointPx>,
pub text_runs: Vec<TextRun>,
pub text: String,
pub icons: Vec<IconInst>,
}
impl FramePools {
fn clear(&mut self) {
self.rects.clear();
self.lines.clear();
self.points.clear();
self.text_runs.clear();
self.text.clear();
self.icons.clear();
}
pub fn text_of(&self, span: Span) -> &str {
self.text.get(span.range()).unwrap_or("")
}
}
#[derive(Clone, Debug, Default)]
pub struct Layer {
pub revision: u64,
pub cmds: Vec<DrawCmd>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LayerId {
Background = 0,
Main = 1,
Overlay = 2,
}
#[derive(Clone, Debug, Default)]
pub struct Frame {
pub size_px: SizePx,
pub clear_color: Rgba,
pub layers: [Layer; 3],
pub invalidation: Invalidation,
pub pools: FramePools,
pub styles: Vec<SeriesStyle>,
}
fn span_of(start: usize, len: usize) -> Span {
Span {
start: u32::try_from(start).unwrap_or(u32::MAX),
len: u32::try_from(len).unwrap_or(0),
}
}
impl Frame {
pub fn begin(&mut self, size: SizePx, clear: Rgba, invalidation: Invalidation) {
self.size_px = size;
self.clear_color = clear;
self.invalidation = invalidation;
self.pools.clear();
self.styles.clear();
for layer in &mut self.layers {
layer.cmds.clear();
}
}
pub fn reserve(&mut self) {
let _ = self.pools.rects.try_reserve(256);
let _ = self.pools.lines.try_reserve(512);
let _ = self.pools.text_runs.try_reserve(512);
let _ = self.pools.text.try_reserve(8192);
let _ = self.pools.icons.try_reserve(256);
let _ = self.styles.try_reserve(16);
for layer in &mut self.layers {
let _ = layer.cmds.try_reserve(64);
}
}
pub fn push_cmd(&mut self, layer: LayerId, cmd: DrawCmd) {
self.layers[layer as usize].cmds.push(cmd);
}
pub fn push_style(&mut self, style: SeriesStyle) -> SeriesStyleRef {
self.styles.push(style);
SeriesStyleRef(u32::try_from(self.styles.len() - 1).unwrap_or(u32::MAX))
}
pub fn push_rect(&mut self, layer: LayerId, rect: RectInst) {
let start = self.pools.rects.len();
self.pools.rects.push(rect);
let cmds = &mut self.layers[layer as usize].cmds;
if let Some(DrawCmd::Rects(s)) = cmds.last_mut()
&& s.range().end == start
{
s.len += 1;
return;
}
cmds.push(DrawCmd::Rects(span_of(start, 1)));
}
pub fn push_line(&mut self, layer: LayerId, line: LineInst) {
let start = self.pools.lines.len();
self.pools.lines.push(line);
let cmds = &mut self.layers[layer as usize].cmds;
if let Some(DrawCmd::Lines(s)) = cmds.last_mut()
&& s.range().end == start
{
s.len += 1;
return;
}
cmds.push(DrawCmd::Lines(span_of(start, 1)));
}
pub fn push_text(&mut self, layer: LayerId, run: TextRun) {
let start = self.pools.text_runs.len();
self.pools.text_runs.push(run);
let cmds = &mut self.layers[layer as usize].cmds;
if let Some(DrawCmd::Text(s)) = cmds.last_mut()
&& s.range().end == start
{
s.len += 1;
return;
}
cmds.push(DrawCmd::Text(span_of(start, 1)));
}
pub fn push_icon(&mut self, layer: LayerId, icon: IconInst) {
let start = self.pools.icons.len();
self.pools.icons.push(icon);
let cmds = &mut self.layers[layer as usize].cmds;
if let Some(DrawCmd::Icon(s)) = cmds.last_mut()
&& s.range().end == start
{
s.len += 1;
return;
}
cmds.push(DrawCmd::Icon(span_of(start, 1)));
}
pub fn push_str(&mut self, s: &str) -> Span {
let start = self.pools.text.len();
self.pools.text.push_str(s);
span_of(start, s.len())
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Reveal {
pub progress: f32,
pub pen: Option<Rgba>,
}
impl Reveal {
pub fn amount(self) -> f32 {
if self.progress.is_nan() {
0.0
} else {
self.progress.clamp(0.0, 1.0)
}
}
pub fn is_done(self) -> bool {
self.amount() >= 1.0
}
pub fn clip(self, plot: RectPx) -> RectPx {
RectPx {
w: (plot.w * self.amount()).round(),
..plot
}
}
pub fn push_pen(self, frame: &mut Frame, layer: LayerId, plot: RectPx, dpr: f32) {
let Some(color) = self.pen else {
return;
};
let a = self.amount();
if a <= 0.0 || a >= 1.0 || plot.w <= 0.0 || plot.h <= 0.0 {
return;
}
let x = plot.x + self.clip(plot).w;
frame.push_cmd(layer, DrawCmd::PushClip(plot));
for (width, alpha) in [(14.0, 0.10), (5.0, 0.28), (1.5, 1.0)] {
let alpha = (f32::from(color.a) * alpha).round() as u8;
frame.push_line(
layer,
LineInst {
from: PointPx { x, y: plot.y },
to: PointPx {
x,
y: plot.bottom(),
},
stroke: StrokeStyle {
width: Px((width * dpr).max(1.0)),
color: color.with_alpha(alpha),
dash: LineDash::Solid,
},
},
);
}
frame.push_cmd(layer, DrawCmd::PopClip);
}
}