Use strongly-typed OverlayID instead of usize
This commit is contained in:
@@ -21,7 +21,7 @@ use crate::{
|
|||||||
state::AppState,
|
state::AppState,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::overlay::OverlayData;
|
use super::overlay::{OverlayData, OverlayID};
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum BackendError {
|
pub enum BackendError {
|
||||||
@@ -97,7 +97,7 @@ where
|
|||||||
state.show_hide = true;
|
state.show_hide = true;
|
||||||
}
|
}
|
||||||
overlays.insert(
|
overlays.insert(
|
||||||
state.id,
|
state.id.0,
|
||||||
OverlayData::<T> {
|
OverlayData::<T> {
|
||||||
state,
|
state,
|
||||||
backend,
|
backend,
|
||||||
@@ -108,16 +108,16 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
let anchor = create_anchor(app)?;
|
let anchor = create_anchor(app)?;
|
||||||
overlays.insert(anchor.state.id, anchor);
|
overlays.insert(anchor.state.id.0, anchor);
|
||||||
|
|
||||||
let mut watch = create_watch::<T>(app)?;
|
let mut watch = create_watch::<T>(app)?;
|
||||||
watch.state.want_visible = true;
|
watch.state.want_visible = true;
|
||||||
overlays.insert(watch.state.id, watch);
|
overlays.insert(watch.state.id.0, watch);
|
||||||
|
|
||||||
let mut keyboard = create_keyboard(app, keymap)?;
|
let mut keyboard = create_keyboard(app, keymap)?;
|
||||||
keyboard.state.show_hide = true;
|
keyboard.state.show_hide = true;
|
||||||
keyboard.state.want_visible = false;
|
keyboard.state.want_visible = false;
|
||||||
overlays.insert(keyboard.state.id, keyboard);
|
overlays.insert(keyboard.state.id.0, keyboard);
|
||||||
|
|
||||||
Ok(Self { overlays, wl })
|
Ok(Self { overlays, wl })
|
||||||
}
|
}
|
||||||
@@ -158,7 +158,7 @@ where
|
|||||||
create_ran = true;
|
create_ran = true;
|
||||||
for (meta, state, backend) in data.screens {
|
for (meta, state, backend) in data.screens {
|
||||||
self.overlays.insert(
|
self.overlays.insert(
|
||||||
state.id,
|
state.id.0,
|
||||||
OverlayData::<T> {
|
OverlayData::<T> {
|
||||||
state,
|
state,
|
||||||
backend,
|
backend,
|
||||||
@@ -175,7 +175,7 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
let meta = &app.screens[idx];
|
let meta = &app.screens[idx];
|
||||||
let removed = self.overlays.remove(meta.id).unwrap();
|
let removed = self.overlays.remove(meta.id.0).unwrap();
|
||||||
removed_overlays.push(removed);
|
removed_overlays.push(removed);
|
||||||
log::info!("{}: Destroyed", meta.name);
|
log::info!("{}: Destroyed", meta.name);
|
||||||
app.screens.remove(idx);
|
app.screens.remove(idx);
|
||||||
@@ -187,7 +187,7 @@ where
|
|||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let output = wl.outputs.get(id).unwrap();
|
let output = wl.outputs.get(id).unwrap();
|
||||||
let Some(overlay) = self.overlays.get_mut(meta.id) else {
|
let Some(overlay) = self.overlays.get_mut(meta.id.0) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let logical_pos =
|
let logical_pos =
|
||||||
@@ -209,7 +209,7 @@ where
|
|||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let output = wl.outputs.get(id).unwrap();
|
let output = wl.outputs.get(id).unwrap();
|
||||||
let Some(overlay) = self.overlays.get_mut(meta.id) else {
|
let Some(overlay) = self.overlays.get_mut(meta.id.0) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -270,7 +270,7 @@ where
|
|||||||
|
|
||||||
pub fn remove_by_selector(&mut self, selector: &OverlaySelector) -> Option<OverlayData<T>> {
|
pub fn remove_by_selector(&mut self, selector: &OverlaySelector) -> Option<OverlayData<T>> {
|
||||||
match selector {
|
match selector {
|
||||||
OverlaySelector::Id(id) => self.overlays.remove(id),
|
OverlaySelector::Id(id) => self.overlays.remove(id.0),
|
||||||
OverlaySelector::Name(name) => {
|
OverlaySelector::Name(name) => {
|
||||||
let id = self
|
let id = self
|
||||||
.overlays
|
.overlays
|
||||||
@@ -282,12 +282,12 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_by_id(&mut self, id: usize) -> Option<&OverlayData<T>> {
|
pub fn get_by_id(&mut self, id: OverlayID) -> Option<&OverlayData<T>> {
|
||||||
self.overlays.get(id)
|
self.overlays.get(id.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mut_by_id(&mut self, id: usize) -> Option<&mut OverlayData<T>> {
|
pub fn mut_by_id(&mut self, id: OverlayID) -> Option<&mut OverlayData<T>> {
|
||||||
self.overlays.get_mut(id)
|
self.overlays.get_mut(id.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_by_name<'a>(&'a mut self, name: &str) -> Option<&'a OverlayData<T>> {
|
pub fn get_by_name<'a>(&'a mut self, name: &str) -> Option<&'a OverlayData<T>> {
|
||||||
@@ -307,7 +307,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, overlay: OverlayData<T>) {
|
pub fn add(&mut self, overlay: OverlayData<T>) {
|
||||||
self.overlays.insert(overlay.state.id, overlay);
|
self.overlays.insert(overlay.state.id.0, overlay);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn show_hide(&mut self, app: &mut AppState) {
|
pub fn show_hide(&mut self, app: &mut AppState) {
|
||||||
@@ -344,7 +344,7 @@ where
|
|||||||
#[derive(Clone, Deserialize, Debug)]
|
#[derive(Clone, Deserialize, Debug)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum OverlaySelector {
|
pub enum OverlaySelector {
|
||||||
Id(usize),
|
Id(OverlayID),
|
||||||
Name(Arc<str>),
|
Name(Arc<str>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ use crate::config::{AStrMapExt, GeneralConfig};
|
|||||||
use crate::overlays::anchor::ANCHOR_NAME;
|
use crate::overlays::anchor::ANCHOR_NAME;
|
||||||
use crate::state::AppState;
|
use crate::state::AppState;
|
||||||
|
|
||||||
|
use super::overlay::OverlayID;
|
||||||
use super::task::{TaskContainer, TaskType};
|
use super::task::{TaskContainer, TaskType};
|
||||||
use super::{common::OverlayContainer, overlay::OverlayData};
|
use super::{common::OverlayContainer, overlay::OverlayData};
|
||||||
|
|
||||||
@@ -140,8 +141,8 @@ impl InputState {
|
|||||||
pub struct InteractionState {
|
pub struct InteractionState {
|
||||||
pub mode: PointerMode,
|
pub mode: PointerMode,
|
||||||
pub grabbed: Option<GrabData>,
|
pub grabbed: Option<GrabData>,
|
||||||
pub clicked_id: Option<usize>,
|
pub clicked_id: Option<OverlayID>,
|
||||||
pub hovered_id: Option<usize>,
|
pub hovered_id: Option<OverlayID>,
|
||||||
pub release_actions: VecDeque<Box<dyn Fn()>>,
|
pub release_actions: VecDeque<Box<dyn Fn()>>,
|
||||||
pub next_push: Instant,
|
pub next_push: Instant,
|
||||||
pub haptics: Option<f32>,
|
pub haptics: Option<f32>,
|
||||||
@@ -204,7 +205,7 @@ pub struct PointerState {
|
|||||||
#[derive(Debug, Clone, Copy, Default)]
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
pub struct PointerHit {
|
pub struct PointerHit {
|
||||||
pub pointer: usize,
|
pub pointer: usize,
|
||||||
pub overlay: usize,
|
pub overlay: OverlayID,
|
||||||
pub mode: PointerMode,
|
pub mode: PointerMode,
|
||||||
pub primary: bool,
|
pub primary: bool,
|
||||||
pub uv: Vec2,
|
pub uv: Vec2,
|
||||||
@@ -237,7 +238,7 @@ impl InteractionHandler for DummyInteractionHandler {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Default)]
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
struct RayHit {
|
struct RayHit {
|
||||||
overlay: usize,
|
overlay: OverlayID,
|
||||||
global_pos: Vec3A,
|
global_pos: Vec3A,
|
||||||
local_pos: Vec2,
|
local_pos: Vec2,
|
||||||
dist: f32,
|
dist: f32,
|
||||||
@@ -246,7 +247,7 @@ struct RayHit {
|
|||||||
#[derive(Debug, Clone, Copy, Default)]
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
pub struct GrabData {
|
pub struct GrabData {
|
||||||
pub offset: Vec3A,
|
pub offset: Vec3A,
|
||||||
pub grabbed_id: usize,
|
pub grabbed_id: OverlayID,
|
||||||
pub old_curvature: Option<f32>,
|
pub old_curvature: Option<f32>,
|
||||||
pub grab_all: bool,
|
pub grab_all: bool,
|
||||||
}
|
}
|
||||||
@@ -299,7 +300,7 @@ where
|
|||||||
&mut app.session.config,
|
&mut app.session.config,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
log::warn!("Grabbed overlay {} does not exist", grab_data.grabbed_id);
|
log::warn!("Grabbed overlay {} does not exist", grab_data.grabbed_id.0);
|
||||||
pointer.interaction.grabbed = None;
|
pointer.interaction.grabbed = None;
|
||||||
}
|
}
|
||||||
return (0.1, None);
|
return (0.1, None);
|
||||||
@@ -341,7 +342,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
let Some(hovered) = overlays.mut_by_id(hit.overlay) else {
|
let Some(hovered) = overlays.mut_by_id(hit.overlay) else {
|
||||||
log::warn!("Hit overlay {} does not exist", hit.overlay);
|
log::warn!("Hit overlay {} does not exist", hit.overlay.0);
|
||||||
return (0.0, None); // no hit
|
return (0.0, None); // no hit
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -535,7 +536,7 @@ impl Pointer {
|
|||||||
overlay.state.realign(hmd);
|
overlay.state.realign(hmd);
|
||||||
overlay.state.dirty = true;
|
overlay.state.dirty = true;
|
||||||
} else {
|
} else {
|
||||||
log::error!("Grabbed overlay {} does not exist", overlay.state.id);
|
log::error!("Grabbed overlay {} does not exist", overlay.state.id.0);
|
||||||
self.interaction.grabbed = None;
|
self.interaction.grabbed = None;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -572,7 +573,7 @@ impl Pointer {
|
|||||||
|
|
||||||
fn ray_test(
|
fn ray_test(
|
||||||
&self,
|
&self,
|
||||||
overlay: usize,
|
overlay: OverlayID,
|
||||||
transform: &Affine3A,
|
transform: &Affine3A,
|
||||||
curvature: &Option<f32>,
|
curvature: &Option<f32>,
|
||||||
) -> Option<RayHit> {
|
) -> Option<RayHit> {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ use crate::state::AppState;
|
|||||||
|
|
||||||
use super::overlay::OpenVrOverlayData;
|
use super::overlay::OpenVrOverlayData;
|
||||||
|
|
||||||
static AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(1);
|
static LINE_AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(1);
|
||||||
|
|
||||||
pub(super) struct LinePool {
|
pub(super) struct LinePool {
|
||||||
lines: IdMap<usize, OverlayData<OpenVrOverlayData>>,
|
lines: IdMap<usize, OverlayData<OpenVrOverlayData>>,
|
||||||
@@ -59,7 +59,7 @@ impl LinePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn allocate(&mut self) -> usize {
|
pub fn allocate(&mut self) -> usize {
|
||||||
let id = AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed);
|
let id = LINE_AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed);
|
||||||
|
|
||||||
let mut data = OverlayData::<OpenVrOverlayData> {
|
let mut data = OverlayData::<OpenVrOverlayData> {
|
||||||
state: OverlayState {
|
state: OverlayState {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ use super::{
|
|||||||
CompositionLayer, XrState,
|
CompositionLayer, XrState,
|
||||||
};
|
};
|
||||||
|
|
||||||
static AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(1);
|
static LINE_AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(1);
|
||||||
pub(super) const LINE_WIDTH: f32 = 0.002;
|
pub(super) const LINE_WIDTH: f32 = 0.002;
|
||||||
|
|
||||||
pub(super) struct LinePool {
|
pub(super) struct LinePool {
|
||||||
@@ -66,7 +66,7 @@ impl LinePool {
|
|||||||
xr: &XrState,
|
xr: &XrState,
|
||||||
graphics: Arc<WlxGraphics>,
|
graphics: Arc<WlxGraphics>,
|
||||||
) -> anyhow::Result<usize> {
|
) -> anyhow::Result<usize> {
|
||||||
let id = AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed);
|
let id = LINE_AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed);
|
||||||
|
|
||||||
let srd =
|
let srd =
|
||||||
create_swapchain_render_data(xr, graphics, [1, 1, 1], SwapchainOpts::new().srgb())?;
|
create_swapchain_render_data(xr, graphics, [1, 1, 1], SwapchainOpts::new().srgb())?;
|
||||||
|
|||||||
@@ -8,21 +8,25 @@ use std::{
|
|||||||
|
|
||||||
use anyhow::Ok;
|
use anyhow::Ok;
|
||||||
use glam::{Affine2, Affine3A, Mat3A, Quat, Vec2, Vec3, Vec3A};
|
use glam::{Affine2, Affine3A, Mat3A, Quat, Vec2, Vec3, Vec3A};
|
||||||
|
use serde::Deserialize;
|
||||||
use vulkano::image::view::ImageView;
|
use vulkano::image::view::ImageView;
|
||||||
|
|
||||||
use crate::{config::AStrMapExt, state::AppState};
|
use crate::{config::AStrMapExt, state::AppState};
|
||||||
|
|
||||||
use super::input::{DummyInteractionHandler, Haptics, InteractionHandler, PointerHit};
|
use super::input::{DummyInteractionHandler, Haptics, InteractionHandler, PointerHit};
|
||||||
|
|
||||||
static AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(0);
|
static OVERLAY_AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
|
||||||
pub trait OverlayBackend: OverlayRenderer + InteractionHandler {
|
pub trait OverlayBackend: OverlayRenderer + InteractionHandler {
|
||||||
fn set_renderer(&mut self, renderer: Box<dyn OverlayRenderer>);
|
fn set_renderer(&mut self, renderer: Box<dyn OverlayRenderer>);
|
||||||
fn set_interaction(&mut self, interaction: Box<dyn InteractionHandler>);
|
fn set_interaction(&mut self, interaction: Box<dyn InteractionHandler>);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Default)]
|
||||||
|
pub struct OverlayID(pub usize);
|
||||||
|
|
||||||
pub struct OverlayState {
|
pub struct OverlayState {
|
||||||
pub id: usize,
|
pub id: OverlayID,
|
||||||
pub name: Arc<str>,
|
pub name: Arc<str>,
|
||||||
pub want_visible: bool,
|
pub want_visible: bool,
|
||||||
pub show_hide: bool,
|
pub show_hide: bool,
|
||||||
@@ -48,7 +52,7 @@ pub struct OverlayState {
|
|||||||
impl Default for OverlayState {
|
impl Default for OverlayState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
OverlayState {
|
OverlayState {
|
||||||
id: AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed),
|
id: OverlayID(OVERLAY_AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed)),
|
||||||
name: Arc::from(""),
|
name: Arc::from(""),
|
||||||
want_visible: false,
|
want_visible: false,
|
||||||
show_hide: false,
|
show_hide: false,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use smallvec::{smallvec, SmallVec};
|
|||||||
use vulkano::image::view::ImageView;
|
use vulkano::image::view::ImageView;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
backend::{input::InputState, task::TaskContainer},
|
backend::{input::InputState, overlay::OverlayID, task::TaskContainer},
|
||||||
config::{AStrMap, GeneralConfig},
|
config::{AStrMap, GeneralConfig},
|
||||||
config_io,
|
config_io,
|
||||||
graphics::WlxGraphics,
|
graphics::WlxGraphics,
|
||||||
@@ -160,7 +160,7 @@ impl AudioOutput {
|
|||||||
|
|
||||||
pub struct ScreenMeta {
|
pub struct ScreenMeta {
|
||||||
pub name: Arc<str>,
|
pub name: Arc<str>,
|
||||||
pub id: usize,
|
pub id: OverlayID,
|
||||||
pub native_handle: u32,
|
pub native_handle: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user