1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use geom::Polygon;
use crate::{
svg, Drawable, EventCtx, GeomBatch, GfxCtx, RewriteColor, ScreenDims, ScreenPt,
ScreenRectangle, Text, Widget, WidgetImpl, WidgetOutput,
};
pub struct JustDraw {
pub draw: Drawable,
pub top_left: ScreenPt,
pub dims: ScreenDims,
}
impl JustDraw {
pub(crate) fn wrap(ctx: &EventCtx, batch: GeomBatch) -> Widget {
Widget::new(Box::new(JustDraw {
dims: batch.get_dims(),
draw: ctx.upload(batch),
top_left: ScreenPt::new(0.0, 0.0),
}))
}
pub fn svg(ctx: &EventCtx, filename: String) -> Widget {
let (batch, bounds) = svg::load_svg(ctx.prerender, &filename);
Widget::new(Box::new(JustDraw {
dims: ScreenDims::new(bounds.width(), bounds.height()),
draw: ctx.upload(batch),
top_left: ScreenPt::new(0.0, 0.0),
}))
}
pub fn svg_transform(ctx: &EventCtx, filename: &str, rewrite: RewriteColor) -> Widget {
let (batch, bounds) = svg::load_svg(ctx.prerender, filename);
let batch = batch.color(rewrite);
Widget::new(Box::new(JustDraw {
dims: ScreenDims::new(bounds.width(), bounds.height()),
draw: ctx.upload(batch),
top_left: ScreenPt::new(0.0, 0.0),
}))
}
}
impl WidgetImpl for JustDraw {
fn get_dims(&self) -> ScreenDims {
self.dims
}
fn set_pos(&mut self, top_left: ScreenPt) {
self.top_left = top_left;
}
fn event(&mut self, _: &mut EventCtx, _: &mut WidgetOutput) {}
fn draw(&self, g: &mut GfxCtx) {
g.redraw_at(self.top_left, &self.draw);
}
}
pub struct DrawWithTooltips {
draw: Drawable,
tooltips: Vec<(Polygon, Text)>,
hover: Box<dyn Fn(&Polygon) -> GeomBatch>,
top_left: ScreenPt,
dims: ScreenDims,
}
impl DrawWithTooltips {
pub fn new(
ctx: &EventCtx,
batch: GeomBatch,
tooltips: Vec<(Polygon, Text)>,
hover: Box<dyn Fn(&Polygon) -> GeomBatch>,
) -> Widget {
Widget::new(Box::new(DrawWithTooltips {
dims: batch.get_dims(),
top_left: ScreenPt::new(0.0, 0.0),
hover,
draw: ctx.upload(batch),
tooltips,
}))
}
}
impl WidgetImpl for DrawWithTooltips {
fn get_dims(&self) -> ScreenDims {
self.dims
}
fn set_pos(&mut self, top_left: ScreenPt) {
self.top_left = top_left;
}
fn event(&mut self, _: &mut EventCtx, _: &mut WidgetOutput) {}
fn draw(&self, g: &mut GfxCtx) {
g.redraw_at(self.top_left, &self.draw);
if let Some(cursor) = g.canvas.get_cursor_in_screen_space() {
if !ScreenRectangle::top_left(self.top_left, self.dims).contains(cursor) {
return;
}
let translated =
ScreenPt::new(cursor.x - self.top_left.x, cursor.y - self.top_left.y).to_pt();
for (region, txt) in &self.tooltips {
if region.contains_pt(translated) {
let extra = g.upload((self.hover)(region));
g.redraw_at(self.top_left, &extra);
g.draw_mouse_tooltip(txt.clone());
return;
}
}
}
}
}
pub struct DeferDraw {
pub batch: GeomBatch,
pub top_left: ScreenPt,
dims: ScreenDims,
}
impl DeferDraw {
pub fn new(batch: GeomBatch) -> Widget {
Widget::new(Box::new(DeferDraw {
dims: batch.get_dims(),
batch,
top_left: ScreenPt::new(0.0, 0.0),
}))
}
}
impl WidgetImpl for DeferDraw {
fn get_dims(&self) -> ScreenDims {
self.dims
}
fn set_pos(&mut self, top_left: ScreenPt) {
self.top_left = top_left;
}
fn event(&mut self, _: &mut EventCtx, _: &mut WidgetOutput) {
unreachable!()
}
fn draw(&self, _: &mut GfxCtx) {
unreachable!()
}
}