Lift Cached to widgetry

This commit is contained in:
Dustin Carlino 2020-12-26 14:38:43 -08:00
parent c3f211ccfd
commit af5811c33c
7 changed files with 47 additions and 47 deletions

View File

@ -9,12 +9,12 @@ use geom::{Distance, Duration};
use map_gui::tools::{
amenity_type, nice_map_name, open_browser, CityPicker, ColorLegend, PopupMsg,
};
use map_gui::{Cached, ID};
use map_gui::ID;
use map_model::connectivity::WalkingOptions;
use map_model::{Building, BuildingID};
use widgetry::table::{Col, Filter, Table};
use widgetry::{
lctrl, Btn, Checkbox, Choice, Color, Drawable, EventCtx, GeomBatch, GfxCtx,
lctrl, Btn, Cached, Checkbox, Choice, Color, Drawable, EventCtx, GeomBatch, GfxCtx,
HorizontalAlignment, Key, Line, Outcome, Panel, RewriteColor, State, Text, TextExt, Transition,
VerticalAlignment, Widget,
};

View File

@ -2,11 +2,10 @@ use std::collections::{BTreeMap, HashSet};
use abstutil::Counter;
use geom::{ArrowCap, Circle, Distance, Duration, PolyLine, Polygon, Pt2D};
use map_gui::Cached;
use sim::{AgentID, DelayCause};
use widgetry::{
Btn, Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Line, Outcome, Panel,
State, Text, VerticalAlignment, Widget,
Btn, Cached, Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Line, Outcome,
Panel, State, Text, VerticalAlignment, Widget,
};
use crate::app::App;

View File

@ -6,13 +6,13 @@ use map_gui::load::MapLoader;
use map_gui::options::OptionsPanel;
use map_gui::render::{calculate_corners, DrawOptions};
use map_gui::tools::{ChooseSomething, PopupMsg, PromptInput};
use map_gui::{Cached, ID};
use map_gui::ID;
use map_model::{osm, ControlTrafficSignal, IntersectionID, NORMAL_LANE_THICKNESS};
use sim::Sim;
use widgetry::{
lctrl, Btn, Checkbox, Choice, Color, DrawBaselayer, Drawable, EventCtx, GeomBatch, GfxCtx,
HorizontalAlignment, Key, Line, Outcome, Panel, State, Text, UpdateType, VerticalAlignment,
Widget,
lctrl, Btn, Cached, Checkbox, Choice, Color, DrawBaselayer, Drawable, EventCtx, GeomBatch,
GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, State, Text, UpdateType,
VerticalAlignment, Widget,
};
use crate::app::{App, ShowLayers, ShowObject, Transition};

View File

@ -7,9 +7,9 @@ use abstutil::Timer;
use geom::{Duration, Pt2D, Time};
use map_model::{AreaID, BuildingID, BusStopID, IntersectionID, LaneID, Map, ParkingLotID, RoadID};
use sim::{AgentID, CarID, PedestrianID, Sim};
use widgetry::{EventCtx, GfxCtx, State};
use widgetry::{Cached, EventCtx, GfxCtx, State};
pub use self::simple_app::{Cached, SimpleApp};
pub use self::simple_app::SimpleApp;
use crate::render::DrawOptions;
use colors::{ColorScheme, ColorSchemeChoice};
use options::Options;

View File

@ -307,39 +307,3 @@ impl<T> State<SimpleApp<T>> for SimpleWarper {
fn draw(&self, _: &mut GfxCtx, _: &SimpleApp<T>) {}
}
/// Store a cached key/value pair, only recalculating when the key changes.
pub struct Cached<K: PartialEq + Clone, V> {
contents: Option<(K, V)>,
}
impl<K: PartialEq + Clone, V> Cached<K, V> {
pub fn new() -> Cached<K, V> {
Cached { contents: None }
}
/// Get the current key.
pub fn key(&self) -> Option<K> {
self.contents.as_ref().map(|(k, _)| k.clone())
}
/// Get the current value.
pub fn value(&self) -> Option<&V> {
self.contents.as_ref().map(|(_, v)| v)
}
/// Update the value if the key has changed.
pub fn update<F: FnMut(K) -> V>(&mut self, key: Option<K>, mut produce_value: F) {
if let Some(new_key) = key {
if self.key() != Some(new_key.clone()) {
self.contents = Some((new_key.clone(), produce_value(new_key)));
}
} else {
self.contents = None;
}
}
pub fn clear(&mut self) {
self.contents = None;
}
}

View File

@ -42,6 +42,7 @@ pub use crate::screen_geom::{ScreenDims, ScreenPt, ScreenRectangle};
pub use crate::style::Style;
pub use crate::text::{Line, Text, TextExt, TextSpan};
pub use crate::tools::warper::Warper;
pub use crate::tools::Cached;
pub use crate::widgets::autocomplete::Autocomplete;
pub(crate) use crate::widgets::button::Button;
pub use crate::widgets::button::{Btn, MultiButton};

View File

@ -1,2 +1,38 @@
pub mod screenshot;
pub mod warper;
/// Store a cached key/value pair, only recalculating when the key changes.
pub struct Cached<K: PartialEq + Clone, V> {
contents: Option<(K, V)>,
}
impl<K: PartialEq + Clone, V> Cached<K, V> {
pub fn new() -> Cached<K, V> {
Cached { contents: None }
}
/// Get the current key.
pub fn key(&self) -> Option<K> {
self.contents.as_ref().map(|(k, _)| k.clone())
}
/// Get the current value.
pub fn value(&self) -> Option<&V> {
self.contents.as_ref().map(|(_, v)| v)
}
/// Update the value if the key has changed.
pub fn update<F: FnMut(K) -> V>(&mut self, key: Option<K>, mut produce_value: F) {
if let Some(new_key) = key {
if self.key() != Some(new_key.clone()) {
self.contents = Some((new_key.clone(), produce_value(new_key)));
}
} else {
self.contents = None;
}
}
pub fn clear(&mut self) {
self.contents = None;
}
}