Merge pull request #2679 from kacpherek/feat/spectator-menu
🤖 Agent: Merged by Ludovic's AI agent (Claude, contrib-gate-19) after the contribution gate. Checked against vanilla 26.3 and proven in union/cg19-r2 and union/cg19-r3. After this batch it was merged with main again and proven in union/cg19-r4 (clippy, 1726 tests, 121/121 harnesses). Thanks @kacpherek.
This commit is contained in:
@@ -177,6 +177,7 @@ mod sign_edit;
|
||||
mod sniffers;
|
||||
mod spawner;
|
||||
mod spear;
|
||||
mod spectator;
|
||||
mod spill;
|
||||
mod startup_menu;
|
||||
mod station_control;
|
||||
@@ -1491,6 +1492,8 @@ struct Game<'a> {
|
||||
/// The shader pack's entity and block entity ids, while one draws entities.
|
||||
pack_ids: Option<shader_packs::PackIds>,
|
||||
started: Instant,
|
||||
/// `SpectatorGui`: the menu a spectator opens with the hotbar keys.
|
||||
spectator_gui: horizon_client::spectator_menu::SpectatorGui,
|
||||
previous: Instant,
|
||||
/// FramerateLimitTracker.latestInputTime.
|
||||
last_input: Instant,
|
||||
@@ -5865,7 +5868,9 @@ impl Game<'_> {
|
||||
let draws = hud_layout::crosshair(wi, hi, attack, full);
|
||||
hud_layout::paint(&mut batch, &self.gui.atlas, &draws);
|
||||
}
|
||||
if !spectator {
|
||||
if spectator {
|
||||
self.draw_spectator_hotbar(&mut batch);
|
||||
} else {
|
||||
let partial = self.clock.fraction() as f32;
|
||||
let inputs = hud_layout::HotbarInputs {
|
||||
selected: self.selected as i32,
|
||||
@@ -12069,11 +12074,16 @@ impl Game<'_> {
|
||||
KeyCode::Digit8 => 7,
|
||||
_ => 8,
|
||||
};
|
||||
// Minecraft.handleKeybinds: a creative player holding the load
|
||||
// or save toolbar key loads or saves a toolbar instead (load
|
||||
// first when both are held).
|
||||
// Minecraft.handleKeybinds: a spectator's number keys drive
|
||||
// the spectator menu, not the hotbar; a creative player
|
||||
// holding the load or save toolbar key loads or saves a
|
||||
// toolbar instead (load first when both are held).
|
||||
let load = self.mapped_key_down("key.loadToolbarActivator");
|
||||
if self.creative && (load || self.mapped_key_down("key.saveToolbarActivator")) {
|
||||
if self.spectator() {
|
||||
self.spectator_hotbar_key(slot as i32);
|
||||
} else if self.creative
|
||||
&& (load || self.mapped_key_down("key.saveToolbarActivator"))
|
||||
{
|
||||
self.hotbar_load_or_save(slot, load);
|
||||
} else {
|
||||
self.selected = slot;
|
||||
@@ -12092,6 +12102,14 @@ impl Game<'_> {
|
||||
if self.sleeping.is_some() {
|
||||
return Ok(());
|
||||
}
|
||||
// key.spectatorHotbar (the middle button) opens the spectator menu
|
||||
// and uses its selected item.
|
||||
if button == MouseButton::Middle && self.spectator() {
|
||||
if pressed {
|
||||
self.spectator_action_key();
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if self.remote.is_some() {
|
||||
return self.remote_mouse(button, pressed);
|
||||
}
|
||||
@@ -12656,7 +12674,9 @@ impl ApplicationHandler for Game<'_> {
|
||||
self.mouse
|
||||
.on_scroll(raw_x, raw_y, discrete, wheel_sensitivity)
|
||||
{
|
||||
if self.spectator() {
|
||||
if self.spectator() && self.spectator_gui.is_menu_active() {
|
||||
self.spectator_scroll(-wheel);
|
||||
} else if self.spectator() {
|
||||
if let Some(player) = &mut self.player {
|
||||
player.flying_speed = horizon_client::mouse::spectator_flying_speed(
|
||||
player.flying_speed,
|
||||
@@ -13573,6 +13593,7 @@ impl<'a> Game<'a> {
|
||||
pack_ids: None,
|
||||
health_hud: hud_layout::HealthHud::default(),
|
||||
started: now,
|
||||
spectator_gui: Default::default(),
|
||||
previous: now,
|
||||
last_input: now,
|
||||
pad: Default::default(),
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
//! blocks, attacking and the commands all behave exactly as they do for the
|
||||
//! host, and every world change they cause reaches all players through the
|
||||
//! block log, the level events and the entity tracker.
|
||||
use super::{Game, GuestUse, Hit, Mining, Screen, lan::LanHost, respawn_anchor::RespawnConfig};
|
||||
use super::{
|
||||
Dim, Game, GuestUse, Hit, Mining, Screen, lan::LanHost, respawn_anchor::RespawnConfig,
|
||||
};
|
||||
use horizon_client::{
|
||||
command::vanilla,
|
||||
containers::BlockEntity,
|
||||
@@ -1439,6 +1441,9 @@ impl Game<'_> {
|
||||
}
|
||||
self.sync_rules();
|
||||
}
|
||||
// handleTeleportToEntityPacket: a spectator goes to the player,
|
||||
// in whichever level they are.
|
||||
S::TeleportToEntity(p) if spectator => self.guest_spectate_teleport(host, gi, p.uuid),
|
||||
S::ChangeGameMode(p) if host.hub.guests[gi].permission >= 2 => {
|
||||
if let Some(mode) = usize::try_from(p.mode)
|
||||
.ok()
|
||||
@@ -1566,6 +1571,51 @@ impl Game<'_> {
|
||||
/// and `Player.remove`'s `inventoryMenu.removed` drops what the cursor
|
||||
/// held (`dropOrPlaceInInventory` for a removed player). Respawning
|
||||
/// sooner removes it too.
|
||||
/// `ServerGamePacketListenerImpl.handleTeleportToEntityPacket` for a
|
||||
/// player: the host's own, or a guest in the world.
|
||||
fn guest_spectate_teleport(&mut self, host: &mut LanHost, gi: usize, uuid: u128) {
|
||||
let own = host.hub.guests[gi].conn;
|
||||
let host_player = self
|
||||
.player
|
||||
.as_ref()
|
||||
.filter(|_| self.options.dedicated.is_none() && uuid == host.host_uuid)
|
||||
.map(|p| {
|
||||
(
|
||||
[p.position.x, p.position.y, p.position.z],
|
||||
self.yaw,
|
||||
self.pitch,
|
||||
self.dim,
|
||||
)
|
||||
});
|
||||
let target = host_player.or_else(|| {
|
||||
host.hub
|
||||
.guests
|
||||
.iter()
|
||||
.find(|g| g.placed && g.conn != own && g.uuid == uuid)
|
||||
.map(|g| (g.position, g.yaw, g.pitch, Dim::from_id(&g.dimension)))
|
||||
});
|
||||
let Some((at, yaw, pitch, dim)) = target else {
|
||||
return;
|
||||
};
|
||||
if Dim::from_id(&host.hub.guests[gi].dimension) != dim {
|
||||
// Only a dedicated server keeps its players in different levels.
|
||||
if self.options.dedicated.is_some() {
|
||||
self.level_teleports
|
||||
.push(super::server_travel::LevelTeleport {
|
||||
conn: own,
|
||||
dim,
|
||||
at,
|
||||
rotation: Some([yaw, pitch]),
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
let g = &mut host.hub.guests[gi];
|
||||
(g.yaw, g.pitch) = (yaw, pitch);
|
||||
// A new body there: no fall distance or motion carried over.
|
||||
g.motion = None;
|
||||
host.hub.teleport(gi, Some(at));
|
||||
}
|
||||
fn guest_tick_death(&mut self, host: &mut LanHost, gi: usize) {
|
||||
let g = &mut host.hub.guests[gi];
|
||||
g.death_time += 1;
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
//! The spectator menu (`SpectatorGui`): its players and teams from the
|
||||
//! server's player list and teams, and the teleport request.
|
||||
use super::Game;
|
||||
use horizon_client::{
|
||||
hud::HudBatch,
|
||||
hud_layout, keybinds,
|
||||
scoreboard::{comp_text, team_color_rgb},
|
||||
spectator_menu::{Member, Outcome, SpectatorMenu, Team},
|
||||
tab_list::{SPECTATOR, TabPlayer},
|
||||
text::Text,
|
||||
};
|
||||
use horizon_protocol::packets::play::{self, Serverbound as S};
|
||||
|
||||
impl Game<'_> {
|
||||
/// `Util.getMillis` for the menu's fade.
|
||||
fn spectator_now(&self) -> i64 {
|
||||
self.started.elapsed().as_millis() as i64
|
||||
}
|
||||
/// `new SpectatorMenu`: the listed players, and the teams with a player
|
||||
/// to go to (`TeleportToTeamMenuCategory.createTeamEntries`).
|
||||
fn new_spectator_menu(&mut self) -> SpectatorMenu {
|
||||
let tab: Vec<TabPlayer> = self
|
||||
.remote_tab_players()
|
||||
.map_or_else(Vec::new, |(players, _, _)| players);
|
||||
let member = |p: &TabPlayer| Member {
|
||||
uuid: p.uuid,
|
||||
name: p.name.clone(),
|
||||
skin: p.skin.clone(),
|
||||
enabled: p.game_mode != SPECTATOR,
|
||||
};
|
||||
let players: Vec<Member> = tab.iter().map(member).collect();
|
||||
let teams = self
|
||||
.score
|
||||
.board
|
||||
.teams
|
||||
.iter()
|
||||
.filter_map(|team| {
|
||||
let members: Vec<Member> = team
|
||||
.players
|
||||
.iter()
|
||||
.filter_map(|name| tab.iter().find(|p| &p.name == name))
|
||||
.filter(|p| p.game_mode != SPECTATOR)
|
||||
.map(member)
|
||||
.collect();
|
||||
if members.is_empty() {
|
||||
return None;
|
||||
}
|
||||
// A random member's face is the team's icon.
|
||||
let pick = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.subsec_nanos() as usize)
|
||||
% members.len();
|
||||
Some(Team {
|
||||
name: comp_text(&team.display_name),
|
||||
color: team.color.as_deref().and_then(team_color_rgb),
|
||||
icon: members[pick].skin.clone(),
|
||||
players: members,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
SpectatorMenu::new(players, teams)
|
||||
}
|
||||
fn spectator_outcome(&mut self, outcome: Outcome) {
|
||||
if let Outcome::Teleport(uuid) = outcome {
|
||||
// PlayerMenuItem.selectItem.
|
||||
self.send(S::TeleportToEntity(play::TeleportToEntity { uuid }));
|
||||
}
|
||||
}
|
||||
/// A hotbar key as a spectator (`SpectatorGui.onHotbarSelected`).
|
||||
pub(super) fn spectator_hotbar_key(&mut self, slot: i32) {
|
||||
let now = self.spectator_now();
|
||||
let menu = self.spectator_menu_if_needed();
|
||||
let outcome = self.spectator_gui.on_hotbar_selected(slot, now, menu);
|
||||
self.spectator_outcome(outcome);
|
||||
}
|
||||
/// `key.spectatorHotbar`: opens the menu, or uses the selected item.
|
||||
pub(super) fn spectator_action_key(&mut self) {
|
||||
let now = self.spectator_now();
|
||||
let menu = self.spectator_menu_if_needed();
|
||||
let outcome = self.spectator_gui.on_action_key(now, menu);
|
||||
self.spectator_outcome(outcome);
|
||||
}
|
||||
/// The menu a press would open, built only when none is up.
|
||||
fn spectator_menu_if_needed(&mut self) -> Option<SpectatorMenu> {
|
||||
(!self.spectator_gui.is_menu_active()).then(|| self.new_spectator_menu())
|
||||
}
|
||||
/// `MouseHandler.onScroll` in spectator mode with the menu up.
|
||||
pub(super) fn spectator_scroll(&mut self, wheel: i32) {
|
||||
let now = self.spectator_now();
|
||||
self.spectator_gui.on_mouse_scrolled(wheel, now);
|
||||
}
|
||||
/// `Hud.extractHotbar` for a spectator, and `extractAction`'s prompt.
|
||||
pub(super) fn draw_spectator_hotbar(&mut self, batch: &mut HudBatch) {
|
||||
let (w, h) = (batch.width as i32, batch.height as i32);
|
||||
let now = self.spectator_now();
|
||||
let labels: Vec<Text> = (1..=9)
|
||||
.map(|n| keybinds::display_name(self.settings.keys.get(&format!("key.hotbar.{n}"))))
|
||||
.collect();
|
||||
let font = &self.gui.atlas.font;
|
||||
let mut draws = self
|
||||
.spectator_gui
|
||||
.hotbar(w, h, now, &|slot| labels[slot].clone(), font);
|
||||
draws.extend(self.spectator_gui.action(
|
||||
w,
|
||||
h,
|
||||
now,
|
||||
font,
|
||||
self.settings.text_background(0.0),
|
||||
));
|
||||
hud_layout::paint(batch, &self.gui.atlas, &draws);
|
||||
}
|
||||
}
|
||||
@@ -163,6 +163,7 @@ pub mod sound_engine;
|
||||
pub mod sound_events;
|
||||
pub mod spans;
|
||||
pub mod spear;
|
||||
pub mod spectator_menu;
|
||||
pub mod sprint;
|
||||
pub mod sprites;
|
||||
pub mod stations;
|
||||
|
||||
@@ -0,0 +1,419 @@
|
||||
//! The spectator menu: `SpectatorGui` (the faded hotbar and the prompt under
|
||||
//! it) over `SpectatorMenu` with its root page, Teleport to Player and
|
||||
//! Teleport to Team Member, laid out as [`GuiDraw`] calls.
|
||||
use crate::{
|
||||
font::Font,
|
||||
hud_layout::{Blend, GuiDraw, argb, color_from_float, fill, white, width},
|
||||
hud_overlays::text_with_backdrop,
|
||||
text::Text,
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// `SpectatorGui.FADE_OUT_DELAY` and `FADE_OUT_TIME`, in milliseconds.
|
||||
const FADE_OUT_DELAY: i64 = 5000;
|
||||
const FADE_OUT_TIME: f32 = 2000.0;
|
||||
|
||||
/// A player of the menu (`PlayerMenuItem`).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Member {
|
||||
pub uuid: u128,
|
||||
pub name: String,
|
||||
/// Skin texture path, as the tab list draws it.
|
||||
pub skin: String,
|
||||
/// `PlayerInfo.getGameMode() != GameType.SPECTATOR`.
|
||||
pub enabled: bool,
|
||||
}
|
||||
|
||||
/// A team with players to go to (`TeamSelectionItem`).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Team {
|
||||
pub name: Text,
|
||||
/// The team colour's RGB.
|
||||
pub color: Option<u32>,
|
||||
pub players: Vec<Member>,
|
||||
/// The skin of the face drawn on the icon.
|
||||
pub icon: String,
|
||||
}
|
||||
|
||||
/// A `SpectatorMenuItem`.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Item {
|
||||
Empty,
|
||||
Close,
|
||||
Scroll { left: bool, enabled: bool },
|
||||
TeleportToPlayer { enabled: bool },
|
||||
TeleportToTeam { enabled: bool },
|
||||
Player(Member),
|
||||
Team(Team),
|
||||
}
|
||||
impl Item {
|
||||
pub fn enabled(&self) -> bool {
|
||||
match self {
|
||||
Item::Empty => false,
|
||||
Item::Close | Item::Team(_) => true,
|
||||
Item::Scroll { enabled, .. }
|
||||
| Item::TeleportToPlayer { enabled }
|
||||
| Item::TeleportToTeam { enabled } => *enabled,
|
||||
Item::Player(m) => m.enabled,
|
||||
}
|
||||
}
|
||||
/// `SpectatorMenuItem.getName`.
|
||||
pub fn name(&self) -> Text {
|
||||
match self {
|
||||
Item::Empty => Text::literal(""),
|
||||
Item::Close => Text::translate("spectatorMenu.close", &[]),
|
||||
Item::Scroll { left: true, .. } => Text::translate("spectatorMenu.previous_page", &[]),
|
||||
Item::Scroll { .. } => Text::translate("spectatorMenu.next_page", &[]),
|
||||
Item::TeleportToPlayer { .. } => Text::translate("spectatorMenu.teleport", &[]),
|
||||
Item::TeleportToTeam { .. } => Text::translate("spectatorMenu.team_teleport", &[]),
|
||||
Item::Player(m) => Text::literal(m.name.clone()),
|
||||
Item::Team(t) => t.name.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What a selected item asks the game to do.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Outcome {
|
||||
None,
|
||||
/// `SpectatorMenu.exit`.
|
||||
Exit,
|
||||
/// `ServerboundTeleportToEntityPacket` for this player.
|
||||
Teleport(u128),
|
||||
}
|
||||
|
||||
enum Category {
|
||||
Root,
|
||||
Players,
|
||||
Teams,
|
||||
TeamPlayers(Vec<Member>),
|
||||
}
|
||||
|
||||
/// `TeleportToPlayerMenuCategory`: players that are not spectators, ordered
|
||||
/// by `UUID.compareTo` (signed halves).
|
||||
pub fn players_page(mut players: Vec<Member>) -> Vec<Member> {
|
||||
players.retain(|m| m.enabled);
|
||||
players.sort_by_key(|m| ((m.uuid >> 64) as i64, m.uuid as i64));
|
||||
players
|
||||
}
|
||||
|
||||
/// `SpectatorMenu`: the page, the selected slot and the page of its items.
|
||||
pub struct SpectatorMenu {
|
||||
players: Vec<Member>,
|
||||
teams: Vec<Team>,
|
||||
category: Category,
|
||||
selected: i32,
|
||||
page: i32,
|
||||
}
|
||||
impl SpectatorMenu {
|
||||
/// `new SpectatorMenu`: the root page, whose categories take the players
|
||||
/// and teams as they are now.
|
||||
pub fn new(players: Vec<Member>, teams: Vec<Team>) -> Self {
|
||||
Self {
|
||||
players: players_page(players),
|
||||
teams,
|
||||
category: Category::Root,
|
||||
selected: -1,
|
||||
page: 0,
|
||||
}
|
||||
}
|
||||
/// `SpectatorMenuCategory.getItems`.
|
||||
fn category_items(&self) -> Vec<Item> {
|
||||
match &self.category {
|
||||
Category::Root => vec![
|
||||
Item::TeleportToPlayer {
|
||||
enabled: !self.players.is_empty(),
|
||||
},
|
||||
Item::TeleportToTeam {
|
||||
enabled: !self.teams.is_empty(),
|
||||
},
|
||||
],
|
||||
Category::Players => self.players.iter().cloned().map(Item::Player).collect(),
|
||||
Category::Teams => self.teams.iter().cloned().map(Item::Team).collect(),
|
||||
Category::TeamPlayers(list) => list.iter().cloned().map(Item::Player).collect(),
|
||||
}
|
||||
}
|
||||
/// `SpectatorMenuCategory.getPrompt`.
|
||||
fn prompt(&self) -> Text {
|
||||
Text::translate(
|
||||
match self.category {
|
||||
Category::Root => "spectatorMenu.root.prompt",
|
||||
Category::Teams => "spectatorMenu.team_teleport.prompt",
|
||||
_ => "spectatorMenu.teleport.prompt",
|
||||
},
|
||||
&[],
|
||||
)
|
||||
}
|
||||
/// `SpectatorMenu.getItem`: six items a page between the scroll arrows.
|
||||
pub fn item(&self, slot: i32) -> Item {
|
||||
let items = self.category_items();
|
||||
let index = slot + self.page * 6;
|
||||
if self.page > 0 && slot == 0 {
|
||||
Item::Scroll {
|
||||
left: true,
|
||||
enabled: true,
|
||||
}
|
||||
} else if slot == 7 {
|
||||
Item::Scroll {
|
||||
left: false,
|
||||
enabled: index >= 0 && (index as usize) < items.len(),
|
||||
}
|
||||
} else if slot == 8 {
|
||||
Item::Close
|
||||
} else if index >= 0 && (index as usize) < items.len() {
|
||||
items[index as usize].clone()
|
||||
} else {
|
||||
Item::Empty
|
||||
}
|
||||
}
|
||||
pub fn selected_slot(&self) -> i32 {
|
||||
self.selected
|
||||
}
|
||||
/// `SpectatorMenu.selectSlot`: a first press selects, a second uses it.
|
||||
pub fn select_slot(&mut self, slot: i32) -> Outcome {
|
||||
let item = self.item(slot);
|
||||
if item == Item::Empty {
|
||||
return Outcome::None;
|
||||
}
|
||||
if self.selected != slot || !item.enabled() {
|
||||
self.selected = slot;
|
||||
return Outcome::None;
|
||||
}
|
||||
match item {
|
||||
Item::Close => return Outcome::Exit,
|
||||
Item::Scroll { left, .. } => self.page += if left { -1 } else { 1 },
|
||||
Item::TeleportToPlayer { .. } => self.select_category(Category::Players),
|
||||
Item::TeleportToTeam { .. } => self.select_category(Category::Teams),
|
||||
Item::Team(t) => self.select_category(Category::TeamPlayers(players_page(t.players))),
|
||||
Item::Player(m) => return Outcome::Teleport(m.uuid),
|
||||
Item::Empty => {}
|
||||
}
|
||||
Outcome::None
|
||||
}
|
||||
/// `SpectatorMenu.selectCategory`.
|
||||
fn select_category(&mut self, category: Category) {
|
||||
self.category = category;
|
||||
self.selected = -1;
|
||||
self.page = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// `SpectatorGui`: the menu while it is up and when it was last used.
|
||||
#[derive(Default)]
|
||||
pub struct SpectatorGui {
|
||||
menu: Option<SpectatorMenu>,
|
||||
last_selection: i64,
|
||||
}
|
||||
impl SpectatorGui {
|
||||
pub fn is_menu_active(&self) -> bool {
|
||||
self.menu.is_some()
|
||||
}
|
||||
fn apply(&mut self, outcome: Outcome) -> Outcome {
|
||||
if outcome == Outcome::Exit {
|
||||
// onSpectatorMenuClosed.
|
||||
self.menu = None;
|
||||
self.last_selection = 0;
|
||||
}
|
||||
outcome
|
||||
}
|
||||
/// `SpectatorGui.onHotbarSelected`: a number key.
|
||||
/// `fresh` is the menu to open when none is up.
|
||||
pub fn on_hotbar_selected(
|
||||
&mut self,
|
||||
slot: i32,
|
||||
now: i64,
|
||||
fresh: Option<SpectatorMenu>,
|
||||
) -> Outcome {
|
||||
self.last_selection = now;
|
||||
match &mut self.menu {
|
||||
Some(menu) => {
|
||||
let outcome = menu.select_slot(slot);
|
||||
self.apply(outcome)
|
||||
}
|
||||
None => {
|
||||
self.menu = fresh;
|
||||
Outcome::None
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `SpectatorGui.onHotbarActionKeyPressed`: the key that opens the menu
|
||||
/// and uses the selected item.
|
||||
pub fn on_action_key(&mut self, now: i64, fresh: Option<SpectatorMenu>) -> Outcome {
|
||||
self.last_selection = now;
|
||||
match &mut self.menu {
|
||||
Some(menu) => {
|
||||
let slot = menu.selected_slot();
|
||||
if slot == -1 {
|
||||
return Outcome::None;
|
||||
}
|
||||
let outcome = menu.select_slot(slot);
|
||||
self.apply(outcome)
|
||||
}
|
||||
None => {
|
||||
self.menu = fresh;
|
||||
Outcome::None
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `SpectatorGui.onMouseScrolled`: the next usable slot that way.
|
||||
pub fn on_mouse_scrolled(&mut self, wheel: i32, now: i64) {
|
||||
let Some(menu) = &mut self.menu else {
|
||||
return;
|
||||
};
|
||||
let usable = |menu: &SpectatorMenu, slot: i32| {
|
||||
let item = menu.item(slot);
|
||||
item != Item::Empty && item.enabled()
|
||||
};
|
||||
let mut slot = menu.selected_slot() + wheel;
|
||||
while (0..=8).contains(&slot) && !usable(menu, slot) {
|
||||
slot += wheel;
|
||||
}
|
||||
if (0..=8).contains(&slot) {
|
||||
menu.select_slot(slot);
|
||||
self.last_selection = now;
|
||||
}
|
||||
}
|
||||
/// `SpectatorGui.getHotbarAlpha`.
|
||||
fn alpha(&self, now: i64) -> f32 {
|
||||
let delta = self.last_selection - now + FADE_OUT_DELAY;
|
||||
(delta as f32 / FADE_OUT_TIME).clamp(0.0, 1.0)
|
||||
}
|
||||
/// `SpectatorGui.extractHotbar`: the page's nine slots; a menu that has
|
||||
/// faded out exits instead. `key_label` is the hotbar key of a slot.
|
||||
pub fn hotbar(
|
||||
&mut self,
|
||||
w: i32,
|
||||
h: i32,
|
||||
now: i64,
|
||||
key_label: &dyn Fn(usize) -> Text,
|
||||
font: &Font,
|
||||
) -> Vec<GuiDraw> {
|
||||
let mut out = Vec::new();
|
||||
let alpha = self.alpha(now);
|
||||
if self.menu.is_some() && alpha <= 0.0 {
|
||||
self.apply(Outcome::Exit);
|
||||
}
|
||||
let Some(menu) = &self.menu else {
|
||||
return out;
|
||||
};
|
||||
let center = w / 2;
|
||||
let y = (h as f32 - 22.0 * alpha).floor() as i32;
|
||||
let color = white(alpha);
|
||||
out.push(sprite("hud/hotbar", center - 91, y, 182, 22, color));
|
||||
if menu.selected_slot() >= 0 {
|
||||
out.push(sprite(
|
||||
"hud/hotbar_selection",
|
||||
center - 91 - 1 + menu.selected_slot() * 20,
|
||||
y - 1,
|
||||
24,
|
||||
23,
|
||||
color,
|
||||
));
|
||||
}
|
||||
for slot in 0..9 {
|
||||
let item = menu.item(slot);
|
||||
if item == Item::Empty {
|
||||
continue;
|
||||
}
|
||||
let x = center - 90 + slot * 20 + 2;
|
||||
let brightness = if item.enabled() { 1.0 } else { 0.25 };
|
||||
icon(&mut out, &item, x, y + 3, brightness, alpha);
|
||||
if item.enabled() {
|
||||
let label = key_label(slot as usize);
|
||||
let lw = width(font, &label);
|
||||
out.push(GuiDraw::Text {
|
||||
text: label,
|
||||
x: x + 19 - 2 - lw,
|
||||
y: y + 3 + 6 + 3,
|
||||
color,
|
||||
shadow: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
/// `SpectatorGui.extractAction`: the selected item's name, or the
|
||||
/// page's prompt, over the hotbar.
|
||||
pub fn action(&self, w: i32, h: i32, now: i64, font: &Font, background: u32) -> Vec<GuiDraw> {
|
||||
let mut out = Vec::new();
|
||||
let alpha = self.alpha(now);
|
||||
let Some(menu) = &self.menu else {
|
||||
return out;
|
||||
};
|
||||
if alpha <= 0.0 {
|
||||
return out;
|
||||
}
|
||||
let item = menu.item(menu.selected_slot());
|
||||
let action = if item == Item::Empty {
|
||||
menu.prompt()
|
||||
} else {
|
||||
item.name()
|
||||
};
|
||||
let str_width = width(font, &action);
|
||||
text_with_backdrop(
|
||||
&mut out,
|
||||
action,
|
||||
(w - str_width) / 2,
|
||||
h - 35,
|
||||
str_width,
|
||||
white(alpha),
|
||||
background,
|
||||
);
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
fn sprite(name: &'static str, x: i32, y: i32, w: i32, h: i32, color: u32) -> GuiDraw {
|
||||
GuiDraw::Sprite {
|
||||
sprite: Cow::Borrowed(name),
|
||||
x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
region: None,
|
||||
color,
|
||||
blend: Blend::Textured,
|
||||
}
|
||||
}
|
||||
|
||||
/// `SpectatorMenuItem.extractIcon` at the slot's corner.
|
||||
fn icon(out: &mut Vec<GuiDraw>, item: &Item, x: i32, y: i32, brightness: f32, alpha: f32) {
|
||||
let tint = color_from_float(alpha, brightness, brightness, brightness);
|
||||
match item {
|
||||
Item::Empty => {}
|
||||
Item::Close => out.push(sprite("spectator/close", x, y, 16, 16, tint)),
|
||||
Item::Scroll { left, .. } => out.push(sprite(
|
||||
if *left {
|
||||
"spectator/scroll_left"
|
||||
} else {
|
||||
"spectator/scroll_right"
|
||||
},
|
||||
x,
|
||||
y,
|
||||
16,
|
||||
16,
|
||||
tint,
|
||||
)),
|
||||
Item::TeleportToPlayer { .. } => {
|
||||
out.push(sprite("spectator/teleport_to_player", x, y, 16, 16, tint));
|
||||
}
|
||||
Item::TeleportToTeam { .. } => {
|
||||
out.push(sprite("spectator/teleport_to_team", x, y, 16, 16, tint));
|
||||
}
|
||||
Item::Player(m) => crate::tab_list::face(out, &m.skin, x + 2, y + 2, 12, true, false, tint),
|
||||
Item::Team(t) => {
|
||||
if let Some(rgb) = t.color {
|
||||
// ARGB.scaleRGB.
|
||||
let c = |s: u32| (((rgb >> s) & 255) as f32 * brightness) as u32;
|
||||
out.push(fill(
|
||||
x + 1,
|
||||
y + 1,
|
||||
x + 15,
|
||||
y + 15,
|
||||
argb(255, c(16), c(8), c(0)),
|
||||
));
|
||||
}
|
||||
crate::tab_list::face(out, &t.icon, x + 2, y + 2, 12, true, false, tint);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,7 +96,17 @@ fn sprite(name: &'static str, x: i32, y: i32, w: i32, h: i32) -> GuiDraw {
|
||||
|
||||
/// `PlayerFaceExtractor.extractRenderState`: the skin's face, then its hat
|
||||
/// layer, upside down for Dinnerbone and Grumm.
|
||||
fn face(out: &mut Vec<GuiDraw>, skin: &str, x: i32, y: i32, size: i32, hat: bool, flip: bool) {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn face(
|
||||
out: &mut Vec<GuiDraw>,
|
||||
skin: &str,
|
||||
x: i32,
|
||||
y: i32,
|
||||
size: i32,
|
||||
hat: bool,
|
||||
flip: bool,
|
||||
color: u32,
|
||||
) {
|
||||
let (v, height) = if flip { (16.0, -8) } else { (8.0, 8) };
|
||||
for u in [8.0, 40.0].into_iter().take(if hat { 2 } else { 1 }) {
|
||||
out.push(GuiDraw::Blit {
|
||||
@@ -109,7 +119,7 @@ fn face(out: &mut Vec<GuiDraw>, skin: &str, x: i32, y: i32, size: i32, hat: bool
|
||||
uv: [u, v],
|
||||
src: [8, height],
|
||||
tex: [64, 64],
|
||||
color: WHITE,
|
||||
color,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -448,6 +458,7 @@ impl TabList {
|
||||
8,
|
||||
p.show_hat,
|
||||
(i.upside_down)(p.uuid),
|
||||
WHITE,
|
||||
);
|
||||
xo += 9;
|
||||
}
|
||||
|
||||
@@ -2046,6 +2046,7 @@ impl Codec<TrackedWaypoint> for TrackedWaypoint {
|
||||
|
||||
packet!(AcceptTeleportation { id: i32 => VarInt, x: f64 => Double, y: f64 => Double, z: f64 => Double, yaw: f32 => Float, pitch: f32 => Float });
|
||||
packet!(Attack { id: i32 => VarInt });
|
||||
packet!(TeleportToEntity { uuid: u128 => Uuid });
|
||||
packet!(
|
||||
/// GameType ids: 0 survival, 1 creative, 2 adventure, 3 spectator.
|
||||
ChangeGameMode { mode: i32 => VarInt }
|
||||
@@ -2293,6 +2294,7 @@ packet_enum!(Serverbound, crate::ids::play::serverbound {
|
||||
SetGameRule(SetGameRule) = SET_GAME_RULE,
|
||||
AcceptTeleportation(AcceptTeleportation) = ACCEPT_TELEPORTATION,
|
||||
Attack(Attack) = ATTACK,
|
||||
TeleportToEntity(TeleportToEntity) = TELEPORT_TO_ENTITY,
|
||||
ChangeGameMode(ChangeGameMode) = CHANGE_GAME_MODE,
|
||||
Chat(Chat) = CHAT,
|
||||
ChatAck(ChatAck) = CHAT_ACK,
|
||||
|
||||
Reference in New Issue
Block a user