diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-14 18:51:24 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-14 18:51:24 (GMT) |
| commit | 2c9295007a287dc01ff63fcf7b3da141f7474e37 (patch) | |
| tree | e8065e920ca45686a40e41fd46513e13d46f47b0 /src/interface | |
| parent | fc2f52099c0bbb2412046252bf7b5e4113bbe8e4 (diff) | |
| download | powder-2c9295007a287dc01ff63fcf7b3da141f7474e37.zip powder-2c9295007a287dc01ff63fcf7b3da141f7474e37.tar.gz | |
Various things, also IEF UI
Diffstat (limited to 'src/interface')
| -rw-r--r-- | src/interface/Button.cpp | 81 | ||||
| -rw-r--r-- | src/interface/Component.cpp | 118 | ||||
| -rw-r--r-- | src/interface/ControlFactory.cpp | 5 | ||||
| -rw-r--r-- | src/interface/Engine.cpp | 138 | ||||
| -rw-r--r-- | src/interface/Label.cpp | 38 | ||||
| -rw-r--r-- | src/interface/Panel.cpp | 395 | ||||
| -rw-r--r-- | src/interface/Sandbox.cpp | 58 | ||||
| -rw-r--r-- | src/interface/State.cpp | 384 | ||||
| -rw-r--r-- | src/interface/Window.cpp | 23 |
9 files changed, 961 insertions, 279 deletions
diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp index a357c36..e34f66c 100644 --- a/src/interface/Button.cpp +++ b/src/interface/Button.cpp @@ -12,32 +12,49 @@ namespace ui { -Button::Button(int x, int y, int width, int height, const std::string& buttonText): - Component(x, y, width, height), - Toggleable(false), - ButtonText(buttonText), - isMouseInside(false), - isButtonDown(false), - state(false) +Button::Button(State* parent_state, std::string buttonText): + Component(parent_state), + ButtonText(buttonText), + isMouseInside(false), + isButtonDown(false) { } -void Button::Draw(void* userdata) +Button::Button(Point position, Point size, std::string buttonText): + Component(position, size), + ButtonText(buttonText), + isMouseInside(false), + isButtonDown(false) { - Graphics * g = reinterpret_cast<Graphics*>(userdata); + +} + +Button::Button(std::string buttonText): + Component(), + ButtonText(buttonText), + isMouseInside(false), + isButtonDown(false) +{ + +} + +void Button::Draw(const Point& screenPos) +{ + Graphics * g = ui::Engine::Ref().g; + // = reinterpret_cast<Graphics*>(userdata); //TODO: Cache text location, that way we don't have the text alignment code here if(isButtonDown) { - g->fillrect(X, Y, Width, Height, 255, 255, 255, 255); - g->drawtext(X+(Width-Graphics::textwidth((char *)ButtonText.c_str()))/2, Y+(Height-10)/2, ButtonText, 0, 0, 0, 255); + g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); + g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2, ButtonText, 0, 0, 0, 255); } else { if(isMouseInside) - g->fillrect(X, Y, Width, Height, 20, 20, 20, 255); - g->drawrect(X, Y, Width, Height, 255, 255, 255, 255); - g->drawtext(X+(Width-Graphics::textwidth((char *)ButtonText.c_str()))/2, Y+(Height-10)/2, ButtonText, 255, 255, 255, 255); + g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 20, 20, 20, 255); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); + g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2, ButtonText, 255, 255, 255, 255); } /*sf::RenderWindow* rw = reinterpret_cast<sf::RenderWindow*>(userdata); //it better be a RenderWindow or so help your god @@ -71,6 +88,7 @@ void Button::Draw(void* userdata) void Button::OnMouseUnclick(int x, int y, unsigned int button) { + std::cout << "Unclick!" << std::endl; if(button != 1) { return; //left click only! @@ -78,44 +96,35 @@ void Button::OnMouseUnclick(int x, int y, unsigned int button) if(isButtonDown) { - if(state) - { - state = false; - } - else - { - if(Toggleable) - { - state = true; - } - DoAction(); - } + DoAction(); } isButtonDown = false; } -void Button::OnMouseUp(int x, int y, unsigned int button) //mouse unclick is called before this -{ - if(button != 1) return; //left click only! +//void Button::OnMouseUp(int x, int y, unsigned int button) //mouse unclick is called before this +//{ + // if(button != 1) return; //left click only! - isButtonDown = false; -} +// isButtonDown = false; +//} void Button::OnMouseClick(int x, int y, unsigned int button) { + std::cout << "Click!" << std::endl; if(button != 1) return; //left click only! - isButtonDown = true; } -void Button::OnMouseEnter(int x, int y, int dx, int dy) +void Button::OnMouseEnter(int x, int y) { + std::cout << "Enter!"<<std::endl; isMouseInside = true; } -void Button::OnMouseLeave(int x, int y, int dx, int dy) +void Button::OnMouseLeave(int x, int y) { + std::cout << "Leave!"<<std::endl; isMouseInside = false; } @@ -124,4 +133,8 @@ void Button::DoAction() std::cout << "Do action!"<<std::endl; } +Button::~Button() +{ +} + } /* namespace ui */ diff --git a/src/interface/Component.cpp b/src/interface/Component.cpp index 48a329b..ac66f92 100644 --- a/src/interface/Component.cpp +++ b/src/interface/Component.cpp @@ -1,61 +1,111 @@ -/* - * Component.cpp - * - * Created on: Jan 8, 2012 - * Author: Simon - */ - +//#include "Platform.h" #include "interface/Component.h" +#include "interface/Engine.h" +#include "interface/Point.h" +#include "interface/State.h" +#include "interface/Panel.h" -namespace ui { +using namespace ui; -Component::Component(int x, int y, int width, int height): - X(x), - Y(y), - Width(width), - Height(height), - Enabled(true), - Visible(true) +Component::Component(State* parent_state): + parentstate_(parent_state), + _parent(NULL), + Position(Point(0,0)), + Size(Point(0,0)), + Locked(false), + Visible(true) { + } -Component::~Component() +Component::Component(Point position, Point size): + parentstate_(NULL), + _parent(NULL), + Position(position), + Size(size), + Locked(false), + Visible(true) { + } -void Component::Draw(void* userdata) +Component::Component(): + parentstate_(NULL), + _parent(NULL), + Position(Point(0,0)), + Size(Point(0,0)), + Locked(false), + Visible(true) { + } -void Component::Tick(float dt) +bool Component::IsFocused() const { + return parentstate_->IsFocused(this); } -void Component::OnKeyPress(int key, bool shift, bool ctrl, bool alt) +void Component::SetParentState(State* state) { + parentstate_ = state; } -void Component::OnKeyRelease(int key, bool shift, bool ctrl, bool alt) +void Component::SetParent(Panel* new_parent) +{ + if(new_parent == NULL) + { + if(_parent != NULL) + { + // remove from current parent and send component to parent state + for(int i = 0; i < _parent->GetChildCount(); ++i) + { + if(_parent->GetChild(i) == this) + { + // remove ourself from parent component + _parent->RemoveChild(i, false); + + // add ourself to the parent state + GetParentState()->AddComponent(this); + + //done in this loop. + break; + } + } + } + } + else + { + // remove from parent state (if in parent state) and place in new parent + GetParentState()->RemoveComponent(this); + new_parent->children.push_back(this); + } + this->_parent = new_parent; +} + +// ***** OVERRIDEABLES ***** +// Kept empty. + +void Component::Draw(const Point& screenPos) { } -void Component::OnMouseEnter(int localx, int localy, int dx, int dy) +void Component::Tick(float dt) { } -void Component::OnMouseLeave(int localx, int localy, int dx, int dy) +void Component::OnKeyPress(int key, bool shift, bool ctrl, bool alt) { } -void Component::OnMouseClick(int localx, int localy, unsigned int button) +void Component::OnKeyRelease(int key, bool shift, bool ctrl, bool alt) { } -void Component::OnMouseUnclick(int localx, int localy, unsigned int button) +void Component::OnMouseClick(int localx, int localy, unsigned button) { } -void Component::OnMouseDown(int localx, int localy, unsigned int button) +void Component::OnMouseDown(int x, int y, unsigned button) { } @@ -71,7 +121,19 @@ void Component::OnMouseMovedInside(int localx, int localy, int dx, int dy) { } -void Component::OnMouseUp(int localx, int localy, unsigned int button) +void Component::OnMouseEnter(int localx, int localy) +{ +} + +void Component::OnMouseLeave(int localx, int localy) +{ +} + +void Component::OnMouseUnclick(int localx, int localy, unsigned button) +{ +} + +void Component::OnMouseUp(int x, int y, unsigned button) { } @@ -83,7 +145,7 @@ void Component::OnMouseWheelInside(int localx, int localy, int d) { } -void Component::OnMouseWheelFocused(int localx, int localy, int d) +Component::~Component() { + } -} /* namespace ui */ diff --git a/src/interface/ControlFactory.cpp b/src/interface/ControlFactory.cpp index 300ceba..25822cd 100644 --- a/src/interface/ControlFactory.cpp +++ b/src/interface/ControlFactory.cpp @@ -1,11 +1,12 @@ +#include "interface/Point.h" #include "interface/ControlFactory.h" #include "interface/Button.h" #include "interface/Panel.h" -#include "interface/Window.h" +#include "interface/Engine.h" ui::Panel * ControlFactory::MainMenu(GameSession * session, int x, int y, int width, int height) { - ui::Panel * mainMenu = new ui::Panel(x, y, width, height); + ui::Panel * mainMenu = new ui::Panel(ui::Point(x, y), ui::Point(width, height)); //mainMenu->Add(new ui::Button(0, 0, 20, 20, "Turd")); return mainMenu; } diff --git a/src/interface/Engine.cpp b/src/interface/Engine.cpp new file mode 100644 index 0000000..4468561 --- /dev/null +++ b/src/interface/Engine.cpp @@ -0,0 +1,138 @@ +#include "interface/Platform.h" +#include "interface/Engine.h" +#include "interface/State.h" +#include "Graphics.h" + +using namespace ui; + +Engine::Engine() +: + g(NULL), +state_(NULL), +statequeued_(NULL), +mousex_(0), +mousey_(0), +mousexp_(0), +mouseyp_(0) +{ +} + +Engine::~Engine() +{ + if(state_ != NULL) + delete state_; +} + +void Engine::Begin(int width, int height, SDL_Surface * surface) +{ + g = new Graphics(); + g->AttachSDLSurface(surface); + //engine is now ready + running_ = true; + + width_ = width; + height_ = height; +} + +void Engine::Exit() +{ + running_ = false; +} + +void Engine::SetState(State * state) +{ + if(state_) //queue if currently in a state + statequeued_ = state; + else + { + state_ = state; + if(state_) + state_->DoInitialized(); + } +} + +void Engine::SetSize(int width, int height) +{ + width_ = width; + height_ = height; +} + +void Engine::Tick(float dt) +{ + if(state_ != NULL) + state_->DoTick(dt); + + if(statequeued_ != NULL) + { + if(state_ != NULL) + { + state_->DoExit(); + delete state_; + state_ = NULL; + } + state_ = statequeued_; + statequeued_ = NULL; + + if(state_ != NULL) + state_->DoInitialized(); + } +} + +void Engine::Draw() +{ + if(state_) + state_->DoDraw(); + g->Blit(); + g->Clear(); +} + +void Engine::onKeyPress(int key, bool shift, bool ctrl, bool alt) +{ + if(state_) + state_->DoKeyPress(key, shift, ctrl, alt); +} + +void Engine::onKeyRelease(int key, bool shift, bool ctrl, bool alt) +{ + if(state_) + state_->DoKeyRelease(key, shift, ctrl, alt); +} + +void Engine::onMouseClick(int x, int y, unsigned button) +{ + if(state_) + state_->DoMouseDown(x, y, button); +} + +void Engine::onMouseUnclick(int x, int y, unsigned button) +{ + if(state_) + state_->DoMouseUp(x, y, button); +} + +void Engine::onMouseMove(int x, int y) +{ + mousex_ = x; + mousey_ = y; + if(state_) + state_->DoMouseMove(x, y, mousex_ - mousexp_, mousey_ - mouseyp_); + mousexp_ = x; + mouseyp_ = y; +} + +void Engine::onMouseWheel(int x, int y, int delta) +{ + if(state_) + state_->DoMouseWheel(x, y, delta); +} + +void Engine::onResize(int newWidth, int newHeight) +{ + SetSize(newWidth, newHeight); +} + +void Engine::onClose() +{ + if(state_) + state_->DoExit(); +} diff --git a/src/interface/Label.cpp b/src/interface/Label.cpp new file mode 100644 index 0000000..3a5ea85 --- /dev/null +++ b/src/interface/Label.cpp @@ -0,0 +1,38 @@ +#include <string> +#include "interface/Point.h" +#include "interface/Label.h" + +using namespace ui; + +Label::Label(State* parent_state, std::string labelText): + Component(parent_state), + LabelText(labelText) +{ + +} + +Label::Label(Point position, Point size, std::string labelText): + Component(position, size), + LabelText(labelText) +{ + +} + +Label::Label(std::string labelText): + Component(), + LabelText(labelText) +{ + +} + +Label::~Label() +{ + +} + + +void Label::Draw(const Point& screenPos) +{ + Graphics * g = ui::Engine::Ref().g; + g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)LabelText.c_str()))/2, Position.Y+(Size.Y-10)/2, LabelText, 255, 255, 255, 255); +} diff --git a/src/interface/Panel.cpp b/src/interface/Panel.cpp index 164bfa3..e44663a 100644 --- a/src/interface/Panel.cpp +++ b/src/interface/Panel.cpp @@ -1,23 +1,392 @@ -/* - * Panel.cpp - * - * Created on: Jan 8, 2012 - * Author: Simon - */ +#pragma once +#include <vector> +//#include "Platform.h" #include "interface/Panel.h" -namespace ui { +#include "interface/Point.h" +#include "interface/State.h" +#include "interface/Component.h" -Panel::Panel(int x, int y, int width, int height): - Component(x, y, width, height) +using namespace ui; + +Panel::Panel(State* parent_state): + Component(parent_state) +{ + +} + +Panel::Panel(Point position, Point size): + Component(position, size) +{ + +} + +Panel::Panel(): + Component() +{ + +} + +Panel::~Panel() +{ + for(unsigned i = 0; i < children.size(); ++i) + { + if( children[i] ) + delete children[i]; + } +} + +void Panel::AddChild(Component* c) +{ + c->SetParent(this); +} + +int Panel::GetChildCount() +{ + return children.size(); +} + +Component* Panel::GetChild(unsigned idx) +{ + return children[idx]; +} + +void Panel::RemoveChild(Component* c) +{ + for(int i = 0; i < children.size(); ++i) + { + if(children[i] == c) + { + //remove child from parent. Does not free memory + children.erase(children.begin() + i); + break; + } + } +} + +void Panel::RemoveChild(unsigned idx, bool freeMem) +{ + if(freeMem) + delete children[idx]; + + children.erase(children.begin() + idx); +} + +void Panel::Draw(const Point& screenPos) +{ + // draw ourself first + XDraw(screenPos); + + // attempt to draw all children + for(int i = 0; i < children.size(); ++i) + { + // the component must be visible + if(children[i]->Visible) + { + if(GetParentState()->AllowExclusiveDrawing) + { + //who cares if the component is off the screen? draw anyway. + Point scrpos = screenPos + children[i]->Position; + children[i]->Draw(scrpos); + } + else + { + //check if the component is in the screen, draw if it is + if( children[i]->Position.X + children[i]->Size.X >= 0 && + children[i]->Position.Y + children[i]->Size.Y >= 0 && + children[i]->Position.X < ui::Engine::Ref().GetWidth() && + children[i]->Position.Y < ui::Engine::Ref().GetHeight() ) + { + Point scrpos = screenPos + children[i]->Position; + children[i]->Draw(scrpos); + } + } + } + } +} + +void Panel::Tick(float dt) +{ + // tick ourself first + XTick(dt); + + // tick our children + for(unsigned i = 0; i < children.size(); ++i) + children[i]->Tick(dt); +} + +void Panel::OnKeyPress(int key, bool shift, bool ctrl, bool alt) +{ + XOnKeyPress(key, shift, ctrl, alt); +} + +void Panel::OnKeyRelease(int key, bool shift, bool ctrl, bool alt) +{ + XOnKeyRelease(key, shift, ctrl, alt); +} + +void Panel::OnMouseClick(int localx, int localy, unsigned button) +{ + bool childclicked = false; + + //check if clicked a child + for(int i = children.size()-1; i >= 0 ; --i) + { + //child must be unlocked + if(!children[i]->Locked) + { + //is mouse inside? + if( localx >= children[i]->Position.X && + localy >= children[i]->Position.Y && + localx < children[i]->Position.X + children[i]->Size.X && + localy < children[i]->Position.Y + children[i]->Size.Y ) + { + childclicked = true; + GetParentState()->FocusComponent(children[i]); + children[i]->OnMouseClick(localx - children[i]->Position.X, localy - children[i]->Position.Y, button); + break; + } + } + } + + //if a child wasn't clicked, send click to ourself + if(!childclicked) + { + XOnMouseClick(localx, localy, button); + GetParentState()->FocusComponent(this); + } +} + +void Panel::OnMouseDown(int x, int y, unsigned button) +{ + XOnMouseDown(x, y, button); + for(int i = 0; i < children.size(); ++i) + { + if(!children[i]->Locked) + children[i]->OnMouseDown(x, y, button); + } +} + +void Panel::OnMouseHover(int localx, int localy) +{ + // check if hovering on children + for(int i = children.size() - 1; i >= 0; --i) + { + if(!children[i]->Locked) + { + if( localx >= children[i]->Position.X && + localy >= children[i]->Position.Y && + localx < children[i]->Position.X + children[i]->Size.X && + localy < children[i]->Position.Y + children[i]->Size.Y ) + { + children[i]->OnMouseHover(localx - children[i]->Position.X, localy - children[i]->Position.Y); + break; + } + } + } + + // always allow hover on parent (?) + XOnMouseHover(localx, localy); +} + +void Panel::OnMouseMoved(int localx, int localy, int dx, int dy) +{ + XOnMouseMoved(localx, localy, dx, dy); + for(int i = 0; i < children.size(); ++i) + { + if(!children[i]->Locked) + children[i]->OnMouseMoved(localx - children[i]->Position.X, localy - children[i]->Position.Y, dx, dy); + } +} + +void Panel::OnMouseMovedInside(int localx, int localy, int dx, int dy) +{ + for(int i = 0; i < children.size(); ++i) + { + if(!children[i]->Locked) + { + Point local (localx - children[i]->Position.X, localy - children[i]->Position.Y) + , prevlocal (local.X - dx, local.Y - dy); + + // mouse currently inside? + if( local.X >= 0 && + local.Y >= 0 && + local.X < children[i]->Size.X && + local.Y < children[i]->Size.Y ) + { + children[i]->OnMouseMovedInside(localx - children[i]->Position.X, localy - children[i]->Position.Y, dx, dy); + + // was the mouse outside? + if(!(prevlocal.X >= 0 && + prevlocal.Y >= 0 && + prevlocal.X < children[i]->Size.X && + prevlocal.Y < children[i]->Size.Y ) ) + { + children[i]->OnMouseEnter(local.X, local.Y); + } + } + // if not currently inside + else + { + // was the mouse inside? + if( prevlocal.X >= 0 && + prevlocal.Y >= 0 && + prevlocal.X < children[i]->Size.X && + prevlocal.Y < children[i]->Size.Y ) + { + children[i]->OnMouseLeave(local.X, local.Y); + } + + } + } + } + + // always allow hover on parent (?) + XOnMouseMovedInside(localx, localy, dx, dy); +} + +void Panel::OnMouseEnter(int localx, int localy) +{ + XOnMouseEnter(localx, localy); +} + +void Panel::OnMouseLeave(int localx, int localy) +{ + XOnMouseLeave(localx, localy); +} + +void Panel::OnMouseUnclick(int localx, int localy, unsigned button) +{ + bool childunclicked = false; + + //check if clicked a child + for(int i = children.size()-1; i >= 0 ; --i) + { + //child must be unlocked + if(!children[i]->Locked) + { + //is mouse inside? + if( localx >= children[i]->Position.X && + localy >= children[i]->Position.Y && + localx < children[i]->Position.X + children[i]->Size.X && + localy < children[i]->Position.Y + children[i]->Size.Y ) + { + childunclicked = true; + children[i]->OnMouseUnclick(localx - children[i]->Position.X, localy - children[i]->Position.Y, button); + break; + } + } + } + + //if a child wasn't clicked, send click to ourself + if(!childunclicked) + { + XOnMouseUnclick(localx, localy, button); + } +} + +void Panel::OnMouseUp(int x, int y, unsigned button) +{ + XOnMouseUp(x, y, button); + for(int i = 0; i < children.size(); ++i) + { + if(!children[i]->Locked) + children[i]->OnMouseUp(x, y, button); + } +} + +void Panel::OnMouseWheel(int localx, int localy, int d) +{ + XOnMouseWheel(localx, localy, d); + for(int i = 0; i < children.size(); ++i) + { + if(!children[i]->Locked) + children[i]->OnMouseWheel(localx - children[i]->Position.X, localy - children[i]->Position.Y, d); + } +} + +void Panel::OnMouseWheelInside(int localx, int localy, int d) +{ + XOnMouseWheelInside(localx, localy, d); + //check if clicked a child + for(int i = children.size()-1; i >= 0 ; --i) + { + //child must be unlocked + if(!children[i]->Locked) + { + //is mouse inside? + if( localx >= children[i]->Position.X && + localy >= children[i]->Position.Y && + localx < children[i]->Position.X + children[i]->Size.X && + localy < children[i]->Position.Y + children[i]->Size.Y ) + { + children[i]->OnMouseWheelInside(localx - children[i]->Position.X, localy - children[i]->Position.Y, d); + break; + } + } + } +} + +// ***** OVERRIDEABLES ***** +// Kept empty. + +void Panel::XDraw(const Point& screenPos) +{ +} + +void Panel::XTick(float dt) +{ +} + +void Panel::XOnKeyPress(int key, bool shift, bool ctrl, bool alt) +{ +} + +void Panel::XOnKeyRelease(int key, bool shift, bool ctrl, bool alt) { - // TODO Auto-generated constructor stub +} + +void Panel::XOnMouseClick(int localx, int localy, unsigned button) +{ +} +void Panel::XOnMouseDown(int x, int y, unsigned button) +{ +} + +void Panel::XOnMouseHover(int localx, int localy) +{ } -Panel::~Panel() { - // TODO Auto-generated destructor stub +void Panel::XOnMouseMoved(int localx, int localy, int dx, int dy) +{ +} + +void Panel::XOnMouseMovedInside(int localx, int localy, int dx, int dy) +{ +} + +void Panel::XOnMouseEnter(int localx, int localy) +{ } -} /* namespace ui */ +void Panel::XOnMouseLeave(int localx, int localy) +{ +} + +void Panel::XOnMouseUnclick(int localx, int localy, unsigned button) +{ +} + +void Panel::XOnMouseUp(int x, int y, unsigned button) +{ +} + +void Panel::XOnMouseWheel(int localx, int localy, int d) +{ +} + +void Panel::XOnMouseWheelInside(int localx, int localy, int d) +{ +} diff --git a/src/interface/Sandbox.cpp b/src/interface/Sandbox.cpp index 5e29bae..a9f1d9c 100644 --- a/src/interface/Sandbox.cpp +++ b/src/interface/Sandbox.cpp @@ -6,9 +6,11 @@ */ #include <iostream> +#include <queue> #include "Config.h" +#include "interface/Point.h" #include "interface/Sandbox.h" #include "interface/Component.h" #include "Renderer.h" @@ -17,7 +19,8 @@ namespace ui { Sandbox::Sandbox(): - Component(0, 0, XRES, YRES), + Component(Point(0, 0), Point(XRES, YRES)), + pointQueue(std::queue<Point*>()), ren(NULL), isMouseDown(false), activeElement(1) @@ -30,35 +33,33 @@ Simulation * Sandbox::GetSimulation() return sim; } -void Sandbox::OnMouseMovedInside(int localx, int localy, int dx, int dy) +void Sandbox::OnMouseMoved(int localx, int localy, int dx, int dy) { if(isMouseDown) { - sim->create_line(lastCoordX, lastCoordY, localx, localy, 2, 2, activeElement, 0); - lastCoordX = localx; - lastCoordY = localy; + pointQueue.push(new Point(localx-dx, localy-dy)); + pointQueue.push(new Point(localx, localy)); } } -void Sandbox::OnMouseDown(int localx, int localy, unsigned int button) +void Sandbox::OnMouseClick(int localx, int localy, unsigned int button) { - sim->create_line(localx, localy, localx, localy, 2, 2, activeElement, 0); - lastCoordX = localx; - lastCoordY = localy; isMouseDown = true; + pointQueue.push(new Point(localx, localy)); } -void Sandbox::OnMouseUp(int localx, int localy, unsigned int button) +void Sandbox::OnMouseUnclick(int localx, int localy, unsigned int button) { - sim->create_line(lastCoordX, lastCoordY, localx, localy, 2, 2, activeElement, 0); - lastCoordX = localx; - lastCoordY = localy; - isMouseDown = false; + if(isMouseDown) + { + isMouseDown = false; + pointQueue.push(new Point(localx, localy)); + } } -void Sandbox::Draw(void* userdata) +void Sandbox::Draw(const Point& screenPos) { - Graphics * g = reinterpret_cast<Graphics*>(userdata); + Graphics * g = ui::Engine::Ref().g; if(!ren) ren = new Renderer(g, sim); ren->render_parts(); @@ -66,7 +67,32 @@ void Sandbox::Draw(void* userdata) void Sandbox::Tick(float delta) { + if(!pointQueue.empty()) + { + Point * sPoint = NULL; + while(!pointQueue.empty()) + { + Point * fPoint = pointQueue.front(); + pointQueue.pop(); + if(sPoint) + { + sim->create_line(fPoint->X, fPoint->Y, sPoint->X, sPoint->Y, 2, 2, activeElement, 0); + delete sPoint; + sPoint = fPoint; + } + else + { + sim->create_parts(fPoint->X, fPoint->Y, 2, 2, activeElement, 0); + } + if(sPoint) + delete sPoint; + sPoint = fPoint; + } + if(sPoint) + delete sPoint; + } sim->update_particles(); + sim->sys_pause = 1; } Sandbox::~Sandbox() { diff --git a/src/interface/State.cpp b/src/interface/State.cpp index 2828751..665e2e8 100644 --- a/src/interface/State.cpp +++ b/src/interface/State.cpp @@ -1,233 +1,291 @@ -/* - * State.cpp - * - * Created on: Jan 8, 2012 - * Author: Simon - */ - #include <vector> -#include <iostream> -#include <cstring> - +#include "interface/Component.h" +#include "interface/Engine.h" #include "interface/State.h" +//#include "Platform.h" -namespace ui { +using namespace ui; -State::State(int w, int h): - mouseX(0), - mouseY(0), - mouseXP(0), - mouseYP(0), - width(w), - height(h), - Components() +State::State() +: UserData(NULL) +, focusedComponent_(NULL) { } State::~State() { - //Components.~vector(); // just in case // devnote : Nope.jpg Nate :3 -frankbro + for(unsigned i = 0, sz = Components.size(); i < sz; ++i) + if( Components[i] ) + delete Components[i]; +} + +void State::AddComponent(Component* c) +{ + // TODO: do a check if component was already added? + if(c->GetParentState()==NULL) + { + c->SetParentState(this); + Components.push_back(c); + } + else + { + //Component already has a state, don't sad it + } +} + +unsigned State::GetComponentCount() +{ + return Components.size(); +} + +Component* State::GetComponent(unsigned idx) +{ + return Components[idx]; +} + +void State::RemoveComponent(Component* c) +{ + // remove component WITHOUT freeing it. + for(unsigned i = 0; i < Components.size(); ++i) + { + // find the appropriate component index + if(Components[i] == c) + { + Components.erase(Components.begin() + i); + + // we're done + return; + } + } +} + +void State::RemoveComponent(unsigned idx) +{ + // free component and remove it. + delete Components[idx]; + Components.erase(Components.begin() + idx); } -void State::Add(Component* child) +bool State::IsFocused(const Component* c) const { - Components.push_back(child); - child->Parent = this; + return c == focusedComponent_; } -void State::Remove(Component* child) +void State::FocusComponent(Component* c) { - for(int i = 0; i < Components.size(); i++) - if(Components[i] == child) - { - Components.erase(Components.begin() + i); - break; - } + this->focusedComponent_ = c; } -void State::Draw(void* userdata) +void State::DoExit() { - //draw - for(int i = 0; i < Components.size(); i++) - { - if(Components[i]->Visible) - { - if(AllowExclusiveDrawing) - { - Components[i]->Draw(userdata); - } - else if( - Components[i]->X + Components[i]->Width >= 0 && - Components[i]->Y + Components[i]->Height >= 0 && - Components[i]->X < width && - Components[i]->Y < height ) - { - Components[i]->Draw(userdata); - } - } - } + + OnExit(); } -void State::Tick(float dt) +void State::DoInitialized() +{ + + OnInitialized(); +} + +void State::DoDraw() +{ + //draw + for(int i = 0, sz = Components.size(); i < sz; ++i) + if(Components[i]->Visible) + { + if(AllowExclusiveDrawing) + { + Point scrpos(Components[i]->Position.X, Components[i]->Position.Y); + Components[i]->Draw(scrpos); + } + else + { + if( Components[i]->Position.X + Components[i]->Size.X >= 0 && + Components[i]->Position.Y + Components[i]->Size.Y >= 0 && + Components[i]->Position.X < ui::Engine::Ref().GetWidth() && + Components[i]->Position.Y < ui::Engine::Ref().GetHeight() ) + { + Point scrpos(Components[i]->Position.X, Components[i]->Position.Y); + Components[i]->Draw( Point(scrpos) ); + } + } + } + + OnDraw(); +} + +void State::DoTick(float dt) { //on mouse hover - for(int i = 0; i < Components.size(); i++) - if( mouseX >= Components[i]->X && - mouseY >= Components[i]->Y && - mouseX < Components[i]->X + Components[i]->Width && - mouseY < Components[i]->Y + Components[i]->Height ) - { - if(Components[i]->Enabled) - { - Components[i]->OnMouseHover(mouseX, mouseY); - } + for(int i = Components.size() - 1; i >= 0; --i) + { + if(!Components[i]->Locked && + ui::Engine::Ref().GetMouseX() >= Components[i]->Position.X && + ui::Engine::Ref().GetMouseY() >= Components[i]->Position.Y && + ui::Engine::Ref().GetMouseX() < Components[i]->Position.X + Components[i]->Size.X && + ui::Engine::Ref().GetMouseY() < Components[i]->Position.Y + Components[i]->Size.Y ) + { + Components[i]->OnMouseHover(ui::Engine::Ref().GetMouseX() - Components[i]->Position.X, ui::Engine::Ref().GetMouseY() - Components[i]->Position.Y); break; } - + } + //tick - for(int i = 0; i < Components.size(); i++) + for(int i = 0, sz = Components.size(); i < sz; ++i) + { Components[i]->Tick(dt); + } + + OnTick(dt); } -void State::OnKeyPress(int key, bool shift, bool ctrl, bool alt) +void State::DoKeyPress(int key, bool shift, bool ctrl, bool alt) { //on key press if(focusedComponent_ != NULL) - if(focusedComponent_->Enabled) + { + if(!focusedComponent_->Locked) focusedComponent_->OnKeyPress(key, shift, ctrl, alt); + } + + OnKeyPress(key, shift, ctrl, alt); } -void State::OnKeyRelease(int key, bool shift, bool ctrl, bool alt) +void State::DoKeyRelease(int key, bool shift, bool ctrl, bool alt) { //on key unpress if(focusedComponent_ != NULL) - if(focusedComponent_->Enabled) + { + if(!focusedComponent_->Locked) focusedComponent_->OnKeyRelease(key, shift, ctrl, alt); + } + + OnKeyRelease(key, shift, ctrl, alt); } -void State::OnMouseDown(int x, int y, unsigned int button) +void State::DoMouseDown(int x, int y, unsigned button) { //on mouse click - for(int i = Components.size() - 1; i > -1 ; i--) - if(Components[i]->Enabled) - if(x >= Components[i]->X && y >= Components[i]->Y && x < Components[i]->X + Components[i]->Width && y < Components[i]->Y + Components[i]->Height) + bool clickState = false; + for(int i = Components.size() - 1; i > -1 ; --i) + { + if(!Components[i]->Locked) + { + if(x >= Components[i]->Position.X && y >= Components[i]->Position.Y && x < Components[i]->Position.X + Components[i]->Size.X && y < Components[i]->Position.Y + Components[i]->Size.Y) { - Components[i]->OnMouseClick(x - Components[i]->X, y - Components[i]->Y, button); - this->focusedComponent_ = Components[i]; //set this component as the focused component + FocusComponent(Components[i]); + Components[i]->OnMouseClick(x - Components[i]->Position.X, y - Components[i]->Position.Y, button); + clickState = true; break; } + } + } + + if(!clickState) + FocusComponent(NULL); //on mouse down - for(int i = Components.size() - 1; i > -1 ; i--) - if(Components[i]->Enabled) - Components[i]->OnMouseDown(x - Components[i]->X, y - Components[i]->Y, button); + for(int i = Components.size() - 1; i > -1 ; --i) + { + if(!Components[i]->Locked) + Components[i]->OnMouseDown(x, y, button); + } + + OnMouseDown(x, y, button); } -void State::OnMouseMove(int x, int y) +void State::DoMouseMove(int x, int y, int dx, int dy) { - //update mouse coords - mouseX = x; - mouseY = y; - //on mouse move (if true, and inside) - for(int i = Components.size() - 1; i > -1 ; i--) - if(Components[i]->Enabled) + for(int i = Components.size() - 1; i > -1 ; --i) + { + if(!Components[i]->Locked) { - int localX = x - Components[i]->X; - int localY = y - Components[i]->Y; - int localXP = mouseXP - Components[i]->X; - int localYP = mouseYP - Components[i]->Y; - int dx = x - mouseXP; - int dy = x - mouseYP; - - Components[i]->OnMouseMoved(localX, localY, dx, dy); - - //is the mouse inside - if(localX >= 0 && - localY >= 0 && - localX < Components[i]->Width && - localY < Components[i]->Height ) - { - //was the mouse outside last tick? - if(localXP < 0 || - localXP >= Components[i]->Width || - localYP < 0 || - localYP >= Components[i]->Height ) - { - Components[i]->OnMouseEnter(localX, localY, dx, dy); - } - - Components[i]->OnMouseMovedInside(localX, localY, dx, dy); - - break; //found the top-most component under mouse, break that shit - } - //not inside, let's see if it used to be inside last tick - else if (localXP >= 0 && - localYP >= 0 && - localXP < Components[i]->Width && - localYP < Components[i]->Height ) - { - Components[i]->OnMouseLeave(localX, localY, x - mouseXP, y - mouseYP); - } - } - else //is locked - { - int localX = x - Components[i]->X; - int localY = y - Components[i]->Y; - - //is the mouse inside - if(localX >= 0 && - localY >= 0 && - localX < Components[i]->Width && - localY < Components[i]->Height ) - { - break; //it's the top-most component under the mouse, we don't want to go under it. - } + Point local (x - Components[i]->Position.X, y - Components[i]->Position.Y) + , a (local.X - dx, local.Y - dy); + + Components[i]->OnMouseMoved(local.X, local.Y, dx, dy); + + if(local.X >= 0 && + local.Y >= 0 && + local.X < Components[i]->Size.X && + local.Y < Components[i]->Size.Y ) + { + Components[i]->OnMouseMovedInside(local.X, local.Y, dx, dy); + + // entering? + if(!( + a.X >= 0 && + a.Y >= 0 && + a.X < Components[i]->Size.X && + a.Y < Components[i]->Size.Y )) + { + Components[i]->OnMouseEnter(local.X, local.Y); + } + } + else + { + // leaving? + if( a.X >= 0 && + a.Y >= 0 && + a.X < Components[i]->Size.X && + a.Y < Components[i]->Size.Y ) + { + Components[i]->OnMouseLeave(local.X, local.Y); + } + + } } - // end of for loop here + } - //set the previous mouse coords - mouseXP = x; - mouseYP = y; + OnMouseMove(x, y, dx, dy); } -void State::OnMouseUp(int x, int y, unsigned int button) +void State::DoMouseUp(int x, int y, unsigned button) { //on mouse unclick - for(int i = Components.size() - 1; i > -1 ; i--) - if(Components[i]->Enabled) - if(x >= Components[i]->X && y >= Components[i]->Y && x < Components[i]->X + Components[i]->Width && y < Components[i]->Y + Components[i]->Height) + for(int i = Components.size() - 1; i >= 0 ; --i) + { + if(!Components[i]->Locked) + { + if(x >= Components[i]->Position.X && y >= Components[i]->Position.Y && x < Components[i]->Position.X + Components[i]->Size.X && y < Components[i]->Position.Y + Components[i]->Size.Y) { - Components[i]->OnMouseUnclick(x - Components[i]->X, y - Components[i]->Y, button); + Components[i]->OnMouseUnclick(x - Components[i]->Position.X, y - Components[i]->Position.Y, button); break; } + } + } //on mouse up - for(int i = Components.size() - 1; i > -1 ; i--) - if(Components[i]->Enabled) - Components[i]->OnMouseUp(x - Components[i]->X, y - Components[i]->Y, button); + for(int i = Components.size() - 1; i >= 0 ; --i) + { + if(!Components[i]->Locked) + Components[i]->OnMouseUp(x, y, button); + } + + OnMouseUp(x, y, button); } -void State::OnMouseWheel(int x, int y, int d) +void State::DoMouseWheel(int x, int y, int d) { - //focused mouse wheel - if(focusedComponent_ != NULL) - focusedComponent_->OnMouseWheelFocused(x - focusedComponent_->X, y - focusedComponent_->Y, d); - - //mouse wheel inside - for(int i = Components.size() - 1; i > -1 ; i--) - if(x >= Components[i]->X && y >= Components[i]->Y && x < Components[i]->X + Components[i]->Width && y < Components[i]->Y + Components[i]->Height) + //on mouse wheel focused + for(int i = Components.size() - 1; i >= 0 ; --i) + { + if(x >= Components[i]->Position.X && y >= Components[i]->Position.Y && x < Components[i]->Position.X + Components[i]->Size.X && y < Components[i]->Position.Y + Components[i]->Size.Y) { - if(Components[i]->Enabled) - Components[i]->OnMouseWheelInside(x - Components[i]->X, y - Components[i]->Y, d); - break; //found top-most component under mouse + if(!Components[i]->Locked) + Components[i]->OnMouseWheelInside(x - Components[i]->Position.X, y - Components[i]->Position.Y, d); + break; } - + } + //on mouse wheel - for(int i = Components.size() - 1; i > -1 ; i--) - if(Components[i]->Enabled) - Components[i]->OnMouseWheel(x - Components[i]->X, y - Components[i]->Y, d); -} - + for(int i = Components.size() - 1; i >= 0 ; --i) + { + if(!Components[i]->Locked) + Components[i]->OnMouseWheel(x - Components[i]->Position.X, y - Components[i]->Position.Y, d); + } -} /* namespace ui */ + OnMouseWheel(x, y, d); +} diff --git a/src/interface/Window.cpp b/src/interface/Window.cpp deleted file mode 100644 index 624bf9a..0000000 --- a/src/interface/Window.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Window.cpp - * - * Created on: Jan 8, 2012 - * Author: Simon - */ - -#include "interface/Window.h" - -namespace ui { - -Window::Window(): - State(width, height) -{ - // TODO Auto-generated constructor stub - -} - -Window::~Window() { - // TODO Auto-generated destructor stub -} - -} /* namespace ui */ |
