UGDK
|
00001 #ifndef UGDK_UI_MENU_H_ 00002 #define UGDK_UI_MENU_H_ 00003 00004 #include <string> 00005 #include <ugdk/portable/tr1.h> 00006 #include FROM_TR1(functional) 00007 #include <map> 00008 #include <ugdk/math/vector2D.h> 00009 #include <ugdk/action.h> 00010 #include <ugdk/graphic.h> 00011 #include <ugdk/util.h> 00012 #include <ugdk/ui.h> 00013 #include <ugdk/action/entity.h> 00014 #include <ugdk/graphic/node.h> 00015 #include <ugdk/graphic/drawable.h> 00016 #include <ugdk/input.h> 00017 #include <ugdk/input/keys.h> 00018 #include <pyramidworks/collision.h> 00019 00020 namespace ugdk { 00021 namespace ui { 00022 //typedef std::list<const UIElement *> UICollisionList; 00023 00024 typedef std::tr1::function<void (Menu*)> MenuCallback; 00025 typedef std::map<input::Key, MenuCallback > InputCallbacks; 00026 00027 class Menu: public action::Entity { 00028 typedef ikdtree::IntervalKDTree<UIElement*, 2> ObjectTree; 00029 public: 00030 Menu(const ikdtree::Box<2>& tree_bounding_box, const Vector2D& offset, const graphic::Drawable::HookPoint& hook = graphic::Drawable::TOP_LEFT); 00031 ~Menu(); 00032 00033 void Update(double dt); 00034 void OnSceneAdd(action::Scene* scene); 00035 00036 std::vector<UIElement *>* GetMouseCollision(); 00037 00038 void AddCallback(input::Key key, MenuCallback callback) { 00039 input_callbacks_[key] = callback; 00040 } 00041 void SetOptionDrawable(graphic::Drawable* option_graphic, int index = 0) { 00042 if(!option_node_[index]) option_node_[index] = new graphic::Node; 00043 option_node_[index]->set_drawable(option_graphic); 00044 } 00045 00046 void AddObject(UIElement* obj); 00047 void RemoveObject(UIElement* obj); 00048 void RefreshObject(UIElement* obj); 00049 00050 graphic::Node* node() { return node_; } 00051 const UIElement* focused_element() const { return focused_element_; } 00052 00053 const InputCallbacks& input_callbacks() const { return input_callbacks_; } 00054 00055 void FinishScene() const; 00056 00057 const static MenuCallback FINISH_MENU; 00058 const static MenuCallback INTERACT_MENU; 00059 00060 private: 00061 void CheckInteraction(const Vector2D& mouse_pos); 00062 void InteractWithFocused(); 00063 void SelectUIElement(UIElement* target); 00064 void FocusNextElement(int offset); 00065 void PositionSelectionDrawables(); 00066 00067 graphic::Node* node_; 00068 graphic::Node* option_node_[2]; 00069 action::Scene* owner_scene_; 00070 Vector2D last_mouse_position_; 00071 UIElement* focused_element_; 00072 std::list< UIElement* > uielements_; 00073 ObjectTree* objects_tree_; 00074 InputCallbacks input_callbacks_; 00075 graphic::Drawable::HookPoint hook_; 00076 }; 00077 00078 } // namespace ui 00079 } // namespace ugdk 00080 00081 #endif /* UGDK_UI_MENU_H_ */