UGDK  0.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
menu.h
Go to the documentation of this file.
1 #ifndef UGDK_UI_MENU_H_
2 #define UGDK_UI_MENU_H_
3 
4 #include <string>
5 #include <ugdk/portable/tr1.h>
6 #include FROM_TR1(functional)
7 #include <map>
8 #include <ugdk/math/vector2D.h>
9 #include <ugdk/action.h>
10 #include <ugdk/graphic.h>
11 #include <ugdk/util.h>
12 #include <ugdk/ui.h>
13 #include <ugdk/action/entity.h>
14 #include <ugdk/graphic/node.h>
15 #include <ugdk/graphic/drawable.h>
16 #include <ugdk/input.h>
17 #include <ugdk/input/keys.h>
18 #include <pyramidworks/collision.h>
19 
20 namespace ugdk {
21 namespace ui {
22 //typedef std::list<const UIElement *> UICollisionList;
23 
24 typedef std::tr1::function<void (Menu*)> MenuCallback;
25 typedef std::map<input::Key, MenuCallback > InputCallbacks;
26 
27 class Menu: public action::Entity {
29  public:
30  Menu(const ikdtree::Box<2>& tree_bounding_box, const Vector2D& offset, const graphic::Drawable::HookPoint& hook = graphic::Drawable::TOP_LEFT);
31  ~Menu();
32 
33  void Update(double dt);
34  void OnSceneAdd(action::Scene* scene);
35 
36  std::vector<UIElement *>* GetMouseCollision();
37 
38  void AddCallback(input::Key key, MenuCallback callback) {
39  input_callbacks_[key] = callback;
40  }
41  void SetOptionDrawable(graphic::Drawable* option_graphic, int index = 0) {
42  if(!option_node_[index]) option_node_[index] = new graphic::Node;
43  option_node_[index]->set_drawable(option_graphic);
44  }
45 
46  void AddObject(UIElement* obj);
47  void RemoveObject(UIElement* obj);
48  void RefreshObject(UIElement* obj);
49 
50  graphic::Node* node() { return node_; }
51  const UIElement* focused_element() const { return focused_element_; }
52 
53  const InputCallbacks& input_callbacks() const { return input_callbacks_; }
54 
55  void FinishScene() const;
56 
57  const static MenuCallback FINISH_MENU;
59 
60  private:
61  void CheckInteraction(const Vector2D& mouse_pos);
62  void InteractWithFocused();
63  void SelectUIElement(UIElement* target);
64  void FocusNextElement(int offset);
65  void PositionSelectionDrawables();
66 
67  graphic::Node* node_;
68  graphic::Node* option_node_[2];
69  action::Scene* owner_scene_;
70  Vector2D last_mouse_position_;
71  UIElement* focused_element_;
72  std::list< UIElement* > uielements_;
73  ObjectTree* objects_tree_;
74  InputCallbacks input_callbacks_;
76 };
77 
78 } // namespace ui
79 } // namespace ugdk
80 
81 #endif /* UGDK_UI_MENU_H_ */