1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::collections::HashMap;

use geom::Percent;
use map_gui::load::MapLoader;
use map_gui::tools::{open_browser, PopupMsg};
use map_model::PermanentMapEdits;
use widgetry::{EventCtx, Key, Line, Panel, SimpleState, State, Text, Widget};

use crate::app::{App, Transition};
use crate::edit::apply_map_edits;
use crate::sandbox::{GameplayMode, SandboxMode};

pub struct Proposals {
    proposals: HashMap<String, PermanentMapEdits>,
    current: Option<String>,
}

impl Proposals {
    pub fn new_state(ctx: &mut EventCtx, current: Option<String>) -> Box<dyn State<App>> {
        let mut proposals = HashMap::new();
        let mut tab_buttons = Vec::new();
        let mut current_tab_rows = Vec::new();
        // If a proposal has fallen out of date, it'll be skipped with an error logged. Since these
        // are under version control, much more likely to notice when they break (or we could add a
        // step to data/regen.sh).
        for (name, edits) in
            abstio::load_all_objects::<PermanentMapEdits>(abstio::path("system/proposals"))
        {
            if current == Some(name.clone()) {
                let mut txt = Text::new();
                txt.add_line(Line(edits.get_title()).small_heading());
                for l in edits.proposal_description.iter().skip(1) {
                    txt.add_line(l);
                }
                current_tab_rows.push(
                    txt.wrap_to_pct(ctx, 70)
                        .into_widget(ctx)
                        .margin_below(15)
                        .margin_above(15),
                );

                if edits.proposal_link.is_some() {
                    current_tab_rows.push(
                        ctx.style()
                            .btn_plain
                            .btn()
                            .label_underlined_text("Read detailed write-up")
                            .build_def(ctx)
                            .margin_below(10),
                    );
                }
                current_tab_rows.push(
                    ctx.style()
                        .btn_solid_primary
                        .text("Try out this proposal")
                        .hotkey(Key::Enter)
                        .build_def(ctx),
                );

                tab_buttons.push(
                    ctx.style()
                        .btn_tab
                        .text(edits.get_title())
                        .disabled(true)
                        .build_def(ctx)
                        .margin_below(10),
                );
            } else {
                let hotkey = Key::NUM_KEYS
                    .get(proposals.len())
                    .map(|key| widgetry::MultiKey::from(*key));
                tab_buttons.push(
                    ctx.style()
                        .btn_outline
                        .text(edits.get_title())
                        .no_tooltip()
                        .hotkey(hotkey)
                        .build_widget(ctx, &name)
                        .margin_below(10),
                );
            }

            proposals.insert(name, edits);
        }

        let panel = Panel::new_builder(Widget::col(vec![
            Widget::row(vec![
                Line("Community proposals").small_heading().into_widget(ctx),
                ctx.style().btn_close_widget(ctx),
            ]),
            {
                let mut txt =
                    Text::from("These are proposed changes to Seattle made by community members.");
                txt.add_line("Contact dabreegster@gmail.com to add your idea here!");
                txt.into_widget(ctx).centered_horiz()
            },
            Widget::custom_row(tab_buttons)
                .flex_wrap(ctx, Percent::int(80))
                .margin_above(60),
            Widget::col(current_tab_rows),
        ]))
        .build(ctx);
        <dyn SimpleState<_>>::new_state(panel, Box::new(Proposals { proposals, current }))
    }
}

impl SimpleState<App> for Proposals {
    fn on_click(&mut self, ctx: &mut EventCtx, app: &mut App, x: &str, _: &Panel) -> Transition {
        match x {
            "close" => Transition::Pop,
            "Try out this proposal" => launch(
                ctx,
                app,
                self.proposals[self.current.as_ref().unwrap()].clone(),
            ),
            "Read detailed write-up" => {
                open_browser(
                    self.proposals[self.current.as_ref().unwrap()]
                        .proposal_link
                        .clone()
                        .unwrap(),
                );
                Transition::Keep
            }
            x => Transition::Replace(Proposals::new_state(ctx, Some(x.to_string()))),
        }
    }
}

fn launch(ctx: &mut EventCtx, app: &App, edits: PermanentMapEdits) -> Transition {
    #[cfg(not(target_arch = "wasm32"))]
    {
        if !abstio::file_exists(edits.map_name.path()) {
            return map_gui::tools::prompt_to_download_missing_data(ctx, edits.map_name);
        }
    }

    Transition::Push(MapLoader::new_state(
        ctx,
        app,
        edits.map_name.clone(),
        Box::new(move |ctx, app| {
            // Apply edits before setting up the sandbox, for simplicity
            let maybe_err = ctx.loading_screen("apply edits", |ctx, mut timer| {
                match edits.into_edits(&app.primary.map) {
                    Ok(edits) => {
                        apply_map_edits(ctx, app, edits);
                        app.primary
                            .map
                            .recalculate_pathfinding_after_edits(&mut timer);
                        None
                    }
                    Err(err) => Some(err),
                }
            });
            if let Some(err) = maybe_err {
                Transition::Replace(PopupMsg::new_state(
                    ctx,
                    "Can't load proposal",
                    vec![err.to_string()],
                ))
            } else {
                app.primary.layer = Some(Box::new(crate::layer::map::Static::edits(ctx, app)));
                Transition::Replace(SandboxMode::simple_new(
                    app,
                    GameplayMode::PlayScenario(
                        app.primary.map.get_name().clone(),
                        crate::pregame::default_scenario_for_map(app.primary.map.get_name()),
                        Vec::new(),
                    ),
                ))
            }
        }),
    ))
}