-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add menu item api * MenuItemKind implementation to add some protection around the checkbox and options items * Removing unused menuItem handling constants * Resolve soundness issue with dropping menu items * clarify drop handling of menu items * `System::remove_all_menu_items` no longer makes sense, removed * remove menu_item tests from example
- Loading branch information
Showing
3 changed files
with
308 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#![no_std] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::rc::Rc; | ||
use alloc::string::String; | ||
use alloc::vec; | ||
use alloc::vec::Vec; | ||
use core::cell::RefCell; | ||
|
||
use hashbrown::HashMap; | ||
|
||
use crankstart::system::MenuItemKind; | ||
use { | ||
alloc::boxed::Box, | ||
anyhow::Error, | ||
crankstart::{ | ||
crankstart_game, | ||
geometry::ScreenPoint, | ||
graphics::{Graphics, LCDColor, LCDSolidColor}, | ||
log_to_console, | ||
system::{MenuItem, System}, | ||
Game, Playdate, | ||
}, | ||
euclid::point2, | ||
}; | ||
|
||
struct State { | ||
_menu_items: Rc<RefCell<HashMap<&'static str, MenuItem>>>, | ||
text_location: ScreenPoint, | ||
} | ||
|
||
impl State { | ||
pub fn new(_playdate: &Playdate) -> Result<Box<Self>, Error> { | ||
crankstart::display::Display::get().set_refresh_rate(20.0)?; | ||
let menu_items = Rc::new(RefCell::new(HashMap::new())); | ||
let system = System::get(); | ||
let normal_item = { | ||
system.add_menu_item( | ||
"Select Me", | ||
Box::new(|| { | ||
log_to_console!("Normal option picked"); | ||
}), | ||
)? | ||
}; | ||
let checkmark_item = { | ||
let ref_menu_items = menu_items.clone(); | ||
system.add_checkmark_menu_item( | ||
"Toggle Me", | ||
false, | ||
Box::new(move || { | ||
let value_of_item = { | ||
let menu_items = ref_menu_items.borrow(); | ||
let this_menu_item = menu_items.get("checkmark").unwrap(); | ||
System::get().get_menu_item_value(this_menu_item).unwrap() != 0 | ||
}; | ||
log_to_console!("Checked option picked: Value is now: {}", value_of_item); | ||
}), | ||
)? | ||
}; | ||
let options_item = { | ||
let ref_menu_items = menu_items.clone(); | ||
let options: Vec<String> = vec!["Small".into(), "Medium".into(), "Large".into()]; | ||
system.add_options_menu_item( | ||
"Size", | ||
options, | ||
Box::new(move || { | ||
let value_of_item = { | ||
let menu_items = ref_menu_items.borrow(); | ||
let this_menu_item = menu_items.get("options").unwrap(); | ||
let idx = System::get().get_menu_item_value(this_menu_item).unwrap(); | ||
match &this_menu_item.kind { | ||
MenuItemKind::Options(opts) => opts.get(idx).map(|s| s.clone()), | ||
_ => None, | ||
} | ||
}; | ||
log_to_console!("Checked option picked: Value is now {:?}", value_of_item); | ||
}), | ||
)? | ||
}; | ||
{ | ||
let mut menu_items = menu_items.borrow_mut(); | ||
menu_items.insert("normal", normal_item); | ||
menu_items.insert("checkmark", checkmark_item); | ||
menu_items.insert("options", options_item); | ||
} | ||
Ok(Box::new(Self { | ||
_menu_items: menu_items, | ||
text_location: point2(100, 100), | ||
})) | ||
} | ||
} | ||
|
||
impl Game for State { | ||
fn update(&mut self, _playdate: &mut Playdate) -> Result<(), Error> { | ||
let graphics = Graphics::get(); | ||
graphics.clear(LCDColor::Solid(LCDSolidColor::kColorWhite))?; | ||
graphics | ||
.draw_text("Menu Items", self.text_location) | ||
.unwrap(); | ||
|
||
System::get().draw_fps(0, 0)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
crankstart_game!(State); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters