From 6729ca3ca36e795e740eecffb24152fdea20bd16 Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Sat, 27 Jul 2024 17:41:54 +0200 Subject: [PATCH] Same idea again + drop atomic crate Signed-off-by: Pierre Fenoll --- Cargo.toml | 3 --- examples/demo.rs | 57 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dfff62c..550f8c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,9 +15,6 @@ once_cell = "1.9.0" cgmath = "0.18.0" libc = "0.2.69" -atomic = "0.6.0" -bytemuck = { version = "1.13.1", features = ["derive"] } - # framebuffer memmap2 = { version = "0.5.2", optional = true } ioctl-gen = { version = "0.1.1", optional = true } diff --git a/examples/demo.rs b/examples/demo.rs index 5a8e84a..fb73d2e 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -22,13 +22,12 @@ use once_cell::sync::Lazy; use std::collections::VecDeque; use std::fmt; use std::process::Command; -use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU8, Ordering}; use std::sync::Mutex; use std::thread::sleep; use std::time::Duration; #[derive(Copy, Clone, PartialEq)] -#[repr(i64)] enum DrawMode { Draw(u32), Erase(u32), @@ -55,6 +54,24 @@ impl DrawMode { } } +impl From for i32 { + fn from(mode: DrawMode) -> Self { + match mode { + DrawMode::Draw(s) => s as i32, + DrawMode::Erase(s) => -(s as i32), + } + } +} +impl From for DrawMode { + fn from(mode: i32) -> Self { + if mode >= 0 { + Self::Draw(mode as u32) + } else { + Self::Erase((-mode) as u32) + } + } +} + #[derive(Copy, Clone, PartialEq, Debug)] enum TouchMode { OnlyUI, @@ -88,14 +105,14 @@ impl fmt::Display for TouchMode { } } -impl Into for TouchMode { - fn into(self) -> u8 { - match self { - Self::OnlyUI => 1, - Self::Bezier => 2, - Self::Circles => 3, - Self::Diamonds => 4, - Self::FillDiamonds => 5, +impl From for u8 { + fn from(mode: TouchMode) -> Self { + match mode { + TouchMode::OnlyUI => 1, + TouchMode::Bezier => 2, + TouchMode::Circles => 3, + TouchMode::Diamonds => 4, + TouchMode::FillDiamonds => 5, } } } @@ -125,8 +142,7 @@ const CANVAS_REGION: mxcfb_rect = mxcfb_rect { type PointAndPressure = (cgmath::Point2, i32); static G_TOUCH_MODE: Lazy = Lazy::new(|| AtomicU8::new(TouchMode::OnlyUI.into())); -static G_DRAW_MODE: Lazy> = - Lazy::new(|| atomic::Atomic::new(DrawMode::Draw(2))); +static G_DRAW_MODE: Lazy = Lazy::new(|| AtomicI32::new(DrawMode::Draw(2).into())); static UNPRESS_OBSERVED: Lazy = Lazy::new(|| AtomicBool::new(false)); static WACOM_IN_RANGE: Lazy = Lazy::new(|| AtomicBool::new(false)); static WACOM_RUBBER_SIDE: Lazy = Lazy::new(|| AtomicBool::new(false)); @@ -335,11 +351,11 @@ fn on_touch_rustlogo(app: &mut appctx::ApplicationContext<'_>, _element: UIEleme } fn on_toggle_eraser(app: &mut appctx::ApplicationContext<'_>, _: UIElementHandle) { - let (new_mode, name) = match G_DRAW_MODE.load(Ordering::Relaxed) { + let (new_mode, name) = match G_DRAW_MODE.load(Ordering::Relaxed).into() { DrawMode::Erase(s) => (DrawMode::Draw(s), "Black".to_owned()), DrawMode::Draw(s) => (DrawMode::Erase(s), "White".to_owned()), }; - G_DRAW_MODE.store(new_mode, Ordering::Relaxed); + G_DRAW_MODE.store(new_mode.into(), Ordering::Relaxed); let indicator = app.get_element_by_name("colorIndicator"); if let UIElement::Text { ref mut text, .. } = indicator.unwrap().write().inner { @@ -424,7 +440,7 @@ fn draw_color_test_rgb(app: &mut appctx::ApplicationContext<'_>, _element: UIEle } fn change_brush_width(app: &mut appctx::ApplicationContext<'_>, delta: i32) { - let current = G_DRAW_MODE.load(Ordering::Relaxed); + let current = DrawMode::from(G_DRAW_MODE.load(Ordering::Relaxed)); let current_size = current.get_size() as i32; let proposed_size = current_size + delta; let new_size = proposed_size.clamp(1, 99); @@ -432,7 +448,7 @@ fn change_brush_width(app: &mut appctx::ApplicationContext<'_>, delta: i32) { return; } - G_DRAW_MODE.store(current.set_size(new_size as u32), Ordering::Relaxed); + G_DRAW_MODE.store(current.set_size(new_size as u32).into(), Ordering::Relaxed); let element = app.get_element_by_name("displaySize").unwrap(); if let UIElement::Text { ref mut text, .. } = element.write().inner { @@ -496,7 +512,7 @@ fn on_wacom_input(app: &mut appctx::ApplicationContext<'_>, input: input::WacomE return; } - let (mut col, mut mult) = match G_DRAW_MODE.load(Ordering::Relaxed) { + let (mut col, mut mult) = match G_DRAW_MODE.load(Ordering::Relaxed).into() { DrawMode::Draw(s) => (color::BLACK, s), DrawMode::Erase(s) => (color::WHITE, s * 3), }; @@ -902,7 +918,7 @@ fn main() { onclick: None, inner: UIElement::Text { foreground: color::BLACK, - text: G_DRAW_MODE.load(Ordering::Relaxed).color_as_string(), + text: DrawMode::from(G_DRAW_MODE.load(Ordering::Relaxed)).color_as_string(), scale: 40.0, border_px: 0, }, @@ -952,7 +968,10 @@ fn main() { refresh: UIConstraintRefresh::Refresh, inner: UIElement::Text { foreground: color::BLACK, - text: format!("size: {0}", G_DRAW_MODE.load(Ordering::Relaxed).get_size()), + text: format!( + "size: {0}", + DrawMode::from(G_DRAW_MODE.load(Ordering::Relaxed)).get_size() + ), scale: 45.0, border_px: 0, },