Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TODO: Item Events Handling is awkard #26

Open
john32b opened this issue Apr 21, 2023 · 2 comments
Open

TODO: Item Events Handling is awkard #26

john32b opened this issue Apr 21, 2023 · 2 comments

Comments

@john32b
Copy link
Owner

john32b commented Apr 21, 2023

Right now, if you just want to get an FlxMenu items trigger callback you have to do something like this:

menu.onItemEvent = (itemEvent, itemData)->{
  if(itemEvent == fire)
  {
    if(itemData.ID == "something")
    {
      handle_item_clicked();
    }
    if(itemData.ID == "something_other")
    {
      handle_item_other_clicked();
    }
    // etc
  }
};

For big menus with many items I guess this can work, but if you only want to handle a single item this code can get a bit ugly or cumbersome.

Improvement

  • Adding callback functions to the menu items themselves, something like this:
menu.pages.get('main').get('options').onPress = (itemData)->{
    trace('OK item with id ${itemData.ID} triggered');
}

So you are adding the callback directly to the MItemData object
A quicker way would be for the FlxMenu to have the last page added on standby

menu.getItem('options').onPress = (itemData)->{ ... }
// Here getItem searches the last added MenuPage
  • The same principle would apply for the rest of the ItemEvents
menu.getItem('options').onFocus= (itemData)->{ /* Item was focused! */ }
menu.getItem('volume').onChange= (itemData)->{ /* Item value was changed! */ }
@T1mL3arn
Copy link

Just want to mention I don't find it awkward when using with switch:

menu.onMenuEvent = (e, id) -> {
  switch ([e, id]) {
    case [it_fire, 'create_lobby']:
      // do creation
    case [it_fire, 'connect_to_lobby']:
      // do connecting
  }
}

menu.onItemEvent = (e, item) -> {
  switch([e, item.ID]) {
    case [change, 'ai_smarteness']:
      // change ai brains
    case [change, 'player_pos']:
      // update player pos
  }
}

@john32b
Copy link
Owner Author

john32b commented Apr 21, 2023

hey, Haxe Array matching! hadn't thought of that, indeed it looks cleaner.
but if I add this, it will not replace anything, it will be an extra option to have if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants