Peoplemon  0.1.0
Peoplemon 3 game source documentation
ItemSelector.cpp
Go to the documentation of this file.
2 
3 namespace editor
4 {
5 namespace component
6 {
7 std::vector<core::item::Id> ItemSelector::idLookup;
8 
10 
11 ItemSelector::ItemSelector(const ChangeCb& cb)
12 : ComboBox() {
13  refresh();
14  getSignal(bl::gui::Event::ValueChanged)
15  .willAlwaysCall([this, cb](const bl::gui::Event&, bl::gui::Element*) {
16  if (cb) cb(currentItem());
17  });
18  setMaxHeight(300.f);
19 }
20 
22  return getSelectedOption() >= 0 && !idLookup.empty() ? idLookup[getSelectedOption()] :
24 }
25 
27  setSelectedOption(core::item::Item::getName(item), false);
28 }
29 
31  const core::item::Id ci = currentItem();
32 
33  idLookup.clear();
34  idLookup.reserve(core::item::Item::validIds().size());
35  clearOptions();
36 
37  int i = 0;
38  for (const core::item::Id item : core::item::Item::validIds()) {
39  idLookup.push_back(item);
40  addOption(core::item::Item::getName(item));
41  if (item == ci) { setSelectedOption(i, false); }
42  ++i;
43  }
44 
45  if (ci == core::item::Id::Unknown) setSelectedOption(0, false);
46 }
47 
48 } // namespace component
49 } // namespace editor
Id
Represents an item in its simplist form.
Definition: Id.hpp:24
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
static const std::string & getName(item::Id item)
Returns the name of the given item.
Definition: Item.cpp:91
static const std::vector< Id > & validIds()
Returns the list of valid item ids.
Definition: Item.cpp:125
Wrapper over ComboBox that allows selection of any item in the item database.
void setItem(core::item::Id item)
Set the current item.
core::item::Id currentItem() const
Returns the current selected item.
void refresh()
Refreshes the items from the item database.
std::function< void(core::item::Id)> ChangeCb
Called when the item changes.
std::shared_ptr< ItemSelector > Ptr
Pointer to this component.
static Ptr create(const ChangeCb &cb=[](core::item::Id) {})
Creates a new ItemSelector.
Definition: ItemSelector.cpp:9