Browse Source

Use bitflags 1.0 for button presses

pull/10/head
Fenrir 7 years ago committed by FenrirWolf
parent
commit
fc0e124f43
  1. 4
      ctru-rs/Cargo.toml
  2. 21
      ctru-rs/src/services/fs.rs
  3. 8
      ctru-rs/src/services/hid.rs
  4. 14
      examples/src/bin/buttons.rs
  5. 8
      examples/src/bin/hello-both-screens.rs
  6. 4
      examples/src/bin/hello-world.rs

4
ctru-rs/Cargo.toml

@ -4,7 +4,7 @@ description = "A safe wrapper around smealum's ctrulib."
license = "https://en.wikipedia.org/wiki/Zlib_License" license = "https://en.wikipedia.org/wiki/Zlib_License"
name = "ctru-rs" name = "ctru-rs"
links = "ctru" links = "ctru"
version = "0.6.0" version = "0.7.0"
[lib] [lib]
crate-type = ["rlib"] crate-type = ["rlib"]
@ -15,7 +15,7 @@ path = "../ctru-sys"
version = "0.4" version = "0.4"
[dependencies.bitflags] [dependencies.bitflags]
version = "0.9.0" version = "1.0.0"
[dependencies.widestring] [dependencies.widestring]
version = "0.2.2" version = "0.2.2"

21
ctru-rs/src/services/fs.rs

