aboutsummaryrefslogtreecommitdiff
path: root/pixel-client/src/menu/main.rs
blob: 38dcaaeae9def5a7606ce4c8e8f79e42f15415ea (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
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
/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
use super::{
    background::MenuBackground, credits::CreditsMenu, ingame::IngameMenu, settings::SettingsMenu,
};
use crate::{
    config::Config,
    game::Game,
    strings::tr,
    render::{sprite::SpriteDraw, AtlasLayout, Renderer},
    ui::UiState,
    State,
};
use hurrycurry_client_lib::network::sync::Network;
use hurrycurry_protocol::glam::Vec2;
use sdl2::{
    keyboard::{KeyboardState, Keycode},
    mouse::MouseState,
};

pub struct MainMenu {
    background: MenuBackground,
    fade_in: f32,
    pub ui_state: UiState,
    server_address: String,
    next_state: Option<Box<State>>,
    settings: Option<SettingsMenu>,
    credits: Option<CreditsMenu>,
}

impl MainMenu {
    pub fn new(layout: &AtlasLayout) -> Self {
        Self {
            background: MenuBackground::new(layout),
            fade_in: 0.,
            server_address: String::from("ws://127.0.0.1"),
            ui_state: UiState::default(),
            next_state: None,
            settings: None,
            credits: None,
        }
    }
    pub fn tick(
        &mut self,
        dt: f32,
        keyboard: &KeyboardState,
        mouse: &MouseState,
        _layout: &AtlasLayout,
    ) -> Option<Box<State>> {
        self.fade_in = (self.fade_in + dt).min(1.);
        self.ui_state.update(keyboard, mouse, dt);
        self.background.tick(dt);
        self.next_state.take()
    }
    pub fn keyboard_event(&mut self, keycode: Keycode, down: bool) {
        self.ui_state.keyboard_event(keycode, down);
    }
    pub fn draw(&mut self, ctx: &mut Renderer, config: &mut Config) {
        self.background.draw(ctx);

        self.ui_state.draw(ctx, |ui| {
            if let Some(settings) = &mut self.settings {
                if settings.draw(ui, config) {
                    self.settings = None;
                }
                return;
            }
            if let Some(credits) = &mut self.credits {
                if credits.draw(ui) {
                    self.credits = None;
                }
                return;
            }
            if ui.button(80., tr("c.menu.play.connect")) {
                self.next_state = Some(Box::new(State::Ingame(IngameMenu::new(Game::new(
                    Network::connect(&self.server_address).unwrap(),
                    config,
                    ui.renderer.atlas_layout(),
                )))))
            }
            ui.textedit(80., &mut self.server_address);
            if ui.button(80., tr("c.menu.settings")) {
                self.settings = Some(SettingsMenu::default())
            }
            if ui.button(80., tr("c.menu.about.credits")) {
                self.credits = Some(CreditsMenu::default())
            }
            if ui.button(80., tr("c.menu.quit")) {
                self.next_state = Some(Box::new(State::Quit));
            }
            ui.fill();
        });

        ctx.draw_ui(SpriteDraw::overlay(
            ctx.misc_textures.solid,
            Vec2::ZERO,
            ctx.ui_size,
            Some([0, 0, 0, 255 - (self.fade_in * 255.) as u8]),
        ));
    }
}