use crate::render::callbacks::CallbackRegistry;
use crate::render::widget::*;
use crate::render::widget_cache::WidgetContainer;
use crate::render::widget_config::{
WidgetConfig, CONFIG_BORDER_WIDTH, CONFIG_COLOR_BASE, CONFIG_COLOR_BORDER, CONFIG_COLOR_TEXT,
CONFIG_IMAGE_POSITION,
};
use crate::render::Points;
use sdl2::render::Canvas;
use sdl2::video::Window;
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 std::collections::HashMap;
pub type OnClickCallbackType = Option<Box<dyn FnMut(&mut ImageButtonWidget, &[WidgetContainer])>>;
pub struct ImageButtonWidget {
config: WidgetConfig,
system_properties: HashMap<i32, String>,
callback_registry: CallbackRegistry,
base_widget: BaseWidget,
text_widget: TextWidget,
image_widget: ImageWidget,
active: bool,
in_bounds: bool,
on_click: OnClickCallbackType,
}
impl ImageButtonWidget {
pub fn new(
x: i32,
y: i32,
w: u32,
h: u32,
text: String,
font_size: i32,
image_name: String,
) -> Self {
let mut base_widget = BaseWidget::new(x, y, w, h);
let mut text_widget = TextWidget::new(
String::from("assets/OpenSans-Regular.ttf"),
sdl2::ttf::FontStyle::NORMAL,
font_size,
TextJustify::Left,
text.clone(),
x + h as i32 + 6,
y + 2,
w - h - 10,
h - 4,
);
let mut image_widget = ImageWidget::new(image_name, x + 2, y + 2, h - 4, h - 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(x, y, w, h),
system_properties: HashMap::new(),
callback_registry: CallbackRegistry::new(),
base_widget,
text_widget,
image_widget,
active: false,
in_bounds: 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_invalidate(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_invalidate(true);
}
pub fn on_click<F>(&mut self, callback: F)
where
F: FnMut(&mut ImageButtonWidget, &[WidgetContainer]) + 'static,
{
self.on_click = Some(Box::new(callback));
}
fn call_click_callback(&mut self, widgets: &[WidgetContainer]) {
if let Some(mut cb) = self.on_click.take() {
cb(self, widgets);
self.on_click = Some(cb);
}
}
}
impl Widget for ImageButtonWidget {
fn draw(&mut self, c: &mut Canvas<Window>) {
self.base_widget.draw(c);
self.text_widget.draw(c);
self.image_widget.draw(c);
}
fn mouse_entered(&mut self, _widgets: &[WidgetContainer]) {
if self.active {
self.draw_hovered();
}
self.in_bounds = true;
self.mouse_entered_callback(_widgets);
}
fn mouse_exited(&mut self, _widgets: &[WidgetContainer]) {
if self.active {
self.draw_unhovered();
}
self.in_bounds = false;
self.mouse_exited_callback(_widgets);
}
fn button_clicked(
&mut self,
_widgets: &[WidgetContainer],
_button: u8,
_clicks: u8,
_state: bool,
) {
if _button == 1 {
if _state {
self.draw_hovered();
self.active = true;
} else {
let had_bounds = self.active;
self.draw_unhovered();
self.active = false;
if self.in_bounds && had_bounds {
eprintln!("Call callback here: clicks={}", _clicks);
self.call_click_callback(_widgets);
}
}
}
self.button_clicked_callback(_widgets, _button, _clicks, _state);
}
default_widget_properties!();
default_widget_callbacks!();
}