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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use super::{ConnectionState, Imgs, Message};

use crate::{
    game_input::GameInput,
    menu::main::DetailedInitializationStage,
    settings::ControlSettings,
    ui::{
        fonts::IcedFonts as Fonts,
        ice::{component::neat_button, style, widget::Image, Element, IcedUi as Ui, Id},
        Graphic,
    },
};
use client::ClientInitStage;
use common::assets::{self, AssetExt};
use i18n::Localization;
use iced::{button, Align, Column, Container, Length, Row, Space, Text};
use keyboard_keynames::key_layout::KeyLayout;
use serde::{Deserialize, Serialize};
#[cfg(feature = "singleplayer")]
use server::{ServerInitStage, WorldCivStage, WorldGenerateStage, WorldSimStage};

struct LoadingAnimation {
    speed_factor: f32,
    frames: Vec<Id>,
}
impl LoadingAnimation {
    fn new(raw: &(f32, Vec<String>), ui: &mut Ui) -> Self {
        let mut frames = vec![];
        for frame_path in raw.1.iter() {
            if let Ok(image) = assets::Image::load(frame_path) {
                frames.push(ui.add_graphic(Graphic::Image(image.read().to_image(), None)));
            } else {
                frames.push(
                    ui.add_graphic(Graphic::Image(
                        assets::Image::load("voxygen.element.not_found")
                            .unwrap_or_else(|_| panic!("Missing asset '{}'", frame_path))
                            .read()
                            .to_image(),
                        None,
                    )),
                )
            }
        }
        Self {
            speed_factor: raw.0,
            frames,
        }
    }
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
struct LoadingAnimationManifest(Vec<(f32, Vec<String>)>);
impl assets::Asset for LoadingAnimationManifest {
    type Loader = assets::RonLoader;

    const EXTENSION: &'static str = "ron";
}

/// Connecting screen for the main menu
pub struct Screen {
    cancel_button: button::State,
    add_button: button::State,
    tip_number: u16,
    loading_animation: LoadingAnimation,
}

impl Screen {
    pub fn new(ui: &mut Ui) -> Self {
        let animations =
            LoadingAnimationManifest::load("voxygen.element.animation.loaders.manifest")
                .expect(
                    "Missing loader manifest file 'voxygen/element/animation/loaders/manifest.ron'",
                )
                .cloned()
                .0;
        Self {
            cancel_button: Default::default(),
            add_button: Default::default(),
            tip_number: rand::random(),
            loading_animation: LoadingAnimation::new(
                &animations[rand::random::<usize>() % animations.len()],
                ui,
            ),
        }
    }

