make chat arrow only appear when scrolled up and enable it

Former-commit-id: 72bf830acea7fa273d90b996ce7bcf88f215d1bf
This commit is contained in:
Imbris 2019-03-29 22:59:31 -04:00
parent 820967838d
commit ca454d0c8b
3 changed files with 48 additions and 33 deletions

@ -1 +1 @@
Subproject commit 495b381034bf0e788ef1c35adf034f2090aba481 Subproject commit e25c963fe1726e7bdfd81633ed9b7004bc36dcf8

View File

@ -3,7 +3,7 @@ use conrod_core::{
input::Key, input::Key,
position::Dimension, position::Dimension,
text::font::Id as FontId, text::font::Id as FontId,
widget::{Id, List, Rectangle, Text, TextEdit}, widget::{Id, Button, List, Rectangle, Text, TextEdit},
widget_ids, Color, Colorable, Positionable, Sizeable, UiCell, Widget, widget_ids, Color, Colorable, Positionable, Sizeable, UiCell, Widget,
}; };
use std::collections::VecDeque; use std::collections::VecDeque;
@ -14,6 +14,7 @@ widget_ids! {
message_box_bg, message_box_bg,
input, input,
input_bg, input_bg,
chat_arrow,
} }
} }
// Chat Behaviour: // Chat Behaviour:
@ -42,28 +43,43 @@ impl Chat {
self.ids.input self.ids.input
} }
pub fn new_message(&mut self, msg: String) { pub fn new_message(&mut self, msg: String) {
self.messages.push_front(msg); self.messages.push_back(msg);
self.new_messages = true; self.new_messages = true;
} }
// Determine if the message box is scrolled to the bottom // Determine if the message box is scrolled to the bottom
// (i.e. the player is viewing new messages) // (i.e. the player is viewing new messages)
// If so scroll down when new messages are added // If so scroll down when new messages are added
fn scroll_new_messages(&mut self, ui_widgets: &mut UiCell) { fn scroll_new_messages(&self, ui_widgets: &mut UiCell) {
if let Some(scroll) = ui_widgets if let Some(scroll) = ui_widgets
.widget_graph() .widget_graph()
.widget(self.ids.message_box) .widget(self.ids.message_box)
.and_then(|widget| widget.maybe_y_scroll_state) .and_then(|widget| widget.maybe_y_scroll_state)
{ {
// If previously scrolled to the bottom stay there // If previously scrolled to the bottom stay there
if scroll.offset >= scroll.offset_bounds.start { if self.scrolled_to_bottom(ui_widgets) {
self.scroll_to_bottom(ui_widgets);
}
}
}
fn scrolled_to_bottom(&self, ui_widgets: &UiCell) -> bool {
// could be more efficient to cache result and update it when a scroll event has occurred instead of every frame
if let Some(scroll) = ui_widgets
.widget_graph()
.widget(self.ids.message_box)
.and_then(|widget| widget.maybe_y_scroll_state)
{
scroll.offset >= scroll.offset_bounds.start
} else {
false
}
}
fn scroll_to_bottom(&self, ui_widgets: &mut UiCell) {
ui_widgets.scroll_widget(self.ids.message_box, [0.0, std::f64::MAX]); ui_widgets.scroll_widget(self.ids.message_box, [0.0, std::f64::MAX]);
} }
} pub fn update_layout(&mut self, ui_widgets: &mut UiCell, font: FontId, imgs: &super::Imgs) -> Option<String> {
}
pub fn update_layout(&mut self, ui_widgets: &mut UiCell, font: FontId) -> Option<String> {
// Maintain scrolling // Maintain scrolling
if self.new_messages { if self.new_messages {
//self.scroll_new_messages(ui_widgets); self.scroll_new_messages(ui_widgets);
self.new_messages = false; self.new_messages = false;
} }
@ -96,7 +112,7 @@ impl Chat {
.rgba(0.0, 0.0, 0.0, 0.4) .rgba(0.0, 0.0, 0.0, 0.4)
.up_from(self.ids.input, 0.0) .up_from(self.ids.input, 0.0)
.set(self.ids.message_box_bg, ui_widgets); .set(self.ids.message_box_bg, ui_widgets);
let (mut items, scrollbar) = List::flow_up(self.messages.len()) let (mut items, scrollbar) = List::flow_down(self.messages.len())
.middle_of(self.ids.message_box_bg) .middle_of(self.ids.message_box_bg)
.scrollbar_next_to() .scrollbar_next_to()
.scrollbar_thickness(18.0) .scrollbar_thickness(18.0)
@ -111,8 +127,22 @@ impl Chat {
ui_widgets, ui_widgets,
) )
} }
if let Some(s) = scrollbar { //if let Some(s) = scrollbar {
s.set(ui_widgets) // s.set(ui_widgets)
//}
// Chat Arrow
if !self.scrolled_to_bottom(ui_widgets) {
if Button::image(imgs.chat_arrow)
.w_h(22.0, 22.0)
.hover_image(imgs.chat_arrow_mo)
.press_image(imgs.chat_arrow_press)
.bottom_right_with_margins_on(self.ids.message_box_bg, 2.0, 2.0)
.set(self.ids.chat_arrow, ui_widgets)
.was_clicked()
{
self.scroll_to_bottom(ui_widgets);
}
} }
// If enter is pressed send the current message // If enter is pressed send the current message

