pd-rs / crankstart

A barely functional, wildly incomplete and basically undocumented Rust crate whose aim is to let you write Games for the Playdate handheld gaming system in Rust.
MIT License
235 stars 24 forks source link

Expose bindings for the Playdate System Menu API #16

Closed adamsoutar closed 1 year ago

adamsoutar commented 2 years ago

👋 This PR adds bindings for interacting with the System Menu.

For example:

unsafe extern "C" fn callback (_: *mut c_void) {
    log_to_console!("Hello, system menu!")
}

system.add_menu_item("say hi", Some(callback))?;

It also supports "checkmark" and "options" menu items:

let checkbox = system.add_checkmark_menu_item("cool?", true, Some(callback))?;

let is_cool = checkbox.get_checked()?; // true/false

let options = system.add_options_menu_item("choice", vec![
    "one", "two", "three"
], Some(callback))?; 

let selected_index = options.get_value(); // 0, 1, or 2

Totally open to feedback/thoughts on this one since it's a bit bigger than my previous PRs and involves a bit of API design 🙂

rtsuk commented 2 years ago

I feel we need to figure out a safe mechanism to put in place of the unsafe callback before adding this.

Does the SKD doc say anything about when this callback will be called, relative to the other callbacks Crankstart is already handling?

adamsoutar commented 2 years ago

Hmm, I think you're right. Initially I couldn't figure out a way to pass in a Rust function with any useful context since closures etc. can't be passed as simple function pointers. However, this SO question seems to provide some ways to do it - I'll look into it.

In terms of timing, the docs say that pressing a menu item

in that order

rtsuk commented 2 years ago

As long as we are reasonably certain that none of the other callbacks ever get called while the menu is open, we could handle it the way we handle the other callbacks and perhaps add a method to the Game trait.

rtsuk commented 2 years ago

Reading the system menu docs I think that there are two ways to go; either make menu items like sprites or design an API for Rust that doesn't require an immediate callback per item and keep all the references to menu items internal to Crankstart.

rtsuk commented 1 year ago

Closing this as it is quite old. Please feel free to re-open.

tjkirch commented 1 year ago

I'd really like to have system menu access for my game. @rtsuk would you mind if I create an issue to track this, and try to summarize the issues under consideration above?

rtsuk commented 1 year ago

Go for it!