use crate::render::{Points, Size};
use sdl2::pixels::Color;
use std::collections::HashMap;
pub const CONFIG_COLOR_BASE: u8 = 0;
pub const CONFIG_COLOR_HOVER: u8 = 1;
pub const CONFIG_COLOR_BORDER: u8 = 2;
pub const CONFIG_COLOR_TEXT: u8 = 3;
pub const CONFIG_COLOR_SELECTED: u8 = 4;
pub const CONFIG_COLOR_SECONDARY: u8 = 5;
pub const CONFIG_ORIGIN: u8 = 6;
pub const CONFIG_SIZE: u8 = 7;
pub const CONFIG_BORDER_WIDTH: u8 = 8;
pub const CONFIG_TEXT: u8 = 9;
pub const CONFIG_IMAGE_POSITION: u8 = 11;
pub const CONFIG_FONT_SIZE: u8 = 12;
pub const CONFIG_SELECTED_STATE: u8 = 13;
#[derive(Clone, Debug, Copy)]
pub enum CompassPosition {
NW,
N,
NE,
W,
Center,
E,
SW,
S,
SE,
}
#[derive(Clone, Default, Debug, Copy)]
pub struct PaddingConstraint {
pub top: i32,
pub bottom: i32,
pub left: i32,
pub right: i32,
pub spacing: i32,
}
impl PaddingConstraint {
pub fn new(top: i32, bottom: i32, left: i32, right: i32, spacing: i32) -> Self {
Self {
top,
bottom,
left,
right,
spacing,
}
}
}
#[derive(Clone, Debug)]
pub enum Config {
Points(Points),
Size(Size),
Color(Color),
Numeric(i32),
Text(String),
Toggle(bool),
CompassPosition(CompassPosition),
PaddingConstraint(PaddingConstraint),
}
pub struct WidgetConfig {
pub config: HashMap<u8, Config>,
hidden: bool,
enabled: bool,
invalidated: bool,
}
impl WidgetConfig {
pub fn new(points: Points, size: Size) -> Self {
Self {
config: [
(CONFIG_ORIGIN, Config::Points(points)),
(CONFIG_SIZE, Config::Size(size)),
(CONFIG_COLOR_BASE, Config::Color(Color::RGB(255, 255, 255))),
(CONFIG_BORDER_WIDTH, Config::Numeric(0)),
]
.iter()
.cloned()
.collect(),
hidden: false,
enabled: true,
invalidated: true,
}
}
pub fn to_x(&self, x: i32) -> i32 {
self.get_point(CONFIG_ORIGIN)[0] + x
}
pub fn to_y(&self, y: i32) -> i32 {
self.get_point(CONFIG_ORIGIN)[1] + y
}
pub fn set_invalidated(&mut self, flag: bool) {
self.invalidated = flag;
}
pub fn invalidated(&self) -> bool {
self.invalidated
}
pub fn enable(&mut self) {
self.enabled = true;
self.invalidated = true;
}
pub fn disable(&mut self) {
self.enabled = false;
self.invalidated = true;
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn hide(&mut self) {
self.hidden = true;
self.invalidated = true;
}
pub fn show(&mut self) {
self.hidden = false;
self.invalidated = true;
}
pub fn is_hidden(&self) -> bool {
self.hidden
}
pub fn set_point(&mut self, config: u8, x: i32, y: i32) {
self.config.insert(config, Config::Points(vec![x, y]));
}
pub fn set_size(&mut self, config: u8, w: u32, h: u32) {
self.config.insert(config, Config::Size(vec![w, h]));
}
pub fn set_color(&mut self, config: u8, color: Color) {
self.config.insert(config, Config::Color(color));
}
pub fn set_numeric(&mut self, config: u8, value: i32) {
self.config.insert(config, Config::Numeric(value));
}
pub fn set_text(&mut self, config: u8, text: String) {
self.config.insert(config, Config::Text(text));
}
pub fn set_toggle(&mut self, config: u8, flag: bool) {
self.config.insert(config, Config::Toggle(flag));
}
pub fn set_compass(&mut self, config: u8, value: CompassPosition) {
self.config.insert(config, Config::CompassPosition(value));
}
pub fn set_padding(&mut self, config: u8, value: PaddingConstraint) {
self.config.insert(config, Config::PaddingConstraint(value));
}
pub fn get_point(&self, k: u8) -> Points {
match self.config.get(&k) {
Some(Config::Points(point)) => point.clone(),
_ => Points::default(),
}
}
pub fn get_size(&self, k: u8) -> Size {
match self.config.get(&k) {
Some(Config::Size(size)) => size.clone(),
_ => Size::default(),
}
}
pub fn get_color(&self, k: u8) -> Color {
match self.config.get(&k) {
Some(Config::Color(color)) => *color,
_ => Color::RGB(255, 255, 255),
}
}
pub fn get_numeric(&self, k: u8) -> i32 {
match self.config.get(&k) {
Some(Config::Numeric(numeric)) => *numeric,
_ => 0,
}
}
pub fn get_text(&self, k: u8) -> String {
match self.config.get(&k) {
Some(Config::Text(text)) => text.clone(),
_ => String::from(""),
}
}
pub fn get_toggle(&self, k: u8) -> bool {
match self.config.get(&k) {
Some(Config::Toggle(toggle)) => *toggle,
_ => false,
}
}
pub fn get_compass(&self, k: u8) -> CompassPosition {
match self.config.get(&k) {
Some(Config::CompassPosition(position)) => *position,
_ => CompassPosition::W,
}
}
pub fn get_padding(&self, k: u8) -> PaddingConstraint {
match self.config.get(&k) {
Some(Config::PaddingConstraint(constraint)) => *constraint,
_ => PaddingConstraint::default(),
}
}
}