2019-11-24 18:21:23 +03:00
|
|
|
use crate::game::{State, Transition};
|
|
|
|
use crate::ui::UI;
|
2019-11-30 20:15:51 +03:00
|
|
|
use ezgui::layout::Widget;
|
2019-12-09 00:44:43 +03:00
|
|
|
use ezgui::{
|
2019-12-19 21:59:39 +03:00
|
|
|
Button, Color, DrawBoth, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, JustDraw,
|
2019-12-21 00:42:58 +03:00
|
|
|
Line, MultiKey, Plot, RewriteColor, ScreenDims, ScreenPt, ScreenRectangle, Slider, Text,
|
2019-12-19 21:59:39 +03:00
|
|
|
VerticalAlignment,
|
2019-12-09 00:44:43 +03:00
|
|
|
};
|
2019-12-21 00:42:58 +03:00
|
|
|
use geom::{Distance, Duration, Polygon};
|
2019-12-17 01:13:08 +03:00
|
|
|
use std::collections::HashMap;
|
2019-12-16 04:09:41 +03:00
|
|
|
use stretch::geometry::{Rect, Size};
|
2019-11-30 20:15:51 +03:00
|
|
|
use stretch::node::{Node, Stretch};
|
2019-11-30 21:09:31 +03:00
|
|
|
use stretch::style::{AlignItems, Dimension, FlexDirection, FlexWrap, JustifyContent, Style};
|
2019-11-24 18:21:23 +03:00
|
|
|
|
2019-12-19 02:14:59 +03:00
|
|
|
type Callback = Box<dyn Fn(&mut EventCtx, &mut UI) -> Option<Transition>>;
|
2019-11-24 18:21:23 +03:00
|
|
|
|
2019-12-16 04:09:41 +03:00
|
|
|
pub struct ManagedWidget {
|
|
|
|
widget: WidgetType,
|
|
|
|
style: LayoutStyle,
|
2019-12-20 22:20:09 +03:00
|
|
|
rect: ScreenRectangle,
|
2019-12-16 04:09:41 +03:00
|
|
|
bg: Option<Drawable>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum WidgetType {
|
2019-11-30 20:15:51 +03:00
|
|
|
Draw(JustDraw),
|
2019-12-16 21:10:01 +03:00
|
|
|
Btn(Button, Option<Callback>),
|
2019-12-17 01:13:08 +03:00
|
|
|
Slider(String),
|
2019-12-21 00:42:58 +03:00
|
|
|
// TODO Sadness. Can't have some kind of wildcard generic here?
|
|
|
|
DurationPlot(Plot<Duration>),
|
|
|
|
UsizePlot(Plot<usize>),
|
2019-12-16 04:09:41 +03:00
|
|
|
Row(Vec<ManagedWidget>),
|
|
|
|
Column(Vec<ManagedWidget>),
|
2019-11-30 21:09:31 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 04:09:41 +03:00
|
|
|
struct LayoutStyle {
|
|
|
|
bg_color: Option<Color>,
|
|
|
|
align_items: Option<AlignItems>,
|
|
|
|
justify_content: Option<JustifyContent>,
|
|
|
|
flex_wrap: Option<FlexWrap>,
|
|
|
|
padding: Option<Rect<Dimension>>,
|
2019-12-19 02:27:52 +03:00
|
|
|
margin: Option<Rect<Dimension>>,
|
2019-11-30 21:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LayoutStyle {
|
|
|
|
fn apply(&self, style: &mut Style) {
|
2019-12-16 04:09:41 +03:00
|
|
|
if let Some(x) = self.align_items {
|
|
|
|
style.align_items = x;
|
|
|
|
}
|
|
|
|
if let Some(x) = self.justify_content {
|
|
|
|
style.justify_content = x;
|
|
|
|
}
|
|
|
|
if let Some(x) = self.flex_wrap {
|
|
|
|
style.flex_wrap = x;
|
|
|
|
}
|
|
|
|
if let Some(x) = self.padding {
|
|
|
|
style.padding = x;
|
2019-11-30 21:09:31 +03:00
|
|
|
}
|
2019-12-19 02:27:52 +03:00
|
|
|
if let Some(x) = self.margin {
|
|
|
|
style.margin = x;
|
2019-12-16 20:42:12 +03:00
|
|
|
}
|
2019-11-30 21:09:31 +03:00
|
|
|
}
|
2019-11-24 18:21:23 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 02:27:52 +03:00
|
|
|
// Layouting
|
|
|
|
// TODO Maybe I just want margin, not padding. And maybe more granular controls per side. And to
|
|
|
|
// apply margin to everything in a row or column.
|
2019-12-20 22:00:38 +03:00
|
|
|
// TODO Row and columns feel backwards when using them.
|
2019-11-30 20:15:51 +03:00
|
|
|
impl ManagedWidget {
|
2019-12-16 04:09:41 +03:00
|
|
|
pub fn centered(mut self) -> ManagedWidget {
|
|
|
|
self.style.align_items = Some(AlignItems::Center);
|
|
|
|
self.style.justify_content = Some(JustifyContent::SpaceAround);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-17 02:40:32 +03:00
|
|
|
pub fn centered_cross(mut self) -> ManagedWidget {
|
|
|
|
self.style.align_items = Some(AlignItems::Center);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-16 05:42:14 +03:00
|
|
|
pub fn evenly_spaced(mut self) -> ManagedWidget {
|
|
|
|
self.style.justify_content = Some(JustifyContent::SpaceBetween);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-16 04:09:41 +03:00
|
|
|
pub fn flex_wrap(mut self) -> ManagedWidget {
|
|
|
|
self.style.flex_wrap = Some(FlexWrap::Wrap);
|
|
|
|
self.style.justify_content = Some(JustifyContent::SpaceAround);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bg(mut self, color: Color) -> ManagedWidget {
|
|
|
|
self.style.bg_color = Some(color);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn padding(mut self, pixels: usize) -> ManagedWidget {
|
|
|
|
self.style.padding = Some(Rect {
|
|
|
|
start: Dimension::Points(pixels as f32),
|
|
|
|
end: Dimension::Points(pixels as f32),
|
|
|
|
top: Dimension::Points(pixels as f32),
|
|
|
|
bottom: Dimension::Points(pixels as f32),
|
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-19 02:27:52 +03:00
|
|
|
pub fn margin(mut self, pixels: usize) -> ManagedWidget {
|
|
|
|
self.style.margin = Some(Rect {
|
|
|
|
start: Dimension::Points(pixels as f32),
|
|
|
|
end: Dimension::Points(pixels as f32),
|
|
|
|
top: Dimension::Points(pixels as f32),
|
|
|
|
bottom: Dimension::Points(pixels as f32),
|
2019-12-16 20:42:12 +03:00
|
|
|
});
|
|
|
|
self
|
|
|
|
}
|
2019-12-19 02:27:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenient?? constructors
|
|
|
|
impl ManagedWidget {
|
|
|
|
fn new(widget: WidgetType) -> ManagedWidget {
|
|
|
|
ManagedWidget {
|
|
|
|
widget,
|
|
|
|
style: LayoutStyle {
|
|
|
|
bg_color: None,
|
|
|
|
align_items: None,
|
|
|
|
justify_content: None,
|
|
|
|
flex_wrap: None,
|
|
|
|
padding: None,
|
|
|
|
margin: None,
|
|
|
|
},
|
2019-12-20 22:20:09 +03:00
|
|
|
rect: ScreenRectangle::placeholder(),
|
2019-12-19 02:27:52 +03:00
|
|
|
bg: None,
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 20:42:12 +03:00
|
|
|
|
2019-12-16 05:42:14 +03:00
|
|
|
pub fn draw_batch(ctx: &EventCtx, batch: GeomBatch) -> ManagedWidget {
|
|
|
|
ManagedWidget::new(WidgetType::Draw(JustDraw::wrap(DrawBoth::new(
|
|
|
|
ctx,
|
|
|
|
batch,
|
|
|
|
Vec::new(),
|
|
|
|
))))
|
|
|
|
}
|
|
|
|
|
2019-12-16 21:27:52 +03:00
|
|
|
pub fn just_draw(j: JustDraw) -> ManagedWidget {
|
|
|
|
ManagedWidget::new(WidgetType::Draw(j))
|
|
|
|
}
|
|
|
|
|
2019-11-30 20:15:51 +03:00
|
|
|
// TODO Helpers that should probably be written differently
|
|
|
|
pub fn draw_text(ctx: &EventCtx, txt: Text) -> ManagedWidget {
|
2019-12-16 04:09:41 +03:00
|
|
|
ManagedWidget::new(WidgetType::Draw(JustDraw::text(txt, ctx)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn draw_svg(ctx: &EventCtx, filename: &str) -> ManagedWidget {
|
|
|
|
ManagedWidget::new(WidgetType::Draw(JustDraw::svg(filename, ctx)))
|
2019-11-24 18:21:23 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 20:42:12 +03:00
|
|
|
pub fn btn(btn: Button, onclick: Callback) -> ManagedWidget {
|
2019-12-16 21:10:01 +03:00
|
|
|
ManagedWidget::new(WidgetType::Btn(btn, Some(onclick)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn btn_no_cb(btn: Button) -> ManagedWidget {
|
|
|
|
ManagedWidget::new(WidgetType::Btn(btn, None))
|
2019-12-16 20:42:12 +03:00
|
|
|
}
|
|
|
|
|
2019-11-30 20:15:51 +03:00
|
|
|
pub fn img_button(
|
|
|
|
ctx: &EventCtx,
|
|
|
|
filename: &str,
|
|
|
|
hotkey: Option<MultiKey>,
|
|
|
|
onclick: Callback,
|
|
|
|
) -> ManagedWidget {
|
2019-12-16 20:42:12 +03:00
|
|
|
ManagedWidget::btn(Button::rectangle_img(filename, hotkey, ctx), onclick)
|
2019-11-24 18:21:27 +03:00
|
|
|
}
|
|
|
|
|
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>,
|
|
|
|
onclick: Callback,
|
2019-11-30 20:15:51 +03:00
|
|
|
) -> ManagedWidget {
|
2019-12-16 20:42:12 +03:00
|
|
|
ManagedWidget::btn(
|
|
|
|
Button::rectangle_svg(
|
|
|
|
filename,
|
|
|
|
tooltip,
|
|
|
|
hotkey,
|
|
|
|
RewriteColor::Change(Color::WHITE, Color::ORANGE),
|
|
|
|
ctx,
|
|
|
|
),
|
|
|
|
onclick,
|
|
|
|
)
|
2019-11-24 18:21:27 +03:00
|
|
|
}
|
|
|
|
|
2019-11-30 20:15:51 +03:00
|
|
|
pub fn text_button(
|
|
|
|
ctx: &EventCtx,
|
|
|
|
label: &str,
|
|
|
|
hotkey: Option<MultiKey>,
|
|
|
|
onclick: Callback,
|
|
|
|
) -> ManagedWidget {
|
|
|
|
ManagedWidget::detailed_text_button(
|
|
|
|
ctx,
|
|
|
|
Text::from(Line(label).fg(Color::BLACK)),
|
|
|
|
hotkey,
|
|
|
|
onclick,
|
|
|
|
)
|
2019-11-24 18:21:30 +03:00
|
|
|
}
|
|
|
|
|
2019-11-30 20:15:51 +03:00
|
|
|
pub fn detailed_text_button(
|
|
|
|
ctx: &EventCtx,
|
|
|
|
txt: Text,
|
|
|
|
hotkey: Option<MultiKey>,
|
|
|
|
onclick: Callback,
|
|
|
|
) -> ManagedWidget {
|
2019-11-24 18:21:27 +03:00
|
|
|
// TODO Default style. Lots of variations.
|
2019-12-16 20:42:12 +03:00
|
|
|
ManagedWidget::btn(
|
|
|
|
Button::text(txt, Color::WHITE, Color::ORANGE, hotkey, "", ctx),
|
|
|
|
onclick,
|
|
|
|
)
|
2019-12-16 04:09:41 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 01:13:08 +03:00
|
|
|
pub fn slider(label: &str) -> ManagedWidget {
|
|
|
|
ManagedWidget::new(WidgetType::Slider(label.to_string()))
|
2019-12-17 00:41:17 +03:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:42:58 +03:00
|
|
|
pub fn duration_plot(plot: Plot<Duration>) -> ManagedWidget {
|
|
|
|
ManagedWidget::new(WidgetType::DurationPlot(plot))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn usize_plot(plot: Plot<usize>) -> ManagedWidget {
|
|
|
|
ManagedWidget::new(WidgetType::UsizePlot(plot))
|
|
|
|
}
|
|
|
|
|
2019-12-16 04:09:41 +03:00
|
|
|
pub fn row(widgets: Vec<ManagedWidget>) -> ManagedWidget {
|
|
|
|
ManagedWidget::new(WidgetType::Row(widgets))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn col(widgets: Vec<ManagedWidget>) -> ManagedWidget {
|
|
|
|
ManagedWidget::new(WidgetType::Column(widgets))
|
2019-11-30 20:15:51 +03:00
|
|
|
}
|
2019-12-19 02:27:52 +03:00
|
|
|
}
|
2019-11-30 20:15:51 +03:00
|
|
|
|
2019-12-19 02:27:52 +03:00
|
|
|
// Internals
|
|
|
|
impl ManagedWidget {
|
2019-12-17 01:13:08 +03:00
|
|
|
fn event(
|
|
|
|
&mut self,
|
|
|
|
ctx: &mut EventCtx,
|
|
|
|
ui: &mut UI,
|
|
|
|
sliders: &mut HashMap<String, &mut Slider>,
|
|
|
|
) -> Option<Outcome> {
|
2019-12-16 04:09:41 +03:00
|
|
|
match self.widget {
|
|
|
|
WidgetType::Draw(_) => {}
|
2019-12-16 21:10:01 +03:00
|
|
|
WidgetType::Btn(ref mut btn, ref maybe_onclick) => {
|
2019-11-30 20:15:51 +03:00
|
|
|
btn.event(ctx);
|
|
|
|
if btn.clicked() {
|
2019-12-16 21:10:01 +03:00
|
|
|
if let Some(ref cb) = maybe_onclick {
|
|
|
|
if let Some(t) = (cb)(ctx, ui) {
|
|
|
|
return Some(Outcome::Transition(t));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Some(Outcome::Clicked(btn.action.clone()));
|
2019-11-30 20:15:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 01:13:08 +03:00
|
|
|
WidgetType::Slider(ref name) => {
|
|
|
|
sliders.get_mut(name).unwrap().event(ctx);
|
2019-12-17 00:41:17 +03:00
|
|
|
}
|
2019-12-21 00:42:58 +03:00
|
|
|
WidgetType::DurationPlot(_) | WidgetType::UsizePlot(_) => {}
|
2019-12-16 04:09:41 +03:00
|
|
|
WidgetType::Row(ref mut widgets) | WidgetType::Column(ref mut widgets) => {
|
2019-11-30 20:15:51 +03:00
|
|
|
for w in widgets {
|
2019-12-17 01:13:08 +03:00
|
|
|
if let Some(o) = w.event(ctx, ui, sliders) {
|
2019-12-16 21:10:01 +03:00
|
|
|
return Some(o);
|
2019-11-30 20:15:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2019-11-24 18:21:23 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 01:13:08 +03:00
|
|
|
fn draw(&self, g: &mut GfxCtx, sliders: &HashMap<String, &Slider>) {
|
2019-12-16 04:09:41 +03:00
|
|
|
if let Some(ref bg) = self.bg {
|
|
|
|
g.fork_screenspace();
|
|
|
|
g.redraw(bg);
|
|
|
|
g.unfork();
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.widget {
|
|
|
|
WidgetType::Draw(ref j) => j.draw(g),
|
|
|
|
WidgetType::Btn(ref btn, _) => btn.draw(g),
|
2019-12-17 01:13:08 +03:00
|
|
|
WidgetType::Slider(ref name) => sliders[name].draw(g),
|
2019-12-21 00:42:58 +03:00
|
|
|
WidgetType::DurationPlot(ref plot) => plot.draw(g),
|
|
|
|
WidgetType::UsizePlot(ref plot) => plot.draw(g),
|
2019-12-16 04:09:41 +03:00
|
|
|
WidgetType::Row(ref widgets) | WidgetType::Column(ref widgets) => {
|
2019-11-30 20:15:51 +03:00
|
|
|
for w in widgets {
|
2019-12-17 01:13:08 +03:00
|
|
|
w.draw(g, sliders);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate a flattened list of Nodes, matching the traversal order
|
|
|
|
fn get_flexbox(
|
|
|
|
&self,
|
|
|
|
parent: Node,
|
|
|
|
sliders: &HashMap<String, &mut Slider>,
|
|
|
|
stretch: &mut Stretch,
|
|
|
|
nodes: &mut Vec<Node>,
|
|
|
|
) {
|
2019-12-21 00:42:58 +03:00
|
|
|
// TODO Can I use | in the match and "cast" to Widget?
|
|
|
|
let widget: &dyn Widget = match self.widget {
|
|
|
|
WidgetType::Draw(ref widget) => widget,
|
|
|
|
WidgetType::Btn(ref widget, _) => widget,
|
|
|
|
WidgetType::Slider(ref name) => sliders[name],
|
|
|
|
WidgetType::DurationPlot(ref widget) => widget,
|
|
|
|
WidgetType::UsizePlot(ref widget) => widget,
|
2019-12-17 01:13:08 +03:00
|
|
|
WidgetType::Row(ref widgets) => {
|
|
|
|
let mut style = Style {
|
|
|
|
flex_direction: FlexDirection::Row,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
self.style.apply(&mut style);
|
|
|
|
let row = stretch.new_node(style, Vec::new()).unwrap();
|
|
|
|
nodes.push(row);
|
|
|
|
for widget in widgets {
|
|
|
|
widget.get_flexbox(row, sliders, stretch, nodes);
|
|
|
|
}
|
|
|
|
stretch.add_child(parent, row).unwrap();
|
2019-12-21 00:42:58 +03:00
|
|
|
return;
|
2019-12-17 01:13:08 +03:00
|
|
|
}
|
|
|
|
WidgetType::Column(ref widgets) => {
|
|
|
|
let mut style = Style {
|
|
|
|
flex_direction: FlexDirection::Column,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
self.style.apply(&mut style);
|
|
|
|
let col = stretch.new_node(style, Vec::new()).unwrap();
|
|
|
|
nodes.push(col);
|
|
|
|
for widget in widgets {
|
|
|
|
widget.get_flexbox(col, sliders, stretch, nodes);
|
|
|
|
}
|
|
|
|
stretch.add_child(parent, col).unwrap();
|
2019-12-21 00:42:58 +03:00
|
|
|
return;
|
2019-12-17 01:13:08 +03:00
|
|
|
}
|
2019-12-21 00:42:58 +03:00
|
|
|
};
|
|
|
|
let dims = widget.get_dims();
|
|
|
|
let mut style = Style {
|
|
|
|
size: Size {
|
|
|
|
width: Dimension::Points(dims.width as f32),
|
|
|
|
height: Dimension::Points(dims.height as f32),
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
self.style.apply(&mut style);
|
|
|
|
let node = stretch.new_node(style, Vec::new()).unwrap();
|
|
|
|
stretch.add_child(parent, node).unwrap();
|
|
|
|
nodes.push(node);
|
2019-12-17 01:13:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_flexbox(
|
|
|
|
&mut self,
|
|
|
|
sliders: &mut HashMap<String, &mut Slider>,
|
|
|
|
stretch: &Stretch,
|
|
|
|
nodes: &mut Vec<Node>,
|
|
|
|
dx: f64,
|
|
|
|
dy: f64,
|
2019-12-17 01:50:21 +03:00
|
|
|
ctx: &EventCtx,
|
2019-12-17 01:13:08 +03:00
|
|
|
) {
|
|
|
|
let result = stretch.layout(nodes.pop().unwrap()).unwrap();
|
|
|
|
let x: f64 = result.location.x.into();
|
|
|
|
let y: f64 = result.location.y.into();
|
|
|
|
let width: f64 = result.size.width.into();
|
|
|
|
let height: f64 = result.size.height.into();
|
2019-12-20 22:20:09 +03:00
|
|
|
self.rect = ScreenRectangle::top_left(
|
2019-12-17 01:13:08 +03:00
|
|
|
ScreenPt::new(x + dx, y + dy),
|
|
|
|
ScreenDims::new(width, height),
|
2019-12-20 22:20:09 +03:00
|
|
|
);
|
2019-12-17 01:13:08 +03:00
|
|
|
if let Some(color) = self.style.bg_color {
|
|
|
|
let mut batch = GeomBatch::new();
|
|
|
|
batch.push(
|
|
|
|
color,
|
|
|
|
Polygon::rounded_rectangle(
|
|
|
|
Distance::meters(width),
|
|
|
|
Distance::meters(height),
|
|
|
|
Distance::meters(5.0),
|
|
|
|
)
|
|
|
|
.translate(x + dx, y + dy),
|
|
|
|
);
|
|
|
|
self.bg = Some(batch.upload(ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.widget {
|
|
|
|
WidgetType::Draw(ref mut widget) => {
|
|
|
|
widget.set_pos(ScreenPt::new(x + dx, y + dy));
|
|
|
|
}
|
|
|
|
WidgetType::Btn(ref mut widget, _) => {
|
|
|
|
widget.set_pos(ScreenPt::new(x + dx, y + dy));
|
|
|
|
}
|
|
|
|
WidgetType::Slider(ref name) => {
|
|
|
|
sliders
|
|
|
|
.get_mut(name)
|
|
|
|
.unwrap()
|
|
|
|
.set_pos(ScreenPt::new(x + dx, y + dy));
|
|
|
|
}
|
2019-12-21 00:42:58 +03:00
|
|
|
WidgetType::DurationPlot(ref mut widget) => {
|
|
|
|
widget.set_pos(ScreenPt::new(x + dx, y + dy));
|
|
|
|
}
|
|
|
|
WidgetType::UsizePlot(ref mut widget) => {
|
|
|
|
widget.set_pos(ScreenPt::new(x + dx, y + dy));
|
|
|
|
}
|
2019-12-17 01:13:08 +03:00
|
|
|
WidgetType::Row(ref mut widgets) => {
|
|
|
|
// layout() doesn't return absolute position; it's relative to the container.
|
|
|
|
for widget in widgets {
|
|
|
|
widget.apply_flexbox(sliders, stretch, nodes, x + dx, y + dy, ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WidgetType::Column(ref mut widgets) => {
|
|
|
|
for widget in widgets {
|
|
|
|
widget.apply_flexbox(sliders, stretch, nodes, x + dx, y + dy, ctx);
|
2019-11-30 20:15:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 18:21:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 04:09:41 +03:00
|
|
|
pub struct Composite {
|
2019-11-30 20:15:51 +03:00
|
|
|
top_level: ManagedWidget,
|
2019-12-16 04:09:41 +03:00
|
|
|
pos: CompositePosition,
|
2019-12-20 20:35:27 +03:00
|
|
|
scroll_y_offset: f64,
|
2019-11-24 18:21:23 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 21:10:01 +03:00
|
|
|
pub enum Outcome {
|
|
|
|
Transition(Transition),
|
|
|
|
Clicked(String),
|
|
|
|
}
|
|
|
|
|
2019-12-16 04:09:41 +03:00
|
|
|
enum CompositePosition {
|
|
|
|
FillScreen,
|
|
|
|
MinimalTopLeft(ScreenPt),
|
2019-12-19 21:59:39 +03:00
|
|
|
Aligned(HorizontalAlignment, VerticalAlignment),
|
2019-11-24 18:21:23 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 04:09:41 +03:00
|
|
|
impl Composite {
|
2019-12-20 22:20:09 +03:00
|
|
|
fn new(
|
|
|
|
ctx: &EventCtx,
|
|
|
|
top_level: ManagedWidget,
|
|
|
|
pos: CompositePosition,
|
|
|
|
mut sliders: HashMap<String, &mut Slider>,
|
|
|
|
) -> Composite {
|
|
|
|
let mut c = Composite {
|
2019-12-16 04:09:41 +03:00
|
|
|
top_level,
|
2019-12-20 22:20:09 +03:00
|
|
|
pos,
|
2019-12-20 20:35:27 +03:00
|
|
|
scroll_y_offset: 0.0,
|
2019-12-20 22:20:09 +03:00
|
|
|
};
|
|
|
|
c.recompute_layout(ctx, &mut sliders);
|
|
|
|
c
|
2019-12-16 04:09:41 +03:00
|
|
|
}
|
|
|
|
|
2019-12-20 22:20:09 +03:00
|
|
|
pub fn minimal_size(ctx: &EventCtx, top_level: ManagedWidget, top_left: ScreenPt) -> Composite {
|
|
|
|
Composite::new(
|
|
|
|
ctx,
|
2019-12-16 04:09:41 +03:00
|
|
|
top_level,
|
2019-12-20 22:20:09 +03:00
|
|
|
CompositePosition::MinimalTopLeft(top_left),
|
|
|
|
HashMap::new(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fill_screen(ctx: &EventCtx, top_level: ManagedWidget) -> Composite {
|
|
|
|
Composite::new(
|
|
|
|
ctx,
|
|
|
|
top_level,
|
|
|
|
CompositePosition::FillScreen,
|
|
|
|
HashMap::new(),
|
|
|
|
)
|
2019-12-16 04:09:41 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 21:59:39 +03:00
|
|
|
pub fn aligned(
|
2019-12-20 22:20:09 +03:00
|
|
|
ctx: &EventCtx,
|
2019-12-19 21:59:39 +03:00
|
|
|
(horiz, vert): (HorizontalAlignment, VerticalAlignment),
|
|
|
|
top_level: ManagedWidget,
|
|
|
|
) -> Composite {
|
2019-12-20 22:20:09 +03:00
|
|
|
Composite::new(
|
|
|
|
ctx,
|
2019-12-19 21:59:39 +03:00
|
|
|
top_level,
|
2019-12-20 22:20:09 +03:00
|
|
|
CompositePosition::Aligned(horiz, vert),
|
|
|
|
HashMap::new(),
|
|
|
|
)
|
2019-12-19 21:59:39 +03:00
|
|
|
}
|
|
|
|
|
2019-12-20 22:20:09 +03:00
|
|
|
pub fn aligned_with_sliders(
|
|
|
|
ctx: &EventCtx,
|
|
|
|
sliders: HashMap<String, &mut Slider>,
|
|
|
|
(horiz, vert): (HorizontalAlignment, VerticalAlignment),
|
|
|
|
top_level: ManagedWidget,
|
|
|
|
) -> Composite {
|
|
|
|
Composite::new(
|
|
|
|
ctx,
|
|
|
|
top_level,
|
|
|
|
CompositePosition::Aligned(horiz, vert),
|
|
|
|
sliders,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn recompute_layout(&mut self, ctx: &EventCtx, sliders: &mut HashMap<String, &mut Slider>) {
|
2019-11-30 20:15:51 +03:00
|
|
|
let mut stretch = Stretch::new();
|
|
|
|
let root = stretch
|
|
|
|
.new_node(
|
2019-12-16 04:09:41 +03:00
|
|
|
match self.pos {
|
|
|
|
CompositePosition::FillScreen => Style {
|
|
|
|
size: Size {
|
|
|
|
width: Dimension::Points(ctx.canvas.window_width as f32),
|
|
|
|
height: Dimension::Points(ctx.canvas.window_height as f32),
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
},
|
2019-12-19 21:59:39 +03:00
|
|
|
CompositePosition::MinimalTopLeft(_) | CompositePosition::Aligned(_, _) => {
|
|
|
|
Style {
|
|
|
|
// TODO There a way to encode the offset in stretch?
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
2019-11-30 20:15:51 +03:00
|
|
|
},
|
|
|
|
Vec::new(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut nodes = vec![];
|
2019-12-17 01:13:08 +03:00
|
|
|
self.top_level
|
2019-12-17 01:50:21 +03:00
|
|
|
.get_flexbox(root, sliders, &mut stretch, &mut nodes);
|
2019-11-30 20:15:51 +03:00
|
|
|
nodes.reverse();
|
|
|
|
|
|
|
|
stretch.compute_layout(root, Size::undefined()).unwrap();
|
2019-12-16 04:09:41 +03:00
|
|
|
let top_left = match self.pos {
|
|
|
|
CompositePosition::FillScreen => ScreenPt::new(0.0, 0.0),
|
|
|
|
CompositePosition::MinimalTopLeft(pt) => pt,
|
2019-12-19 21:59:39 +03:00
|
|
|
CompositePosition::Aligned(horiz, vert) => {
|
|
|
|
let result = stretch.layout(root).unwrap();
|
|
|
|
ctx.canvas.align_window(
|
|
|
|
ScreenDims::new(result.size.width.into(), result.size.height.into()),
|
|
|
|
horiz,
|
|
|
|
vert,
|
|
|
|
)
|
|
|
|
}
|
2019-12-16 04:09:41 +03:00
|
|
|
};
|
2019-12-20 20:35:27 +03:00
|
|
|
self.top_level.apply_flexbox(
|
|
|
|
sliders,
|
|
|
|
&stretch,
|
|
|
|
&mut nodes,
|
|
|
|
top_left.x,
|
|
|
|
top_left.y - self.scroll_y_offset,
|
|
|
|
ctx,
|
|
|
|
);
|
2019-11-30 20:15:51 +03:00
|
|
|
assert!(nodes.is_empty());
|
2019-12-17 01:50:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Option<Outcome> {
|
|
|
|
self.event_with_sliders(ctx, ui, HashMap::new())
|
|
|
|
}
|
2019-11-30 20:15:51 +03:00
|
|
|
|
2019-12-17 01:50:21 +03:00
|
|
|
pub fn event_with_sliders(
|
|
|
|
&mut self,
|
|
|
|
ctx: &mut EventCtx,
|
|
|
|
ui: &mut UI,
|
|
|
|
mut sliders: HashMap<String, &mut Slider>,
|
|
|
|
) -> Option<Outcome> {
|
2019-12-20 22:20:09 +03:00
|
|
|
if ctx.input.is_window_resized() {
|
|
|
|
self.recompute_layout(ctx, &mut sliders);
|
|
|
|
}
|
2019-12-17 01:13:08 +03:00
|
|
|
self.top_level.event(ctx, ui, &mut sliders)
|
2019-11-24 18:21:23 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 04:09:41 +03:00
|
|
|
pub fn draw(&self, g: &mut GfxCtx) {
|
2019-12-17 01:13:08 +03:00
|
|
|
self.draw_with_sliders(g, HashMap::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn draw_with_sliders(&self, g: &mut GfxCtx, sliders: HashMap<String, &Slider>) {
|
2019-12-20 22:20:09 +03:00
|
|
|
g.canvas.mark_covered_area(self.top_level.rect.clone());
|
2019-12-17 01:13:08 +03:00
|
|
|
self.top_level.draw(g, &sliders);
|
2019-11-24 18:21:23 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-16 04:09:41 +03:00
|
|
|
|
|
|
|
pub struct ManagedGUIState {
|
|
|
|
composite: Composite,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ManagedGUIState {
|
2019-12-20 22:20:09 +03:00
|
|
|
pub fn new(ctx: &EventCtx, top_level: ManagedWidget) -> Box<dyn State> {
|
2019-12-16 04:09:41 +03:00
|
|
|
Box::new(ManagedGUIState {
|
2019-12-20 22:20:09 +03:00
|
|
|
composite: Composite::fill_screen(ctx, top_level),
|
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) {
|
|
|
|
Some(Outcome::Transition(t)) => t,
|
|
|
|
Some(Outcome::Clicked(x)) => panic!(
|
|
|
|
"Can't have a button {} without a callback in ManagedGUIState",
|
|
|
|
x
|
|
|
|
),
|
|
|
|
None => Transition::Keep,
|
2019-12-16 04:09:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_default_ui(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(&self, g: &mut GfxCtx, ui: &UI) {
|
|
|
|
// Happens to be a nice background color too ;)
|
|
|
|
g.clear(ui.cs.get("grass"));
|
|
|
|
self.composite.draw(g);
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 20:35:27 +03:00
|
|
|
|
|
|
|
const SCROLL_SPEED: f64 = 5.0;
|
|
|
|
|
|
|
|
// TODO Build into Composite directly
|
|
|
|
// TODO This doesn't clip. There's no way to express that the scrollable thing should occupy a
|
|
|
|
// small part of the screen.
|
|
|
|
// TODO Horizontal scrolling?
|
2019-12-20 21:30:56 +03:00
|
|
|
pub struct Scroller {
|
2019-12-20 20:35:27 +03:00
|
|
|
composite: Composite,
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:56 +03:00
|
|
|
impl Scroller {
|
|
|
|
pub fn new(composite: Composite) -> Scroller {
|
|
|
|
Scroller { composite }
|
2019-12-20 20:35:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Option<Outcome> {
|
2019-12-20 22:20:09 +03:00
|
|
|
if self
|
|
|
|
.composite
|
|
|
|
.top_level
|
|
|
|
.rect
|
|
|
|
.contains(ctx.canvas.get_cursor_in_screen_space())
|
|
|
|
{
|
|
|
|
if let Some(scroll) = ctx.input.get_mouse_scroll() {
|
|
|
|
self.composite.scroll_y_offset -= scroll * SCROLL_SPEED;
|
|
|
|
let max =
|
|
|
|
(self.composite.top_level.rect.height() - ctx.canvas.window_height).max(0.0);
|
|
|
|
self.composite.scroll_y_offset =
|
|
|
|
abstutil::clamp(self.composite.scroll_y_offset, 0.0, max);
|
|
|
|
self.composite.recompute_layout(ctx, &mut HashMap::new());
|
2019-12-20 20:35:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.composite.event(ctx, ui)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn draw(&self, g: &mut GfxCtx) {
|
|
|
|
self.composite.draw(g);
|
|
|
|
}
|
2019-12-20 21:25:14 +03:00
|
|
|
|
|
|
|
pub fn preserve_scroll(&self) -> f64 {
|
|
|
|
self.composite.scroll_y_offset
|
|
|
|
}
|
|
|
|
|
2019-12-21 00:11:50 +03:00
|
|
|
pub fn restore_scroll(&mut self, ctx: &EventCtx, offset: f64) {
|
2019-12-20 21:25:14 +03:00
|
|
|
self.composite.scroll_y_offset = offset;
|
2019-12-21 00:11:50 +03:00
|
|
|
self.composite.recompute_layout(ctx, &mut HashMap::new());
|
2019-12-20 21:25:14 +03:00
|
|
|
}
|
2019-12-20 20:35:27 +03:00
|
|
|
}
|