crankstart icon indicating copy to clipboard operation
crankstart copied to clipboard

Expose bindings for the Playdate System Menu API

Open adamsoutar opened this issue 2 years ago • 4 comments

👋 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 🙂

adamsoutar avatar Apr 24 '22 22:04 adamsoutar

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?

rtsuk avatar Apr 25 '22 00:04 rtsuk

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

  • Fires the menu item callback
  • Closes the system menu
  • Calls eventHandler() with kEventResume

in that order

adamsoutar avatar Apr 25 '22 08:04 adamsoutar

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 avatar Apr 25 '22 13:04 rtsuk

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 avatar Apr 25 '22 13:04 rtsuk

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

rtsuk avatar Feb 26 '23 17:02 rtsuk

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?

tjkirch avatar Feb 26 '23 21:02 tjkirch

Go for it!

rtsuk avatar Feb 27 '23 03:02 rtsuk