From 215693137bc2ae0f3cc5ba5972b5790dd66c1ee9 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Fri, 5 Jul 2019 18:21:11 +0200 Subject: [PATCH] Implement chat command key --- Cargo.lock | 6 +++--- voxygen/src/hud/chat.rs | 34 +++++++++++++++++++++++++++++++--- voxygen/src/hud/mod.rs | 25 ++++++++++++++++++++++--- voxygen/src/settings.rs | 2 ++ voxygen/src/window.rs | 2 ++ 5 files changed, 60 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7f998a273..4be83ee31a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -360,7 +360,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "conrod_core" version = "0.63.0" -source = "git+https://gitlab.com/veloren/conrod.git#93f02e61838b475ff190b3563a0f41f8981cc228" +source = "git+https://gitlab.com/veloren/conrod.git#d603363488870eae9df91ba45ba795509c8a6ab4" dependencies = [ "conrod_derive 0.63.0 (git+https://gitlab.com/veloren/conrod.git)", "daggy 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -374,7 +374,7 @@ dependencies = [ [[package]] name = "conrod_derive" version = "0.63.0" -source = "git+https://gitlab.com/veloren/conrod.git#93f02e61838b475ff190b3563a0f41f8981cc228" +source = "git+https://gitlab.com/veloren/conrod.git#d603363488870eae9df91ba45ba795509c8a6ab4" dependencies = [ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -384,7 +384,7 @@ dependencies = [ [[package]] name = "conrod_winit" version = "0.63.0" -source = "git+https://gitlab.com/veloren/conrod.git#93f02e61838b475ff190b3563a0f41f8981cc228" +source = "git+https://gitlab.com/veloren/conrod.git#d603363488870eae9df91ba45ba795509c8a6ab4" [[package]] name = "constant_time_eq" diff --git a/voxygen/src/hud/chat.rs b/voxygen/src/hud/chat.rs index eaca8fafdb..d6e29d8e72 100644 --- a/voxygen/src/hud/chat.rs +++ b/voxygen/src/hud/chat.rs @@ -2,6 +2,7 @@ use super::{img_ids::Imgs, Fonts, TEXT_COLOR}; use conrod_core::{ input::Key, position::Dimension, + text::cursor::Index, widget::{self, Button, Id, List, Rectangle, Text, TextEdit}, widget_ids, Colorable, Positionable, Sizeable, UiCell, Widget, WidgetCommon, }; @@ -22,6 +23,8 @@ const MAX_MESSAGES: usize = 100; #[derive(WidgetCommon)] pub struct Chat<'a> { new_messages: &'a mut VecDeque, + force_input: Option, + force_cursor: Option, imgs: &'a Imgs, fonts: &'a Fonts, @@ -34,12 +37,24 @@ impl<'a> Chat<'a> { pub fn new(new_messages: &'a mut VecDeque, imgs: &'a Imgs, fonts: &'a Fonts) -> Self { Self { new_messages, + force_input: None, + force_cursor: None, imgs, fonts, common: widget::CommonBuilder::default(), } } + pub fn input(mut self, input: String) -> Self { + self.force_input = Some(input); + self + } + + pub fn cursor_pos(mut self, index: Index) -> Self { + self.force_cursor = Some(index); + self + } + fn scrolled_to_bottom(state: &State, ui: &UiCell) -> bool { // Might be more efficient to cache result and update it when a scroll event has occurred // instead of every frame. @@ -74,8 +89,8 @@ impl<'a> Widget for Chat<'a> { fn init_state(&self, id_gen: widget::id::Generator) -> Self::State { State { - messages: VecDeque::new(), input: "".to_owned(), + messages: VecDeque::new(), ids: Ids::new(id_gen), } } @@ -101,18 +116,30 @@ impl<'a> Widget for Chat<'a> { }); let keyboard_capturer = ui.global_input().current.widget_capturing_keyboard; - let input_focused = keyboard_capturer == Some(state.ids.input); + + if let Some(input) = &self.force_input { + state.update(|s| s.input = input.clone()); + } + + let input_focused = + keyboard_capturer == Some(state.ids.input) || keyboard_capturer == Some(id); // Only show if it has the keyboard captured. // Chat input uses a rectangle as its background. if input_focused { - let text_edit = TextEdit::new(&state.input) + let input = self.force_input.as_ref().unwrap_or(&state.input); + let mut text_edit = TextEdit::new(input) .w(460.0) .restrict_to_height(false) .color(TEXT_COLOR) .line_spacing(2.0) .font_size(15) .font_id(self.fonts.opensans); + + if let Some(pos) = self.force_cursor { + text_edit = text_edit.cursor_pos(pos); + } + let y = match text_edit.get_y_dimension(ui) { Dimension::Absolute(y) => y + 6.0, _ => 0.0, @@ -122,6 +149,7 @@ impl<'a> Widget for Chat<'a> { .bottom_left_with_margins_on(ui.window, 10.0, 10.0) .w(470.0) .set(state.ids.input_bg, ui); + if let Some(str) = text_edit .top_left_with_margins_on(state.ids.input_bg, 1.0, 1.0) .set(state.ids.input, ui) diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index cc5a55dcef..cdc65ea362 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -33,6 +33,7 @@ use crate::{ use client::Client; use common::{comp, terrain::TerrainChunkSize, vol::VolSize}; use conrod_core::{ + text::cursor::Index, widget::{self, Button, Image, Rectangle, Text}, widget_ids, Color, Colorable, Labelable, Positionable, Sizeable, Widget, }; @@ -259,6 +260,8 @@ pub struct Hud { show: Show, to_focus: Option>, force_ungrab: bool, + force_chat_input: Option, + force_chat_cursor: Option, } impl Hud { @@ -296,6 +299,8 @@ impl Hud { }, to_focus: None, force_ungrab: false, + force_chat_input: None, + force_chat_cursor: None, } } @@ -561,9 +566,17 @@ impl Hud { } // Chat box - match Chat::new(&mut self.new_messages, &self.imgs, &self.fonts) - .set(self.ids.chat, ui_widgets) - { + let mut chat = Chat::new(&mut self.new_messages, &self.imgs, &self.fonts); + + if let Some(input) = self.force_chat_input.take() { + chat = chat.input(input); + } + + if let Some(pos) = self.force_chat_cursor.take() { + chat = chat.cursor_pos(pos); + } + + match chat.set(self.ids.chat, ui_widgets) { Some(chat::Event::SendMessage(message)) => { events.push(Event::SendMessage(message)); } @@ -739,6 +752,12 @@ impl Hud { // Press key while not typing WinEvent::InputUpdate(key, true) if !self.typing() => match key { + GameInput::Command => { + self.force_chat_input = Some("/".to_owned()); + self.force_chat_cursor = Some(Index { line: 0, char: 1 }); + self.ui.focus_widget(Some(self.ids.chat)); + true + } GameInput::Map => { self.show.toggle_map(); true diff --git a/voxygen/src/settings.rs b/voxygen/src/settings.rs index 251e904453..a37864efef 100644 --- a/voxygen/src/settings.rs +++ b/voxygen/src/settings.rs @@ -11,6 +11,7 @@ pub struct ControlSettings { pub toggle_cursor: KeyMouse, pub escape: KeyMouse, pub enter: KeyMouse, + pub command: KeyMouse, pub move_forward: KeyMouse, pub move_left: KeyMouse, pub move_back: KeyMouse, @@ -41,6 +42,7 @@ impl Default for ControlSettings { toggle_cursor: KeyMouse::Key(VirtualKeyCode::Tab), escape: KeyMouse::Key(VirtualKeyCode::Escape), enter: KeyMouse::Key(VirtualKeyCode::Return), + command: KeyMouse::Key(VirtualKeyCode::Slash), move_forward: KeyMouse::Key(VirtualKeyCode::W), move_left: KeyMouse::Key(VirtualKeyCode::A), move_back: KeyMouse::Key(VirtualKeyCode::S), diff --git a/voxygen/src/window.rs b/voxygen/src/window.rs index 2a6673bcc3..dddd8d86d8 100644 --- a/voxygen/src/window.rs +++ b/voxygen/src/window.rs @@ -19,6 +19,7 @@ pub enum GameInput { Jump, Glide, Enter, + Command, Escape, Map, Bag, @@ -110,6 +111,7 @@ impl Window { key_map.insert(settings.controls.toggle_cursor, GameInput::ToggleCursor); key_map.insert(settings.controls.escape, GameInput::Escape); key_map.insert(settings.controls.enter, GameInput::Enter); + key_map.insert(settings.controls.command, GameInput::Command); key_map.insert(settings.controls.move_forward, GameInput::MoveForward); key_map.insert(settings.controls.move_left, GameInput::MoveLeft); key_map.insert(settings.controls.move_back, GameInput::MoveBack);