Add leader board

This commit is contained in:
hodasemi 2023-01-18 07:09:44 +01:00
parent da50c97958
commit 2e6072812f
8 changed files with 250 additions and 14 deletions

View file

@ -11,6 +11,7 @@ crate-type = ["cdylib"]
[dependencies]
vulkan-rs = { git = "https://gavania.de/hodasemi/vulkan_lib.git" }
assetpath = { git = "https://gavania.de/hodasemi/vulkan_lib.git" }
utilities = { git = "https://gavania.de/hodasemi/utilities.git" }
rfactor_sm_reader = { git = "https://gavania.de/hodasemi/rfactor_sm_reader.git" }
ui = { git = "https://gavania.de/hodasemi/ui.git" }

View file

@ -0,0 +1,223 @@
use std::{
ffi::{c_char, CStr},
sync::Arc,
};
use anyhow::Result;
use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01};
use ui::prelude::*;
use utilities::prelude::Color;
use crate::overlay::{rfactor_data::DataReceiver, UiOverlay};
pub struct LeaderBoard {
gui_handler: Arc<GuiHandler>,
gui: Arc<GuiBuilder>,
main_grid: Arc<Grid>,
entries: Vec<LeaderBoardEntry>,
entry_backgrounds: [Color; 2],
}
impl LeaderBoard {
const GRID: &str = include_str!("ui_files/leaderboard_grid.xml");
const ENTRY: &str = include_str!("ui_files/leaderboard_entry.xml");
pub fn new(gui_handler: &Arc<GuiHandler>) -> Result<Self> {
let gui = GuiBuilder::from_str(gui_handler, Self::GRID)?;
let main_grid = gui.element("main_grid")?;
Ok(Self {
gui_handler: gui_handler.clone(),
gui,
main_grid,
entries: Vec::new(),
entry_backgrounds: [Color::try_from("#838383")?, Color::try_from("#545454")?],
})
}
}
impl UiOverlay for LeaderBoard {}
impl DataReceiver for LeaderBoard {
fn scoring_update(&mut self, vehicle_scorings: &[VehicleScoringInfoV01]) -> Result<()> {
for vehicle_scoring in vehicle_scorings {
// check driver list
match self
.entries
.iter_mut()
.find(|entry| vehicle_scoring.mID == entry.id)
{
Some(entry) => {
entry.update_place(vehicle_scoring.mPlace)?;
entry.update_time_behind_leader(vehicle_scoring.mTimeBehindLeader)?;
entry.update_time_behind_next(vehicle_scoring.mTimeBehindNext)?;
}
None => {
let entry = LeaderBoardEntry::new(
&self.gui_handler,
vehicle_scoring.mID,
unsafe { CStr::from_ptr(&vehicle_scoring.mDriverName as *const c_char) }
.to_str()
.unwrap()
.to_string(),
vehicle_scoring.mPlace,
vehicle_scoring.mTimeBehindLeader,
vehicle_scoring.mTimeBehindNext,
)?;
self.entries.push(entry);
}
}
}
// check if entry count in grid is the same as the gathered entries
let force_update = if self
.main_grid
.child_at(0, self.entries.len() - 1)?
.is_none()
{
for i in 0..self.entries.len() {
self.main_grid.detach(0, i)?;
}
true
} else {
false
};
// check if any entry needs resorting
if force_update || self.entries.iter().any(|entry| entry.needs_resorting()) {
self.entries
.sort_by(|lhs, rhs| lhs.place().cmp(&rhs.place()));
for (i, entry) in self.entries.iter_mut().enumerate() {
entry.resorting_finished();
entry.change_background_color(self.entry_backgrounds[i % 2])?;
self.main_grid.attach(entry.snippet(), 0, i, 1, 1)?;
}
}
if self.entries.is_empty() {
self.gui.disable()?;
} else {
self.gui.enable()?;
}
Ok(())
}
fn telemetry_update(
&mut self,
_player_id: Option<i32>,
_telemetries: &[rF2VehicleTelemetry],
) -> Result<()> {
Ok(())
}
}
struct LeaderBoardEntry {
id: i32,
name: String,
place: u8,
time_behind_leader: f64,
time_behind_next: f64,
snippet: Arc<GuiSnippet>,
grid: Arc<Grid>,
name_label: Arc<Label>,
place_label: Arc<Label>,
time_behind_label: Arc<Label>,
place_updated: bool,
}
impl LeaderBoardEntry {
pub fn new(
gui_handler: &Arc<GuiHandler>,
id: i32,
name: String,
place: u8,
time_behind_leader: f64,
time_behind_next: f64,
) -> Result<Self> {
let snippet = GuiSnippet::from_str(gui_handler, LeaderBoard::ENTRY)?;
let background = snippet.element("grid")?;
let name_label: Arc<Label> = snippet.element("name")?;
let place_label: Arc<Label> = snippet.element("place")?;
let time_behind_label: Arc<Label> = snippet.element("time_behind")?;
name_label.set_text(&name)?;
place_label.set_text(place)?;
time_behind_label.set_text(format!("{:.3}", time_behind_leader))?;
Ok(Self {
id,
name,
place,
time_behind_leader,
time_behind_next,
snippet,
grid: background,
name_label,
place_label,
time_behind_label,
place_updated: true,
})
}
pub fn place(&self) -> u8 {
self.place
}
pub fn snippet(&self) -> Arc<GuiSnippet> {
self.snippet.clone()
}
pub fn change_background_color(&self, color: Color) -> Result<()> {
self.grid.set_background(color)
}
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, time: f64) -> Result<()> {
self.time_behind_leader = time;
self.time_behind_label
.set_text(format!("{:.3}", self.time_behind_leader))
}
pub fn update_time_behind_next(&mut self, time: f64) -> Result<()> {
self.time_behind_next = time;
Ok(())
}
pub fn needs_resorting(&self) -> bool {
self.place_updated
}
pub fn resorting_finished(&mut self) {
self.place_updated = false;
}
}