@ -455,7 +455,7 @@ impl File {
self.offset, self.offset,
buf.as_ptr() as _, buf.as_ptr() as _,
buf.len() as u32, buf.len() as u32,
FS_WRITE_UPDATE_TIME.bits() FsWrite::FS_WRITE_UPDATE_TIME.bits()
); );
self.offset += n_written as u64; self.offset += n_written as u64;
if r < 0 { if r < 0 {
@ -470,7 +470,7 @@ impl File {
impl Metadata { impl Metadata {
/// Returns whether this metadata is for a directory. /// Returns whether this metadata is for a directory.
pub fn is_dir(&self) -> bool { pub fn is_dir(&self) -> bool {
self.attributes == self.attributes | FS_ATTRIBUTE_DIRECTORY.bits() self.attributes == self.attributes | FsAttribute::FS_ATTRIBUTE_DIRECTORY.bits()
} }
/// Returns whether this metadata is for a regular file. /// Returns whether this metadata is for a regular file.
@ -613,12 +613,13 @@ impl OpenOptions {
fn get_open_flags(&self) -> FsOpen { fn get_open_flags(&self) -> FsOpen {
match (self.read, self.write || self.append, self.create) { match (self.read, self.write || self.append, self.create) {
(true, false, false) => FS_OPEN_READ, (true, false, false) => FsOpen::FS_OPEN_READ,
(false, true, false) => FS_OPEN_WRITE, (false, true, false) => FsOpen::FS_OPEN_WRITE,
(false, true, true) => FS_OPEN_WRITE | FS_OPEN_CREATE, (false, true, true) => FsOpen::FS_OPEN_WRITE | FsOpen::FS_OPEN_CREATE,
(true, false, true) => FS_OPEN_READ | FS_OPEN_CREATE, (true, false, true) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_CREATE,
(true, true, false) => FS_OPEN_READ | FS_OPEN_WRITE, (true, true, false) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_WRITE,
(true, true, true) => FS_OPEN_READ | FS_OPEN_WRITE | FS_OPEN_CREATE, (true, true, true) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_WRITE |
FsOpen::FS_OPEN_CREATE,
_ => FsOpen::empty(), //failure case _ => FsOpen::empty(), //failure case
} }
} }
@ -688,7 +689,7 @@ pub fn create_dir<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<()> {
let path = to_utf16(path.as_ref()); let path = to_utf16(path.as_ref());
let fs_path = ::libctru::fsMakePath(PathType::UTF16.into(), path.as_ptr() as _); let fs_path = ::libctru::fsMakePath(PathType::UTF16.into(), path.as_ptr() as _);
let r = ::libctru::FSUSER_CreateDirectory(arch.handle, fs_path, let r = ::libctru::FSUSER_CreateDirectory(arch.handle, fs_path,
FS_ATTRIBUTE_DIRECTORY.bits()); FsAttribute::FS_ATTRIBUTE_DIRECTORY.bits());
if r < 0 { if r < 0 {
Err(IoError::new(IoErrorKind::Other, ::Error::from(r))) Err(IoError::new(IoErrorKind::Other, ::Error::from(r)))
} else { } else {
@ -725,7 +726,7 @@ pub fn metadata<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<Metadata> {
let maybe_dir = read_dir(&arch, path.as_ref()); let maybe_dir = read_dir(&arch, path.as_ref());
match (maybe_file, maybe_dir) { match (maybe_file, maybe_dir) {
(Ok(file), _) => file.metadata(), (Ok(file), _) => file.metadata(),
(_, Ok(_dir)) => Ok(Metadata { attributes: FS_ATTRIBUTE_DIRECTORY.bits(), size: 0 }), (_, Ok(_dir)) => Ok(Metadata { attributes: FsAttribute::FS_ATTRIBUTE_DIRECTORY.bits(), size: 0 }),
(Err(e), _) => Err(e), (Err(e), _) => Err(e),
} }
} }

8
ctru-rs/src/services/hid.rs

@ -34,10 +34,10 @@ bitflags! {
const KEY_CPAD_UP = 1u32 << 30; const KEY_CPAD_UP = 1u32 << 30;
const KEY_CPAD_DOWN = 1u32 << 31; const KEY_CPAD_DOWN = 1u32 << 31;
// convenience catch-all for the dpad and cpad // convenience catch-all for the dpad and cpad
const KEY_UP = KEY_DUP.bits | KEY_CPAD_UP.bits; const KEY_UP = KeyPad::KEY_DUP.bits | KeyPad::KEY_CPAD_UP.bits;
const KEY_DOWN = KEY_DDOWN.bits | KEY_CPAD_DOWN.bits; const KEY_DOWN = KeyPad::KEY_DDOWN.bits | KeyPad::KEY_CPAD_DOWN.bits;
const KEY_LEFT = KEY_DLEFT.bits | KEY_CPAD_LEFT.bits; const KEY_LEFT = KeyPad::KEY_DLEFT.bits | KeyPad::KEY_CPAD_LEFT.bits;
const KEY_RIGHT = KEY_DRIGHT.bits | KEY_CPAD_RIGHT.bits; const KEY_RIGHT = KeyPad::KEY_DRIGHT.bits | KeyPad::KEY_CPAD_RIGHT.bits;
} }
} }

14
examples/src/bin/buttons.rs

@ -3,14 +3,14 @@ extern crate ctru;
use ctru::gfx::Gfx; use ctru::gfx::Gfx;
use ctru::console::Console; use ctru::console::Console;
use ctru::services::apt::Apt; use ctru::services::apt::Apt;
use ctru::services::hid::{self, Hid, KeyPad}; use ctru::services::hid::{Hid, KeyPad};
fn main() { fn main() {
// Setup services // Setup services
let apt = Apt::init().unwrap(); let apt = Apt::init().unwrap();
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();
let mut gfx = Gfx::default(); let mut gfx = Gfx::default();
let mut console = Console::default(); let console = Console::default();
println!("Hi there! Try pressing a button"); println!("Hi there! Try pressing a button");
println!("\x1b[29;16HPress Start to exit"); println!("\x1b[29;16HPress Start to exit");
@ -47,19 +47,19 @@ fn main() {
// You can also use the .bits() method to do direct comparisons on // You can also use the .bits() method to do direct comparisons on
// the underlying bits // the underlying bits
if keys.contains(hid::KEY_A) { if keys.contains(KeyPad::KEY_A) {
println!("You held A!"); println!("You held A!");
} }
if keys.bits() & hid::KEY_B.bits() != 0 { if keys.bits() & KeyPad::KEY_B.bits() != 0 {
println!("You held B!"); println!("You held B!");
} }
if keys.contains(hid::KEY_X | hid::KEY_Y) { if keys.contains(KeyPad::KEY_X | KeyPad::KEY_Y) {
println!("You held X and Y!"); println!("You held X and Y!");
} }
if keys.intersects(hid::KEY_L | hid::KEY_R | hid::KEY_ZL | hid::KEY_ZR) { if keys.intersects(KeyPad::KEY_L | KeyPad::KEY_R | KeyPad::KEY_ZL | KeyPad::KEY_ZR) {
println!("You held a shoulder button!"); println!("You held a shoulder button!");
} }
if keys.intersects(hid::KEY_START) { if keys.intersects(KeyPad::KEY_START) {
println!("See ya!"); println!("See ya!");
break break
} }

8
examples/src/bin/hello-both-screens.rs

@ -3,7 +3,7 @@ extern crate ctru;
use ctru::gfx::{Gfx, Screen}; use ctru::gfx::{Gfx, Screen};
use ctru::console::Console; use ctru::console::Console;
use ctru::services::apt::Apt; use ctru::services::apt::Apt;
use ctru::services::hid::{self, Hid}; use ctru::services::hid::{Hid, KeyPad};
fn main() { fn main() {
// Initialize services // Initialize services
@ -12,11 +12,11 @@ fn main() {
let mut gfx = Gfx::default(); let mut gfx = Gfx::default();
// Start a console on the top screen // Start a console on the top screen
let mut top_screen = Console::init(Screen::Top); let top_screen = Console::init(Screen::Top);
// Start a console on the bottom screen. // Start a console on the bottom screen.
// The most recently initialized console will be active by default // The most recently initialized console will be active by default
let mut bottom_screen = Console::init(Screen::Bottom); let bottom_screen = Console::init(Screen::Bottom);
// Let's print on the top screen first // Let's print on the top screen first
top_screen.select(); top_screen.select();
@ -35,7 +35,7 @@ fn main() {
gfx.swap_buffers(); gfx.swap_buffers();
hid.scan_input(); hid.scan_input();
if hid.keys_down().contains(hid::KEY_START) { if hid.keys_down().contains(KeyPad::KEY_START) {
break; break;
} }
} }

4
examples/src/bin/hello-world.rs

@ -3,7 +3,7 @@ extern crate ctru;
use ctru::gfx::Gfx; use ctru::gfx::Gfx;
use ctru::console::Console; use ctru::console::Console;
use ctru::services::apt::Apt; use ctru::services::apt::Apt;
use ctru::services::hid::{self, Hid}; use ctru::services::hid::{Hid, KeyPad};
fn main() { fn main() {
// Initialize ctrulib service handles. // Initialize ctrulib service handles.
@ -45,7 +45,7 @@ fn main() {
hid.scan_input(); hid.scan_input();
// Check if the user has pressed the given button on this frame. // Check if the user has pressed the given button on this frame.
// If so, break out of the loop. // If so, break out of the loop.
if hid.keys_down().contains(hid::KEY_START) { if hid.keys_down().contains(KeyPad::KEY_START) {
break; break;
} }
} }

Loading…
Cancel
Save