View File

@ -106,9 +106,6 @@ widget_ids! {
questlog_icon, questlog_icon,
questlog_close, questlog_close,
questlog_title, questlog_title,
// Chat-Arrow
chat_arrow,
} }
} }
@ -198,12 +195,9 @@ struct Imgs {
//help //help
//help: ImgId, //help: ImgId,
// Chat-Arrow // Chat-Arrow
chat_arrow_active: ImgId, chat_arrow: ImgId,
chat_arrow_inactive: ImgId, chat_arrow_mo: ImgId,
chat_arrow_active_mo: ImgId, chat_arrow_press: ImgId,
chat_arrow_active_press: ImgId,
} }
impl Imgs { impl Imgs {
fn new(ui: &mut Ui, renderer: &mut Renderer) -> Imgs { fn new(ui: &mut Ui, renderer: &mut Renderer) -> Imgs {
@ -303,10 +297,9 @@ impl Imgs {
questlog_icon: load("element/icons/questlog.png"), questlog_icon: load("element/icons/questlog.png"),
// Chat-Arrows // Chat-Arrows
chat_arrow_active: load("element/buttons/arrow/chat_arrow_active.png"), chat_arrow: load("element/buttons/arrow/chat_arrow.png"),
chat_arrow_inactive: load("element/buttons/arrow/chat_arrow_inactive.png"), chat_arrow_mo: load("element/buttons/arrow/chat_arrow_mo.png"),
chat_arrow_active_mo: load("element/buttons/arrow/chat_arrow_active_mo.png"), chat_arrow_press: load("element/buttons/arrow/chat_arrow_press.png"),
chat_arrow_active_press: load("element/buttons/arrow/chat_arrow_active_press.png"),
} }
} }
} }
@ -408,17 +401,9 @@ impl Hud {
let ref mut ui_widgets = self.ui.set_widgets(); let ref mut ui_widgets = self.ui.set_widgets();
// Chat box // Chat box
if let Some(msg) = self.chat.update_layout(ui_widgets, self.font_opensans) { if let Some(msg) = self.chat.update_layout(ui_widgets, self.font_opensans, &self.imgs) {
events.push(Event::SendMessage(msg)); events.push(Event::SendMessage(msg));
} }
// Chat Arrow
Button::image(self.imgs.chat_arrow_active)
.w_h(22.0, 22.0)
.hover_image(self.imgs.chat_arrow_active_mo)
.press_image(self.imgs.chat_arrow_active_press)
.bottom_left_with_margins_on(ui_widgets.window, 26.0, 14.0)
.set(self.ids.chat_arrow, ui_widgets);
// Help Text // Help Text
if self.show_help { if self.show_help {
Image::new(self.imgs.window_frame_2) Image::new(self.imgs.window_frame_2)