View file

@ -1,8 +1,10 @@
mod leaderboard;
mod pedals;
mod pipeline;
mod radar;
mod watermark;
pub use leaderboard::*;
pub use pedals::*;
pub use radar::*;
pub use watermark::*;

View file

@ -36,12 +36,6 @@ impl Pedals {
}
}
impl Drop for Pedals {
fn drop(&mut self) {
self.gui.disable().unwrap();
}
}
impl UiOverlay for Pedals {}
impl DataReceiver for Pedals {

View file

@ -0,0 +1,7 @@
<root>
<grid id="grid" x_dim="6" y_dim="1">
<label id="place" x_slot="0" y_slot="0" text_color="black"></label>
<label id="name" x_slot="1" y_slot="0" x_size="3" text_color="black"></label>
<label id="time_behind" x_slot="4" y_slot="0" x_size="2" text_color="black"></label>
</grid>
</root>

View file

@ -0,0 +1,5 @@
<?xml-model href="gui.xsd" type="application/xml" schematypens="http://www.w3.org/2001/XMLSchema"?>
<root reference_width="2560" reference_height="1440">
<grid id="main_grid" x_dim="1" y_dim="25" x_offset="10" y_offset="70" width="250" height="1000"
vert_align="top" hori_align="left" margin="0" padding="0"> </grid>
</root>

View file

@ -7,7 +7,7 @@ use ui::prelude::*;
use crate::overlay::{rfactor_data::DataReceiver, UiOverlay};
pub struct Watermark {
gui: Arc<GuiBuilder>,
_gui: Arc<GuiBuilder>,
}
impl Watermark {
@ -18,13 +18,7 @@ impl Watermark {
gui.enable()?;
Ok(Self { gui })
}
}
impl Drop for Watermark {
fn drop(&mut self) {
self.gui.disable().unwrap()
Ok(Self { _gui: gui })
}
}

View file

@ -31,6 +31,7 @@ pub struct UiSelectorConfig {
pub enable_radar: bool,
pub enable_pedals: bool,
pub enable_leaderboard: bool,
}
impl UiSelectorConfig {
@ -39,6 +40,7 @@ impl UiSelectorConfig {
enable_watermark: true,
enable_radar: true,
enable_pedals: true,
enable_leaderboard: true,
}
}
}
@ -200,6 +202,14 @@ impl Overlay {
write_log!("Pedals successfully created");
}
// create leaderboard
if self.config.ui_config.enable_leaderboard {
let leaderboard = Rc::new(RefCell::new(LeaderBoard::new(&gui_handler)?));
self.ui_elements.push(leaderboard);
write_log!("Leader Board successfully created");
}
// add rendering callbacks
rendering.add_render_callback({
let gui_handler = gui_handler.clone();