211 lines
5.2 KiB
Rust
211 lines
5.2 KiB
Rust
use std::sync::Arc;
|
|
|
|
use anyhow::Result;
|
|
|
|
use ui::prelude::*;
|
|
use utilities::prelude::Color;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
|
|
pub enum BehindLeader {
|
|
Time(f64),
|
|
Laps(i32),
|
|
}
|
|
|
|
pub struct LeaderBoardEntry {
|
|
id: i32,
|
|
|
|
name: String,
|
|
place: u8,
|
|
behind: BehindLeader,
|
|
time_behind_next: f64,
|
|
best_lap: f64,
|
|
|
|
snippet: Arc<GuiSnippet>,
|
|
|
|
name_label: Arc<Label>,
|
|
place_label: Arc<Label>,
|
|
time_label: Arc<Label>,
|
|
|
|
place_updated: bool,
|
|
}
|
|
|
|
impl LeaderBoardEntry {
|
|
const ENTRY: &str = include_str!("leaderboard_entry.xml");
|
|
|
|
pub fn empty(gui_handler: &Arc<GuiHandler>) -> Result<Self> {
|
|
Self::new(
|
|
gui_handler,
|
|
-1,
|
|
"".to_string(),
|
|
0,
|
|
BehindLeader::Laps(0),
|
|
0.0,
|
|
0.0,
|
|
)
|
|
}
|
|
|
|
pub fn new(
|
|
gui_handler: &Arc<GuiHandler>,
|
|
id: i32,
|
|
name: String,
|
|
place: u8,
|
|
behind: BehindLeader,
|
|
time_behind_next: f64,
|
|
best_lap: f64,
|
|
) -> Result<Self> {
|
|
let snippet = GuiSnippet::from_str(gui_handler, Self::ENTRY)?;
|
|
|
|
let name_label: Arc<Label> = snippet.element("name")?;
|
|
let place_label: Arc<Label> = snippet.element("place")?;
|
|
let time_label: Arc<Label> = snippet.element("time")?;
|
|
|
|
name_label.set_text(&name)?;
|
|
place_label.set_text(place)?;
|
|
time_label.set_text("---")?;
|
|
|
|
Ok(Self {
|
|
id,
|
|
|
|
name,
|
|
place,
|
|
behind,
|
|
time_behind_next,
|
|
best_lap,
|
|
|
|
snippet,
|
|
|
|
name_label,
|
|
place_label,
|
|
time_label,
|
|
|
|
place_updated: true,
|
|
})
|
|
}
|
|
|
|
pub fn change_id(&mut self, id: i32) {
|
|
self.id = id;
|
|
}
|
|
|
|
pub fn id(&self) -> i32 {
|
|
self.id
|
|
}
|
|
|
|
pub fn place(&self) -> u8 {
|
|
self.place
|
|
}
|
|
|
|
pub fn name(&self) -> &str {
|
|
&self.name
|
|
}
|
|
|
|
pub fn change_name(&mut self, name: String) -> Result<()> {
|
|
self.name = name;
|
|
|
|
self.name_label.set_text(&self.name)
|
|
}
|
|
|
|
pub fn snippet(&self) -> Arc<GuiSnippet> {
|
|
self.snippet.clone()
|
|
}
|
|
|
|
pub fn change_background_color(&self, color: Color) -> Result<()> {
|
|
self.time_label.set_background(color)?;
|
|
self.name_label.set_background(color)?;
|
|
self.place_label.set_background(color)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn update_place(&mut self, place: u8) -> Result<()> {
|
|
if self.place != place {
|
|
self.place_updated = true;
|
|
}
|
|
|
|
self.place = place;
|
|
|
|
self.place_label.set_text(self.place)
|
|
}
|
|
|
|
pub fn update_time_behind_leader(&mut self, behind: BehindLeader) -> Result<()> {
|
|
if self.behind != behind {
|
|
self.behind = behind;
|
|
|
|
match self.behind {
|
|
BehindLeader::Time(time_behind) => {
|
|
// check if we are leader
|
|
if time_behind == 0.0 {
|
|
self.time_label.set_text("---")?;
|
|
} else {
|
|
let text = if time_behind > 60.0 {
|
|
let full_minutes = (self.best_lap / 60.0).floor();
|
|
let remainder = self.best_lap - (full_minutes * 60.0);
|
|
|
|
format!("+{:.0}:{:.0}", full_minutes, remainder)
|
|
} else {
|
|
format!("+{:.3}", time_behind)
|
|
};
|
|
|
|
self.time_label.set_text(text)?;
|
|
}
|
|
}
|
|
BehindLeader::Laps(laps_behind) => {
|
|
let text = if laps_behind == 1 {
|
|
format!("+{} Lap", laps_behind)
|
|
} else {
|
|
format!("+{} Laps", laps_behind)
|
|
};
|
|
|
|
self.time_label.set_text(text)?;
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn update_best_lap(&mut self, time: f64) -> Result<()> {
|
|
if self.best_lap != time {
|
|
self.best_lap = time;
|
|
|
|
if self.best_lap < 0.0 {
|
|
self.time_label.set_text("---")?;
|
|
} else {
|
|
let text = if self.best_lap > 60.0 {
|
|
let full_minutes = (self.best_lap / 60.0).floor();
|
|
let remainder = self.best_lap - (full_minutes * 60.0);
|
|
|
|
format!("{:.0}:{:.3}", full_minutes, remainder)
|
|
} else {
|
|
format!("{:.3}", self.best_lap)
|
|
};
|
|
|
|
self.time_label.set_text(text)?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn update_time_behind_next(&mut self, time: f64) -> Result<()> {
|
|
self.time_behind_next = time;
|
|
|
|
let text = if self.time_behind_next > 60.0 {
|
|
let full_minutes = (self.time_behind_next / 60.0).floor();
|
|
let remainder = self.time_behind_next - (full_minutes * 60.0);
|
|
|
|
format!("+{:.0}:{:.3}", full_minutes, remainder)
|
|
} else {
|
|
format!("+{:.3}", self.time_behind_next)
|
|
};
|
|
|
|
self.time_label.set_text(text)
|
|
}
|
|
|
|
pub fn needs_resorting(&self) -> bool {
|
|
self.place_updated
|
|
}
|
|
|
|
pub fn resorting_finished(&mut self) {
|
|
self.place_updated = false;
|
|
}
|
|
}
|