From f70d1c9706c8fa8090e38558cf9ed7fc361c82fa Mon Sep 17 00:00:00 2001 From: Fenrir Date: Sat, 29 Jul 2017 15:39:11 -0600 Subject: [PATCH] Update examples --- examples/src/bin/buttons.rs | 75 ++++++++++++++++++++++++++ examples/src/bin/hello-both-screens.rs | 4 +- examples/src/bin/hello-world.rs | 4 +- 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 examples/src/bin/buttons.rs diff --git a/examples/src/bin/buttons.rs b/examples/src/bin/buttons.rs new file mode 100644 index 0000000..00dd33e --- /dev/null +++ b/examples/src/bin/buttons.rs @@ -0,0 +1,75 @@ +extern crate ctru; + +use ctru::gfx::Gfx; +use ctru::console::Console; +use ctru::services::apt::Apt; +use ctru::services::hid::{self, Hid, KeyPad}; + +fn main() { + // Setup services + let apt = Apt::init().unwrap(); + let hid = Hid::init().unwrap(); + let mut gfx = Gfx::default(); + let mut console = Console::default(); + + println!("Hi there! Try pressing a button"); + println!("\x1b[29;16HPress Start to exit"); + + // This struct will contain the keys that we held on the previous frame + let mut old_keys = KeyPad::empty(); + + while apt.main_loop() { + // Scan for user input on the current frame. + hid.scan_input(); + + // Get information about which keys were held down on this frame + let keys = hid.keys_held(); + + // We only want to print when the keys we're holding now are different + // from what they were on the previous frame + if keys != old_keys { + + // Clear the screen + console.clear(); + + // We print these again because we just cleared the screen above + println!("Hi there! Try pressing a button"); + println!("\x1b[29;16HPress Start to exit"); + + // Move the cursor back to the top of the screen + println!("\x1b[3;0H"); + + // Print to the screen depending on which keys were held. + // + // The .contains() method checks for all of the provided keys, + // and the .intersects() method checks for any of the provided keys. + // + // You can also use the .bits() method to do direct comparisons on + // the underlying bits + + if keys.contains(hid::KEY_A) { + println!("You held A!"); + } + if keys.bits() & hid::KEY_B.bits() != 0 { + println!("You held B!"); + } + if keys.contains(hid::KEY_X | hid::KEY_Y) { + println!("You held X and Y!"); + } + if keys.intersects(hid::KEY_L | hid::KEY_R | hid::KEY_ZL | hid::KEY_ZR) { + println!("You held a shoulder button!"); + } + if keys.intersects(hid::KEY_START) { + println!("See ya!"); + break + } + } + + // Save our current key presses for the next frame + old_keys = keys; + + // Flush and swap framebuffers + gfx.flush_buffers(); + gfx.swap_buffers(); + } +} diff --git a/examples/src/bin/hello-both-screens.rs b/examples/src/bin/hello-both-screens.rs index ade33d4..627d9b0 100644 --- a/examples/src/bin/hello-both-screens.rs +++ b/examples/src/bin/hello-both-screens.rs @@ -3,7 +3,7 @@ extern crate ctru; use ctru::gfx::{Gfx, Screen}; use ctru::console::Console; use ctru::services::apt::Apt; -use ctru::services::hid::{Hid, PadKey}; +use ctru::services::hid::{self, Hid}; fn main() { // Initialize services @@ -35,7 +35,7 @@ fn main() { gfx.swap_buffers(); hid.scan_input(); - if hid.key_down(PadKey::Start) { + if hid.keys_down().contains(hid::KEY_START) { break; } } diff --git a/examples/src/bin/hello-world.rs b/examples/src/bin/hello-world.rs index cb63510..6daee98 100644 --- a/examples/src/bin/hello-world.rs +++ b/examples/src/bin/hello-world.rs @@ -3,7 +3,7 @@ extern crate ctru; use ctru::gfx::Gfx; use ctru::console::Console; use ctru::services::apt::Apt; -use ctru::services::hid::{Hid, PadKey}; +use ctru::services::hid::{self, Hid}; fn main() { // Initialize ctrulib service handles. @@ -45,7 +45,7 @@ fn main() { hid.scan_input(); // Check if the user has pressed the given button on this frame. // If so, break out of the loop. - if hid.key_down(PadKey::Start) { + if hid.keys_down().contains(hid::KEY_START) { break; } }