2020-03-20 19:52:32 +00:00
|
|
|
use super::{img_ids::Imgs, Show, TEXT_COLOR, TEXT_COLOR_3, UI_MAIN};
|
2019-08-07 13:14:26 +00:00
|
|
|
|
2020-01-26 19:29:46 +00:00
|
|
|
use crate::{i18n::VoxygenLocalization, ui::fonts::ConrodVoxygenFonts};
|
2020-01-17 23:43:18 +00:00
|
|
|
use client::{self, Client};
|
2020-07-19 21:49:18 +00:00
|
|
|
use common::sync::Uid;
|
2019-08-07 13:14:26 +00:00
|
|
|
use conrod_core::{
|
|
|
|
color,
|
|
|
|
widget::{self, Button, Image, Rectangle, Scrollbar, Text},
|
2020-02-01 20:39:39 +00:00
|
|
|
widget_ids, Colorable, Labelable, Positionable, Sizeable, Widget, WidgetCommon,
|
2019-08-07 13:14:26 +00:00
|
|
|
};
|
2020-07-12 00:39:50 +00:00
|
|
|
use std::time::Instant;
|
2019-08-07 13:14:26 +00:00
|
|
|
|
|
|
|
widget_ids! {
|
|
|
|
pub struct Ids {
|
|
|
|
social_frame,
|
|
|
|
social_close,
|
|
|
|
social_title,
|
|
|
|
frame,
|
|
|
|
align,
|
|
|
|
content_align,
|
|
|
|
online_tab,
|
|
|
|
friends_tab,
|
|
|
|
faction_tab,
|
|
|
|
online_title,
|
|
|
|
online_no,
|
|
|
|
scrollbar,
|
|
|
|
friends_test,
|
|
|
|
faction_test,
|
|
|
|
player_names[],
|
2020-07-12 00:39:50 +00:00
|
|
|
invite_button,
|
2019-08-07 13:14:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 00:39:50 +00:00
|
|
|
pub struct State {
|
|
|
|
ids: Ids,
|
|
|
|
// Holds the time when selection is made since this selection can be overriden
|
|
|
|
// by selecting an entity in-game
|
|
|
|
selected_uid: Option<(Uid, Instant)>,
|
|
|
|
}
|
|
|
|
|
2019-08-07 13:14:26 +00:00
|
|
|
pub enum SocialTab {
|
|
|
|
Online,
|
|
|
|
Friends,
|
|
|
|
Faction,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(WidgetCommon)]
|
|
|
|
pub struct Social<'a> {
|
|
|
|
show: &'a Show,
|
|
|
|
client: &'a Client,
|
|
|
|
imgs: &'a Imgs,
|
2020-01-26 19:29:46 +00:00
|
|
|
fonts: &'a ConrodVoxygenFonts,
|
2020-01-17 23:43:18 +00:00
|
|
|
localized_strings: &'a std::sync::Arc<VoxygenLocalization>,
|
|
|
|
|
2020-07-12 00:39:50 +00:00
|
|
|
selected_entity: Option<(specs::Entity, Instant)>,
|
|
|
|
|
2019-08-07 13:14:26 +00:00
|
|
|
#[conrod(common_builder)]
|
|
|
|
common: widget::CommonBuilder,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Social<'a> {
|
2020-01-17 23:43:18 +00:00
|
|
|
pub fn new(
|
|
|
|
show: &'a Show,
|
|
|
|
client: &'a Client,
|
|
|
|
imgs: &'a Imgs,
|
2020-01-26 19:29:46 +00:00
|
|
|
fonts: &'a ConrodVoxygenFonts,
|
2020-01-17 23:43:18 +00:00
|
|
|
localized_strings: &'a std::sync::Arc<VoxygenLocalization>,
|
2020-07-12 00:39:50 +00:00
|
|
|
selected_entity: Option<(specs::Entity, Instant)>,
|
2020-01-17 23:43:18 +00:00
|
|
|
) -> Self {
|
2019-08-07 13:14:26 +00:00
|
|
|
Self {
|
2020-01-17 23:43:18 +00:00
|
|
|
show,
|
|
|
|
client,
|
2019-08-07 13:14:26 +00:00
|
|
|
imgs,
|
2020-01-17 23:43:18 +00:00
|
|
|
fonts,
|
|
|
|
localized_strings,
|
2020-07-12 00:39:50 +00:00
|
|
|
selected_entity,
|
2019-08-07 13:14:26 +00:00
|
|
|
common: widget::CommonBuilder::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Event {
|
|
|
|
Close,
|
2020-07-12 00:39:50 +00:00
|
|
|
Invite(Uid),
|
2020-07-19 21:49:18 +00:00
|
|
|
ChangeSocialTab(SocialTab),
|
2019-08-07 13:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for Social<'a> {
|
2020-02-01 20:39:39 +00:00
|
|
|
type Event = Vec<Event>;
|
2020-07-12 00:39:50 +00:00
|
|
|
type State = State;
|
2019-08-07 13:14:26 +00:00
|
|
|
type Style = ();
|
|
|
|
|
2020-07-12 00:39:50 +00:00
|
|
|
fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
|
|
|
|
Self::State {
|
|
|
|
ids: Ids::new(id_gen),
|
|
|
|
selected_uid: None,
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:14:26 +00:00
|
|
|
|
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-08-07 13:14:26 +00:00
|
|
|
|
|
|
|
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
|
2020-07-12 00:39:50 +00:00
|
|
|
let widget::UpdateArgs { state, ui, .. } = args;
|
2019-08-07 13:14:26 +00:00
|
|
|
|
|
|
|
let mut events = Vec::new();
|
|
|
|
|
2020-03-16 18:56:15 +00:00
|
|
|
Image::new(self.imgs.window_3)
|
|
|
|
.top_left_with_margins_on(ui.window, 200.0, 25.0)
|
2020-03-20 19:52:32 +00:00
|
|
|
.color(Some(UI_MAIN))
|
2020-03-16 18:56:15 +00:00
|
|
|
.w_h(103.0 * 4.0, 122.0 * 4.0)
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.social_frame, ui);
|
2019-08-07 13:14:26 +00:00
|
|
|
|
|
|
|
// X-Button
|
|
|
|
if Button::image(self.imgs.close_button)
|
|
|
|
.w_h(28.0, 28.0)
|
|
|
|
.hover_image(self.imgs.close_button_hover)
|
|
|
|
.press_image(self.imgs.close_button_press)
|
2020-07-12 00:39:50 +00:00
|
|
|
.top_right_with_margins_on(state.ids.social_frame, 0.0, 0.0)
|
|
|
|
.set(state.ids.social_close, ui)
|
2019-08-07 13:14:26 +00:00
|
|
|
.was_clicked()
|
|
|
|
{
|
|
|
|
events.push(Event::Close);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Title
|
2020-01-17 23:43:18 +00:00
|
|
|
Text::new(&self.localized_strings.get("hud.social"))
|
2020-07-12 00:39:50 +00:00
|
|
|
.mid_top_with_margin_on(state.ids.social_frame, 6.0)
|
2020-01-26 19:29:46 +00:00
|
|
|
.font_id(self.fonts.cyri.conrod_id)
|
|
|
|
.font_size(self.fonts.cyri.scale(14))
|
2019-08-07 13:14:26 +00:00
|
|
|
.color(TEXT_COLOR)
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.social_title, ui);
|
2019-08-07 13:14:26 +00:00
|
|
|
|
|
|
|
// Alignment
|
|
|
|
Rectangle::fill_with([99.0 * 4.0, 112.0 * 4.0], color::TRANSPARENT)
|
2020-07-12 00:39:50 +00:00
|
|
|
.mid_top_with_margin_on(state.ids.social_frame, 8.0 * 4.0)
|
|
|
|
.set(state.ids.align, ui);
|
2019-08-07 13:14:26 +00:00
|
|
|
// Content Alignment
|
|
|
|
Rectangle::fill_with([94.0 * 4.0, 94.0 * 4.0], color::TRANSPARENT)
|
2020-07-12 00:39:50 +00:00
|
|
|
.middle_of(state.ids.frame)
|
2019-08-07 13:14:26 +00:00
|
|
|
.scroll_kids()
|
|
|
|
.scroll_kids_vertically()
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.content_align, ui);
|
|
|
|
Scrollbar::y_axis(state.ids.content_align)
|
2019-08-07 13:14:26 +00:00
|
|
|
.thickness(5.0)
|
|
|
|
.rgba(0.33, 0.33, 0.33, 1.0)
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.scrollbar, ui);
|
2019-08-07 13:14:26 +00:00
|
|
|
// Frame
|
|
|
|
Image::new(self.imgs.social_frame)
|
|
|
|
.w_h(99.0 * 4.0, 100.0 * 4.0)
|
2020-07-12 00:39:50 +00:00
|
|
|
.mid_bottom_of(state.ids.align)
|
2020-03-20 19:52:32 +00:00
|
|
|
.color(Some(UI_MAIN))
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.frame, ui);
|
2019-08-07 13:14:26 +00:00
|
|
|
|
|
|
|
// Online Tab
|
|
|
|
|
|
|
|
if Button::image(if let SocialTab::Online = self.show.social_tab {
|
|
|
|
self.imgs.social_button_pressed
|
|
|
|
} else {
|
|
|
|
self.imgs.social_button
|
|
|
|
})
|
|
|
|
.w_h(30.0 * 4.0, 12.0 * 4.0)
|
|
|
|
.hover_image(if let SocialTab::Online = self.show.social_tab {
|
|
|
|
self.imgs.social_button_pressed
|
|
|
|
} else {
|
|
|
|
self.imgs.social_button_hover
|
|
|
|
})
|
|
|
|
.press_image(if let SocialTab::Online = self.show.social_tab {
|
|
|
|
self.imgs.social_button_pressed
|
|
|
|
} else {
|
|
|
|
self.imgs.social_button_press
|
|
|
|
})
|
2020-07-12 00:39:50 +00:00
|
|
|
.top_left_with_margins_on(state.ids.align, 4.0, 0.0)
|
2020-01-17 23:43:18 +00:00
|
|
|
.label(&self.localized_strings.get("hud.social.online"))
|
2020-01-26 19:29:46 +00:00
|
|
|
.label_font_size(self.fonts.cyri.scale(14))
|
|
|
|
.label_font_id(self.fonts.cyri.conrod_id)
|
2020-07-12 00:39:50 +00:00
|
|
|
.parent(state.ids.frame)
|
2020-03-20 19:52:32 +00:00
|
|
|
.color(UI_MAIN)
|
2019-08-07 13:14:26 +00:00
|
|
|
.label_color(TEXT_COLOR)
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.online_tab, ui)
|
2019-08-07 13:14:26 +00:00
|
|
|
.was_clicked()
|
|
|
|
{
|
|
|
|
events.push(Event::ChangeSocialTab(SocialTab::Online));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contents
|
|
|
|
|
|
|
|
if let SocialTab::Online = self.show.social_tab {
|
|
|
|
// Players list
|
2020-02-01 20:39:39 +00:00
|
|
|
// TODO: this list changes infrequently enough that it should not have to be
|
|
|
|
// recreated every frame
|
2020-07-12 00:39:50 +00:00
|
|
|
let players = self.client.player_list.iter().filter(|(_, p)| p.is_online);
|
2020-06-11 03:40:48 +00:00
|
|
|
let count = players.clone().count();
|
2020-07-12 00:39:50 +00:00
|
|
|
if state.ids.player_names.len() < count {
|
|
|
|
state.update(|s| {
|
|
|
|
s.ids
|
|
|
|
.player_names
|
2019-12-23 06:02:00 +00:00
|
|
|
.resize(count, &mut ui.widget_id_generator())
|
|
|
|
})
|
2019-08-07 13:14:26 +00:00
|
|
|
}
|
2020-01-17 23:43:18 +00:00
|
|
|
Text::new(
|
|
|
|
&self
|
|
|
|
.localized_strings
|
|
|
|
.get("hud.social.play_online_fmt")
|
|
|
|
.replace("{nb_player}", &format!("{:?}", count)),
|
|
|
|
)
|
2020-07-12 00:39:50 +00:00
|
|
|
.top_left_with_margins_on(state.ids.content_align, -2.0, 7.0)
|
2020-01-26 19:29:46 +00:00
|
|
|
.font_size(self.fonts.cyri.scale(14))
|
|
|
|
.font_id(self.fonts.cyri.conrod_id)
|
2020-01-17 23:43:18 +00:00
|
|
|
.color(TEXT_COLOR)
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.online_title, ui);
|
|
|
|
|
|
|
|
// Clear selected player if an entity was selected
|
|
|
|
if state
|
|
|
|
.selected_uid
|
|
|
|
.zip(self.selected_entity)
|
|
|
|
// Compare instants
|
|
|
|
.map_or(false, |(u, e)| u.1 < e.1)
|
|
|
|
{
|
|
|
|
state.update(|s| s.selected_uid = None);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i, (&uid, player_info)) in players.enumerate() {
|
|
|
|
let selected = state.selected_uid.map_or(false, |u| u.0 == uid);
|
|
|
|
let alias = &player_info.player_alias;
|
|
|
|
let character_name_level = match &player_info.character {
|
|
|
|
Some(character) => format!("{} Lvl {}", &character.name, &character.level),
|
|
|
|
None => "<None>".to_string(), // character select or spectating
|
|
|
|
};
|
|
|
|
let text = if selected {
|
|
|
|
format!("-> [{}] {}", alias, character_name_level)
|
|
|
|
} else {
|
|
|
|
format!("[{}] {}", alias, character_name_level)
|
|
|
|
};
|
|
|
|
Text::new(&text)
|
|
|
|
.down(3.0)
|
|
|
|
.font_size(self.fonts.cyri.scale(15))
|
|
|
|
.font_id(self.fonts.cyri.conrod_id)
|
|
|
|
.color(TEXT_COLOR)
|
|
|
|
.set(state.ids.player_names[i], ui);
|
|
|
|
// Check for click
|
|
|
|
if ui
|
|
|
|
.widget_input(state.ids.player_names[i])
|
|
|
|
.clicks()
|
|
|
|
.left()
|
|
|
|
.next()
|
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
state.update(|s| s.selected_uid = Some((uid, Instant::now())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:49:18 +00:00
|
|
|
// Invite Button
|
|
|
|
if self
|
2020-07-12 00:39:50 +00:00
|
|
|
.client
|
2020-07-19 21:49:18 +00:00
|
|
|
.group_info()
|
|
|
|
.map_or(true, |(_, l_uid)| self.client.uid() == Some(l_uid))
|
2020-07-12 00:39:50 +00:00
|
|
|
{
|
|
|
|
let selected = state.selected_uid.map(|s| s.0).or_else(|| {
|
|
|
|
self.selected_entity
|
|
|
|
.and_then(|s| self.client.state().read_component_copied(s.0))
|
|
|
|
});
|
|
|
|
|
|
|
|
if Button::image(self.imgs.button)
|
|
|
|
.down(3.0)
|
|
|
|
.w_h(150.0, 30.0)
|
|
|
|
.hover_image(self.imgs.button_hover)
|
|
|
|
.press_image(self.imgs.button_press)
|
|
|
|
.label(&self.localized_strings.get("hud.group.invite"))
|
|
|
|
.label_y(conrod_core::position::Relative::Scalar(3.0))
|
|
|
|
.label_color(if selected.is_some() {
|
|
|
|
TEXT_COLOR
|
|
|
|
} else {
|
|
|
|
TEXT_COLOR_3
|
|
|
|
})
|
|
|
|
.label_font_size(self.fonts.cyri.scale(15))
|
|
|
|
.label_font_id(self.fonts.cyri.conrod_id)
|
|
|
|
.set(state.ids.invite_button, ui)
|
|
|
|
.was_clicked()
|
|
|
|
{
|
|
|
|
if let Some(uid) = selected {
|
|
|
|
events.push(Event::Invite(uid));
|
2020-07-12 03:12:03 +00:00
|
|
|
state.update(|s| {
|
|
|
|
s.selected_uid = None;
|
|
|
|
});
|
2020-05-24 09:20:54 +00:00
|
|
|
}
|
2020-07-12 00:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Friends Tab
|
|
|
|
|
|
|
|
if Button::image(if let SocialTab::Friends = self.show.social_tab {
|
|
|
|
self.imgs.social_button_pressed
|
|
|
|
} else {
|
|
|
|
self.imgs.social_button
|
|
|
|
})
|
|
|
|
.w_h(30.0 * 4.0, 12.0 * 4.0)
|
|
|
|
.hover_image(if let SocialTab::Friends = self.show.social_tab {
|
|
|
|
self.imgs.social_button_pressed
|
|
|
|
} else {
|
|
|
|
self.imgs.social_button
|
|
|
|
})
|
|
|
|
.press_image(if let SocialTab::Friends = self.show.social_tab {
|
|
|
|
self.imgs.social_button_pressed
|
|
|
|
} else {
|
|
|
|
self.imgs.social_button
|
|
|
|
})
|
2020-07-12 00:39:50 +00:00
|
|
|
.right_from(state.ids.online_tab, 0.0)
|
2020-01-17 23:43:18 +00:00
|
|
|
.label(&self.localized_strings.get("hud.social.friends"))
|
2020-01-26 19:29:46 +00:00
|
|
|
.label_font_size(self.fonts.cyri.scale(14))
|
|
|
|
.label_font_id(self.fonts.cyri.conrod_id)
|
2020-07-12 00:39:50 +00:00
|
|
|
.parent(state.ids.frame)
|
2020-03-20 19:52:32 +00:00
|
|
|
.color(UI_MAIN)
|
2019-08-07 13:14:26 +00:00
|
|
|
.label_color(TEXT_COLOR_3)
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.friends_tab, ui)
|
2019-08-07 13:14:26 +00:00
|
|
|
.was_clicked()
|
|
|
|
{
|
|
|
|
events.push(Event::ChangeSocialTab(SocialTab::Friends));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contents
|
|
|
|
|
|
|
|
if let SocialTab::Friends = self.show.social_tab {
|
2020-01-17 23:43:18 +00:00
|
|
|
Text::new(&self.localized_strings.get("hud.social.not_yet_available"))
|
2020-07-12 00:39:50 +00:00
|
|
|
.middle_of(state.ids.content_align)
|
2020-01-26 19:29:46 +00:00
|
|
|
.font_size(self.fonts.cyri.scale(18))
|
|
|
|
.font_id(self.fonts.cyri.conrod_id)
|
2019-08-07 13:14:26 +00:00
|
|
|
.color(TEXT_COLOR_3)
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.friends_test, ui);
|
2019-08-07 13:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Faction Tab
|
|
|
|
let button_img = if let SocialTab::Faction = self.show.social_tab {
|
|
|
|
self.imgs.social_button_pressed
|
|
|
|
} else {
|
|
|
|
self.imgs.social_button
|
|
|
|
};
|
|
|
|
if Button::image(button_img)
|
|
|
|
.w_h(30.0 * 4.0, 12.0 * 4.0)
|
2020-07-12 00:39:50 +00:00
|
|
|
.right_from(state.ids.friends_tab, 0.0)
|
2020-01-17 23:43:18 +00:00
|
|
|
.label(&self.localized_strings.get("hud.social.faction"))
|
2020-07-12 00:39:50 +00:00
|
|
|
.parent(state.ids.frame)
|
2020-01-26 19:29:46 +00:00
|
|
|
.label_font_size(self.fonts.cyri.scale(14))
|
|
|
|
.label_font_id(self.fonts.cyri.conrod_id)
|
2020-03-20 19:52:32 +00:00
|
|
|
.color(UI_MAIN)
|
2019-08-07 13:14:26 +00:00
|
|
|
.label_color(TEXT_COLOR_3)
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.faction_tab, ui)
|
2019-08-07 13:14:26 +00:00
|
|
|
.was_clicked()
|
|
|
|
{
|
|
|
|
events.push(Event::ChangeSocialTab(SocialTab::Faction));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contents
|
|
|
|
|
|
|
|
if let SocialTab::Faction = self.show.social_tab {
|
2020-01-17 23:43:18 +00:00
|
|
|
Text::new(&self.localized_strings.get("hud.social.not_yet_available"))
|
2020-07-12 00:39:50 +00:00
|
|
|
.middle_of(state.ids.content_align)
|
2020-01-26 19:29:46 +00:00
|
|
|
.font_size(self.fonts.cyri.scale(18))
|
|
|
|
.font_id(self.fonts.cyri.conrod_id)
|
2019-08-07 13:14:26 +00:00
|
|
|
.color(TEXT_COLOR_3)
|
2020-07-12 00:39:50 +00:00
|
|
|
.set(state.ids.faction_test, ui);
|
2019-08-07 13:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
events
|
|
|
|
}
|
|
|
|
}
|