Skip to content

Commit

Permalink
Use strongly-typed OverlayID instead of usize
Browse files Browse the repository at this point in the history
  • Loading branch information
olekolek1000 authored and galister committed Oct 17, 2024
1 parent 022ae53 commit 9dbd1f9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
32 changes: 16 additions & 16 deletions src/backend/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
state::AppState,
};

use super::overlay::OverlayData;
use super::overlay::{OverlayData, OverlayID};

#[derive(Error, Debug)]
pub enum BackendError {
Expand Down Expand Up @@ -97,7 +97,7 @@ where
state.show_hide = true;
}
overlays.insert(
state.id,
state.id.0,
OverlayData::<T> {
state,
backend,
Expand All @@ -108,16 +108,16 @@ where
}

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)?;
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)?;
keyboard.state.show_hide = true;
keyboard.state.want_visible = false;
overlays.insert(keyboard.state.id, keyboard);
overlays.insert(keyboard.state.id.0, keyboard);

Ok(Self { overlays, wl })
}
Expand Down Expand Up @@ -158,7 +158,7 @@ where
create_ran = true;
for (meta, state, backend) in data.screens {
self.overlays.insert(
state.id,
state.id.0,
OverlayData::<T> {
state,
backend,
Expand All @@ -175,7 +175,7 @@ where
};

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);
log::info!("{}: Destroyed", meta.name);
app.screens.remove(idx);
Expand All @@ -187,7 +187,7 @@ where
continue;
};
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;
};
let logical_pos =
Expand All @@ -209,7 +209,7 @@ where
continue;
};
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;
};

Expand Down Expand Up @@ -270,7 +270,7 @@ where

