From 3d29c3254a39996197a175e6364d505d5d08217c Mon Sep 17 00:00:00 2001 From: CapsizeGlimmer <> Date: Wed, 24 Jun 2020 01:46:29 -0400 Subject: [PATCH] Add setting for displaying character names in chat. Placate cargo clippy. --- assets/voxygen/i18n/en.ron | 3 ++- chat-cli/src/main.rs | 3 ++- client/src/cmd.rs | 43 ++++++++++++++---------------- client/src/lib.rs | 20 +++++++++----- common/src/cmd.rs | 2 +- common/src/comp/chat.rs | 2 +- server/src/cmd.rs | 32 ++++++++++------------ server/src/events/interaction.rs | 2 +- server/src/events/player.rs | 4 +-- server/src/sys/entity_sync.rs | 2 +- server/src/sys/message.rs | 4 +-- server/src/sys/subscription.rs | 4 +-- voxygen/src/hud/chat.rs | 3 ++- voxygen/src/hud/mod.rs | 4 +++ voxygen/src/hud/settings_window.rs | 29 +++++++++++++++++++- voxygen/src/session.rs | 4 +++ voxygen/src/settings.rs | 2 ++ 17 files changed, 101 insertions(+), 62 deletions(-) diff --git a/assets/voxygen/i18n/en.ron b/assets/voxygen/i18n/en.ron index e4afafe9cd..3c974bed87 100644 --- a/assets/voxygen/i18n/en.ron +++ b/assets/voxygen/i18n/en.ron @@ -259,7 +259,8 @@ magically infused items?"#, "hud.settings.values": "Values", "hud.settings.percentages": "Percentages", "hud.settings.chat": "Chat", - "hud.settings.background_transparency": "Background Transparency", + "hud.settings.background_transparency": "Background Transparency", + "hud.settings.chat_character_name": "Show Character Names", "hud.settings.pan_sensitivity": "Pan Sensitivity", "hud.settings.zoom_sensitivity": "Zoom Sensitivity", diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index 7e80e3099f..f833d5ce66 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -82,9 +82,10 @@ fn main() { }, }; + const SHOW_NAME: bool = false; for event in events { match event { - Event::Chat(m) => println!("{}", client.format_message(&m)), + Event::Chat(m) => println!("{}", client.format_message(&m, SHOW_NAME)), Event::Disconnect => {}, // TODO Event::DisconnectionNotification(time) => { let message = match time { diff --git a/client/src/cmd.rs b/client/src/cmd.rs index 09d59ee15f..0e07f0365a 100644 --- a/client/src/cmd.rs +++ b/client/src/cmd.rs @@ -79,43 +79,40 @@ fn nth_word(line: &str, n: usize) -> Option { None } -#[allow(clippy::chars_next_cmp)] // TODO: Pending review in #587 pub fn complete(line: &str, client: &Client) -> Vec { let word = if line.chars().last().map_or(true, char::is_whitespace) { "" } else { line.split_whitespace().last().unwrap_or("") }; - if line.chars().next() == Some('/') { + if line.starts_with('/') { let mut iter = line.split_whitespace(); let cmd = iter.next().unwrap(); let i = iter.count() + if word.is_empty() { 1 } else { 0 }; if i == 0 { // Completing chat command name complete_command(word) - } else { - if let Ok(cmd) = cmd.parse::() { - if let Some(arg) = cmd.data().args.get(i - 1) { - // Complete ith argument - arg.complete(word, &client) - } else { - // Complete past the last argument - match cmd.data().args.last() { - Some(ArgumentSpec::SubCommand) => { - if let Some(index) = nth_word(line, cmd.data().args.len()) { - complete(&line[index..], &client) - } else { - vec![] - } - }, - Some(ArgumentSpec::Message(_)) => complete_player(word, &client), - _ => vec![], // End of command. Nothing to complete - } - } + } else if let Ok(cmd) = cmd.parse::() { + if let Some(arg) = cmd.data().args.get(i - 1) { + // Complete ith argument + arg.complete(word, &client) } else { - // Completing for unknown chat command - complete_player(word, &client) + // Complete past the last argument + match cmd.data().args.last() { + Some(ArgumentSpec::SubCommand) => { + if let Some(index) = nth_word(line, cmd.data().args.len()) { + complete(&line[index..], &client) + } else { + vec![] + } + }, + Some(ArgumentSpec::Message(_)) => complete_player(word, &client), + _ => vec![], // End of command. Nothing to complete + } } + } else { + // Completing for unknown chat command + complete_player(word, &client) } } else { // Not completing a command diff --git a/client/src/lib.rs b/client/src/lib.rs index 869f8d5788..17000154ec 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -875,7 +875,6 @@ impl Client { if self .state .read_component_cloned::(self.entity) - .map(|u| u.into()) != Some(entity) { self.state @@ -1020,7 +1019,8 @@ impl Client { } /// Format a message for the client (voxygen chat box or chat-cli) - pub fn format_message(&self, comp::ChatMsg { chat_type, message }: &comp::ChatMsg) -> String { + pub fn format_message(&self, msg: &comp::ChatMsg, character_name: bool) -> String { + let comp::ChatMsg { chat_type, message } = &msg; let alias_of_uid = |uid| { self.player_list .get(uid) @@ -1032,11 +1032,19 @@ impl Client { } }) }; + let name_of_uid = |uid| { + let ecs = self.state.ecs(); + (&ecs.read_storage::(), &ecs.read_storage::()) + .join().find(|(_, u)| u == &uid).map(|(c, _)| c.name.clone()) + }; let message_format = |uid, message, group| { - if let Some(group) = group { - format!("({}) [{}]: {}", group, alias_of_uid(uid), message) - } else { - format!("[{}]: {}", alias_of_uid(uid), message) + let alias = alias_of_uid(uid); + let name = if character_name { name_of_uid(uid) } else { None }; + match (group, name) { + (Some(group), None) => format!("({}) [{}]: {}", group, alias, message), + (None, None) => format!("[{}]: {}", alias, message), + (Some(group), Some(name)) => format!("({}) [{}] {}: {}", group, alias, name, message), + (None, Some(name)) => format!("[{}] {}: {}", alias, name, message), } }; match chat_type { diff --git a/common/src/cmd.rs b/common/src/cmd.rs index 68da230d76..94ee54c532 100644 --- a/common/src/cmd.rs +++ b/common/src/cmd.rs @@ -443,7 +443,7 @@ impl FromStr for ChatCommand { if keyword.len() == 1 { if let Some(c) = keyword .chars() - .nth(0) + .next() .as_ref() .and_then(|k| CHAT_SHORTCUTS.get(k)) { diff --git a/common/src/comp/chat.rs b/common/src/comp/chat.rs index 17e355df27..1da7e3c8a5 100644 --- a/common/src/comp/chat.rs +++ b/common/src/comp/chat.rs @@ -254,7 +254,7 @@ impl SpeechBubble { { match &self.message { SpeechBubbleMessage::Plain(m) => m.to_string(), - SpeechBubbleMessage::Localized(k, i) => i18n_variation(&k, *i).to_string(), + SpeechBubbleMessage::Localized(k, i) => i18n_variation(&k, *i), } } } diff --git a/server/src/cmd.rs b/server/src/cmd.rs index f41bfb8a0a..e67cf8afa5 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -457,19 +457,15 @@ fn handle_tp( } else if client != target { Some(client) } else { - if client != target { - Some(client) - } else { - server.notify_client( - client, - ChatType::CommandError.server_msg("You must specify a player name"), - ); - server.notify_client( - client, - ChatType::CommandError.server_msg(action.help_string()), - ); - return; - } + server.notify_client( + client, + ChatType::CommandError.server_msg("You must specify a player name"), + ); + server.notify_client( + client, + ChatType::CommandError.server_msg(action.help_string()), + ); + return; }; if let Some(_pos) = server.state.read_component_cloned::(target) { if let Some(player) = opt_player { @@ -1089,7 +1085,7 @@ fn handle_faction( if let Some(uid) = ecs.read_storage().get(client) { server .state - .send_chat(mode.new_message(*uid, msg.to_string())); + .send_chat(mode.new_message(*uid, msg)); } } } else { @@ -1123,7 +1119,7 @@ fn handle_group( if let Some(uid) = ecs.read_storage().get(client) { server .state - .send_chat(mode.new_message(*uid, msg.to_string())); + .send_chat(mode.new_message(*uid, msg)); } } } else { @@ -1159,7 +1155,7 @@ fn handle_region( if let Some(uid) = server.state.ecs().read_storage().get(client) { server .state - .send_chat(mode.new_message(*uid, msg.to_string())); + .send_chat(mode.new_message(*uid, msg)); } } } @@ -1189,7 +1185,7 @@ fn handle_say( if let Some(uid) = server.state.ecs().read_storage().get(client) { server .state - .send_chat(mode.new_message(*uid, msg.to_string())); + .send_chat(mode.new_message(*uid, msg)); } } } @@ -1219,7 +1215,7 @@ fn handle_world( if let Some(uid) = server.state.ecs().read_storage().get(client) { server .state - .send_chat(mode.new_message(*uid, msg.to_string())); + .send_chat(mode.new_message(*uid, msg)); } } } diff --git a/server/src/events/interaction.rs b/server/src/events/interaction.rs index 389bce0ff4..ebb47a9b73 100644 --- a/server/src/events/interaction.rs +++ b/server/src/events/interaction.rs @@ -120,7 +120,7 @@ pub fn handle_possess(server: &Server, possessor_uid: Uid, possesse_uid: Uid) { let mut clients = ecs.write_storage::(); if clients.get_mut(possesse).is_none() { if let Some(mut client) = clients.remove(possessor) { - client.notify(ServerMsg::SetPlayerEntity(possesse_uid.into())); + client.notify(ServerMsg::SetPlayerEntity(possesse_uid)); clients .insert(possesse, client) .err() diff --git a/server/src/events/player.rs b/server/src/events/player.rs index 32682c166b..aae303c47f 100644 --- a/server/src/events/player.rs +++ b/server/src/events/player.rs @@ -53,9 +53,7 @@ pub fn handle_client_disconnect(server: &mut Server, entity: EcsEntity) -> Event state.read_storage::().get(entity), state.read_storage::().get(entity), ) { - state.notify_registered_clients(ServerMsg::PlayerListUpdate(PlayerListUpdate::Remove( - (*uid).into(), - ))) + state.notify_registered_clients(ServerMsg::PlayerListUpdate(PlayerListUpdate::Remove(*uid))) } // Make sure to remove the player from the logged in list. (See AuthProvider) diff --git a/server/src/sys/entity_sync.rs b/server/src/sys/entity_sync.rs index d792e4baa8..d4bbf62d20 100644 --- a/server/src/sys/entity_sync.rs +++ b/server/src/sys/entity_sync.rs @@ -147,7 +147,7 @@ impl<'a> System<'a> for Sys { .map(|key| !regions.contains(key)) .unwrap_or(true) { - client.notify(ServerMsg::DeleteEntity(uid.into())); + client.notify(ServerMsg::DeleteEntity(uid)); } } } diff --git a/server/src/sys/message.rs b/server/src/sys/message.rs index fb1d0a9134..3b7227a9a5 100644 --- a/server/src/sys/message.rs +++ b/server/src/sys/message.rs @@ -94,7 +94,7 @@ impl<'a> System<'a> for Sys { let player_list = (&uids, &players, stats.maybe(), admins.maybe()) .join() .map(|(uid, player, stats, admin)| { - ((*uid).into(), PlayerInfo { + (*uid, PlayerInfo { is_online: true, is_admin: admin.is_some(), player_alias: player.alias.clone(), @@ -465,7 +465,7 @@ impl<'a> System<'a> for Sys { for entity in new_players { if let (Some(uid), Some(player)) = (uids.get(entity), players.get(entity)) { let msg = - ServerMsg::PlayerListUpdate(PlayerListUpdate::Add((*uid).into(), PlayerInfo { + ServerMsg::PlayerListUpdate(PlayerListUpdate::Add(*uid, PlayerInfo { player_alias: player.alias.clone(), is_online: true, is_admin: admins.get(entity).is_some(), diff --git a/server/src/sys/subscription.rs b/server/src/sys/subscription.rs index 4716fc84c9..870b4f14b3 100644 --- a/server/src/sys/subscription.rs +++ b/server/src/sys/subscription.rs @@ -151,7 +151,7 @@ impl<'a> System<'a> for Sys { .map(|key| subscription.regions.contains(key)) .unwrap_or(false) { - client.notify(ServerMsg::DeleteEntity(uid.into())); + client.notify(ServerMsg::DeleteEntity(uid)); } } }, @@ -159,7 +159,7 @@ impl<'a> System<'a> for Sys { } // Tell client to delete entities in the region for (&uid, _) in (&uids, region.entities()).join() { - client.notify(ServerMsg::DeleteEntity(uid.into())); + client.notify(ServerMsg::DeleteEntity(uid)); } } // Send deleted entities since they won't be processed for this client in entity diff --git a/voxygen/src/hud/chat.rs b/voxygen/src/hud/chat.rs index 329d7d267a..99fc644ab2 100644 --- a/voxygen/src/hud/chat.rs +++ b/voxygen/src/hud/chat.rs @@ -327,12 +327,13 @@ impl<'a> Widget for Chat<'a> { }); } + let show_char_name = self.global_state.settings.gameplay.chat_character_name; while let Some(item) = items.next(ui) { // This would be easier if conrod used the v-metrics from rusttype. if item.i < state.messages.len() { let message = &state.messages[item.i]; let (color, icon) = render_chat_line(&message.chat_type, &self.imgs); - let msg = self.client.format_message(message); + let msg = self.client.format_message(message, show_char_name); let text = Text::new(&msg) .font_size(self.fonts.opensans.scale(15)) .font_id(self.fonts.opensans.conrod_id) diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 0169f0344e..80fb0f4da5 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -256,6 +256,7 @@ pub enum Event { ChangeFluidMode(FluidMode), CrosshairTransp(f32), ChatTransp(f32), + ChatCharName(bool), CrosshairType(CrosshairType), ToggleXpBar(XpBar), Intro(Intro), @@ -1662,6 +1663,9 @@ impl Hud { settings_window::Event::ChatTransp(chat_transp) => { events.push(Event::ChatTransp(chat_transp)); }, + settings_window::Event::ChatCharName(chat_char_name) => { + events.push(Event::ChatCharName(chat_char_name)); + }, settings_window::Event::ToggleZoomInvert(zoom_inverted) => { events.push(Event::ToggleZoomInvert(zoom_inverted)); }, diff --git a/voxygen/src/hud/settings_window.rs b/voxygen/src/hud/settings_window.rs index 7f06b2f39c..0009f0de3f 100644 --- a/voxygen/src/hud/settings_window.rs +++ b/voxygen/src/hud/settings_window.rs @@ -137,6 +137,8 @@ widget_ids! { chat_transp_title, chat_transp_text, chat_transp_slider, + chat_char_name_text, + chat_char_name_button, sct_title, sct_show_text, sct_show_radio, @@ -240,6 +242,7 @@ pub enum Event { CrosshairType(CrosshairType), UiScale(ScaleChange), ChatTransp(f32), + ChatCharName(bool), Sct(bool), SctPlayerBatch(bool), SctDamageBatch(bool), @@ -1132,8 +1135,32 @@ impl<'a> Widget for SettingsWindow<'a> { events.push(Event::ChatTransp(new_val)); } + // "Show character names in chat" toggle button + let chat_char_name = ToggleButton::new( + self.global_state.settings.gameplay.chat_character_name, + self.imgs.checkbox, + self.imgs.checkbox_checked, + ) + .w_h(18.0, 18.0) + .down_from(state.ids.chat_transp_slider, 20.0) + .hover_images(self.imgs.checkbox_mo, self.imgs.checkbox_checked_mo) + .press_images(self.imgs.checkbox_press, self.imgs.checkbox_checked) + .set(state.ids.chat_char_name_button, ui); + if self.global_state.settings.gameplay.chat_character_name != chat_char_name { + events.push(Event::ChatCharName( + !self.global_state.settings.gameplay.chat_character_name, + )); + } + Text::new(&self.localized_strings.get("hud.settings.chat_character_name")) + .right_from(state.ids.chat_char_name_button, 20.0) + .font_size(self.fonts.cyri.scale(14)) + .font_id(self.fonts.cyri.conrod_id) + .color(TEXT_COLOR) + .set(state.ids.chat_char_name_text, ui); + + // Language select drop down Text::new(&self.localized_strings.get("common.languages")) - .down_from(state.ids.chat_transp_slider, 20.0) + .down_from(state.ids.chat_char_name_button, 20.0) .font_size(self.fonts.cyri.scale(18)) .font_id(self.fonts.cyri.conrod_id) .color(TEXT_COLOR) diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 83b3d8220d..4b19d39795 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -690,6 +690,10 @@ impl PlayState for SessionState { global_state.settings.gameplay.chat_transp = chat_transp; global_state.settings.save_to_file_warn(); }, + HudEvent::ChatCharName(chat_char_name) => { + global_state.settings.gameplay.chat_character_name = chat_char_name; + global_state.settings.save_to_file_warn(); + }, HudEvent::CrosshairType(crosshair_type) => { global_state.settings.gameplay.crosshair_type = crosshair_type; global_state.settings.save_to_file_warn(); diff --git a/voxygen/src/settings.rs b/voxygen/src/settings.rs index 792d643a71..3427b3a8fe 100644 --- a/voxygen/src/settings.rs +++ b/voxygen/src/settings.rs @@ -463,6 +463,7 @@ pub struct GameplaySettings { pub smooth_pan_enable: bool, pub crosshair_transp: f32, pub chat_transp: f32, + pub chat_character_name: bool, pub crosshair_type: CrosshairType, pub intro_show: Intro, pub xp_bar: XpBar, @@ -490,6 +491,7 @@ impl Default for GameplaySettings { speech_bubble_icon: true, crosshair_transp: 0.6, chat_transp: 0.4, + chat_character_name: true, crosshair_type: CrosshairType::Round, intro_show: Intro::Show, xp_bar: XpBar::Always,