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
use graphics;
use graphics::character::CharacterCache;
use graphics::draw_state::DrawState;
use graphics::*;
use opengl_graphics::{GlGraphics, GlyphCache, TextureSettings};
use crate::widget::config::*;
use crate::widget::widget::*;
pub enum TextJustify {
Left,
Center,
Right,
}
pub struct TextWidget {
config: Configurable,
font_cache: GlyphCache<'static>,
font_size: u32,
justify: TextJustify,
pub desired_width: i32,
need_text_resize: bool,
}
impl TextWidget {
pub fn new(font_name: String, text: String, font_size: u32, justify: TextJustify) -> Self {
let mut configurable = Configurable::new();
let cache = GlyphCache::new(font_name.clone(), (), TextureSettings::new()).unwrap();
configurable.set(CONFIG_DISPLAY_TEXT, Config::Text(text.clone()));
Self {
config: configurable,
font_cache: cache,
font_size,
justify,
desired_width: 0 as i32,
need_text_resize: true,
}
}
fn recalculate_desired_size(&mut self) {
let text = self.config().get_text(CONFIG_DISPLAY_TEXT).clone();
let mut width = 0.0;
for ch in text.chars() {
let character = self.font_cache.character(self.font_size, ch).unwrap();
width += character.width();
}
self.desired_width = width as i32;
self.need_text_resize = false;
}
fn draw_text(&mut self, c: Context, g: &mut GlGraphics, clip: &DrawState) {
let size: crate::core::point::Size = self.config().get_size(CONFIG_BODY_SIZE);
let start_x = match self.justify {
TextJustify::Left => 0.0,
TextJustify::Center => ((size.w - self.desired_width) / 2) as f64,
TextJustify::Right => (size.w - self.desired_width) as f64,
};
let start_y = (self.font_size - 2 + size.h as u32) / 2 - 1;
text::Text::new_color(self.config().get_color(CONFIG_TEXT_COLOR), self.font_size)
.draw(
self.config().get_text(CONFIG_DISPLAY_TEXT).as_str(),
&mut self.font_cache,
clip,
c.transform.trans(start_x, start_y as f64),
g,
)
.unwrap();
}
}
impl Widget for TextWidget {
fn config(&mut self) -> &mut Configurable {
&mut self.config
}
fn set_config(&mut self, config: u8, config_value: Config) {
self.config().set(config, config_value.clone());
self.invalidate();
}
fn set_text(&mut self, config: u8, text: String) {
self.set_config(config, Config::Text(text.clone()));
self.need_text_resize = true;
self.invalidate();
}
fn draw(&mut self, c: Context, g: &mut GlGraphics, clip: &DrawState) {
if self.need_text_resize {
self.recalculate_desired_size();
}
let size = self.config().get_size(CONFIG_BODY_SIZE);
g.rectangle(
&Rectangle::new(self.config().get_color(CONFIG_MAIN_COLOR)),
[0.0f64, 0.0f64, size.w as f64, size.h as f64],
clip,
c.transform,
);
self.draw_text(c, g, &clip);
self.clear_invalidate();
}
}