mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-24 15:02:59 +03:00
Lift Cached to widgetry
This commit is contained in:
parent
c3f211ccfd
commit
af5811c33c
@ -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,
|
||||
};
|
||||
|
@ -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;
|
||||
|
@ -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};
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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};
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user