Skip to content

Commit

Permalink
Plan: replace all results at once + show ok/error symbol for each plan
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjørnar Luteberget committed Sep 13, 2019
1 parent dff9bf2 commit 2734d6e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "junction"
version = "0.2.3"
version = "0.2.5"
authors = ["Bjørnar Luteberget <bjlut@railcomplete.no>"]
edition = "2018"

Expand Down
26 changes: 13 additions & 13 deletions src/document/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub struct AnalysisOutput {
pub dgraph :Option<(Generation, Arc<DGraph>)>,
pub interlocking :Option<(Generation, Arc<interlocking::Interlocking>)>,
pub dispatch :Vec<Option<(Generation, dispatch::DispatchOutput)>>,
pub plandispatches :HashMap<usize, Vec<Option<(Generation, dispatch::DispatchOutput)>>>,
//pub plandispatches :HashMap<usize, Vec<Option<(Generation, dispatch::DispatchOutput)>>>,
pub plandispatches :Vec<Option<(Generation, Vec<dispatch::DispatchOutput>)>>,
}

pub struct Analysis {
Expand All @@ -41,7 +42,7 @@ pub enum SetData {
DGraph(Generation, Arc<DGraph>),
Interlocking(Generation, Arc<interlocking::Interlocking>),
Dispatch(Generation, usize,dispatch::DispatchOutput),
PlanDispatch(Generation, usize,usize,dispatch::DispatchOutput),
PlanDispatch(Generation, usize,Vec<dispatch::DispatchOutput>),
}

impl app::BackgroundUpdates for Analysis {
Expand All @@ -54,10 +55,11 @@ impl app::BackgroundUpdates for Analysis {
self.output.dispatch.vecmap_insert(idx, (g, h));
//cache.clear_dispatch(idx);
},
SetData::PlanDispatch(g, plan_idx,dispatch_idx,h) => {
self.output.plandispatches.entry(plan_idx)
.or_insert(Vec::new())
.vecmap_insert(dispatch_idx, (g, h));
SetData::PlanDispatch(g, plan_idx,hs) => {
//self.output.plandispatches.entry(plan_idx)
//.or_insert(Vec::new())
//.vecmap_insert(dispatch_idx, (g, h));
self.output.plandispatches.vecmap_insert(plan_idx, (g,hs));
},
}
}
Expand Down Expand Up @@ -137,14 +139,12 @@ impl Analysis {

info!("Planning successful. {:?}", planresults);

for (dispatch_idx,(dispatch,history)) in planresults.into_iter().enumerate() {
let view = dispatch::DispatchOutput::from_history(
dispatch.clone(), &dgraph, history);
let send_ok = tx.send(SetData::PlanDispatch(gen, *plan_idx, dispatch_idx, view));
if !send_ok.is_ok() { println!("job cancelled after plan dispatch {}/{}",
plan_idx, dispatch_idx); }
}
let dispatches = planresults.into_iter().map(|(d,h)| {
dispatch::DispatchOutput::from_history(d, &dgraph, h)
}).collect();

let send_ok = tx.send(SetData::PlanDispatch(gen, *plan_idx, dispatches));
if !send_ok.is_ok() { println!("job cancelled after plan dispatch {}", plan_idx); }
}

});
Expand Down
10 changes: 8 additions & 2 deletions src/document/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ impl InstantCache {
let (dgraph_gen,dgraph) = analysis.data().dgraph.as_ref()?;
let (d,time) = r;
let (dispatch_gen, dispatch) = match d {
Ok(d) => analysis.data().dispatch.get(d)?.as_ref()?,
Err((p,d)) => analysis.data().plandispatches.get(&p)?.get(d)?.as_ref()?,
Ok(d) => {
let (gen,d) = analysis.data().dispatch.get(d)?.as_ref()?;
(gen,d)
}
Err((p,d)) => {
let (gen,ds) = analysis.data().plandispatches.get(p)?.as_ref()?;
(gen,ds.get(d)?)
}
};
let cached_gen = self.cached.as_ref().map(|x| &x.0);
let cached_ref = self.cached.as_ref().map(|x| &x.1);
Expand Down
31 changes: 22 additions & 9 deletions src/gui/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ use crate::gui::plan;
use crate::gui::diagram::diagram_view;
use crate::util::VecMap;
use crate::gui::diagram::*;
use crate::gui::plan::planning_icon;
use crate::gui::widgets::Draw;
use crate::document::infview::InfView;

pub fn dispatch_view(config :&Config, inf_canvas :Option<&Draw>, inf_view :&InfView,
analysis :&mut Analysis, dv :&mut DispatchView) -> Option<Option<DispatchView>> {
let mut new_dispatch :Option<Option<DispatchView>> = None;
let sel = dispatch_select_bar(&Some(*dv), analysis);
let sel = dispatch_select_bar(config, &Some(*dv), analysis);
new_dispatch = sel.or(new_dispatch);

match dv {
Expand Down Expand Up @@ -54,13 +55,17 @@ pub fn dispatch_view(config :&Config, inf_canvas :Option<&Draw>, inf_view :&InfV
new_dispatch = new_auto.or(new_dispatch);

if let Some(manual) = &mut auto.dispatch {
let graph = analysis.data().plandispatches.get(&auto.plan_idx)
.and_then(|p| p.vecmap_get(manual.dispatch_idx));
if let Some((_gen,graph)) = graph {
diagram_view(config, inf_canvas, inf_view, analysis, manual, graph);
} else {
// Plan doesn't exist anymore.
auto.dispatch = None;
if let Some(Some((_gen,dispatches))) = analysis.data().plandispatches.get(auto.plan_idx) {
if let Some(graph) = dispatches.get(manual.dispatch_idx) {
diagram_view(config, inf_canvas, inf_view, analysis, manual, graph);
} else {
// Plan doesn't exist anymore.
if dispatches.len() > 0 {
manual.dispatch_idx = dispatches.len()-1;
} else {
auto.dispatch = None;
}
}
}
}
},
Expand All @@ -77,7 +82,8 @@ pub enum Action {
}

/// Select a new dispatch view from manual or auto dispatches already existing in model
pub fn dispatch_select_bar(dispatch_view :&Option<DispatchView>, analysis :&mut Analysis) -> Option<Option<DispatchView>> {
pub fn dispatch_select_bar(config :&Config, dispatch_view :&Option<DispatchView>,
analysis :&mut Analysis) -> Option<Option<DispatchView>> {
unsafe {
let mut new_dispatch_auto = None;
let mut retval = None;
Expand Down Expand Up @@ -162,6 +168,13 @@ pub fn dispatch_select_bar(dispatch_view :&Option<DispatchView>, analysis :&mut

igAlignTextToFramePadding();
widgets::show_text("\u{f0d0}");
igSameLine(0.0,-1.0);

if let Some(Some((gen, ds))) = analysis.data().plandispatches.get(*id) {
planning_icon(config, analysis, *gen, ds);
igSameLine(0.0,-1.0);
}


igSameLine(0.0,-1.0);
if let Some(new_name) = widgets::edit_text(const_cstr!("##pnm").as_ptr(),
Expand Down
2 changes: 1 addition & 1 deletion src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn main(app :&mut App) -> bool {
let frameh = igGetFrameHeight();
let framespace = igGetFrameHeightWithSpacing() - frameh;
igSetCursorPos(pos + ImVec2 { x: 2.0*framespace, y : -frameh-3.0*framespace });
let new_dispatchview = dispatch::dispatch_select_bar(&None, analysis);
let new_dispatchview = dispatch::dispatch_select_bar(config, &None, analysis);
if let Some(nd) = new_dispatchview { *dispatch_view = nd; }
igSetCursorPos(pos);
}
Expand Down
74 changes: 51 additions & 23 deletions src/gui/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::gui::widgets;
use crate::config::*;
use crate::document::infview::{InfView, unround_coord};
use crate::gui::infrastructure::draw::box_around;
use crate::document::dispatch::DispatchOutput;

enum Action {
VisitDelete { key :VisitKey },
Expand Down Expand Up @@ -50,7 +51,7 @@ pub fn edit_plan(config :&Config,
}

igSameLine(0.0,-1.0);
plan_dispatches(analysis, auto_dispatch);
plan_dispatches(config, analysis, auto_dispatch);

widgets::sep();

Expand Down Expand Up @@ -304,38 +305,65 @@ pub fn edit_plan(config :&Config,
new_dispatchview
}

fn plan_dispatches(analysis :&Analysis, adv :&mut AutoDispatchView) {
pub fn planning_icon(config :&Config, analysis :&Analysis, generation :usize, dispatches :&Vec<DispatchOutput>) {
unsafe {
let dispatch_idx = if let Some(ManualDispatchView { dispatch_idx, .. }) = &adv.dispatch {
Some(*dispatch_idx) } else { None };
let dispatch_name = if let Some(dispatch_idx) = dispatch_idx {
CString::new(format!("Dispatch {}", dispatch_idx)).unwrap()
} else { CString::new(format!("None")).unwrap() };
if generation == *analysis.generation() {
if dispatches.len() > 0 {
// Planning was successful
igPushStyleColorU32(ImGuiCol__ImGuiCol_Text as _,
config.color_u32(RailUIColorName::CanvasSignalProceed));
widgets::show_text("\u{f00c}");
igPopStyleColor(1);
} else {
// Planning failed
igPushStyleColorU32(ImGuiCol__ImGuiCol_Text as _,
config.color_u32(RailUIColorName::CanvasSignalStop));
widgets::show_text("\u{f00d}");
igPopStyleColor(1);
}
} else {
// Planning still running
igPushStyleColorU32(ImGuiCol__ImGuiCol_Text as _,
config.color_u32(RailUIColorName::CanvasTrackDrawing));
widgets::show_text("\u{f110}");
igPopStyleColor(1);
}
}
}

if igBeginCombo(const_cstr!("##chtr").as_ptr(), dispatch_name.as_ptr(), 0) {
if igSelectable(const_cstr!("None").as_ptr(), dispatch_idx.is_none(), 0 as _, ImVec2::zero()) {
fn plan_dispatches(config :&Config, analysis :&Analysis, adv :&mut AutoDispatchView) {
unsafe {
if let Some(Some((generation,dispatches))) = analysis.data().plandispatches.get(adv.plan_idx) {
planning_icon(config,analysis,*generation,dispatches);
igSameLine(0.0,-1.0);

adv.dispatch = None;
}
let dispatch_idx = if let Some(ManualDispatchView { dispatch_idx, .. }) = &adv.dispatch {
Some(*dispatch_idx) } else { None };
let dispatch_name = if let Some(dispatch_idx) = dispatch_idx {
CString::new(format!("Dispatch {}", dispatch_idx)).unwrap()
} else { CString::new(format!("None")).unwrap() };

if igBeginCombo(const_cstr!("##chtr").as_ptr(), dispatch_name.as_ptr(), 0) {
if igSelectable(const_cstr!("None").as_ptr(), dispatch_idx.is_none(), 0 as _, ImVec2::zero()) {

adv.dispatch = None;
}

if let Some(dispatches) = analysis.data().plandispatches.get(&adv.plan_idx) {
for (di,d) in dispatches.iter().enumerate() {
igPushIDInt(di as _);
if let Some(d) = d {
if igSelectable(const_cstr!("##asdf").as_ptr(),
dispatch_idx == Some(di),
0 as _ , ImVec2::zero()) {
adv.dispatch = Some(ManualDispatchView::new(di));
}

igSameLine(0.0,-1.0);
widgets::show_text(&format!("Dispatch {}", di));
if igSelectable(const_cstr!("##asdf").as_ptr(),
dispatch_idx == Some(di),
0 as _ , ImVec2::zero()) {
adv.dispatch = Some(ManualDispatchView::new(di));
}

igSameLine(0.0,-1.0);
widgets::show_text(&format!("Dispatch {}", di));
igPopID();
}
}

igEndCombo();
igEndCombo();
}
}
}
}
Expand Down

0 comments on commit 2734d6e

Please sign in to comment.