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
use crate::render::callbacks::CallbackRegistry;
use crate::render::layout_cache::LayoutContainer;
use crate::render::widget::*;
use crate::render::widget_cache::WidgetContainer;
use crate::render::widget_config::{
CompassPosition, Config, WidgetConfig, CONFIG_COLOR_BASE, CONFIG_IMAGE_POSITION, CONFIG_SIZE,
};
use crate::render::{Points, Size};
use sdl2::image::LoadTexture;
use sdl2::rect::Rect;
use sdl2::render::{Canvas, TextureQuery};
use sdl2::video::Window;
use std::any::Any;
use std::collections::HashMap;
use std::path::Path;
pub struct ImageWidget {
config: WidgetConfig,
system_properties: HashMap<i32, String>,
callback_registry: CallbackRegistry,
image_name: String,
scaled: bool,
}
impl ImageWidget {
pub fn new(image_name: String, points: Points, size: Size, scaled: bool) -> Self {
Self {
config: WidgetConfig::new(points.clone(), size.clone()),
system_properties: HashMap::new(),
callback_registry: CallbackRegistry::new(),
image_name,
scaled,
}
}
}
impl Widget for ImageWidget {
fn draw(&mut self, c: &mut Canvas<Window>) {
let base_color = self.get_color(CONFIG_COLOR_BASE);
c.set_draw_color(base_color);
c.fill_rect(self.get_drawing_area()).unwrap();
let texture_creator = c.texture_creator();
let texture = texture_creator
.load_texture(Path::new(&self.image_name))
.unwrap();
let widget_w = self.get_size(CONFIG_SIZE)[0] as i32;
let widget_h = self.get_size(CONFIG_SIZE)[1] as i32;
let TextureQuery { width, height, .. } = texture.query();
let texture_x = match self.get_compass(CONFIG_IMAGE_POSITION) {
CompassPosition::NW | CompassPosition::W | CompassPosition::SW => {
self.get_config().to_x(0)
}
CompassPosition::N | CompassPosition::Center | CompassPosition::S => {
self.get_config().to_x((widget_w - width as i32) / 2)
}
CompassPosition::NE | CompassPosition::E | CompassPosition::SE => {
self.get_config().to_x(widget_w - width as i32)
}
};
let texture_y = match self.get_compass(CONFIG_IMAGE_POSITION) {
CompassPosition::NW | CompassPosition::N | CompassPosition::NE => {
self.get_config().to_y(0)
}
CompassPosition::W | CompassPosition::Center | CompassPosition::E => {
self.get_config().to_y((widget_h - height as i32) / 2)
}
CompassPosition::SW | CompassPosition::S | CompassPosition::SE => {
self.get_config().to_y(widget_h - height as i32)
}
};
if !self.scaled {
c.copy(
&texture,
None,
Rect::new(texture_x, texture_y, width, height),
)
.unwrap();
} else {
c.copy(
&texture,
None,
Rect::new(
self.get_config().to_x(0),
self.get_config().to_y(0),
widget_w as u32,
widget_h as u32,
),
)
.unwrap();
}
}
fn on_config_changed(&mut self, _k: u8, _v: Config) {
if _k == CONFIG_IMAGE_POSITION {
self.get_config().set_invalidated(true);
}
}
default_widget_functions!();
default_widget_properties!();
default_widget_callbacks!();
}