From ca454d0c8b2bd7686e9a665a4f59756e9b769439 Mon Sep 17 00:00:00 2001 From: Imbris Date: Fri, 29 Mar 2019 22:59:31 -0400 Subject: [PATCH] make chat arrow only appear when scrolled up and enable it Former-commit-id: 72bf830acea7fa273d90b996ce7bcf88f215d1bf --- assets/voxygen | 2 +- voxygen/src/hud/chat.rs | 50 ++++++++++++++++++++++++++++++++--------- voxygen/src/hud/mod.rs | 29 ++++++------------------ 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/assets/voxygen b/assets/voxygen index 495b381034..e25c963fe1 160000 --- a/assets/voxygen +++ b/assets/voxygen @@ -1 +1 @@ -Subproject commit 495b381034bf0e788ef1c35adf034f2090aba481 +Subproject commit e25c963fe1726e7bdfd81633ed9b7004bc36dcf8 diff --git a/voxygen/src/hud/chat.rs b/voxygen/src/hud/chat.rs index eb198ad7ee..f007a09d65 100644 --- a/voxygen/src/hud/chat.rs +++ b/voxygen/src/hud/chat.rs @@ -3,7 +3,7 @@ use conrod_core::{ input::Key, position::Dimension, 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, }; use std::collections::VecDeque; @@ -14,6 +14,7 @@ widget_ids! { message_box_bg, input, input_bg, + chat_arrow, } } // Chat Behaviour: @@ -42,28 +43,43 @@ impl Chat { self.ids.input } pub fn new_message(&mut self, msg: String) { - self.messages.push_front(msg); + self.messages.push_back(msg); self.new_messages = true; } // Determine if the message box is scrolled to the bottom // (i.e. the player is viewing new messages) // 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 .widget_graph() .widget(self.ids.message_box) .and_then(|widget| widget.maybe_y_scroll_state) { // If previously scrolled to the bottom stay there - if scroll.offset >= scroll.offset_bounds.start { - ui_widgets.scroll_widget(self.ids.message_box, [0.0, std::f64::MAX]); + if self.scrolled_to_bottom(ui_widgets) { + self.scroll_to_bottom(ui_widgets); } } } - pub fn update_layout(&mut self, ui_widgets: &mut UiCell, font: FontId) -> Option { + 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]); + } + pub fn update_layout(&mut self, ui_widgets: &mut UiCell, font: FontId, imgs: &super::Imgs) -> Option { // Maintain scrolling if self.new_messages { - //self.scroll_new_messages(ui_widgets); + self.scroll_new_messages(ui_widgets); self.new_messages = false; } @@ -96,7 +112,7 @@ impl Chat { .rgba(0.0, 0.0, 0.0, 0.4) .up_from(self.ids.input, 0.0) .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) .scrollbar_next_to() .scrollbar_thickness(18.0) @@ -111,8 +127,22 @@ impl Chat { ui_widgets, ) } - if let Some(s) = scrollbar { - s.set(ui_widgets) + //if let Some(s) = scrollbar { + // 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 diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index fd9643e325..4055fb967f 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -106,9 +106,6 @@ widget_ids! { questlog_icon, questlog_close, questlog_title, - - // Chat-Arrow - chat_arrow, } } @@ -198,12 +195,9 @@ struct Imgs { //help //help: ImgId, // Chat-Arrow - chat_arrow_active: ImgId, - chat_arrow_inactive: ImgId, - chat_arrow_active_mo: ImgId, - chat_arrow_active_press: ImgId, - - + chat_arrow: ImgId, + chat_arrow_mo: ImgId, + chat_arrow_press: ImgId, } impl Imgs { fn new(ui: &mut Ui, renderer: &mut Renderer) -> Imgs { @@ -303,10 +297,9 @@ impl Imgs { questlog_icon: load("element/icons/questlog.png"), // Chat-Arrows - chat_arrow_active: load("element/buttons/arrow/chat_arrow_active.png"), - chat_arrow_inactive: load("element/buttons/arrow/chat_arrow_inactive.png"), - chat_arrow_active_mo: load("element/buttons/arrow/chat_arrow_active_mo.png"), - chat_arrow_active_press: load("element/buttons/arrow/chat_arrow_active_press.png"), + chat_arrow: load("element/buttons/arrow/chat_arrow.png"), + chat_arrow_mo: load("element/buttons/arrow/chat_arrow_mo.png"), + chat_arrow_press: load("element/buttons/arrow/chat_arrow_press.png"), } } } @@ -408,17 +401,9 @@ impl Hud { let ref mut ui_widgets = self.ui.set_widgets(); // 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)); } - // 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 if self.show_help { Image::new(self.imgs.window_frame_2)