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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use map_gui::tools::grey_out_map;
use widgetry::{
    hotkeys, ButtonStyle, Color, ControlState, EventCtx, GeomBatch, GfxCtx, Image, Key, Line,
    Outcome, Panel, State, Text, Widget,
};

use crate::app::App;
use crate::app::Transition;

pub struct CutsceneBuilder {
    name: String,
    scenes: Vec<Scene>,
}

enum Layout {
    PlayerSpeaking,
    BossSpeaking,
    Extra(&'static str, f64),
}

struct Scene {
    layout: Layout,
    msg: Text,
}

impl CutsceneBuilder {
    pub fn new(name: &str) -> CutsceneBuilder {
        CutsceneBuilder {
            name: name.to_string(),
            scenes: Vec::new(),
        }
    }

    fn fg_color() -> Color {
        ButtonStyle::outline_dark_fg().fg
    }

    pub fn player<I: Into<String>>(mut self, msg: I) -> CutsceneBuilder {
        self.scenes.push(Scene {
            layout: Layout::PlayerSpeaking,
            msg: Text::from(Line(msg).fg(Self::fg_color())),
        });
        self
    }

    pub fn boss<I: Into<String>>(mut self, msg: I) -> CutsceneBuilder {
        self.scenes.push(Scene {
            layout: Layout::BossSpeaking,
            msg: Text::from(Line(msg).fg(Self::fg_color())),
        });
        self
    }

    pub fn extra<I: Into<String>>(
        mut self,
        character: &'static str,
        scale: f64,
        msg: I,
    ) -> CutsceneBuilder {
        self.scenes.push(Scene {
            layout: Layout::Extra(character, scale),
            msg: Text::from(Line(msg).fg(Self::fg_color())),
        });
        self
    }

