aboutsummaryrefslogtreecommitdiff
path: root/pixel-client/src/menu/ingame.rs
blob: 66c77bcee06376730e0967717ff6b625d5f41e38 (plain)
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
use super::main::MainMenu;
use crate::{
    config::Config,
    game::Game,
    render::{sprite::SpriteDraw, AtlasLayout, Renderer},
    strings::tr,
    ui::UiState,
    State,
};
use hurrycurry_protocol::glam::Vec2;
use sdl2::{
    keyboard::{KeyboardState, Keycode},
    mouse::MouseState,
};

pub struct IngameMenu {
    game: Box<Game>,
    pub ui_state: UiState,
    overlay_shown: bool,
    next_state: Option<Box<State>>,
}
impl IngameMenu {
    pub fn new(game: Game) -> Self {
        Self {
            overlay_shown: false,
            game: Box::new(game),
            ui_state: UiState::default(),
            next_state: None,
        }
    }
    pub fn tick(
        &mut self,
        dt: f32,
        keyboard: &KeyboardState,
        mouse: &MouseState,
        layout: &AtlasLayout,
    ) -> Option<Box<State>> {
        self.game.tick(dt, keyboard, layout);
        self.ui_state.update(keyboard, mouse, dt);
        self.next_state.take()
    }
    pub fn keyboard_event(&mut self, keycode: Keycode, down: bool) {
        self.ui_state.keyboard_event(keycode, down);
        if down && keycode == Keycode::Escape {
            self.overlay_shown = !self.overlay_shown
        }
    }
    pub fn draw(&mut self, ctx: &mut Renderer, _config: &mut Config) {
        self.game.draw(ctx);
        if self.overlay_shown {
            let mut main_menu = false;
            ctx.draw_ui(SpriteDraw::overlay(
                ctx.misc_textures.solid,
                Vec2::ZERO,
                ctx.ui_size,
                Some([0, 0, 0, 130]),
            ));
            self.ui_state.draw(ctx, |ui| {
                ui.horizontal(|ui| {
                    ui.advance(Vec2::splat(20.));
                    ui.vertical(|ui| {
                        ui.advance(Vec2::splat(20.));
                        let w = 80.;
                        main_menu |= ui.button(w, tr("c.menu.ingame.resume"));
                        ui.advance(Vec2::Y * 10.);
                        main_menu |= ui.button(w, tr("c.menu.ingame.main_menu"));
                        if ui.button(w, tr("c.menu.ingame.quit")) {
                            self.next_state = Some(Box::new(State::Quit))
                        }
                    });
                });
            });
            if main_menu {
                self.next_state = Some(Box::new(State::MainMenu(MainMenu::new(ctx.atlas_layout()))))
            }
        }
    }
}