Create gui handler

This commit is contained in:
hodasemi 2023-01-17 07:57:56 +01:00
parent 1df2f4183c
commit 2504d4ee24
7 changed files with 100 additions and 9 deletions

View file

@ -10,7 +10,9 @@ crate-type = ["cdylib"]
[dependencies]
vulkan-rs = { git = "https://gavania.de/hodasemi/vulkan_lib.git" }
assetpath = { git = "https://gavania.de/hodasemi/vulkan_lib.git" }
rfactor_sm_reader = { git = "https://gavania.de/hodasemi/rfactor_sm_reader.git" }
ui = { git = "https://gavania.de/hodasemi/ui.git" }
anyhow = { version = "1.0.68", features = ["backtrace"] }
cgmath = { version = "0.18.0", features = ["swizzle", "serde"] }

View file

View file

View file

View file

@ -2,7 +2,7 @@ use crate::write_log;
use self::{
rendering::Rendering,
rfactor_data::{DataConfig, RFactorData},
rfactor_data::{RFactorData, RadarConfig},
};
mod pipeline;
@ -10,20 +10,24 @@ mod rendering;
mod rfactor_data;
use anyhow::Result;
use assetpath::AssetPath;
use std::sync::{Arc, Mutex};
use ui::prelude::*;
use vulkan_rs::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct OverlayConfig {
pub data_config: DataConfig,
pub radar_config: RadarConfig,
pub font_path: String,
}
impl OverlayConfig {
pub const fn new() -> Self {
Self {
data_config: DataConfig::new(),
radar_config: RadarConfig::new(),
font_path: String::new(),
}
}
}
@ -35,6 +39,7 @@ pub struct Overlay {
device: Option<Arc<Device>>,
queue: Option<Arc<Mutex<Queue>>>,
rendering: Option<Rendering>,
gui_handler: Option<Arc<GuiHandler>>,
rfactor_data: Option<RFactorData>,
}
@ -48,6 +53,7 @@ impl Overlay {
device: None,
queue: None,
rendering: None,
gui_handler: None,
rfactor_data: None,
}
@ -98,9 +104,29 @@ impl Overlay {
write_log!("-> create rendering: old cleared");
self.rendering = Some(Rendering::new(self.device(), self.queue(), swapchain)?);
let rendering = Rendering::new(self.device(), self.queue(), swapchain.clone())?;
// only font is used
let mut create_info = GuiHandlerCreateInfo::default();
create_info.font_path = AssetPath::from(self.config.font_path.clone());
create_info.font_path.assume_prefix_free();
let ctx = Arc::new(ContextImpl::new(
self.device(),
self.queue(),
swapchain,
rendering.images().clone(),
));
self.gui_handler = Some(GuiHandler::new(
create_info,
&(ctx as Arc<dyn ContextInterface>),
)?);
self.rendering = Some(rendering);
write_log!("-> create rendering: new created");
write_log!("-> create rendering: end");
Ok(())
@ -111,7 +137,7 @@ impl Overlay {
if self.rfactor_data.is_none() {
self.rfactor_data = RFactorData::new(
self.config.data_config,
self.config.radar_config,
self.device(),
self.rendering
.as_mut()
@ -139,3 +165,60 @@ impl Overlay {
self.rendering.as_mut().unwrap().render(swapchain, &objects)
}
}
struct ContextImpl {
device: Arc<Device>,
queue: Arc<Mutex<Queue>>,
swapchain: Arc<Swapchain>,
images: Vec<Arc<Image>>,
}
impl ContextImpl {
fn new(
device: Arc<Device>,
queue: Arc<Mutex<Queue>>,
swapchain: Arc<Swapchain>,
images: Vec<Arc<Image>>,
) -> Self {
Self {
device,
queue,
swapchain,
images,
}
}
}
impl ContextInterface for ContextImpl {
fn device(&self) -> &Arc<Device> {
&self.device
}
fn queue(&self) -> &Arc<Mutex<Queue>> {
&self.queue
}
fn format(&self) -> VkFormat {
self.swapchain.format()
}
fn image_layout(&self) -> VkImageLayout {
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
}
fn image_count(&self) -> usize {
self.images.len()
}
fn images(&self) -> TargetMode<Vec<Arc<Image>>> {
TargetMode::Mono(self.images.clone())
}
fn width(&self) -> u32 {
self.swapchain.width()
}
fn height(&self) -> u32 {
self.swapchain.height()
}
}

View file

@ -51,6 +51,7 @@ pub struct Rendering {
pipeline: SingleColorPipeline,
render_target: RenderTarget,
command_buffer: Arc<CommandBuffer>,
images: Vec<Arc<Image>>,
queue: Arc<Mutex<Queue>>,
}
@ -97,6 +98,7 @@ impl Rendering {
pipeline: SingleColorPipeline::new(device.clone(), render_target.render_pass())?,
render_target,
command_buffer: CommandBuffer::new_primary().build(device.clone(), queue.clone())?,
images,
queue,
})
@ -165,4 +167,8 @@ impl Rendering {
Ok(())
}
pub fn images(&self) -> &Vec<Arc<Image>> {
&self.images
}
}

View file

@ -20,7 +20,7 @@ pub trait RenderObject {
}
#[derive(Deserialize, Serialize, Clone, Copy, Debug)]
pub struct DataConfig {
pub struct RadarConfig {
pub radar_scale: f32,
pub radar_center_factor: f32,
pub radar_transparency: f32,
@ -31,7 +31,7 @@ pub struct DataConfig {
pub danger_color: Vector3<f32>,
}
impl DataConfig {
impl RadarConfig {
pub const fn new() -> Self {
Self {
radar_scale: 1.0,
@ -48,7 +48,7 @@ impl DataConfig {
pub struct RFactorData {
// config
config: DataConfig,
config: RadarConfig,
// rf2 memory mapped data
telemetry_reader: TelemetryReader,
@ -82,7 +82,7 @@ pub struct RFactorData {
impl RFactorData {
pub fn new(
config: DataConfig,
config: RadarConfig,
device: Arc<Device>,
descriptor_layout: &Arc<DescriptorSetLayout>,
width: u32,