pub fn remove_by_selector(&mut self, selector: &OverlaySelector) -> Option<OverlayData<T>> {
match selector {
OverlaySelector::Id(id) => self.overlays.remove(id),
OverlaySelector::Id(id) => self.overlays.remove(id.0),
OverlaySelector::Name(name) => {
let id = self
.overlays
Expand All @@ -282,12 +282,12 @@ where
}
}

pub fn get_by_id(&mut self, id: usize) -> Option<&OverlayData<T>> {
self.overlays.get(id)
pub fn get_by_id(&mut self, id: OverlayID) -> Option<&OverlayData<T>> {
self.overlays.get(id.0)
}

pub fn mut_by_id(&mut self, id: usize) -> Option<&mut OverlayData<T>> {
self.overlays.get_mut(id)
pub fn mut_by_id(&mut self, id: OverlayID) -> Option<&mut OverlayData<T>> {
self.overlays.get_mut(id.0)
}

pub fn get_by_name<'a>(&'a mut self, name: &str) -> Option<&'a OverlayData<T>> {
Expand All @@ -307,7 +307,7 @@ where
}

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) {
Expand Down Expand Up @@ -344,7 +344,7 @@ where
#[derive(Clone, Deserialize, Debug)]
#[serde(untagged)]
pub enum OverlaySelector {
Id(usize),
Id(OverlayID),
Name(Arc<str>),
}

Expand Down
19 changes: 10 additions & 9 deletions src/backend/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::config::{AStrMapExt, GeneralConfig};
use crate::overlays::anchor::ANCHOR_NAME;
use crate::state::AppState;

use super::overlay::OverlayID;
use super::task::{TaskContainer, TaskType};
use super::{common::OverlayContainer, overlay::OverlayData};

Expand Down Expand Up @@ -140,8 +141,8 @@ impl InputState {
pub struct InteractionState {
pub mode: PointerMode,
pub grabbed: Option<GrabData>,
pub clicked_id: Option<usize>,
pub hovered_id: Option<usize>,
pub clicked_id: Option<OverlayID>,
pub hovered_id: Option<OverlayID>,
pub release_actions: VecDeque<Box<dyn Fn()>>,
pub next_push: Instant,
pub haptics: Option<f32>,
Expand Down Expand Up @@ -204,7 +205,7 @@ pub struct PointerState {
#[derive(Debug, Clone, Copy, Default)]
pub struct PointerHit {
pub pointer: usize,
pub overlay: usize,
pub overlay: OverlayID,
pub mode: PointerMode,
pub primary: bool,
pub uv: Vec2,
Expand Down Expand Up @@ -237,7 +238,7 @@ impl InteractionHandler for DummyInteractionHandler {

#[derive(Debug, Clone, Copy, Default)]
struct RayHit {
overlay: usize,
overlay: OverlayID,
global_pos: Vec3A,
local_pos: Vec2,
dist: f32,
Expand All @@ -246,7 +247,7 @@ struct RayHit {
#[derive(Debug, Clone, Copy, Default)]
pub struct GrabData {
pub offset: Vec3A,
pub grabbed_id: usize,
pub grabbed_id: OverlayID,
pub old_curvature: Option<f32>,
pub grab_all: bool,
}
Expand Down Expand Up @@ -299,7 +300,7 @@ where
&mut app.session.config,
);
} 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;
}
return (0.1, None);
Expand Down Expand Up @@ -341,7 +342,7 @@ where
}
}
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
};

Expand Down Expand Up @@ -535,7 +536,7 @@ impl Pointer {
overlay.state.realign(hmd);
overlay.state.dirty = true;
} 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;
}
} else {
Expand Down Expand Up @@ -572,7 +573,7 @@ impl Pointer {

fn ray_test(
&self,
overlay: usize,
overlay: OverlayID,
transform: &Affine3A,
curvature: &Option<f32>,
) -> Option<RayHit> {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/openvr/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::state::AppState;

use super::overlay::OpenVrOverlayData;

static AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(1);
static LINE_AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(1);

pub(super) struct LinePool {
lines: IdMap<usize, OverlayData<OpenVrOverlayData>>,
Expand Down Expand Up @@ -59,7 +59,7 @@ impl LinePool {
}

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> {
state: OverlayState {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/openxr/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::{
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) struct LinePool {
Expand Down Expand Up @@ -66,7 +66,7 @@ impl LinePool {
xr: &XrState,
graphics: Arc<WlxGraphics>,
) -> anyhow::Result<usize> {
let id = AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed);
let id = LINE_AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed);

let srd =
create_swapchain_render_data(xr, graphics, [1, 1, 1], SwapchainOpts::new().srgb())?;
Expand Down
10 changes: 7 additions & 3 deletions src/backend/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ use std::{

use anyhow::Ok;
use glam::{Affine2, Affine3A, Mat3A, Quat, Vec2, Vec3, Vec3A};
use serde::Deserialize;
use vulkano::image::view::ImageView;

use crate::{config::AStrMapExt, state::AppState};

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 {
fn set_renderer(&mut self, renderer: Box<dyn OverlayRenderer>);
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 id: usize,
pub id: OverlayID,
pub name: Arc<str>,
pub want_visible: bool,
pub show_hide: bool,
Expand All @@ -48,7 +52,7 @@ pub struct OverlayState {
impl Default for OverlayState {
fn default() -> Self {
OverlayState {
id: AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed),
id: OverlayID(OVERLAY_AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed)),
name: Arc::from(""),
want_visible: false,
show_hide: false,
Expand Down
4 changes: 2 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use smallvec::{smallvec, SmallVec};
use vulkano::image::view::ImageView;

use crate::{
backend::{input::InputState, task::TaskContainer},
backend::{input::InputState, overlay::OverlayID, task::TaskContainer},
config::{AStrMap, GeneralConfig},
config_io,
graphics::WlxGraphics,
Expand Down Expand Up @@ -160,7 +160,7 @@ impl AudioOutput {

pub struct ScreenMeta {
pub name: Arc<str>,
pub id: usize,
pub id: OverlayID,
pub native_handle: u32,
}

Expand Down

0 comments on commit 9dbd1f9

Please sign in to comment.