Skip to content

Commit

Permalink
Selection drawables now exist again.
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavoteixeira committed May 28, 2012
1 parent 242fa9c commit 2062200
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
25 changes: 21 additions & 4 deletions src/ugdk/ui/menu.cc
Expand Up @@ -7,6 +7,7 @@
#include <ugdk/action/generictask.h>
#include <ugdk/action/scene.h>
#include <ugdk/base/engine.h>
#include <ugdk/graphic/drawable.h>
#include <ugdk/graphic/node.h>
#include <ugdk/input/inputmanager.h>
#include <ugdk/util/intervalkdtree.h>
Expand Down Expand Up @@ -34,15 +35,20 @@ class CallbackCheckTask : public action::Task {
Menu::Menu(const ugdk::ikdtree::Box<2>& tree_bounding_box, const Vector2D& offset, action::Scene* owner_scene)
: objects_tree_(new ObjectTree(tree_bounding_box,5)),
owner_scene_(owner_scene),
node_(new graphic::Node()),
option_graphic_(new graphic::Node()) {
node_(new graphic::Node()) {
node_->modifier()->set_offset(offset);
option_node_[0] = NULL;
option_node_[1] = NULL;
//std::tr1::function<bool (double)> func = std::tr1::bind(&CheckMouse, this, _1);
//this->AddTask(new action::GenericTask(func));
//this->AddTask(new CallbackCheckTask(this));
}

Menu::~Menu() { delete objects_tree_; }
Menu::~Menu() {
if(option_node_[0]) delete option_node_[0];
if(option_node_[1]) delete option_node_[1];
delete objects_tree_;
}

void Menu::Update(double dt) {
input::InputManager* input = INPUT_MANAGER();
Expand All @@ -51,7 +57,9 @@ void Menu::Update(double dt) {
std::vector<UIElement *> *intersecting_uielements = GetMouseCollision();
if(intersecting_uielements->size() > 0) {
focused_element_ = (*intersecting_uielements)[0];
focused_element_->node()->AddChild(option_graphic_);
if(option_node_[0]) focused_element_->node()->AddChild(option_node_[0]);
if(option_node_[1]) focused_element_->node()->AddChild(option_node_[1]);
PositionSelectionDrawables();
}
delete intersecting_uielements;
}
Expand All @@ -60,6 +68,13 @@ void Menu::Update(double dt) {
this->CheckInteraction(mouse_pos);
}

void Menu::PositionSelectionDrawables() {
if(option_node_[0]) option_node_[0]->modifier()->set_offset(
Vector2D(-focused_element_->node()->drawable()->hotspot().x - option_node_[0]->drawable()->hotspot().x, 0.0));
if(option_node_[1]) option_node_[1]->modifier()->set_offset(
Vector2D(focused_element_->node()->drawable()->hotspot().x + option_node_[1]->drawable()->hotspot().x, 0.0));
}

void Menu::OnSceneAdd(action::Scene* scene) {
scene->AddTask(new CallbackCheckTask(this));
}
Expand All @@ -86,12 +101,14 @@ void Menu::CheckInteraction(const Vector2D &mouse_pos) {
}

void Menu::AddObject(UIElement *obj) {
obj->set_owner(this);
objects_tree_->Insert(obj->GetBoundingBox(), obj);
node_->AddChild(obj->node());
uielements_.push_back(obj);
}

void Menu::RemoveObject(UIElement *obj) {
obj->set_owner(NULL);
objects_tree_->Remove(obj);
node_->RemoveChild(obj->node());
uielements_.remove(obj);
Expand Down
8 changes: 6 additions & 2 deletions src/ugdk/ui/menu.h
Expand Up @@ -42,7 +42,11 @@ class Menu: public action::Entity {

void FinishScene() { owner_scene_->Finish(); }
void InteractWithFocused();
void SetOptionDrawable(graphic::Drawable* option_graphic) { option_graphic_->set_drawable(option_graphic); }
void SetOptionDrawable(graphic::Drawable* option_graphic, int index = 0) {
if(!option_node_[index]) option_node_[index] = new graphic::Node;
option_node_[index]->set_drawable(option_graphic);
}
void PositionSelectionDrawables();

void AddObject(UIElement* obj);
void RemoveObject(UIElement* obj);
Expand All @@ -54,7 +58,7 @@ class Menu: public action::Entity {

private:
graphic::Node* node_;
graphic::Node* option_graphic_;
graphic::Node* option_node_[2];
Vector2D last_mouse_position_;
UIElement* focused_element_;
std::list< const UIElement* > uielements_;
Expand Down
6 changes: 3 additions & 3 deletions src/ugdk/ui/uielement.cc
Expand Up @@ -10,11 +10,11 @@
namespace ugdk {
namespace ui {

UIElement::UIElement(const Vector2D& top_left, Menu* owner, UICallback function)
UIElement::UIElement(const Vector2D& top_left, graphic::Drawable* drawable, UICallback function)
: top_left_(top_left),
owner_(owner),
owner_(NULL),
function_(function),
node_(new graphic::Node) {
node_(new graphic::Node(drawable)) {
node_->modifier()->set_offset(top_left_);
}

Expand Down
6 changes: 3 additions & 3 deletions src/ugdk/ui/uielement.h
Expand Up @@ -19,15 +19,15 @@ class UIElement {
public:
typedef std::tr1::function<void (const UIElement *)> UICallback;

UIElement(const Vector2D& top_left, Menu* owner, UICallback function);
UIElement(const Vector2D& top_left, graphic::Drawable* drawable, UICallback function);

virtual ~UIElement();

ikdtree::Box<2> GetBoundingBox() const ;

void set_drawable(graphic::Drawable* drawable) { node_->set_drawable(drawable); }
graphic::Node* node() const { return node_; }

void set_owner(Menu* owner) { owner_ = owner; }

void Interact() const { if(function_) function_(this); }

private:
Expand Down

0 comments on commit 2062200

Please sign in to comment.