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/Panel.cpp | |
| parent | fc2f52099c0bbb2412046252bf7b5e4113bbe8e4 (diff) | |
| download | powder-2c9295007a287dc01ff63fcf7b3da141f7474e37.zip powder-2c9295007a287dc01ff63fcf7b3da141f7474e37.tar.gz | |
Various things, also IEF UI
Diffstat (limited to 'src/interface/Panel.cpp')
| -rw-r--r-- | src/interface/Panel.cpp | 395 |
1 files changed, 382 insertions, 13 deletions
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) +{ +} |
