aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pixel-client/src/main.rs20
-rw-r--r--pixel-client/src/menu.rs150
-rw-r--r--pixel-client/src/menu/background.rs79
-rw-r--r--pixel-client/src/menu/main.rs100
-rw-r--r--pixel-client/src/menu/mod.rs4
-rw-r--r--pixel-client/src/menu/settings.rs11
-rw-r--r--pixel-client/src/ui.rs57
7 files changed, 262 insertions, 159 deletions
diff --git a/pixel-client/src/main.rs b/pixel-client/src/main.rs
index 05e19194..8c231031 100644
--- a/pixel-client/src/main.rs
+++ b/pixel-client/src/main.rs
@@ -19,7 +19,7 @@
use clap::{Parser, Subcommand};
use game::Game;
use hurrycurry_protocol::glam::Vec2;
-use menu::Menu;
+use menu::main::MainMenu;
use network::Network;
use render::Renderer;
use sdl2::{event::Event, keyboard::KeyboardState, mouse::MouseState, pixels::Color};
@@ -51,7 +51,7 @@ pub enum Action {
pub enum State {
Ingame(Box<Game>),
- Menu(Menu),
+ MainMenu(MainMenu),
Quit,
}
@@ -87,7 +87,7 @@ fn main() {
let mut renderer = Renderer::init(&texture_creator);
let mut state = match args.action.unwrap_or_default() {
- Action::Menu => State::Menu(Menu::new(renderer.atlas_layout())),
+ Action::Menu => State::MainMenu(MainMenu::new(renderer.atlas_layout())),
Action::Join { server_address } => State::Ingame(Box::new(Game::new(
Network::connect(&server_address).unwrap(),
renderer.atlas_layout(),
@@ -108,7 +108,9 @@ fn main() {
let dt = last_tick.elapsed().min(Duration::from_secs_f32(1. / 30.));
let next = match &mut state {
State::Ingame(x) => x.tick(dt.as_secs_f32(), &keyboard, renderer.atlas_layout()),
- State::Menu(x) => x.tick(dt.as_secs_f32(), &keyboard, &mouse, renderer.atlas_layout()),
+ State::MainMenu(x) => {
+ x.tick(dt.as_secs_f32(), &keyboard, &mouse, renderer.atlas_layout())
+ }
State::Quit => break,
};
if let Some(next) = next {
@@ -120,7 +122,7 @@ fn main() {
renderer.set_ui_view(4.);
match &mut state {
State::Ingame(x) => x.draw(&mut renderer),
- State::Menu(x) => x.draw(&mut renderer),
+ State::MainMenu(x) => x.draw(&mut renderer),
State::Quit => (),
}
@@ -137,7 +139,7 @@ fn main() {
..
} => match &mut state {
State::Ingame(_) => (),
- State::Menu(menu) => menu.keyboard_event(keycode, false),
+ State::MainMenu(menu) => menu.keyboard_event(keycode, false),
_ => (),
},
Event::KeyDown {
@@ -145,7 +147,11 @@ fn main() {
..
} => match &mut state {
State::Ingame(_) => (),
- State::Menu(menu) => menu.keyboard_event(keycode, true),
+ State::MainMenu(menu) => menu.keyboard_event(keycode, true),
+ _ => (),
+ },
+ Event::TextInput { text, .. } => match &mut state {
+ State::MainMenu(menu) => menu.ui_state.text_input(text),
_ => (),
},
_ => {}
diff --git a/pixel-client/src/menu.rs b/pixel-client/src/menu.rs
deleted file mode 100644
index 86c71d1f..00000000
--- a/pixel-client/src/menu.rs
+++ /dev/null
@@ -1,150 +0,0 @@
-/*
- 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 crate::{
- game::Game,
- network::Network,
- render::{sprite::SpriteDraw, AtlasLayout, Renderer},
- tilemap::Tilemap,
- ui::UiState,
- State,
-};
-use hurrycurry_protocol::{
- glam::{IVec2, Vec2},
- TileIndex,
-};
-use rand::{random, seq::IndexedRandom, thread_rng};
-use sdl2::{
- keyboard::{KeyboardState, Keycode},
- mouse::MouseState,
-};
-
-pub struct Menu {
- map: Tilemap,
- fade_in: f32,
- ui_state: UiState,
- background: Vec2,
- next_state: Option<Box<State>>,
-}
-
-impl Menu {
- pub fn new(layout: &AtlasLayout) -> Self {
- let mut map = Tilemap::default();
- map.init(
- &[
- "floor",
- "tomato-crate",
- "raw-steak-crate",
- "table",
- "chair",
- "counter",
- "sink",
- "stove",
- ]
- .map(String::from),
- layout,
- );
- static BUCKETS: &[&[usize]] = &[&[], &[0, 0, 0, 0, 1, 2], &[3, 4, 5], &[6, 7]];
-
- for x in -10..11 {
- for y in -10..11 {
- let p = Vec2::new(x as f32, y as f32);
- let w = (-p.length() * 0.15).exp();
- let k = ((random::<f32>() * w) * BUCKETS.len() as f32) as usize;
- if let Some(ti) = BUCKETS[k.min(BUCKETS.len())].choose(&mut thread_rng()) {
- map.set(IVec2::new(x, y), Some(TileIndex(*ti)), [None; 4])
- }
- }
- }
-
- Self {
- map,
- fade_in: 0.,
- ui_state: UiState::default(),
- background: Vec2::ZERO,
- next_state: 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.background += Vec2::new(2., 3.) * dt;
- 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);
- }
- pub fn draw(&mut self, ctx: &mut Renderer) {
- ctx.set_world_view(
- ctx.size / ctx.get_world_scale() * Vec2::new(0.8, 0.2),
- ctx.size.max_element() / 32. / 15.,
- );
-
- for x in -1..=2 {
- for y in -1..=2 {
- ctx.draw_ui(SpriteDraw::underlay(
- ctx.misc_textures.clouds,
- Vec2::new(x as f32, y as f32) * 256. + self.background,
- Vec2::ONE * 256.,
- None,
- ));
- }
- }
- ctx.draw_ui(SpriteDraw::underlay(
- ctx.misc_textures.solid,
- Vec2::ZERO,
- ctx.ui_size,
- Some([0, 0, 0, 50]),
- ));
-
- self.map.draw(ctx);
-
- let mut request_join = false;
- self.ui_state.draw(ctx, |ui| {
- if ui.button(80., "Join") {
- request_join = true
- }
- if ui.button(80., "Settings") {
- eprintln!("settings button")
- }
- if ui.button(80., "Quit") {
- self.next_state = Some(Box::new(State::Quit));
- }
- ui.fill();
- });
- if request_join {
- self.next_state = Some(Box::new(State::Ingame(Box::new(Game::new(
- Network::connect("ws://127.0.0.1").unwrap(),
- ctx.atlas_layout(),
- )))))
- }
-
- 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]),
- ));
- }
-}
diff --git a/pixel-client/src/menu/background.rs b/pixel-client/src/menu/background.rs
new file mode 100644
index 00000000..daf89360
--- /dev/null
+++ b/pixel-client/src/menu/background.rs
@@ -0,0 +1,79 @@
+use crate::{
+ render::{sprite::SpriteDraw, AtlasLayout, Renderer},
+ tilemap::Tilemap,
+};
+use hurrycurry_protocol::{
+ glam::{IVec2, Vec2},
+ TileIndex,
+};
+use rand::{random, seq::IndexedRandom, thread_rng};
+
+pub struct MenuBackground {
+ background: Vec2,
+ map: Tilemap,
+}
+
+impl MenuBackground {
+ pub fn new(layout: &AtlasLayout) -> Self {
+ let mut map = Tilemap::default();
+ map.init(
+ &[
+ "floor",
+ "tomato-crate",
+ "raw-steak-crate",
+ "table",
+ "chair",
+ "counter",
+ "sink",
+ "stove",
+ ]
+ .map(String::from),
+ layout,
+ );
+ static BUCKETS: &[&[usize]] = &[&[], &[0, 0, 0, 0, 1, 2], &[3, 4, 5], &[6, 7]];
+
+ for x in -10..11 {
+ for y in -10..11 {
+ let p = Vec2::new(x as f32, y as f32);
+ let w = (-p.length() * 0.15).exp();
+ let k = ((random::<f32>() * w) * BUCKETS.len() as f32) as usize;
+ if let Some(ti) = BUCKETS[k.min(BUCKETS.len())].choose(&mut thread_rng()) {
+ map.set(IVec2::new(x, y), Some(TileIndex(*ti)), [None; 4])
+ }
+ }
+ }
+ Self {
+ map,
+ background: Vec2::ZERO,
+ }
+ }
+
+ pub fn tick(&mut self, dt: f32) {
+ self.background += Vec2::new(2., 3.) * dt;
+ }
+ pub fn draw(&self, ctx: &mut Renderer) {
+ ctx.set_world_view(
+ ctx.size / ctx.get_world_scale() * Vec2::new(0.8, 0.2),
+ ctx.size.max_element() / 32. / 15.,
+ );
+
+ for x in -1..=2 {
+ for y in -1..=2 {
+ ctx.draw_ui(SpriteDraw::underlay(
+ ctx.misc_textures.clouds,
+ Vec2::new(x as f32, y as f32) * 256. + self.background,
+ Vec2::ONE * 256.,
+ None,
+ ));
+ }
+ }
+ ctx.draw_ui(SpriteDraw::underlay(
+ ctx.misc_textures.solid,
+ Vec2::ZERO,
+ ctx.ui_size,
+ Some([0, 0, 0, 50]),
+ ));
+
+ self.map.draw(ctx);
+ }
+}
diff --git a/pixel-client/src/menu/main.rs b/pixel-client/src/menu/main.rs
new file mode 100644
index 00000000..37cecd3e
--- /dev/null
+++ b/pixel-client/src/menu/main.rs
@@ -0,0 +1,100 @@
+/*
+ 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, settings::Settings};
+use crate::{
+ game::Game,
+ network::Network,
+ render::{sprite::SpriteDraw, AtlasLayout, Renderer},
+ ui::UiState,
+ State,
+};
+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<Settings>,
+}
+
+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,
+ }
+ }
+ 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) {
+ self.background.draw(ctx);
+
+ self.ui_state.draw(ctx, |ui| {
+ if let Some(settings) = &mut self.settings {
+ if settings.draw(ui) {
+ self.settings = None;
+ }
+ return;
+ }
+ if ui.button(80., "Join") {
+ self.next_state = Some(Box::new(State::Ingame(Box::new(Game::new(
+ Network::connect(&self.server_address).unwrap(),
+ ui.renderer.atlas_layout(),
+ )))))
+ }
+ ui.textedit(80., &mut self.server_address);
+ if ui.button(80., "Settings") {
+ self.settings = Some(Settings::default())
+ }
+ if ui.button(80., "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]),
+ ));
+ }
+}
diff --git a/pixel-client/src/menu/mod.rs b/pixel-client/src/menu/mod.rs
new file mode 100644
index 00000000..d712eaa9
--- /dev/null
+++ b/pixel-client/src/menu/mod.rs
@@ -0,0 +1,4 @@
+
+pub mod main;
+pub mod background;
+pub mod settings;
diff --git a/pixel-client/src/menu/settings.rs b/pixel-client/src/menu/settings.rs
new file mode 100644
index 00000000..db524496
--- /dev/null
+++ b/pixel-client/src/menu/settings.rs
@@ -0,0 +1,11 @@
+use crate::ui::Ui;
+
+#[derive(Default)]
+pub struct Settings {}
+
+impl Settings {
+ pub fn draw(&mut self, ui: &mut Ui) -> bool {
+ ui.text("Settings placeholder");
+ return ui.button(80., "Back");
+ }
+}
diff --git a/pixel-client/src/ui.rs b/pixel-client/src/ui.rs
index fa3a9691..6410630f 100644
--- a/pixel-client/src/ui.rs
+++ b/pixel-client/src/ui.rs
@@ -36,6 +36,9 @@ pub struct UiState {
mouse_position: Vec2,
ui_scale: Vec2,
+ backspace: bool,
+ text_input: String,
+
keyboard_focus: FocusDevice,
mouse_focus: FocusDevice,
}
@@ -46,7 +49,7 @@ pub struct Ui<'a, 'b> {
cross_height: f32,
index: usize,
direction_horizontal: bool,
- renderer: &'a mut Renderer<'b>,
+ pub renderer: &'a mut Renderer<'b>,
state: &'a mut UiState,
}
@@ -58,11 +61,15 @@ impl UiState {
self.keyboard_focus
.update(keyboard.is_scancode_pressed(Scancode::Space));
}
+ pub fn text_input(&mut self, text: String) {
+ self.text_input = text;
+ }
pub fn keyboard_event(&mut self, keycode: Keycode, down: bool) {
if down {
match keycode {
Keycode::DOWN => self.keyboard_focus.focus += 1,
Keycode::UP if self.keyboard_focus.focus > 0 => self.keyboard_focus.focus -= 1,
+ Keycode::BACKSPACE => self.backspace = true,
_ => (),
}
}
@@ -81,13 +88,17 @@ impl UiState {
index: 0,
};
ui(&mut u);
-
+ self.end()
+ }
+ fn end(&mut self) {
if self.mouse_focus.interact_just_released {
self.mouse_focus.pressing = None;
}
if self.keyboard_focus.interact_just_released {
self.keyboard_focus.pressing = None;
}
+ self.text_input.clear();
+ self.backspace = false;
}
}
@@ -156,6 +167,48 @@ impl<'a, 'b> Ui<'a, 'b> {
released
}
+ pub fn textedit(&mut self, w: f32, content: &mut String) {
+ let c = self.cursor;
+ let margin = Vec2::splat(4.);
+ let text_size = self.renderer.draw_text(self.cursor + margin, &content);
+ let size = margin + Vec2::new(w, text_size.y) + margin;
+
+ self.index += 1;
+
+ let mouse_rel = self.state.mouse_position - c;
+ if mouse_rel.x >= 0. && mouse_rel.y >= 0. && mouse_rel.x < size.x && mouse_rel.y < size.y {
+ self.state.mouse_focus.focus = self.index;
+ }
+
+ if self.state.mouse_focus.interact_just_pressed
+ && self.state.mouse_focus.focus == self.index
+ {
+ self.state.keyboard_focus.focus = self.index;
+ }
+
+ let keyboard_focus = self.state.keyboard_focus.focus == self.index;
+
+ if keyboard_focus {
+ *content += &self.state.text_input;
+ self.state.text_input.clear();
+ if self.state.backspace {
+ content.pop();
+ }
+ }
+
+ let focus = self.state.mouse_focus.focus == self.index || keyboard_focus;
+ let l = if focus { 50 } else { 30 };
+ self.renderer.draw_ui(SpriteDraw::screen(
+ self.renderer.misc_textures.solid,
+ i32::MAX - 1,
+ c,
+ size,
+ Some([l, l, l, 200]),
+ ));
+
+ self.advance(size);
+ }
+
pub fn fill(&mut self) {
self.renderer.draw_ui(SpriteDraw::screen(
self.renderer.misc_textures.solid,