2019-07-21 18:34:52 +00:00
|
|
|
use super::{
|
2019-07-29 14:40:46 +00:00
|
|
|
img_ids::Imgs, Fonts, BROADCAST_COLOR, FACTION_COLOR, GAME_UPDATE_COLOR, GROUP_COLOR,
|
|
|
|
KILL_COLOR, META_COLOR, PRIVATE_COLOR, SAY_COLOR, TELL_COLOR, TEXT_COLOR,
|
2019-07-21 18:34:52 +00:00
|
|
|
};
|
2019-07-17 22:10:42 +00:00
|
|
|
use client::Event as ClientEvent;
|
|
|
|
use common::ChatType;
|
2019-03-15 04:55:52 +00:00
|
|
|
use conrod_core::{
|
|
|
|
input::Key,
|
|
|
|
position::Dimension,
|
2019-07-05 16:21:11 +00:00
|
|
|
text::cursor::Index,
|
2019-05-07 05:40:03 +00:00
|
|
|
widget::{self, Button, Id, List, Rectangle, Text, TextEdit},
|
|
|
|
widget_ids, Colorable, Positionable, Sizeable, UiCell, Widget, WidgetCommon,
|
2019-03-15 04:55:52 +00:00
|
|
|
};
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
widget_ids! {
|
|
|
|
struct Ids {
|
|
|
|
message_box,
|
|
|
|
message_box_bg,
|
|
|
|
input,
|
|
|
|
input_bg,
|
2019-03-30 02:59:31 +00:00
|
|
|
chat_arrow,
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-03 16:56:18 +00:00
|
|
|
|
2019-05-25 19:16:38 +00:00
|
|
|
const MAX_MESSAGES: usize = 100;
|
|
|
|
|
2019-05-03 16:56:18 +00:00
|
|
|
#[derive(WidgetCommon)]
|
|
|
|
pub struct Chat<'a> {
|
2019-07-17 22:10:42 +00:00
|
|
|
new_messages: &'a mut VecDeque<ClientEvent>,
|
2019-07-05 16:21:11 +00:00
|
|
|
force_input: Option<String>,
|
|
|
|
force_cursor: Option<Index>,
|
2019-05-03 16:56:18 +00:00
|
|
|
|
|
|
|
imgs: &'a Imgs,
|
|
|
|
fonts: &'a Fonts,
|
|
|
|
|
|
|
|
#[conrod(common_builder)]
|
|
|
|
common: widget::CommonBuilder,
|
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(
|
|
|
|
new_messages: &'a mut VecDeque<ClientEvent>,
|
|
|
|
imgs: &'a Imgs,
|
|
|
|
fonts: &'a Fonts,
|
|
|
|
) -> Self {
|
2019-05-03 16:56:18 +00:00
|
|
|
Self {
|
|
|
|
new_messages,
|
2019-07-05 16:21:11 +00:00
|
|
|
force_input: None,
|
|
|
|
force_cursor: None,
|
2019-05-03 16:56:18 +00:00
|
|
|
imgs,
|
|
|
|
fonts,
|
|
|
|
common: widget::CommonBuilder::default(),
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-03 16:56:18 +00:00
|
|
|
|
2019-07-05 16:21:11 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-03 16:56:18 +00:00
|
|
|
fn scrolled_to_bottom(state: &State, ui: &UiCell) -> bool {
|
2019-05-17 09:22:32 +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 {
|
2019-07-17 22:10:42 +00:00
|
|
|
messages: VecDeque<ClientEvent>,
|
2019-05-03 16:56:18 +00:00
|
|
|
input: String,
|
2019-07-12 23:20:38 +00:00
|
|
|
history: VecDeque<String>,
|
|
|
|
history_pos: usize,
|
|
|
|
history_max: usize,
|
2019-05-03 16:56:18 +00:00
|
|
|
ids: Ids,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Event {
|
|
|
|
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> {
|
|
|
|
type State = State;
|
|
|
|
type Style = ();
|
2019-05-07 03:25:25 +00:00
|
|
|
type Event = Option<Event>;
|
2019-05-03 16:56:18 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
history_max: 32,
|
2019-05-03 16:56:18 +00:00
|
|
|
ids: Ids::new(id_gen),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn style(&self) -> Self::Style {
|
|
|
|
()
|
2019-03-30 02:59:31 +00:00
|
|
|
}
|
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-05-03 16:56:18 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Maintain scrolling.
|
2019-05-03 16:56:18 +00:00
|
|
|
if !self.new_messages.is_empty() {
|
|
|
|
state.update(|s| s.messages.extend(self.new_messages.drain(..)));
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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 {
|
|
|
|
state.update(|s| s.input = input.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
let input_focused =
|
|
|
|
keyboard_capturer == Some(state.ids.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 {
|
2019-07-05 16:21:11 +00:00
|
|
|
let input = self.force_input.as_ref().unwrap_or(&state.input);
|
|
|
|
let mut text_edit = TextEdit::new(input)
|
2019-04-20 15:43:38 +00:00
|
|
|
.w(460.0)
|
|
|
|
.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)
|
|
|
|
.font_size(15)
|
2019-05-03 16:56:18 +00:00
|
|
|
.font_id(self.fonts.opensans);
|
2019-07-05 16:21:11 +00:00
|
|
|
|
|
|
|
if let Some(pos) = self.force_cursor {
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
Rectangle::fill([470.0, y])
|
|
|
|
.rgba(0.0, 0.0, 0.0, 0.8)
|
2019-05-03 16:56:18 +00:00
|
|
|
.bottom_left_with_margins_on(ui.window, 10.0, 10.0)
|
2019-04-20 15:43:38 +00:00
|
|
|
.w(470.0)
|
2019-05-03 16:56:18 +00:00
|
|
|
.set(state.ids.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
|
2019-05-03 16:56:18 +00:00
|
|
|
.top_left_with_margins_on(state.ids.input_bg, 1.0, 1.0)
|
|
|
|
.set(state.ids.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');
|
|
|
|
state.update(|s| s.input = input);
|
2019-04-20 15:43:38 +00:00
|
|
|
}
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Message box
|
2019-04-20 14:46:36 +00:00
|
|
|
Rectangle::fill([470.0, 174.0])
|
2019-03-28 02:25:08 +00:00
|
|
|
.rgba(0.0, 0.0, 0.0, 0.4)
|
2019-05-07 05:40:03 +00:00
|
|
|
.and(|r| {
|
|
|
|
if input_focused {
|
|
|
|
r.up_from(state.ids.input_bg, 0.0)
|
|
|
|
} else {
|
|
|
|
r.bottom_left_with_margins_on(ui.window, 10.0, 10.0)
|
|
|
|
}
|
2019-04-20 15:43:38 +00:00
|
|
|
})
|
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)
|
|
|
|
.top_left_of(state.ids.message_box_bg)
|
2019-04-20 14:46:36 +00:00
|
|
|
.w_h(470.0, 174.0)
|
|
|
|
.scroll_kids_vertically()
|
2019-05-03 16:56:18 +00:00
|
|
|
.set(state.ids.message_box, ui);
|
|
|
|
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.
|
2019-05-03 16:56:18 +00:00
|
|
|
let widget = if item.i < state.messages.len() {
|
2019-07-17 22:10:42 +00:00
|
|
|
let msg = &state.messages[item.i];
|
|
|
|
match msg {
|
|
|
|
ClientEvent::Chat { chat_type, message } => {
|
|
|
|
let color = match chat_type {
|
2019-07-29 14:40:46 +00:00
|
|
|
ChatType::Meta => META_COLOR,
|
2019-07-17 22:10:42 +00:00
|
|
|
ChatType::Tell => TELL_COLOR,
|
2019-07-21 18:34:52 +00:00
|
|
|
ChatType::Chat => TEXT_COLOR,
|
|
|
|
ChatType::Private => PRIVATE_COLOR,
|
|
|
|
ChatType::Broadcast => BROADCAST_COLOR,
|
|
|
|
ChatType::GameUpdate => GAME_UPDATE_COLOR,
|
2019-07-29 14:40:46 +00:00
|
|
|
ChatType::Say => SAY_COLOR,
|
|
|
|
ChatType::Group => GROUP_COLOR,
|
|
|
|
ChatType::Faction => FACTION_COLOR,
|
|
|
|
ChatType::Kill => KILL_COLOR,
|
2019-07-17 22:10:42 +00:00
|
|
|
};
|
|
|
|
let text = Text::new(&message)
|
|
|
|
.font_size(15)
|
|
|
|
.font_id(self.fonts.opensans)
|
|
|
|
.w(470.0)
|
|
|
|
.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,
|
|
|
|
};
|
|
|
|
Some(text.h(y))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
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.
|
2019-07-17 22:10:42 +00:00
|
|
|
Some(
|
|
|
|
Text::new("")
|
|
|
|
.font_size(6)
|
|
|
|
.font_id(self.fonts.opensans)
|
|
|
|
.w(470.0),
|
|
|
|
)
|
2019-04-20 14:46:36 +00:00
|
|
|
};
|
2019-07-17 22:10:42 +00:00
|
|
|
match widget {
|
|
|
|
Some(widget) => {
|
|
|
|
item.set(widget, ui);
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
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.
|
2019-05-03 16:56:18 +00:00
|
|
|
if !Self::scrolled_to_bottom(state, ui) {
|
|
|
|
if 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)
|
|
|
|
.set(state.ids.chat_arrow, ui)
|
2019-04-10 20:23:49 +00:00
|
|
|
.was_clicked()
|
2019-03-30 02:59:31 +00:00
|
|
|
{
|
2019-05-03 16:56:18 +00:00
|
|
|
ui.scroll_widget(state.ids.message_box, [0.0, std::f64::MAX]);
|
2019-03-30 02:59:31 +00:00
|
|
|
}
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
2019-03-16 02:03:21 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// If the chat widget is focused, return a focus event to pass the focus to the input box.
|
2019-05-07 03:25:25 +00:00
|
|
|
if keyboard_capturer == Some(id) {
|
2019-05-06 12:28:57 +00:00
|
|
|
Some(Event::Focus(state.ids.input))
|
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
// If enter is pressed and the input box is not empty, send the current message.
|
2019-05-07 05:40:03 +00:00
|
|
|
else if ui
|
2019-05-03 16:56:18 +00:00
|
|
|
.widget_input(state.ids.input)
|
2019-03-22 03:55:52 +00:00
|
|
|
.presses()
|
|
|
|
.key()
|
|
|
|
.any(|key_press| match key_press.key {
|
2019-05-03 16:56:18 +00:00
|
|
|
Key::Return if !state.input.is_empty() => true,
|
2019-03-22 03:55:52 +00:00
|
|
|
_ => 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();
|
|
|
|
// update the history
|
|
|
|
s.history.push_front(msg.clone());
|
|
|
|
s.history_pos = 0;
|
|
|
|
while s.history.len() > s.history_max {
|
|
|
|
s.history.pop_back();
|
|
|
|
}
|
|
|
|
});
|
2019-05-06 12:28:57 +00:00
|
|
|
Some(Event::SendMessage(msg))
|
2019-07-12 23:20:38 +00:00
|
|
|
}
|
|
|
|
// If up is pressed, use history
|
|
|
|
else if ui
|
|
|
|
.widget_input(state.ids.input)
|
|
|
|
.presses()
|
|
|
|
.key()
|
|
|
|
.any(|key_press| match key_press.key {
|
|
|
|
Key::Up => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
{
|
|
|
|
state.update(|s| {
|
|
|
|
if !s.history.is_empty() {
|
|
|
|
if s.history_pos < s.history.len() {
|
|
|
|
s.history_pos += 1;
|
|
|
|
if let Some(string) = s.history.get(s.history_pos - 1) {
|
|
|
|
s.input.clear();
|
|
|
|
s.input.push_str(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
None
|
|
|
|
}
|
|
|
|
// If down is pressed, use history
|
|
|
|
else if ui
|
|
|
|
.widget_input(state.ids.input)
|
|
|
|
.presses()
|
|
|
|
.key()
|
|
|
|
.any(|key_press| match key_press.key {
|
|
|
|
Key::Down => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
{
|
|
|
|
state.update(|s| {
|
|
|
|
if !s.history.is_empty() {
|
|
|
|
if s.history_pos > 1 {
|
|
|
|
s.history_pos -= 1;
|
|
|
|
s.input.clear();
|
|
|
|
if let Some(string) = s.history.get(s.history_pos - 1) {
|
|
|
|
s.input.push_str(string);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.history_pos = 0;
|
|
|
|
s.input.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
None
|
2019-05-06 12:28:57 +00:00
|
|
|
} else {
|
|
|
|
None
|
2019-05-07 03:25:25 +00:00
|
|
|
}
|
2019-03-15 04:55:52 +00:00
|
|
|
}
|
|
|
|
}
|