2020-02-03 22:14:17 +03:00
|
|
|
use crate::colors;
|
2020-02-04 23:52:47 +03:00
|
|
|
use crate::game::{DrawBaselayer, State, Transition};
|
2019-11-24 18:21:23 +03:00
|
|
|
use crate::ui::UI;
|
2020-01-22 21:41:58 +03:00
|
|
|
use ezgui::{
|
|
|
|
Button, Color, Composite, EventCtx, GfxCtx, Line, ManagedWidget, MultiKey, Outcome,
|
|
|
|
RewriteColor, Text,
|
|
|
|
};
|
2019-12-17 01:13:08 +03:00
|
|
|
use std::collections::HashMap;
|
2019-11-24 18:21:23 +03:00
|
|
|
|
2019-12-23 07:00:19 +03:00
|
|
|
pub type Callback = Box<dyn Fn(&mut EventCtx, &mut UI) -> Option<Transition>>;
|
2019-11-24 18:21:23 +03:00
|
|
|
|
2020-01-22 21:41:58 +03:00
|
|
|
pub enum WrappedOutcome {
|
2019-12-21 02:56:04 +03:00
|
|
|
Transition(Transition),
|
|
|
|
Clicked(String),
|
2019-11-30 21:09:31 +03:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:41:58 +03:00
|
|
|
pub struct WrappedComposite {
|
|
|
|
pub inner: Composite,
|
2019-12-21 02:56:04 +03:00
|
|
|
callbacks: HashMap<String, Callback>,
|
2019-11-30 21:09:31 +03:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:41:58 +03:00
|
|
|
impl WrappedComposite {
|
|
|
|
pub fn new(inner: Composite) -> WrappedComposite {
|
|
|
|
WrappedComposite {
|
2019-12-21 02:56:04 +03:00
|
|
|
inner,
|
|
|
|
callbacks: HashMap::new(),
|
2019-12-16 20:42:12 +03:00
|
|
|
}
|
2019-11-30 21:09:31 +03:00
|
|
|
}
|
2019-11-24 18:21:23 +03:00
|
|
|
|
2020-02-01 22:39:48 +03:00
|
|
|
pub fn cb(self, action: &str, cb: Callback) -> WrappedComposite {
|
2019-12-21 02:56:04 +03:00
|
|
|
if !self.inner.get_all_click_actions().contains(action) {
|
|
|
|
panic!("No button produces action {}", action);
|
|
|
|
}
|
2020-02-01 22:39:48 +03:00
|
|
|
self.maybe_cb(action, cb)
|
|
|
|
}
|
2019-12-16 04:09:41 +03:00
|
|
|
|
2020-02-01 22:39:48 +03:00
|
|
|
pub fn maybe_cb(mut self, action: &str, cb: Callback) -> WrappedComposite {
|
2019-12-21 02:56:04 +03:00
|
|
|
self.callbacks.insert(action.to_string(), cb);
|
2019-12-16 04:09:41 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-22 21:41:58 +03:00
|
|
|
pub fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Option<WrappedOutcome> {
|
2019-12-24 01:33:43 +03:00
|
|
|
match self.inner.event(ctx)? {
|
2020-01-22 21:41:58 +03:00
|
|
|
Outcome::Clicked(x) => {
|
2019-12-21 02:56:04 +03:00
|
|
|
if let Some(ref cb) = self.callbacks.get(&x) {
|
|
|
|
let t = (cb)(ctx, ui)?;
|
2020-01-22 21:41:58 +03:00
|
|
|
Some(WrappedOutcome::Transition(t))
|
2019-12-21 02:56:04 +03:00
|
|
|
} else {
|
2020-01-22 21:41:58 +03:00
|
|
|
Some(WrappedOutcome::Clicked(x))
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-19 02:27:52 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-16 20:42:12 +03:00
|
|
|
|
2019-12-21 02:56:04 +03:00
|
|
|
pub fn draw(&self, g: &mut GfxCtx) {
|
|
|
|
self.inner.draw(g);
|
2019-12-16 21:10:01 +03:00
|
|
|
}
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
2019-12-16 20:42:12 +03:00
|
|
|
|
2020-01-22 21:41:58 +03:00
|
|
|
impl WrappedComposite {
|
2019-12-08 21:24:01 +03:00
|
|
|
pub fn svg_button(
|
2019-11-30 20:15:51 +03:00
|
|
|
ctx: &EventCtx,
|
2019-11-24 18:21:27 +03:00
|
|
|
filename: &str,
|
2019-11-28 20:55:56 +03:00
|
|
|
tooltip: &str,
|
2019-11-24 18:21:27 +03:00
|
|
|
hotkey: Option<MultiKey>,
|
2019-11-30 20:15:51 +03:00
|
|
|
) -> ManagedWidget {
|
2019-12-21 02:56:04 +03:00
|
|
|
ManagedWidget::btn(Button::rectangle_svg(
|
|
|
|
filename,
|
|
|
|
tooltip,
|
|
|
|
hotkey,
|
2020-02-03 22:14:17 +03:00
|
|
|
RewriteColor::Change(Color::WHITE, colors::HOVERING),
|
2019-12-21 02:56:04 +03:00
|
|
|
ctx,
|
|
|
|
))
|
2019-11-24 18:21:27 +03:00
|
|
|
}
|
|
|
|
|
2020-01-24 02:35:08 +03:00
|
|
|
pub fn nice_text_button(
|
|
|
|
ctx: &EventCtx,
|
|
|
|
txt: Text,
|
|
|
|
hotkey: Option<MultiKey>,
|
|
|
|
label: &str,
|
|
|
|
) -> ManagedWidget {
|
|
|
|
ManagedWidget::btn(Button::text_no_bg(
|
|
|
|
txt.clone(),
|
2020-02-03 22:14:17 +03:00
|
|
|
txt.change_fg(colors::HOVERING),
|
2020-01-24 02:35:08 +03:00
|
|
|
hotkey,
|
|
|
|
label,
|
2020-01-24 03:05:35 +03:00
|
|
|
true,
|
2020-01-24 02:35:08 +03:00
|
|
|
ctx,
|
|
|
|
))
|
|
|
|
.outline(2.0, Color::WHITE)
|
|
|
|
}
|
|
|
|
|
2019-12-21 02:56:04 +03:00
|
|
|
pub fn text_button(ctx: &EventCtx, label: &str, hotkey: Option<MultiKey>) -> ManagedWidget {
|
2020-01-24 03:05:35 +03:00
|
|
|
WrappedComposite::nice_text_button(ctx, Text::from(Line(label)), hotkey, label)
|
2019-11-24 18:21:30 +03:00
|
|
|
}
|
|
|
|
|
2020-01-24 03:05:35 +03:00
|
|
|
pub fn text_bg_button(ctx: &EventCtx, label: &str, hotkey: Option<MultiKey>) -> ManagedWidget {
|
|
|
|
ManagedWidget::btn(Button::text_bg(
|
|
|
|
Text::from(Line(label).fg(Color::BLACK)),
|
2019-12-21 02:56:04 +03:00
|
|
|
Color::WHITE,
|
2020-02-03 22:14:17 +03:00
|
|
|
colors::HOVERING,
|
2019-12-21 02:56:04 +03:00
|
|
|
hotkey,
|
|
|
|
label,
|
2019-12-20 20:35:27 +03:00
|
|
|
ctx,
|
2019-12-21 02:56:04 +03:00
|
|
|
))
|
2019-11-24 18:21:23 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-16 04:09:41 +03:00
|
|
|
|
|
|
|
pub struct ManagedGUIState {
|
2020-01-22 21:41:58 +03:00
|
|
|
composite: WrappedComposite,
|
2020-01-11 22:42:26 +03:00
|
|
|
fullscreen: bool,
|
2019-12-16 04:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ManagedGUIState {
|
2020-01-22 21:41:58 +03:00
|
|
|
pub fn fullscreen(composite: WrappedComposite) -> Box<dyn State> {
|
2020-01-11 22:42:26 +03:00
|
|
|
Box::new(ManagedGUIState {
|
|
|
|
composite,
|
|
|
|
fullscreen: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-22 21:41:58 +03:00
|
|
|
pub fn over_map(composite: WrappedComposite) -> Box<dyn State> {
|
2020-01-11 22:42:26 +03:00
|
|
|
Box::new(ManagedGUIState {
|
|
|
|
composite,
|
|
|
|
fullscreen: false,
|
|
|
|
})
|
2019-12-16 04:09:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State for ManagedGUIState {
|
|
|
|
fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Transition {
|
2019-12-16 21:10:01 +03:00
|
|
|
match self.composite.event(ctx, ui) {
|
2020-01-22 21:41:58 +03:00
|
|
|
Some(WrappedOutcome::Transition(t)) => t,
|
|
|
|
Some(WrappedOutcome::Clicked(x)) => panic!(
|
2019-12-16 21:10:01 +03:00
|
|
|
"Can't have a button {} without a callback in ManagedGUIState",
|
|
|
|
x
|
|
|
|
),
|
|
|
|
None => Transition::Keep,
|
2019-12-16 04:09:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:52:47 +03:00
|
|
|
fn draw_baselayer(&self) -> DrawBaselayer {
|
|
|
|
if self.fullscreen {
|
|
|
|
DrawBaselayer::Custom
|
|
|
|
} else {
|
|
|
|
DrawBaselayer::PreviousState
|
|
|
|
}
|
2019-12-16 04:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(&self, g: &mut GfxCtx, ui: &UI) {
|
2020-01-11 22:42:26 +03:00
|
|
|
if self.fullscreen {
|
|
|
|
// Happens to be a nice background color too ;)
|
|
|
|
g.clear(ui.cs.get("grass"));
|
2020-02-04 23:52:47 +03:00
|
|
|
} else {
|
2020-02-14 00:57:48 +03:00
|
|
|
State::grey_out_map(g);
|
2020-01-11 22:42:26 +03:00
|
|
|
}
|
2020-02-04 23:52:47 +03:00
|
|
|
|
2019-12-16 04:09:41 +03:00
|
|
|
self.composite.draw(g);
|
|
|
|
}
|
|
|
|
}
|