aha, dont need to plumb ScreenRectangle to widgets after all

This commit is contained in:
Dustin Carlino 2020-03-22 21:04:05 -07:00
parent 4df64b4c83
commit 96d7987c7e
13 changed files with 33 additions and 103 deletions

View File

@ -648,10 +648,7 @@ impl Composite {
let before = self.scroll_offset();
let mut redo_layout = false;
let result = self
.top_level
.widget
.event(ctx, &self.top_level.rect, &mut redo_layout);
let result = self.top_level.widget.event(ctx, &mut redo_layout);
if self.scroll_offset() != before || redo_layout {
self.recompute_layout(ctx, true);
}

View File

@ -1,6 +1,6 @@
use crate::{
text, Color, Drawable, EventCtx, GeomBatch, GfxCtx, JustDraw, Line, MultiKey, Outcome,
RewriteColor, ScreenDims, ScreenPt, ScreenRectangle, Text, Widget, WidgetImpl,
RewriteColor, ScreenDims, ScreenPt, Text, Widget, WidgetImpl,
};
use geom::Polygon;
@ -20,7 +20,7 @@ pub struct Button {
hovering: bool,
pub(crate) top_left: ScreenPt,
dims: ScreenDims,
pub(crate) dims: ScreenDims,
}
impl Button {
@ -68,12 +68,7 @@ impl WidgetImpl for Button {
self.top_left = top_left;
}
fn event(
&mut self,
ctx: &mut EventCtx,
_rect: &ScreenRectangle,
_redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, ctx: &mut EventCtx, _redo_layout: &mut bool) -> Option<Outcome> {
if ctx.redo_mouseover() {
if let Some(pt) = ctx.canvas.get_cursor_in_screen_space() {
self.hovering = self

View File

@ -1,4 +1,4 @@
use crate::{Button, EventCtx, GfxCtx, Outcome, ScreenDims, ScreenPt, ScreenRectangle, WidgetImpl};
use crate::{Button, EventCtx, GfxCtx, Outcome, ScreenDims, ScreenPt, WidgetImpl};
pub struct Checkbox {
pub(crate) enabled: bool,
@ -33,14 +33,8 @@ impl WidgetImpl for Checkbox {
self.btn.set_pos(top_left);
}
fn event(
&mut self,
ctx: &mut EventCtx,
rect: &ScreenRectangle,
redo_layout: &mut bool,
) -> Option<Outcome> {
// TODO Lying about the rectangle
if self.btn.event(ctx, rect, redo_layout).is_some() {
fn event(&mut self, ctx: &mut EventCtx, redo_layout: &mut bool) -> Option<Outcome> {
if self.btn.event(ctx, redo_layout).is_some() {
std::mem::swap(&mut self.btn, &mut self.other_btn);
self.btn.set_pos(self.other_btn.top_left);
self.enabled = !self.enabled;

View File

@ -1,4 +1,4 @@
use crate::{EventCtx, GfxCtx, Outcome, ScreenDims, ScreenPt, ScreenRectangle, Widget, WidgetImpl};
use crate::{EventCtx, GfxCtx, Outcome, ScreenDims, ScreenPt, Widget, WidgetImpl};
pub struct Nothing {}
@ -11,12 +11,7 @@ impl WidgetImpl for Nothing {
unreachable!()
}
fn event(
&mut self,
_ctx: &mut EventCtx,
_rect: &ScreenRectangle,
_redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, _ctx: &mut EventCtx, _redo_layout: &mut bool) -> Option<Outcome> {
unreachable!()
}
fn draw(&self, _g: &mut GfxCtx) {
@ -45,14 +40,9 @@ impl WidgetImpl for Container {
unreachable!()
}
fn event(
&mut self,
ctx: &mut EventCtx,
_rect: &ScreenRectangle,
redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, ctx: &mut EventCtx, redo_layout: &mut bool) -> Option<Outcome> {
for w in &mut self.members {
if let Some(o) = w.widget.event(ctx, &w.rect, redo_layout) {
if let Some(o) = w.widget.event(ctx, redo_layout) {
return Some(o);
}
}

View File

@ -1,6 +1,6 @@
use crate::{
Btn, Button, Choice, Color, EventCtx, GfxCtx, InputResult, Outcome, PopupMenu, ScreenDims,
ScreenPt, ScreenRectangle, WidgetImpl,
ScreenPt, WidgetImpl,
};
use geom::{Polygon, Pt2D};
@ -48,16 +48,9 @@ impl<T: 'static + Clone> WidgetImpl for Dropdown<T> {
self.btn.set_pos(top_left);
}
fn event(
&mut self,
ctx: &mut EventCtx,
rect: &ScreenRectangle,
redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, ctx: &mut EventCtx, redo_layout: &mut bool) -> Option<Outcome> {
if let Some(ref mut m) = self.menu {
// TODO Pass in the dropdown's rectangle, not the menu's. This is a lie! But the menu
// doesn't use it, so fine?
m.event(ctx, rect, redo_layout);
m.event(ctx, redo_layout);
match m.state {
InputResult::StillActive => {}
InputResult::Canceled => {
@ -75,8 +68,7 @@ impl<T: 'static + Clone> WidgetImpl for Dropdown<T> {
}
}
} else {
// TODO Again lying about the rectangle
if self.btn.event(ctx, rect, redo_layout).is_some() {
if self.btn.event(ctx, redo_layout).is_some() {
// TODO set current idx in menu
// TODO Choice::map_value?
let mut menu = PopupMenu::new(
@ -87,7 +79,10 @@ impl<T: 'static + Clone> WidgetImpl for Dropdown<T> {
.map(|(idx, c)| c.with_value(idx))
.collect(),
);
menu.set_pos(ScreenPt::new(rect.x1, rect.y2 + 15.0));
menu.set_pos(ScreenPt::new(
self.btn.top_left.x,
self.btn.top_left.y + self.btn.dims.height + 15.0,
));
self.menu = Some(menu);
}
}

View File

@ -1,4 +1,4 @@
use crate::{EventCtx, GfxCtx, Outcome, ScreenDims, ScreenPt, ScreenRectangle, WidgetImpl};
use crate::{EventCtx, GfxCtx, Outcome, ScreenDims, ScreenPt, WidgetImpl};
// Doesn't do anything by itself, just used for widgetsing. Something else reaches in, asks for the
// ScreenRectangle to use.
@ -25,12 +25,7 @@ impl WidgetImpl for Filler {
self.top_left = top_left;
}
fn event(
&mut self,
_ctx: &mut EventCtx,
_rect: &ScreenRectangle,
_redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, _ctx: &mut EventCtx, _redo_layout: &mut bool) -> Option<Outcome> {
None
}
fn draw(&self, _g: &mut GfxCtx) {}

View File

@ -1,6 +1,6 @@
use crate::{
Color, Drawable, EventCtx, GeomBatch, GfxCtx, Line, Outcome, ScreenDims, ScreenPt,
ScreenRectangle, Text, TextExt, Widget, WidgetImpl,
Color, Drawable, EventCtx, GeomBatch, GfxCtx, Line, Outcome, ScreenDims, ScreenPt, Text,
TextExt, Widget, WidgetImpl,
};
use abstutil::prettyprint_usize;
use geom::{Distance, Duration, Polygon, Pt2D};
@ -110,12 +110,7 @@ impl WidgetImpl for Histogram {
self.top_left = top_left;
}
fn event(
&mut self,
_ctx: &mut EventCtx,
_rect: &ScreenRectangle,
_redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, _ctx: &mut EventCtx, _redo_layout: &mut bool) -> Option<Outcome> {
None
}
fn draw(&self, g: &mut GfxCtx) {

View File

@ -12,7 +12,7 @@ pub mod slider;
pub mod text_box;
pub mod wizard;
use crate::{EventCtx, GfxCtx, Outcome, ScreenDims, ScreenPt, ScreenRectangle};
use crate::{EventCtx, GfxCtx, Outcome, ScreenDims, ScreenPt};
/// Create a new widget by implementing this trait. You can instantiate your widget by calling
/// `Widget::new(Box::new(instance of your new widget))`, which gives you the usual style options.
@ -22,16 +22,10 @@ pub trait WidgetImpl: downcast_rs::Downcast {
fn get_dims(&self) -> ScreenDims;
/// Your widget's top left corner should be here. Handle mouse events and draw appropriately.
fn set_pos(&mut self, top_left: ScreenPt);
// TODO I think we can scrap rect
/// Your chance to react to an event. If this event should trigger layouting to be recalculated
/// (because this widget changes dimensions), set `redo_layout` to true. Most widgets should
/// return `None` instead of an `Outcome`.
fn event(
&mut self,
ctx: &mut EventCtx,
rect: &ScreenRectangle,
redo_layout: &mut bool,
) -> Option<Outcome>;
fn event(&mut self, ctx: &mut EventCtx, redo_layout: &mut bool) -> Option<Outcome>;
/// Draw the widget. Be sure to draw relative to the top-left specified by `set_pos`.
fn draw(&self, g: &mut GfxCtx);
}

View File

@ -1,6 +1,6 @@
use crate::{
svg, Drawable, EventCtx, GeomBatch, GfxCtx, Outcome, RewriteColor, ScreenDims, ScreenPt,
ScreenRectangle, Text, Widget, WidgetImpl,
svg, Drawable, EventCtx, GeomBatch, GfxCtx, Outcome, RewriteColor, ScreenDims, ScreenPt, Text,
Widget, WidgetImpl,
};
// Just draw something. A widget just so widgetsing works.
@ -54,12 +54,7 @@ impl WidgetImpl for JustDraw {
self.top_left = top_left;
}
fn event(
&mut self,
_ctx: &mut EventCtx,
_rect: &ScreenRectangle,
_redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, _ctx: &mut EventCtx, _redo_layout: &mut bool) -> Option<Outcome> {
None
}

View File

@ -238,12 +238,7 @@ impl<T: 'static + Yvalue<T>> WidgetImpl for Plot<T> {
self.top_left = top_left;
}
fn event(
&mut self,
_ctx: &mut EventCtx,
_rect: &ScreenRectangle,
_redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, _ctx: &mut EventCtx, _redo_layout: &mut bool) -> Option<Outcome> {
None
}

View File

@ -79,12 +79,7 @@ impl<T: 'static + Clone> WidgetImpl for PopupMenu<T> {
self.top_left = top_left;
}
fn event(
&mut self,
ctx: &mut EventCtx,
_rect: &ScreenRectangle,
_redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, ctx: &mut EventCtx, _redo_layout: &mut bool) -> Option<Outcome> {
match self.state {
InputResult::StillActive => {}
_ => unreachable!(),

View File

@ -203,12 +203,7 @@ impl WidgetImpl for Slider {
self.top_left = top_left;
}
fn event(
&mut self,
ctx: &mut EventCtx,
_rect: &ScreenRectangle,
_redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, ctx: &mut EventCtx, _redo_layout: &mut bool) -> Option<Outcome> {
if self.inner_event(ctx) {
self.recalc(ctx);
}

View File

@ -63,12 +63,7 @@ impl WidgetImpl for TextBox {
self.top_left = top_left;
}
fn event(
&mut self,
ctx: &mut EventCtx,
_rect: &ScreenRectangle,
_redo_layout: &mut bool,
) -> Option<Outcome> {
fn event(&mut self, ctx: &mut EventCtx, _redo_layout: &mut bool) -> Option<Outcome> {
if ctx.redo_mouseover() {
if let Some(pt) = ctx.canvas.get_cursor_in_screen_space() {
self.hovering = ScreenRectangle::top_left(self.top_left, self.dims).contains(pt);