john32b/djFlixel

TODO: Item Events Handling is awkard

Opened this issue · 2 comments

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! */ }

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
  }
}

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.