2019-07-21 18:34:52 +00:00
|
|
|
use super::{
|
2020-07-01 19:05:44 +00:00
|
|
|
img_ids::Imgs, ERROR_COLOR, FACTION_COLOR, GROUP_COLOR, INFO_COLOR, KILL_COLOR, LOOT_COLOR,
|
|
|
|
OFFLINE_COLOR, ONLINE_COLOR, REGION_COLOR, SAY_COLOR, TELL_COLOR, TEXT_COLOR, WORLD_COLOR,
|
2019-07-21 18:34:52 +00:00
|
|
|
};
|
2020-01-26 19:29:46 +00:00
|
|
|
use crate::{ui::fonts::ConrodVoxygenFonts, GlobalState};
|
2020-06-02 02:42:26 +00:00
|
|
|
use client::{cmd, Client};
|
|
|
|
use common::{
|
|
|
|
comp::{ChatMsg, ChatType},
|
|
|
|
msg::validate_chat_msg,
|
|
|
|
};
|
2019-03-15 04:55:52 +00:00
|
|
|
use conrod_core::{
|
|
|
|
input::Key,
|
|
|
|
position::Dimension,
|
2020-05-06 18:45:58 +00:00
|
|
|
text::{
|
|
|
|
self,
|
|
|
|
cursor::{self, Index},
|
|
|
|
},
|
2020-06-05 22:36:31 +00:00
|
|
|
widget::{self, Button, Id, Image, List, Rectangle, Text, TextEdit},
|
|
|
|
widget_ids, Color, Colorable, Positionable, Sizeable, Ui, UiCell, Widget, WidgetCommon,
|
2019-03-15 04:55:52 +00:00
|
|
|
};
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
widget_ids! {
|
|
|
|
struct Ids {
|
|
|
|
message_box,
|
|
|
|
message_box_bg,
|
2020-05-06 18:45:58 +00:00
|
|
|
chat_input,
|
|
|
|
chat_input_bg,
|
2019-03-30 02:59:31 +00:00
|
|
|
chat_arrow,
|
2020-06-05 22:36:31 +00:00
|
|
|
chat_icons[],
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-18 21:56:45 +00:00
|
|
|
/*#[const_tweaker::tweak(min = 0.0, max = 60.0, step = 1.0)]
|
|
|
|
const X: f64 = 18.0;*/
|
2019-05-03 16:56:18 +00:00
|
|
|
|
2019-05-25 19:16:38 +00:00
|
|
|
const MAX_MESSAGES: usize = 100;
|
|
|
|
|
2020-06-05 22:36:31 +00:00
|
|
|
const CHAT_BOX_WIDTH: f64 = 470.0;
|
2020-06-10 04:21:56 +00:00
|
|
|
const CHAT_BOX_INPUT_WIDTH: f64 = 460.0;
|
2020-06-05 22:36:31 +00:00
|
|
|
const CHAT_BOX_HEIGHT: f64 = 174.0;
|
|
|
|
|
2019-05-03 16:56:18 +00:00
|
|
|
#[derive(WidgetCommon)]
|
|
|
|
pub struct Chat<'a> {
|
2020-06-02 02:42:26 +00:00
|
|
|
new_messages: &'a mut VecDeque<ChatMsg>,
|
2020-06-02 06:11:47 +00:00
|
|
|
client: &'a Client,
|
2019-07-05 16:21:11 +00:00
|
|
|
force_input: Option<String>,
|
|
|
|
force_cursor: Option<Index>,
|
2020-05-08 21:38:58 +00:00
|
|
|
force_completions: Option<Vec<String>>,
|
2019-05-03 16:56:18 +00:00
|
|
|
|
2019-10-23 19:40:45 +00:00
|
|
|
global_state: &'a GlobalState,
|
2019-05-03 16:56:18 +00:00
|
|
|
imgs: &'a Imgs,
|
2020-01-26 19:29:46 +00:00
|
|
|
fonts: &'a ConrodVoxygenFonts,
|
2019-05-03 16:56:18 +00:00
|
|
|
|
|
|
|
#[conrod(common_builder)]
|
|
|
|
common: widget::CommonBuilder,
|
2019-09-02 04:54:44 +00:00
|
|
|
|
|
|
|
// TODO: add an option to adjust this
|
|
|
|
history_max: usize,
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
2019-05-03 16:56:18 +00:00
|
|
|
|
|
|
|
impl<'a> Chat<'a> {
|
2019-07-17 22:10:42 +00:00
|
|
|
pub fn new(
|
2020-06-02 02:42:26 +00:00
|
|
|
new_messages: &'a mut VecDeque<ChatMsg>,
|
2020-06-02 06:11:47 +00:00
|
|
|
client: &'a Client,
|
2019-10-23 19:40:45 +00:00
|
|
|
global_state: &'a GlobalState,
|
2019-07-17 22:10:42 +00:00
|
|
|
imgs: &'a Imgs,
|
2020-01-26 19:29:46 +00:00
|
|
|
fonts: &'a ConrodVoxygenFonts,
|
2019-07-17 22:10:42 +00:00
|
|
|
) -> Self {
|
2019-05-03 16:56:18 +00:00
|
|
|
Self {
|
|
|
|
new_messages,
|
2020-06-02 06:11:47 +00:00
|
|
|
client,
|
2019-07-05 16:21:11 +00:00
|
|
|
force_input: None,
|
|
|
|
force_cursor: None,
|
2020-05-08 21:38:58 +00:00
|
|
|
force_completions: None,
|
2019-05-03 16:56:18 +00:00
|
|
|
imgs,
|
|
|
|
fonts,
|
2019-10-23 19:40:45 +00:00
|
|
|
global_state,
|
2019-05-03 16:56:18 +00:00
|
|
|
common: widget::CommonBuilder::default(),
|
2019-09-02 04:54:44 +00:00
|
|
|
history_max: 32,
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-03 16:56:18 +00:00
|
|
|
|
2020-06-02 06:11:47 +00:00
|
|
|
pub fn prepare_tab_completion(mut self, input: String) -> Self {
|
2020-05-08 21:38:58 +00:00
|
|
|
if let Some(index) = input.find('\t') {
|
2020-06-02 06:11:47 +00:00
|
|
|
self.force_completions = Some(cmd::complete(&input[..index], &self.client));
|
2020-05-08 21:38:58 +00:00
|
|
|
} else {
|
|
|
|
self.force_completions = None;
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-07-05 16:21:11 +00:00
|
|
|
pub fn input(mut self, input: String) -> Self {
|
2019-08-14 04:38:54 +00:00
|
|
|
if let Ok(()) = validate_chat_msg(&input) {
|
|
|
|
self.force_input = Some(input);
|
|
|
|
}
|
2019-07-05 16:21:11 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cursor_pos(mut self, index: Index) -> Self {
|
|
|
|
self.force_cursor = Some(index);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-05-03 16:56:18 +00:00
|
|
|
fn scrolled_to_bottom(state: &State, ui: &UiCell) -> bool {
|
2020-02-01 20:39:39 +00:00
|
|
|
// Might be more efficient to cache result and update it when a scroll event has
|
|
|
|
// occurred instead of every frame.
|
2019-05-03 16:56:18 +00:00
|
|
|
if let Some(scroll) = ui
|
2019-03-30 02:59:31 +00:00
|
|
|
.widget_graph()
|
2019-05-03 16:56:18 +00:00
|
|
|
.widget(state.ids.message_box)
|
2019-03-30 02:59:31 +00:00
|
|
|
.and_then(|widget| widget.maybe_y_scroll_state)
|
|
|
|
{
|
|
|
|
scroll.offset >= scroll.offset_bounds.start
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 16:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct State {
|
2020-06-02 02:42:26 +00:00
|
|
|
messages: VecDeque<ChatMsg>,
|
2019-05-03 16:56:18 +00:00
|
|
|
input: String,
|
2019-07-15 17:04:48 +00:00
|
|
|
ids: Ids,
|
2019-07-12 23:20:38 +00:00
|
|
|
history: VecDeque<String>,
|
2019-07-15 17:04:48 +00:00
|
|
|
// Index into the history Vec, history_pos == 0 is history not in use
|
|
|
|
// otherwise index is history_pos -1
|
2019-07-12 23:20:38 +00:00
|
|
|
history_pos: usize,
|
2020-05-06 18:45:58 +00:00
|
|
|
completions: Vec<String>,
|
2020-05-08 21:38:58 +00:00
|
|
|
// Index into the completion Vec
|
|
|
|
completions_index: Option<usize>,
|
|
|
|
// At which character is tab completion happening
|
|
|
|
completion_cursor: Option<usize>,
|
2019-05-03 16:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Event {
|
2020-05-08 21:38:58 +00:00
|
|
|
TabCompletionStart(String),
|
2019-05-03 16:56:18 +00:00
|
|
|
SendMessage(String),
|
2019-05-06 12:28:57 +00:00
|
|
|
Focus(Id),
|
2019-05-03 16:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for Chat<'a> {
|
2020-02-01 20:39:39 +00:00
|
|
|
type Event = Option<Event>;
|
2019-05-03 16:56:18 +00:00
|
|
|
type State = State;
|
|
|
|
type Style = ();
|
|
|
|
|
|
|
|
fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
|
|
|
|
State {
|
|
|
|
input: "".to_owned(),
|
2019-07-05 16:21:11 +00:00
|
|
|
messages: VecDeque::new(),
|
2019-07-12 23:20:38 +00:00
|
|
|
history: VecDeque::new(),
|
|
|
|
history_pos: 0,
|
2020-05-06 18:45:58 +00:00
|
|
|
completions: Vec::new(),
|
2020-05-08 21:38:58 +00:00
|
|
|
completions_index: None,
|
|
|
|
completion_cursor: None,
|
2019-05-03 16:56:18 +00:00
|
|
|
ids: Ids::new(id_gen),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::unused_unit)] // TODO: Pending review in #587
|
2020-02-01 20:39:39 +00:00
|
|
|
fn style(&self) -> Self::Style { () }
|
2019-05-03 16:56:18 +00:00
|
|
|
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::redundant_clone)] // TODO: Pending review in #587
|
|
|
|
#[allow(clippy::single_match)] // TODO: Pending review in #587
|
2019-05-03 16:56:18 +00:00
|
|
|
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
|
2019-05-07 05:40:03 +00:00
|
|
|
let widget::UpdateArgs { id, state, ui, .. } = args;
|
2019-10-23 19:40:45 +00:00
|
|
|
let transp = self.global_state.settings.gameplay.chat_transp;
|
2019-05-17 09:22:32 +00:00
|
|
|
// Maintain scrolling.
|
2019-05-03 16:56:18 +00:00
|
|
|
if !self.new_messages.is_empty() {
|
2020-06-11 05:16:42 +00:00
|
|
|
state.update(|s| s.messages.extend(self.new_messages.drain(..)));
|
2019-05-03 16:56:18 +00:00
|
|
|
ui.scroll_widget(state.ids.message_box, [0.0, std::f64::MAX]);
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 19:16:38 +00:00
|
|
|
// Empty old messages
|
|
|
|
state.update(|s| {
|
|
|
|
while s.messages.len() > MAX_MESSAGES {
|
2019-05-25 20:03:56 +00:00
|
|
|
s.messages.pop_front();
|
2019-05-25 19:16:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-08 21:38:58 +00:00
|
|
|
if let Some(comps) = &self.force_completions {
|
|
|
|
state.update(|s| s.completions = comps.clone());
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:56:58 +00:00
|
|
|
let mut force_cursor = self.force_cursor;
|
|
|
|
|
2020-05-08 21:38:58 +00:00
|
|
|
// If up or down are pressed: move through history
|
|
|
|
// If any key other than up, down, or tab is pressed: stop completion.
|
|
|
|
let (history_dir, tab_dir, stop_tab_completion) =
|
|
|
|
ui.widget_input(state.ids.chat_input).presses().key().fold(
|
|
|
|
(0isize, 0isize, false),
|
|
|
|
|(n, m, tc), key_press| match key_press.key {
|
|
|
|
Key::Up => (n + 1, m - 1, tc),
|
|
|
|
Key::Down => (n - 1, m + 1, tc),
|
|
|
|
Key::Tab => (n, m + 1, tc),
|
|
|
|
_ => (n, m, true),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// Handle tab completion
|
|
|
|
let request_tab_completions = if stop_tab_completion {
|
|
|
|
// End tab completion
|
2020-05-06 18:45:58 +00:00
|
|
|
state.update(|s| {
|
2020-05-08 21:38:58 +00:00
|
|
|
if s.completion_cursor.is_some() {
|
|
|
|
s.completion_cursor = None;
|
|
|
|
}
|
|
|
|
s.completions_index = None;
|
|
|
|
});
|
|
|
|
false
|
|
|
|
} else if let Some(cursor) = state.completion_cursor {
|
|
|
|
// Cycle through tab completions of the current word
|
|
|
|
if state.input.contains('\t') {
|
|
|
|
state.update(|s| s.input.retain(|c| c != '\t'));
|
|
|
|
//tab_dir + 1
|
|
|
|
}
|
2020-06-11 17:06:11 +00:00
|
|
|
if !state.completions.is_empty() && (tab_dir != 0 || state.completions_index.is_none())
|
|
|
|
{
|
|
|
|
state.update(|s| {
|
|
|
|
let len = s.completions.len();
|
|
|
|
s.completions_index = Some(
|
|
|
|
(s.completions_index.unwrap_or(0) + (tab_dir + len as isize) as usize)
|
|
|
|
% len,
|
|
|
|
);
|
|
|
|
if let Some(replacement) = &s.completions.get(s.completions_index.unwrap()) {
|
|
|
|
let (completed, offset) = do_tab_completion(cursor, &s.input, replacement);
|
|
|
|
force_cursor = cursor_offset_to_index(offset, &completed, &ui, &self.fonts);
|
|
|
|
s.input = completed;
|
|
|
|
}
|
|
|
|
});
|
2020-05-08 21:38:58 +00:00
|
|
|
}
|
|
|
|
false
|
|
|
|
} else if let Some(cursor) = state.input.find('\t') {
|
|
|
|
// Begin tab completion
|
|
|
|
state.update(|s| s.completion_cursor = Some(cursor));
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
// Not tab completing
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
// Move through history
|
|
|
|
if history_dir != 0 && state.completion_cursor.is_none() {
|
|
|
|
state.update(|s| {
|
|
|
|
if history_dir > 0 {
|
2020-05-06 18:45:58 +00:00
|
|
|
if s.history_pos < s.history.len() {
|
2019-09-02 04:54:44 +00:00
|
|
|
s.history_pos += 1;
|
2020-05-06 18:45:58 +00:00
|
|
|
}
|
2020-06-11 17:06:11 +00:00
|
|
|
} else if s.history_pos > 0 {
|
|
|
|
s.history_pos -= 1;
|
2020-05-06 18:45:58 +00:00
|
|
|
}
|
|
|
|
if s.history_pos > 0 {
|
|
|
|
s.input = s.history.get(s.history_pos - 1).unwrap().to_owned();
|
|
|
|
force_cursor =
|
|
|
|
cursor_offset_to_index(s.input.len(), &s.input, &ui, &self.fonts);
|
|
|
|
} else {
|
|
|
|
s.input.clear();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-06 12:28:57 +00:00
|
|
|
let keyboard_capturer = ui.global_input().current.widget_capturing_keyboard;
|
2019-07-05 16:21:11 +00:00
|
|
|
|
|
|
|
if let Some(input) = &self.force_input {
|
2020-05-06 18:45:58 +00:00
|
|
|
state.update(|s| s.input = input.to_string());
|
2019-07-05 16:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let input_focused =
|
2020-05-06 18:45:58 +00:00
|
|
|
keyboard_capturer == Some(state.ids.chat_input) || keyboard_capturer == Some(id);
|
2019-05-06 12:28:57 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Only show if it has the keyboard captured.
|
|
|
|
// Chat input uses a rectangle as its background.
|
2019-05-06 12:28:57 +00:00
|
|
|
if input_focused {
|
2020-05-06 18:45:58 +00:00
|
|
|
// Any changes to this TextEdit's width and font size must be reflected in
|
|
|
|
// `cursor_offset_to_index` below.
|
2019-09-02 04:54:44 +00:00
|
|
|
let mut text_edit = TextEdit::new(&state.input)
|
2020-06-10 04:21:56 +00:00
|
|
|
.w(CHAT_BOX_INPUT_WIDTH)
|
2019-04-20 15:43:38 +00:00
|
|
|
.restrict_to_height(false)
|
2019-05-07 03:25:25 +00:00
|
|
|
.color(TEXT_COLOR)
|
2019-04-20 15:43:38 +00:00
|
|
|
.line_spacing(2.0)
|
2020-01-26 19:29:46 +00:00
|
|
|
.font_size(self.fonts.opensans.scale(15))
|
|
|
|
.font_id(self.fonts.opensans.conrod_id);
|
2019-07-05 16:21:11 +00:00
|
|
|
|
2020-05-05 22:56:58 +00:00
|
|
|
if let Some(pos) = force_cursor {
|
2019-07-05 16:21:11 +00:00
|
|
|
text_edit = text_edit.cursor_pos(pos);
|
|
|
|
}
|
|
|
|
|
2019-05-03 16:56:18 +00:00
|
|
|
let y = match text_edit.get_y_dimension(ui) {
|
2019-04-20 15:43:38 +00:00
|
|
|
Dimension::Absolute(y) => y + 6.0,
|
|
|
|
_ => 0.0,
|
|
|
|
};
|
2020-06-05 22:36:31 +00:00
|
|
|
Rectangle::fill([CHAT_BOX_WIDTH, y])
|
2019-10-23 19:40:45 +00:00
|
|
|
.rgba(0.0, 0.0, 0.0, transp + 0.1)
|
2019-05-03 16:56:18 +00:00
|
|
|
.bottom_left_with_margins_on(ui.window, 10.0, 10.0)
|
2020-06-05 22:36:31 +00:00
|
|
|
.w(CHAT_BOX_WIDTH)
|
2020-05-06 18:45:58 +00:00
|
|
|
.set(state.ids.chat_input_bg, ui);
|
2019-07-05 16:21:11 +00:00
|
|
|
|
2019-04-20 15:43:38 +00:00
|
|
|
if let Some(str) = text_edit
|
2020-05-06 18:45:58 +00:00
|
|
|
.top_left_with_margins_on(state.ids.chat_input_bg, 1.0, 1.0)
|
|
|
|
.set(state.ids.chat_input, ui)
|
2019-04-20 15:43:38 +00:00
|
|
|
{
|
2019-05-03 16:56:18 +00:00
|
|
|
let mut input = str.to_owned();
|
|
|
|
input.retain(|c| c != '\n');
|
2019-08-14 04:38:54 +00:00
|
|
|
if let Ok(()) = validate_chat_msg(&input) {
|
|
|
|
state.update(|s| s.input = input);
|
|
|
|
}
|
2019-04-20 15:43:38 +00:00
|
|
|
}
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Message box
|
2020-06-05 22:36:31 +00:00
|
|
|
Rectangle::fill([CHAT_BOX_WIDTH, CHAT_BOX_HEIGHT])
|
2019-10-23 19:40:45 +00:00
|
|
|
.rgba(0.0, 0.0, 0.0, transp)
|
2019-05-07 05:40:03 +00:00
|
|
|
.and(|r| {
|
|
|
|
if input_focused {
|
2020-05-06 18:45:58 +00:00
|
|
|
r.up_from(state.ids.chat_input_bg, 0.0)
|
2019-05-07 05:40:03 +00:00
|
|
|
} else {
|
|
|
|
r.bottom_left_with_margins_on(ui.window, 10.0, 10.0)
|
|
|
|
}
|
2019-04-20 15:43:38 +00:00
|
|
|
})
|
2020-06-05 22:36:31 +00:00
|
|
|
.crop_kids()
|
2019-05-03 16:56:18 +00:00
|
|
|
.set(state.ids.message_box_bg, ui);
|
|
|
|
let (mut items, _) = List::flow_down(state.messages.len() + 1)
|
2020-06-05 22:36:31 +00:00
|
|
|
.top_left_with_margins_on(state.ids.message_box_bg, 0.0, 16.0)
|
2020-07-18 21:56:45 +00:00
|
|
|
.w_h(CHAT_BOX_WIDTH - 16.0, CHAT_BOX_HEIGHT)
|
2019-04-20 14:46:36 +00:00
|
|
|
.scroll_kids_vertically()
|
2019-05-03 16:56:18 +00:00
|
|
|
.set(state.ids.message_box, ui);
|
2020-06-05 22:36:31 +00:00
|
|
|
if state.ids.chat_icons.len() < state.messages.len() {
|
|
|
|
state.update(|s| {
|
|
|
|
s.ids
|
|
|
|
.chat_icons
|
|
|
|
.resize(s.messages.len(), &mut ui.widget_id_generator())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-24 05:46:29 +00:00
|
|
|
let show_char_name = self.global_state.settings.gameplay.chat_character_name;
|
2019-05-03 16:56:18 +00:00
|
|
|
while let Some(item) = items.next(ui) {
|
2019-05-17 09:22:32 +00:00
|
|
|
// This would be easier if conrod used the v-metrics from rusttype.
|
2020-06-02 06:11:47 +00:00
|
|
|
if item.i < state.messages.len() {
|
2020-06-10 04:21:56 +00:00
|
|
|
let message = &state.messages[item.i];
|
|
|
|
let (color, icon) = render_chat_line(&message.chat_type, &self.imgs);
|
2020-06-24 05:46:29 +00:00
|
|
|
let msg = self.client.format_message(message, show_char_name);
|
2020-06-02 06:11:47 +00:00
|
|
|
let text = Text::new(&msg)
|
2020-06-02 02:42:26 +00:00
|
|
|
.font_size(self.fonts.opensans.scale(15))
|
|
|
|
.font_id(self.fonts.opensans.conrod_id)
|
2020-07-18 21:56:45 +00:00
|
|
|
.w(CHAT_BOX_WIDTH - 17.0)
|
2020-06-02 02:42:26 +00:00
|
|
|
.color(color)
|
|
|
|
.line_spacing(2.0);
|
|
|
|
// Add space between messages.
|
|
|
|
let y = match text.get_y_dimension(ui) {
|
|
|
|
Dimension::Absolute(y) => y + 2.0,
|
|
|
|
_ => 0.0,
|
|
|
|
};
|
2020-06-11 05:16:42 +00:00
|
|
|
item.set(text.h(y), ui);
|
2020-06-05 22:36:31 +00:00
|
|
|
let icon_id = state.ids.chat_icons[item.i];
|
|
|
|
Image::new(icon)
|
|
|
|
.w_h(16.0, 16.0)
|
|
|
|
.top_left_with_margins_on(item.widget_id, 2.0, -16.0)
|
|
|
|
.parent(state.ids.message_box_bg)
|
|
|
|
.set(icon_id, ui);
|
2019-04-20 14:46:36 +00:00
|
|
|
} else {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Spacer at bottom of the last message so that it is not cut off.
|
|
|
|
// Needs to be larger than the space above.
|
2020-06-11 05:16:42 +00:00
|
|
|
item.set(
|
|
|
|
Text::new("")
|
|
|
|
.font_size(self.fonts.opensans.scale(6))
|
|
|
|
.font_id(self.fonts.opensans.conrod_id)
|
|
|
|
.w(CHAT_BOX_WIDTH),
|
|
|
|
ui,
|
|
|
|
);
|
2019-04-20 14:46:36 +00:00
|
|
|
};
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
2019-03-30 02:59:31 +00:00
|
|
|
|
2019-04-04 14:45:57 +00:00
|
|
|
// Chat Arrow
|
2019-05-17 09:22:32 +00:00
|
|
|
// Check if already at bottom.
|
2020-06-11 17:06:11 +00:00
|
|
|
if !Self::scrolled_to_bottom(state, ui)
|
|
|
|
&& Button::image(self.imgs.chat_arrow)
|
2019-05-02 18:06:23 +00:00
|
|
|
.w_h(20.0, 20.0)
|
2019-05-03 16:56:18 +00:00
|
|
|
.hover_image(self.imgs.chat_arrow_mo)
|
|
|
|
.press_image(self.imgs.chat_arrow_press)
|
|
|
|
.bottom_right_with_margins_on(state.ids.message_box_bg, 0.0, -22.0)
|
2020-06-05 22:36:31 +00:00
|
|
|
.parent(id)
|
2019-05-03 16:56:18 +00:00
|
|
|
.set(state.ids.chat_arrow, ui)
|
2019-04-10 20:23:49 +00:00
|
|
|
.was_clicked()
|
2020-06-11 17:06:11 +00:00
|
|
|
{
|
|
|
|
ui.scroll_widget(state.ids.message_box, [0.0, std::f64::MAX]);
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
2019-03-16 02:03:21 +00:00
|
|
|
|
2020-05-08 21:38:58 +00:00
|
|
|
// We've started a new tab completion. Populate tab completion suggestions.
|
|
|
|
if request_tab_completions {
|
|
|
|
Some(Event::TabCompletionStart(state.input.to_string()))
|
|
|
|
// If the chat widget is focused, return a focus event to pass the focus
|
|
|
|
// to the input box.
|
|
|
|
} else if keyboard_capturer == Some(id) {
|
2020-05-06 18:45:58 +00:00
|
|
|
Some(Event::Focus(state.ids.chat_input))
|
2019-05-06 12:28:57 +00:00
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
// If enter is pressed and the input box is not empty, send the current message.
|
2019-09-02 04:54:44 +00:00
|
|
|
else if ui
|
2020-05-06 18:45:58 +00:00
|
|
|
.widget_input(state.ids.chat_input)
|
2019-09-02 04:54:44 +00:00
|
|
|
.presses()
|
|
|
|
.key()
|
|
|
|
.any(|key_press| match key_press.key {
|
|
|
|
Key::Return if !state.input.is_empty() => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
{
|
2019-05-03 16:56:18 +00:00
|
|
|
let msg = state.input.clone();
|
2019-07-12 23:20:38 +00:00
|
|
|
state.update(|s| {
|
|
|
|
s.input.clear();
|
2019-07-15 17:04:48 +00:00
|
|
|
// Update the history
|
2019-09-02 05:07:16 +00:00
|
|
|
// Don't add if this is identical to the last message in the history
|
2019-07-12 23:20:38 +00:00
|
|
|
s.history_pos = 0;
|
2019-09-02 05:07:16 +00:00
|
|
|
if s.history.get(0).map_or(true, |h| h != &msg) {
|
|
|
|
s.history.push_front(msg.clone());
|
|
|
|
s.history.truncate(self.history_max);
|
|
|
|
}
|
2019-07-12 23:20:38 +00:00
|
|
|
});
|
2019-05-06 12:28:57 +00:00
|
|
|
Some(Event::SendMessage(msg))
|
|
|
|
} else {
|
|
|
|
None
|
2019-05-07 03:25:25 +00:00
|
|
|
}
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 18:45:58 +00:00
|
|
|
|
|
|
|
fn do_tab_completion(cursor: usize, input: &str, word: &str) -> (String, usize) {
|
|
|
|
let mut pre_ws = None;
|
|
|
|
let mut post_ws = None;
|
|
|
|
for (char_i, (byte_i, c)) in input.char_indices().enumerate() {
|
|
|
|
if c.is_whitespace() && c != '\t' {
|
|
|
|
if char_i < cursor {
|
|
|
|
pre_ws = Some(byte_i);
|
|
|
|
} else {
|
|
|
|
post_ws = Some(byte_i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match (pre_ws, post_ws) {
|
|
|
|
(None, None) => (word.to_string(), word.chars().count()),
|
|
|
|
(None, Some(i)) => (
|
|
|
|
format!("{}{}", word, input.split_at(i).1),
|
|
|
|
word.chars().count(),
|
|
|
|
),
|
|
|
|
(Some(i), None) => {
|
|
|
|
let l_split = input.split_at(i).0;
|
|
|
|
let completed = format!("{} {}", l_split, word);
|
|
|
|
(
|
|
|
|
completed,
|
|
|
|
l_split.chars().count() + 1 + word.chars().count(),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
(Some(i), Some(j)) => {
|
|
|
|
let l_split = input.split_at(i).0;
|
|
|
|
let r_split = input.split_at(j).1;
|
|
|
|
let completed = format!("{} {}{}", l_split, word, r_split);
|
|
|
|
(
|
|
|
|
completed,
|
|
|
|
l_split.chars().count() + 1 + word.chars().count(),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cursor_offset_to_index(
|
|
|
|
offset: usize,
|
|
|
|
text: &str,
|
|
|
|
ui: &Ui,
|
|
|
|
fonts: &ConrodVoxygenFonts,
|
|
|
|
) -> Option<Index> {
|
|
|
|
// This moves the cursor to the given offset. Conrod is a pain.
|
2020-06-10 04:21:56 +00:00
|
|
|
//
|
|
|
|
// Width and font must match that of the chat TextEdit
|
2020-05-06 18:45:58 +00:00
|
|
|
let font = ui.fonts.get(fonts.opensans.conrod_id)?;
|
|
|
|
let font_size = fonts.opensans.scale(15);
|
2020-06-10 04:21:56 +00:00
|
|
|
let infos = text::line::infos(&text, &font, font_size).wrap_by_whitespace(CHAT_BOX_INPUT_WIDTH);
|
2020-05-06 18:45:58 +00:00
|
|
|
|
|
|
|
cursor::index_before_char(infos, offset)
|
|
|
|
}
|
2020-06-05 22:36:31 +00:00
|
|
|
|
2020-06-10 04:21:56 +00:00
|
|
|
/// Get the color and icon for the current line in the chat box
|
|
|
|
fn render_chat_line(chat_type: &ChatType, imgs: &Imgs) -> (Color, conrod_core::image::Id) {
|
2020-06-05 22:36:31 +00:00
|
|
|
match chat_type {
|
2020-06-12 07:43:20 +00:00
|
|
|
ChatType::Online => (ONLINE_COLOR, imgs.chat_online_small),
|
|
|
|
ChatType::Offline => (OFFLINE_COLOR, imgs.chat_offline_small),
|
|
|
|
ChatType::CommandError => (ERROR_COLOR, imgs.chat_command_error_small),
|
|
|
|
ChatType::CommandInfo => (INFO_COLOR, imgs.chat_command_info_small),
|
2020-07-01 19:05:44 +00:00
|
|
|
ChatType::Loot => (LOOT_COLOR, imgs.chat_loot_small),
|
2020-06-12 17:44:29 +00:00
|
|
|
ChatType::GroupMeta(_) => (GROUP_COLOR, imgs.chat_group_small),
|
|
|
|
ChatType::FactionMeta(_) => (FACTION_COLOR, imgs.chat_faction_small),
|
2020-06-10 04:21:56 +00:00
|
|
|
ChatType::Kill => (KILL_COLOR, imgs.chat_kill_small),
|
|
|
|
ChatType::Tell(_from, _to) => (TELL_COLOR, imgs.chat_tell_small),
|
|
|
|
ChatType::Say(_uid) => (SAY_COLOR, imgs.chat_say_small),
|
|
|
|
ChatType::Group(_uid, _s) => (GROUP_COLOR, imgs.chat_group_small),
|
|
|
|
ChatType::Faction(_uid, _s) => (FACTION_COLOR, imgs.chat_faction_small),
|
|
|
|
ChatType::Region(_uid) => (REGION_COLOR, imgs.chat_region_small),
|
|
|
|
ChatType::World(_uid) => (WORLD_COLOR, imgs.chat_world_small),
|
2020-06-05 22:36:31 +00:00
|
|
|
ChatType::Npc(_uid, _r) => panic!("NPCs can't talk"), // Should be filtered by hud/mod.rs
|
2020-06-28 17:10:01 +00:00
|
|
|
ChatType::Meta => (INFO_COLOR, imgs.chat_command_info_small),
|
2020-06-05 22:36:31 +00:00
|
|
|
}
|
|
|
|
}
|