diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 17a307a80..59bd58ec5 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -5,6 +5,7 @@ mod commands; mod control; mod options; mod packs; +mod screens; use horizon_client::{ Result, assets::Assets, @@ -1699,31 +1700,8 @@ impl Game<'_> { batch.fill(atlas, 0.0, 0.0, w, h, rgba(16, 16, 32, a)); } match self.screen { - Screen::Paused => { - batch.fill(atlas, 0.0, 0.0, w, h, rgba(16, 16, 16, 160)); - let title = "Game Menu"; - let tw = HudBatch::text_width(atlas, title); - batch.text(atlas, title, (w - tw) / 2.0, 40.0, u32::MAX, true); - for (i, label) in ["Back to Game", "Options...", "Save and Quit to Title"].iter().enumerate() { - let (bx, by) = (w / 2.0 - 100.0, h / 4.0 + 24.0 + i as f32 * 24.0 + 16.0); - let hover = self.cursor.0 >= bx && self.cursor.0 < bx + 200.0 && self.cursor.1 >= by && self.cursor.1 < by + 20.0; - let sprite = if hover { "gui/sprites/widget/button_highlighted" } else { "gui/sprites/widget/button" }; - batch.sprite_scaled(atlas, sprite, bx, by, 200.0, 20.0, u32::MAX); - let tw = HudBatch::text_width(atlas, label); - batch.text(atlas, label, bx + (200.0 - tw) / 2.0, by + 6.0, u32::MAX, true); - } - } - Screen::Dead => { - batch.fill(atlas, 0.0, 0.0, w, h, rgba(80, 0, 0, 120)); - let title = "You Died!"; - let tw = HudBatch::text_width(atlas, title); - batch.text(atlas, title, (w - tw) / 2.0, 60.0, u32::MAX, true); - let (bx, by) = (w / 2.0 - 100.0, h / 4.0 + 72.0); - batch.sprite_scaled(atlas, "gui/sprites/widget/button", bx, by, 200.0, 20.0, u32::MAX); - let label = "Respawn"; - let tw = HudBatch::text_width(atlas, label); - batch.text(atlas, label, bx + (200.0 - tw) / 2.0, by + 6.0, u32::MAX, true); - } + Screen::Paused => self.draw_pause_menu(&mut batch), + Screen::Dead => self.draw_death_screen(&mut batch), Screen::Inventory => { self.draw_inventory(&mut batch); } @@ -4505,29 +4483,8 @@ impl ApplicationHandler for Game<'_> { Screen::Creative if pressed => { self.click_creative(button == MouseButton::Right); } - Screen::Paused if pressed => { - let size = self.renderer.as_ref().map(|r| [r.config.width, r.config.height]).unwrap_or([1, 1]); - let b = HudBatch::new(size); - for i in 0..3 { - let (bx, by) = (b.width / 2.0 - 100.0, b.height / 4.0 + 24.0 + i as f32 * 24.0 + 16.0); - if self.cursor.0 >= bx && self.cursor.0 < bx + 200.0 && self.cursor.1 >= by && self.cursor.1 < by + 20.0 { - if i == 0 { - self.screen = Screen::Playing; - self.capture(true); - } else if i == 1 { - self.open_options(false); - } else { - self.close_world()?; - } - } - } - } - Screen::Dead if pressed => { - self.player = None; - self.screen = Screen::Playing; - self.spawn_player()?; - self.capture(true); - } + Screen::Paused if pressed => self.pause_click()?, + Screen::Dead if pressed => self.death_click()?, _ => {} } } diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index 28dde48b0..8424d957d 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -74,6 +74,9 @@ pub struct UiState { pub options: horizon_client::options_ui::OptionsUi, /// The prepared resources before resource packs are layered on. pub vanilla_root: std::path::PathBuf, + /// Death screen cause and when the player died (buttons wait 20 ticks). + pub death_text: Option, + pub died_at: Option, } impl Game<'_> { @@ -397,6 +400,8 @@ impl Game<'_> { /// cause is known to the client). pub(super) fn death_message(&mut self) { let cause = self.ui.death_cause.take(); + self.ui.died_at = Some(std::time::Instant::now()); + self.ui.death_text = None; if !self.ui.rules.bool("show_death_messages") { return; } @@ -414,7 +419,9 @@ impl Game<'_> { "death.attack.generic".into() } }); - self.system_message(Text::translate(&key, &[Text::literal(PLAYER_NAME)])); + let message = Text::translate(&key, &[Text::literal(PLAYER_NAME)]); + self.ui.death_text = Some(message.clone()); + self.system_message(message); } /// F1 hides the HUD (and hand); the screenshot key still works. pub(super) fn toggle_hide_gui(&mut self) { diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs new file mode 100644 index 000000000..94aaf1597 --- /dev/null +++ b/crates/horizon-client/src/game/screens.rs @@ -0,0 +1,121 @@ +//! The pause menu (PauseScreen) and death screen (DeathScreen) layouts. +use super::{Game, Screen}; +use horizon_client::{ + Result, + hud::{HudBatch, rgba}, + text::{self, Text}, + ui, +}; + +/// PauseScreen buttons: (x, y, width, label key, action id, active). +fn pause_buttons(w: f32, h: f32) -> Vec<(f32, f32, f32, &'static str, u8, bool)> { + // GridLayout of two 98px columns (padding 4), aligned at (0.5, 0.25). + let (grid_w, grid_h) = (212.0, 166.0); + let x0 = ((w - grid_w) * 0.5).floor(); + let y0 = ((h - grid_h) * 0.25).floor(); + vec![ + (x0 + 4.0, y0 + 50.0, 204.0, "menu.returnToGame", 0, true), + (x0 + 4.0, y0 + 74.0, 98.0, "gui.advancements", 9, false), + (x0 + 110.0, y0 + 74.0, 98.0, "gui.stats", 9, false), + (x0 + 4.0, y0 + 122.0, 98.0, "menu.options", 1, true), + (x0 + 110.0, y0 + 122.0, 98.0, "options.worldOptions.button", 9, false), + (x0 + 4.0, y0 + 146.0, 204.0, "menu.returnToMenu", 2, true), + ] +} +/// The icon row: report bugs, feedback, friends, player reporting. +fn pause_icons(w: f32, h: f32) -> [(f32, f32, &'static str); 4] { + let x0 = ((w - 212.0) * 0.5).floor() + 60.0; + let y = ((h - 166.0) * 0.25).floor() + 98.0; + [ + (x0, y, "gui/sprites/pause_menu/bug"), + (x0 + 24.0, y, "gui/sprites/pause_menu/social_interactions"), + (x0 + 48.0, y, "gui/sprites/icon/friends"), + (x0 + 72.0, y, "gui/sprites/pause_menu/player_reporting"), + ] +} +fn death_buttons(w: f32, h: f32) -> [(f32, f32, &'static str, u8); 2] { + [(w / 2.0 - 100.0, h / 4.0 + 72.0, "deathScreen.respawn", 0), (w / 2.0 - 100.0, h / 4.0 + 96.0, "deathScreen.titleScreen", 1)] +} +impl Game<'_> { + pub(super) fn draw_pause_menu(&mut self, batch: &mut HudBatch) { + let atlas = &self.res.atlas; + let (w, h) = (batch.width, batch.height); + ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true); + let title = Text::translate("menu.game", &[]); + let tw = atlas.font.width_text(&title); + batch.text_styled(atlas, &title, ((w - tw) / 2.0).floor(), 40.0, u32::MAX, true); + for (x, y, bw, key, _, active) in pause_buttons(w, h) { + let hover = active && ui::hovered(self.cursor, x, y, bw, 20.0); + ui::button(batch, atlas, x, y, bw, 20.0, &Text::translate(key, &[]), active, hover); + } + for (x, y, sprite) in pause_icons(w, h) { + ui::button(batch, atlas, x, y, 20.0, 20.0, &Text::empty(), false, false); + batch.sprite_scaled(atlas, sprite, x + 2.0, y + 2.0, 15.0, 15.0, rgba(160, 160, 160, 255)); + } + } + pub(super) fn pause_click(&mut self) -> Result<()> { + let (w, h) = { + let size = self.renderer.as_ref().map_or([1, 1], |r| [r.config.width, r.config.height]); + let b = HudBatch::new(size); + (b.width, b.height) + }; + let hit = pause_buttons(w, h).into_iter().find(|b| b.5 && ui::hovered(self.cursor, b.0, b.1, b.2, 20.0)).map(|b| b.4); + match hit { + Some(0) => { + self.screen = Screen::Playing; + self.capture(true); + } + Some(1) => self.open_options(false), + Some(2) => self.close_world()?, + _ => {} + } + Ok(()) + } + /// DeathScreen: gradient, the title at double size, the cause and score. + pub(super) fn draw_death_screen(&mut self, batch: &mut HudBatch) { + let atlas = &self.res.atlas; + let (w, h) = (batch.width, batch.height); + // fillGradient(0x60500000 -> 0xA0803030). + let rows = h.ceil() as i32; + for y in 0..rows { + let t = y as f32 / h.max(1.0); + let lerp = |a: f32, b: f32| (a + (b - a) * t) as u8; + batch.fill(atlas, 0.0, y as f32, w, 1.0, rgba(lerp(80.0, 128.0), lerp(0.0, 48.0), lerp(0.0, 48.0), lerp(96.0, 160.0))); + } + let title = Text::translate("deathScreen.title", &[]); + let tw = atlas.font.width_text(&title); + ui::text_scaled(batch, atlas, &title, (w / 2.0 - tw).floor(), 60.0, 2.0, u32::MAX); + if let Some(cause) = &self.ui.death_text { + let cw = atlas.font.width_text(cause); + batch.text_styled(atlas, cause, ((w - cw) / 2.0).floor(), 85.0, u32::MAX, true); + } + let score = Text::translate("deathScreen.score.value", &[Text::colored(self.experience.total.to_string(), text::YELLOW)]); + let sw = atlas.font.width_text(&score); + batch.text_styled(atlas, &score, ((w - sw) / 2.0).floor(), 100.0, u32::MAX, true); + let active = self.ui.died_at.is_none_or(|t| t.elapsed().as_millis() >= 1000); + for (x, y, key, _) in death_buttons(w, h) { + let hover = active && ui::hovered(self.cursor, x, y, 200.0, 20.0); + ui::button(batch, atlas, x, y, 200.0, 20.0, &Text::translate(key, &[]), active, hover); + } + } + pub(super) fn death_click(&mut self) -> Result<()> { + if self.ui.died_at.is_some_and(|t| t.elapsed().as_millis() < 1000) { + return Ok(()); + } + let size = self.renderer.as_ref().map_or([1, 1], |r| [r.config.width, r.config.height]); + let b = HudBatch::new(size); + let hit = death_buttons(b.width, b.height).into_iter().find(|d| ui::hovered(self.cursor, d.0, d.1, 200.0, 20.0)).map(|d| d.3); + match hit { + Some(0) => { + self.player = None; + self.screen = Screen::Playing; + self.ui.death_text = None; + self.spawn_player()?; + self.capture(true); + } + Some(1) => self.close_world()?, + _ => {} + } + Ok(()) + } +} diff --git a/crates/horizon-client/src/ui.rs b/crates/horizon-client/src/ui.rs index d253f2c5e..2071b8cd4 100644 --- a/crates/horizon-client/src/ui.rs +++ b/crates/horizon-client/src/ui.rs @@ -118,6 +118,14 @@ pub fn tiled(batch: &mut HudBatch, atlas: &HudAtlas, sprite: &str, x: f32, y: f3 yy += size; } } +/// Text drawn at `scale` times its size (a scaled pose). +pub fn text_scaled(batch: &mut HudBatch, atlas: &HudAtlas, text: &Text, x: f32, y: f32, scale: f32, color: u32) { + let start = batch.vertices.len(); + batch.text_styled(atlas, text, 0.0, 0.0, color, true); + for v in &mut batch.vertices[start..] { + v.pos = [x + v.pos[0] * scale, y + v.pos[1] * scale]; + } +} pub fn hovered(cursor: (f32, f32), x: f32, y: f32, w: f32, h: f32) -> bool { cursor.0 >= x && cursor.0 < x + w && cursor.1 >= y && cursor.1 < y + h }