Skip to content

Commit

Permalink
Same idea again + drop atomic crate
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <[email protected]>
  • Loading branch information
fenollp committed Jul 27, 2024
1 parent 4f09f1b commit 6729ca3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
57 changes: 38 additions & 19 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -55,6 +54,24 @@ impl DrawMode {
}
}

impl From<DrawMode> for i32 {
fn from(mode: DrawMode) -> Self {
match mode {
DrawMode::Draw(s) => s as i32,
DrawMode::Erase(s) => -(s as i32),
}
}
}
impl From<i32> 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,
Expand Down Expand Up @@ -88,14 +105,14 @@ impl fmt::Display for TouchMode {
}
}

impl Into<u8> 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<TouchMode> for u8 {
fn from(mode: TouchMode) -> Self {
match mode {
TouchMode::OnlyUI => 1,
TouchMode::Bezier => 2,
TouchMode::Circles => 3,
TouchMode::Diamonds => 4,
TouchMode::FillDiamonds => 5,
}
}
}
Expand Down Expand Up @@ -125,8 +142,7 @@ const CANVAS_REGION: mxcfb_rect = mxcfb_rect {
type PointAndPressure = (cgmath::Point2<f32>, i32);

static G_TOUCH_MODE: Lazy<AtomicU8> = Lazy::new(|| AtomicU8::new(TouchMode::OnlyUI.into()));
static G_DRAW_MODE: Lazy<atomic::Atomic<DrawMode>> =
Lazy::new(|| atomic::Atomic::new(DrawMode::Draw(2)));
static G_DRAW_MODE: Lazy<AtomicI32> = Lazy::new(|| AtomicI32::new(DrawMode::Draw(2).into()));
static UNPRESS_OBSERVED: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));
static WACOM_IN_RANGE: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));
static WACOM_RUBBER_SIDE: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -424,15 +440,15 @@ 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);
if new_size == current_size {
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 {
Expand Down Expand Up @@ -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),
};
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down

0 comments on commit 6729ca3

Please sign in to comment.