    pub fn build(
        self,
        ctx: &mut EventCtx,
        make_task: Box<dyn Fn(&mut EventCtx) -> Widget>,
    ) -> Box<dyn State<App>> {
        Box::new(CutscenePlayer {
            panel: make_panel(ctx, &self.name, &self.scenes, &make_task, 0),
            name: self.name,
            scenes: self.scenes,
            idx: 0,
            make_task,
        })
    }
}

struct CutscenePlayer {
    name: String,
    scenes: Vec<Scene>,
    idx: usize,
    panel: Panel,
    make_task: Box<dyn Fn(&mut EventCtx) -> Widget>,
}

impl State<App> for CutscenePlayer {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        if let Outcome::Clicked(x) = self.panel.event(ctx) {
            match x.as_ref() {
                "quit" => {
                    // TODO Should SandboxMode use on_destroy for this?
                    app.primary.clear_sim();
                    app.set_prebaked(None);
                    return Transition::Multi(vec![Transition::Pop, Transition::Pop]);
                }
                "back" => {
                    self.idx -= 1;
                    self.panel =
                        make_panel(ctx, &self.name, &self.scenes, &self.make_task, self.idx);
                }
                "next" => {
                    self.idx += 1;
                    self.panel =
                        make_panel(ctx, &self.name, &self.scenes, &self.make_task, self.idx);
                }
                "Skip cutscene" => {
                    self.idx = self.scenes.len();
                    self.panel =
                        make_panel(ctx, &self.name, &self.scenes, &self.make_task, self.idx);
                }
                "Start" => {
                    return Transition::Pop;
                }
                _ => unreachable!(),
            }
        }
        // TODO Should the Panel for text widgets with wrapping do this instead?
        if ctx.input.is_window_resized() {
            self.panel = make_panel(ctx, &self.name, &self.scenes, &self.make_task, self.idx);
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, _: &App) {
        self.panel.draw(g);
    }
}

fn make_panel(
    ctx: &mut EventCtx,
    name: &str,
    scenes: &[Scene],
    make_task: &dyn Fn(&mut EventCtx) -> Widget,
    idx: usize,
) -> Panel {
    let prev_builder = ButtonStyle::plain_dark_fg()
        .icon("system/assets/tools/circled_prev.svg")
        .image_dims(45.0)
        .hotkey(Key::LeftArrow)
        .bg_color(Color::CLEAR, ControlState::Disabled);

    let next = prev_builder
        .clone()
        .image_path("system/assets/tools/circled_next.svg")
        .hotkey(hotkeys(vec![Key::RightArrow, Key::Space, Key::Enter]))
        .build_widget(ctx, "next");

    let prev = prev_builder.disabled(idx == 0).build_widget(ctx, "back");

    let inner = if idx == scenes.len() {
        Widget::custom_col(vec![
            (make_task)(ctx),
            ctx.style()
                .btn_solid_primary
                .text("Start")
                .hotkey(Key::Enter)
                .build_def(ctx)
                .centered_horiz()
                .align_bottom(),
        ])
    } else {
        Widget::custom_col(vec![
            match scenes[idx].layout {
                Layout::PlayerSpeaking => Widget::custom_row(vec![
                    GeomBatch::load_svg(ctx, "system/assets/characters/boss.svg.gz")
                        .scale(0.75)
                        .autocrop()
                        .into_widget(ctx),
                    Widget::custom_row(vec![
                        scenes[idx]
                            .msg
                            .clone()
                            .wrap_to_pct(ctx, 30)
                            .into_widget(ctx),
                        Image::from_path("system/assets/characters/player.svg")
                            .untinted()
                            .into_widget(ctx),
                    ])
                    .align_right(),
                ]),
                Layout::BossSpeaking => Widget::custom_row(vec![
                    GeomBatch::load_svg(ctx, "system/assets/characters/boss.svg.gz")
                        .scale(0.75)
                        .autocrop()
                        .into_widget(ctx),
                    scenes[idx]
                        .msg
                        .clone()
                        .wrap_to_pct(ctx, 30)
                        .into_widget(ctx),
                    Image::from_path("system/assets/characters/player.svg")
                        .untinted()
                        .into_widget(ctx)
                        .align_right(),
                ]),
                Layout::Extra(filename, scale) => Widget::custom_row(vec![
                    GeomBatch::load_svg(ctx, "system/assets/characters/boss.svg.gz")
                        .scale(0.75)
                        .autocrop()
                        .into_widget(ctx),
                    Widget::col(vec![
                        GeomBatch::load_svg(
                            ctx.prerender,
                            format!("system/assets/characters/{}", filename),
                        )
                        .scale(scale)
                        .autocrop()
                        .into_widget(ctx),
                        scenes[idx]
                            .msg
                            .clone()
                            .wrap_to_pct(ctx, 30)
                            .into_widget(ctx),
                    ]),
                    Image::from_path("system/assets/characters/player.svg")
                        .untinted()
                        .into_widget(ctx),
                ])
                .evenly_spaced(),
            }
            .margin_above(100),
            Widget::col(vec![
                Widget::row(vec![prev.margin_right(40), next]).centered_horiz(),
                ButtonStyle::outline_dark_fg()
                    .text("Skip cutscene")
                    .build_def(ctx)
                    .centered_horiz(),
            ])
            .align_bottom(),
        ])
    };

    let col = vec![
        Widget::row(vec![
            Line(name).small_heading().into_widget(ctx),
            ctx.style()
                .btn_back("Home")
                .build_widget(ctx, "quit")
                .align_right(),
        ])
        .margin_below(40),
        inner
            .fill_height()
            .padding(42)
            .bg(Color::WHITE)
            .outline(ctx.style().btn_solid.outline),
    ];

    Panel::new_builder(Widget::col(col)).build(ctx)
}

pub struct ShowMessage {
    panel: Panel,
}

impl ShowMessage {
    pub fn new_state(ctx: &mut EventCtx, contents: Widget, bg: Color) -> Box<dyn State<App>> {
        Box::new(ShowMessage {
            panel: Panel::new_builder(
                Widget::custom_col(vec![
                    contents,
                    ctx.style()
                        .btn_solid_primary
                        .text("OK")
                        .hotkey(hotkeys(vec![Key::Escape, Key::Space, Key::Enter]))
                        .build_def(ctx)
                        .centered_horiz()
                        .align_bottom(),
                ])
                .padding(16)
                .bg(bg),
            )
            .exact_size_percent(50, 50)
            .build_custom(ctx),
        })
    }
}

impl State<App> for ShowMessage {
    fn event(&mut self, ctx: &mut EventCtx, _: &mut App) -> Transition {
        match self.panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "OK" => Transition::Pop,
                _ => unreachable!(),
            },
            _ => Transition::Keep,
        }
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        grey_out_map(g, app);
        self.panel.draw(g);
    }
}