diff --git a/ctru-rs/examples/touch-screen.rs b/ctru-rs/examples/touch-screen.rs new file mode 100644 index 0000000..204f324 --- /dev/null +++ b/ctru-rs/examples/touch-screen.rs @@ -0,0 +1,49 @@ +use ctru::prelude::*; + +fn main() { + ctru::use_panic_handler(); + + let gfx = Gfx::new().expect("Couldn't obtain GFX controller"); + let mut hid = Hid::new().expect("Couldn't obtain HID controller"); + let apt = Apt::new().expect("Couldn't obtain APT controller"); + + let console = Console::new(gfx.top_screen.borrow_mut()); + + // We'll hold the previous touch position for comparison. + let mut old_touch: (u16, u16) = (0, 0); + + println!("\x1b[29;16HPress Start to exit"); + + while apt.main_loop() { + hid.scan_input(); + + if hid.keys_down().contains(KeyPad::START) { + break; + } + + // Get X and Y coordinates of the touch point. + // The touch screen is 320x240. + let touch: (u16, u16) = hid.touch_position(); + + // We only want to print the position when it's different + // from what it was on the previous frame + if touch != old_touch { + // Special case for when the user lifts the stylus/finger from the screen. + // This is done to avoid some screen tearing. + if touch == (0, 0) { + console.clear(); + + // Print again because we just cleared the screen + println!("\x1b[29;16HPress Start to exit"); + } + + // Move the cursor back to the top of the screen and print the coordinates + print!("\x1b[1;1HTouch Screen position: {:#?}", touch); + } + + // Save our current touch position for the next frame + old_touch = touch; + + gfx.wait_for_vblank(); + } +} diff --git a/ctru-rs/src/services/hid.rs b/ctru-rs/src/services/hid.rs index 79a649b..fa91657 100644 --- a/ctru-rs/src/services/hid.rs +++ b/ctru-rs/src/services/hid.rs @@ -46,14 +46,6 @@ bitflags::bitflags! { /// This service requires no special permissions to use. pub struct Hid(()); -/// Represents user input to the touchscreen. -#[derive(Debug, Clone, Copy)] -pub struct TouchPosition(ctru_sys::touchPosition); - -/// Represents the current position of the 3DS circle pad. -#[derive(Debug, Clone, Copy)] -pub struct CirclePosition(ctru_sys::circlePosition); - /// Initializes the HID service. /// /// # Errors @@ -102,47 +94,33 @@ impl Hid { KeyPad::from_bits_truncate(keys) } } -} - -impl Default for TouchPosition { - fn default() -> Self { - TouchPosition(ctru_sys::touchPosition { px: 0, py: 0 }) - } -} -impl TouchPosition { - /// Create a new TouchPosition instance. - pub fn new() -> Self { - Self::default() - } + /// Returns the current touch position in pixels (x, y). + /// + /// # Notes + /// + /// (0, 0) represents the top left corner of the screen. + pub fn touch_position(&mut self) -> (u16, u16) { + let mut res = ctru_sys::touchPosition { px: 0, py: 0 }; - /// Returns the current touch position in pixels. - pub fn get(&mut self) -> (u16, u16) { unsafe { - ctru_sys::hidTouchRead(&mut self.0); + ctru_sys::hidTouchRead(&mut res); } - (self.0.px, self.0.py) - } -} - -impl Default for CirclePosition { - fn default() -> Self { - CirclePosition(ctru_sys::circlePosition { dx: 0, dy: 0 }) + (res.px, res.py) } -} -impl CirclePosition { - /// Create a new CirclePosition instance. - pub fn new() -> Self { - Self::default() - } + /// Returns the current circle pad position in relative (x, y). + /// + /// # Notes + /// + /// (0, 0) represents the center of the circle pad. + pub fn circlepad_position(&mut self) -> (i16, i16) { + let mut res = ctru_sys::circlePosition { dx: 0, dy: 0 }; - /// Returns the current circle pad position in (x, y) form. - pub fn get(&mut self) -> (i16, i16) { unsafe { - ctru_sys::hidCircleRead(&mut self.0); + ctru_sys::hidCircleRead(&mut res); } - (self.0.dx, self.0.dy) + (res.dx, res.dy) } }