use crate::render::callbacks::CallbackRegistry;
use crate::render::widget::*;
use crate::render::widget_cache::WidgetContainer;
use crate::render::widget_config::*;
use crate::render::{
make_points, make_size, Points, Size, POINT_X, POINT_Y, SIZE_HEIGHT, SIZE_WIDTH,
};
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;
use crate::render::layout_cache::LayoutContainer;
use crate::render::texture_cache::TextureCache;
use crate::render::texture_store::TextureStore;
use crate::render::widget_config::CompassPosition::Center;
use crate::widgets::image_widget::ImageWidget;
use crate::widgets::text_widget::{TextJustify, TextWidget};
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use std::any::Any;
use std::collections::HashMap;
pub type OnClickCallbackType =
Option<Box<dyn FnMut(&mut ImageButtonWidget, &[WidgetContainer], &[LayoutContainer])>>;
pub struct ImageButtonWidget {
config: WidgetConfig,
system_properties: HashMap<i32, String>,
callback_registry: CallbackRegistry,
texture_store: TextureStore,
base_widget: BaseWidget,
text_widget: TextWidget,
image_widget: ImageWidget,
active: bool,
in_bounds: bool,
originated: bool,
on_click: OnClickCallbackType,
}
impl ImageButtonWidget {
pub fn new(
points: Points,
size: Size,
text: String,
font_size: i32,
image_name: String,
) -> Self {
let mut base_widget = BaseWidget::new(points.clone(), size.clone());
let mut text_widget = TextWidget::new(
String::from("assets/OpenSans-Regular.ttf"),
sdl2::ttf::FontStyle::NORMAL,
font_size,
TextJustify::Left,
text,
make_points(
points[POINT_X] + size[SIZE_HEIGHT] as i32 + 6,
points[POINT_Y] + 2,
),
make_size(
size[SIZE_WIDTH] - size[SIZE_HEIGHT] - 10,
size[SIZE_HEIGHT] - 4,
),
);
let mut image_widget = ImageWidget::new(
image_name,
make_points(points[POINT_X] + 2, points[POINT_Y] + 2),
make_size(size[SIZE_HEIGHT] - 4, size[SIZE_HEIGHT] - 4),
false,
);
base_widget.set_color(CONFIG_COLOR_BASE, Color::RGB(255, 255, 255));
text_widget.set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
image_widget.set_compass(CONFIG_IMAGE_POSITION, Center);
Self {
config: WidgetConfig::new(points, size),
system_properties: HashMap::new(),
callback_registry: CallbackRegistry::new(),
texture_store: TextureStore::default(),
base_widget,
text_widget,
image_widget,
active: false,
in_bounds: false,
originated: false,
on_click: None,
}
}
fn draw_hovered(&mut self) {
self.base_widget
.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
self.text_widget
.set_color(CONFIG_COLOR_TEXT, Color::RGB(255, 255, 255));
self.text_widget
.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
self.get_config().set_invalidated(true);
}
fn draw_unhovered(&mut self) {
self.base_widget
.set_color(CONFIG_COLOR_BASE, Color::RGB(255, 255, 255));
self.text_widget
.set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
self.text_widget
.set_color(CONFIG_COLOR_BASE, Color::RGB(255, 255, 255));
self.get_config().set_invalidated(true);
}
pub fn on_click<F>(&mut self, callback: F)
where
F: FnMut(&mut ImageButtonWidget, &[WidgetContainer], &[LayoutContainer]) + 'static,
{
self.on_click = Some(Box::new(callback));
}
fn call_click_callback(&mut self, widgets: &[WidgetContainer], layouts: &[LayoutContainer]) {
if let Some(mut cb) = self.on_click.take() {
cb(self, widgets, layouts);
self.on_click = Some(cb);
}
}
}
impl Widget for ImageButtonWidget {
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);
let base_color = self.get_color(CONFIG_COLOR_BASE);
self.texture_store
.create_or_resize_texture(c, bounds[0] as u32, bounds[1] as u32);
let base_widget_texture = self.base_widget.draw(c, t).unwrap();
let text_widget_texture = self.text_widget.draw(c, t).unwrap();
let image_widget_texture = self.image_widget.draw(c, t).unwrap();
c.with_texture_canvas(self.texture_store.get_mut_ref(), |texture| {
texture.set_draw_color(base_color);
texture.clear();
texture
.copy(
base_widget_texture,
None,
Rect::new(0, 0, bounds[0], bounds[1]),
)
.unwrap();
texture
.copy(
text_widget_texture,
None,
Rect::new(
2 + bounds[1] as i32 + 6,
2,
bounds[0] - bounds[1] - 10,
bounds[1] - 4,
),
)
.unwrap();
texture
.copy(
image_widget_texture,
None,
Rect::new(2, 2, bounds[1] - 4, bounds[1] - 4),
)
.unwrap();
})
.unwrap();
}
self.texture_store.get_optional_ref()
}
fn mouse_entered(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer]) {
if self.active {
self.draw_hovered();
}
self.in_bounds = true;
self.mouse_entered_callback(_widgets, _layouts);
}
fn mouse_exited(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer]) {
if self.active {
self.draw_unhovered();
}
self.in_bounds = false;
self.mouse_exited_callback(_widgets, _layouts);
}
fn button_clicked(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
_button: u8,
_clicks: u8,
_state: bool,
) {
if _button == 1 {
if _state {
self.draw_hovered();
self.active = true;
self.originated = true;
} else {
let had_bounds = self.active;
self.draw_unhovered();
self.active = false;
if self.in_bounds && had_bounds && self.originated {
self.call_click_callback(_widgets, _layouts);
}
self.originated = false;
}
}
self.button_clicked_callback(_widgets, _layouts, _button, _clicks, _state);
}
default_widget_functions!();
default_widget_properties!();
default_widget_callbacks!();
}