    pub(super) fn view(
        &mut self,
        fonts: &Fonts,
        imgs: &Imgs,
        connection_state: &ConnectionState,
        init_stage: &DetailedInitializationStage,
        time: f64,
        i18n: &Localization,
        button_style: style::button::Style,
        show_tip: bool,
        controls: &ControlSettings,
        key_layout: &Option<KeyLayout>,
    ) -> Element<Message> {
        // TODO: add built in support for animated images
        let frame_index = (time * self.loading_animation.speed_factor as f64)
            % self.loading_animation.frames.len() as f64;
        let frame_id = self.loading_animation.frames[frame_index as usize];

        let children = match connection_state {
            ConnectionState::InProgress => {
                let tip = if show_tip {
                    let key = |code| match controls.keybindings.get(&code) {
                        Some(Some(key_mouse)) => key_mouse.display_string(key_layout),
                        Some(None) => i18n.get_msg("main-unbound_key_tip").into_owned(),
                        None => match ControlSettings::default_binding(code) {
                            Some(key_mouse) => key_mouse.display_string(key_layout),
                            None => i18n.get_msg("main-unbound_key_tip").into_owned(),
                        },
                    };
                    let keys = i18n::fluent_args! {
                        "gameinput-togglelantern" => key(GameInput::ToggleLantern),
                        "gameinput-controls" => key(GameInput::Controls),
                        "gameinput-settings" => key(GameInput::Settings),
                        "gameinput-social" => key(GameInput::Social),
                        "gameinput-dance" => key(GameInput::Dance),
                        "gameinput-glide" => key(GameInput::Glide),
                        "gameinput-sit" => key(GameInput::Sit),
                        "gameinput-crafting" => key(GameInput::Crafting),
                        "gameinput-roll" => key(GameInput::Roll),
                        "gameinput-screenshot" => key(GameInput::Screenshot),
                    };
                    let tip = &i18n.get_variation_ctx("loading-tips", self.tip_number, &keys);
                    let tip = format!("{} {}", i18n.get_msg("main-tip"), tip);

                    Container::new(Text::new(tip).size(fonts.cyri.scale(25)))
                        .width(Length::Fill)
                        .height(Length::Fill)
                        .center_x()
                        .align_y(Align::End)
                        .into()
                } else {
                    Space::new(Length::Fill, Length::Fill).into()
                };

                let stage = {
                    let stage_message = match init_stage {
                        #[cfg(feature = "singleplayer")]
                        DetailedInitializationStage::Singleplayer => {
                            i18n.get_msg("hud-init-stage-singleplayer")
                        },
                        #[cfg(feature = "singleplayer")]
                        DetailedInitializationStage::SingleplayerServer(server_stage) => {
                            match server_stage {
                                ServerInitStage::DbMigrations => {
                                    i18n.get_msg("hud-init-stage-server-db-migrations")
                                },
                                ServerInitStage::DbVacuum => {
                                    i18n.get_msg("hud-init-stage-server-db-vacuum")
                                },
                                ServerInitStage::WorldGen(worldgen_stage) => match worldgen_stage {
                                    WorldGenerateStage::WorldSimGenerate(worldsim_stage) => {
                                        match worldsim_stage {
                                            WorldSimStage::Erosion(done) => i18n
                                                .get_msg_ctx(
                                                    "hud-init-stage-server-worldsim-erosion",
                                                    &i18n::fluent_args! { "percentage" => format!("{done:.0}") }
                                                ),
                                        }
                                    },
                                    WorldGenerateStage::WorldCivGenerate(worldciv_stage) => {
                                        match worldciv_stage {
                                            WorldCivStage::CivCreation(generated, total) => i18n
                                                .get_msg_ctx(
                                                    "hud-init-stage-server-worldciv-civcreate",
                                                    &i18n::fluent_args! {
                                                        "generated" => generated.to_string(),
                                                        "total" => total.to_string(),
                                                    }
                                                ),
                                            WorldCivStage::SiteGeneration => i18n.get_msg("hud-init-stage-server-worldciv-site"),
                                        }
                                    },
                                    WorldGenerateStage::EconomySimulation => i18n.get_msg("hud-init-stage-server-economysim"),
                                    WorldGenerateStage::SpotGeneration => i18n.get_msg("hud-init-stage-server-spotgen"),
                                },
                                ServerInitStage::StartingSystems => i18n.get_msg("hud-init-stage-server-starting"),
                            }
                        },
                        DetailedInitializationStage::StartingMultiplayer => {
                            i18n.get_msg("hud-init-stage-multiplayer")
                        },
                        DetailedInitializationStage::Client(client_stage) => match client_stage {
                            ClientInitStage::ConnectionEstablish => {
                                i18n.get_msg("hud-init-stage-client-connection-establish")
                            },
                            ClientInitStage::WatingForServerVersion => {
                                i18n.get_msg("hud-init-stage-client-request-server-version")
                            },
                            ClientInitStage::Authentication => {
                                i18n.get_msg("hud-init-stage-client-authentication")
                            },
                            ClientInitStage::LoadingInitData => {
                                i18n.get_msg("hud-init-stage-client-load-init-data")
                            },
                            ClientInitStage::StartingClient => {
                                i18n.get_msg("hud-init-stage-client-starting-client")
                            },
                        },
                        DetailedInitializationStage::CreatingRenderPipeline(done, total) => i18n
                            .get_msg_ctx(
                                "hud-init-stage-render-pipeline",
                                &i18n::fluent_args! { "done" => done, "total" => total },
                            ),
                    };

                    Container::new(Text::new(stage_message).size(fonts.cyri.scale(20)))
                        .width(Length::Fill)
                        .height(Length::Fill)
                        .padding(10)
                        .align_x(Align::Start)
                        .into()
                };

                let cancel = Container::new(neat_button(
                    &mut self.cancel_button,
                    i18n.get_msg("common-cancel"),
                    0.7,
                    button_style,
                    Some(Message::CancelConnect),
                ))
                .width(Length::Fill)
                .height(Length::Units(fonts.cyri.scale(30)))
                .center_x()
                .padding(3);

                let tip_cancel = Column::with_children(vec![tip, cancel.into()])
                    .width(Length::FillPortion(3))
                    .align_items(Align::Center)
                    .spacing(5)
                    .padding(5);

                let gear = Container::new(
                    Image::new(frame_id)
                        .width(Length::Units(64))
                        .height(Length::Units(64)),
                )
                .width(Length::Fill)
                .padding(10)
                .align_x(Align::End);

                let bottom_content =
                    Row::with_children(vec![stage, tip_cancel.into(), gear.into()])
                        .align_items(Align::Center)
                        .width(Length::Fill);

                let left_art = Image::new(imgs.loading_art_l)
                    .width(Length::Units(12))
                    .height(Length::Units(12));
                let right_art = Image::new(imgs.loading_art_r)
                    .width(Length::Units(12))
                    .height(Length::Units(12));

                let bottom_bar = Container::new(Row::with_children(vec![
                    left_art.into(),
                    bottom_content.into(),
                    right_art.into(),
                ]))
                .height(Length::Units(85))
                .style(style::container::Style::image(imgs.loading_art));

                vec![
                    Space::new(Length::Fill, Length::Fill).into(),
                    bottom_bar.into(),
                ]
            },
            ConnectionState::AuthTrustPrompt { msg, .. } => {
                let text = Text::new(msg).size(fonts.cyri.scale(25));

                let cancel = neat_button(
                    &mut self.cancel_button,
                    i18n.get_msg("common-cancel"),
                    0.7,
                    button_style,
                    Some(Message::TrustPromptCancel),
                );
                let add = neat_button(
                    &mut self.add_button,
                    i18n.get_msg("common-add"),
                    0.7,
                    button_style,
                    Some(Message::TrustPromptAdd),
                );

                let content = Column::with_children(vec![
                    text.into(),
                    Container::new(
                        Row::with_children(vec![cancel, add])
                            .spacing(20)
                            .height(Length::Units(25)),
                    )
                    .align_x(Align::End)
                    .width(Length::Fill)
                    .into(),
                ])
                .spacing(4)
                .max_width(520)
                .width(Length::Fill)
                .height(Length::Fill);

                let prompt_window = Container::new(content)
                    .style(
                        style::container::Style::color_with_double_cornerless_border(
                            (22, 18, 16, 255).into(),
                            (11, 11, 11, 255).into(),
                            (54, 46, 38, 255).into(),
                        ),
                    )
                    .padding(20);

                let container = Container::new(prompt_window)
                    .width(Length::Fill)
                    .height(Length::Fill)
                    .center_x()
                    .center_y();

                vec![
                    container.into(),
                    Space::new(Length::Fill, Length::Units(fonts.cyri.scale(15))).into(),
                ]
            },
        };

        Column::with_children(children)
            .width(Length::Fill)
            .height(Length::Fill)
            .into()
    }
}