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::{make_size, Points, Size};
use sdl2::rect::Rect;
use sdl2::render::{Canvas, Texture, TextureQuery};
use sdl2::video::Window;
use crate::render::texture_cache::TextureCache;
use crate::render::texture_store::TextureStore;
use std::any::Any;
use std::collections::HashMap;
pub struct ImageWidget {
config: WidgetConfig,
system_properties: HashMap<i32, String>,
callback_registry: CallbackRegistry,
texture_store: TextureStore,
image_name: String,
scaled: bool,
texture_sizes: Size,
}
impl ImageWidget {
pub fn new(image_name: String, points: Points, size: Size, scaled: bool) -> Self {
Self {
config: WidgetConfig::new(points, size),
system_properties: HashMap::new(),
callback_registry: CallbackRegistry::new(),
texture_store: TextureStore::default(),
image_name,
scaled,
texture_sizes: make_size(0, 0),
}
}
pub fn get_texture_size(&self) -> Size {
self.texture_sizes.clone()
}
}
impl Widget for ImageWidget {
fn draw(&mut self, c: &mut Canvas<Window>, t: &mut TextureCache) -> Option<&Texture> {
if self.get_config().invalidated() {
let bounds = self.get_config().get_size(CONFIG_SIZE);
self.texture_store
.create_or_resize_texture(c, bounds[0] as u32, bounds[1] as u32);
let base_color = self.get_color(CONFIG_COLOR_BASE);
let image_texture = t.get_image(c, self.image_name.clone());
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, .. } = image_texture.query();
let scaled = self.scaled;
self.texture_sizes = make_size(width, height);
let texture_x = match self.get_compass(CONFIG_IMAGE_POSITION) {
CompassPosition::NW | CompassPosition::W | CompassPosition::SW => 0,
CompassPosition::N | CompassPosition::Center | CompassPosition::S => {
(widget_w - width as i32) / 2
}
CompassPosition::NE | CompassPosition::E | CompassPosition::SE => {
widget_w - width as i32
}
};
let texture_y = match self.get_compass(CONFIG_IMAGE_POSITION) {
CompassPosition::NW | CompassPosition::N | CompassPosition::NE => 0,
CompassPosition::W | CompassPosition::Center | CompassPosition::E => {
(widget_h - height as i32) / 2
}
CompassPosition::SW | CompassPosition::S | CompassPosition::SE => {
widget_h - height as i32
}
};
c.with_texture_canvas(self.texture_store.get_mut_ref(), |texture| {
texture.set_draw_color(base_color);
texture.clear();
if !scaled {
texture
.copy(
image_texture,
None,
Rect::new(texture_x, texture_y, width, height),
)
.unwrap();
} else {
texture
.copy(
image_texture,
None,
Rect::new(0, 0, widget_w as u32, widget_h as u32),
)
.unwrap();
}
})
.unwrap();
}
self.texture_store.get_optional_ref()
}
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!();
}