summaryrefslogtreecommitdiff
path: root/src/interface
diff options
context:
space:
mode:
Diffstat (limited to 'src/interface')
-rw-r--r--src/interface/Button.cpp26
-rw-r--r--src/interface/Button.h60
-rw-r--r--src/interface/Component.cpp14
-rw-r--r--src/interface/Component.h204
-rw-r--r--src/interface/ControlFactory.cpp5
-rw-r--r--src/interface/ControlFactory.h14
-rw-r--r--src/interface/Engine.cpp44
-rw-r--r--src/interface/Engine.h71
-rw-r--r--src/interface/Label.cpp4
-rw-r--r--src/interface/Label.h26
-rw-r--r--src/interface/Panel.cpp10
-rw-r--r--src/interface/Panel.h136
-rw-r--r--src/interface/Platform.h108
-rw-r--r--src/interface/Point.h136
-rw-r--r--src/interface/Sandbox.cpp3
-rw-r--r--src/interface/Sandbox.h39
-rw-r--r--src/interface/Window.cpp (renamed from src/interface/State.cpp)90
-rw-r--r--src/interface/Window.h103
18 files changed, 1012 insertions, 81 deletions
diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp
index 6ea9854..a3f76b9 100644
--- a/src/interface/Button.cpp
+++ b/src/interface/Button.cpp
@@ -10,15 +10,17 @@
#include "interface/Button.h"
#include "Graphics.h"
#include "Global.h"
+#include "Engine.h"
namespace ui {
-Button::Button(State* parent_state, std::string buttonText):
+Button::Button(Window* parent_state, std::string buttonText):
Component(parent_state),
ButtonText(buttonText),
isMouseInside(false),
isButtonDown(false),
- isTogglable(false)
+ isTogglable(false),
+ actionCallback(NULL)
{
}
@@ -28,7 +30,8 @@ Button::Button(Point position, Point size, std::string buttonText):
ButtonText(buttonText),
isMouseInside(false),
isButtonDown(false),
- isTogglable(false)
+ isTogglable(false),
+ actionCallback(NULL)
{
}
@@ -38,7 +41,8 @@ Button::Button(std::string buttonText):
ButtonText(buttonText),
isMouseInside(false),
isButtonDown(false),
- isTogglable(false)
+ isTogglable(false),
+ actionCallback(NULL)
{
}
@@ -68,13 +72,13 @@ inline void Button::SetToggleState(bool state)
void Button::Draw(const Point& screenPos)
{
- Graphics * g = Global::Ref().g;
+ Graphics * g = ui::Engine::Ref().g;
Point Position = screenPos;
// = reinterpret_cast<Graphics*>(userdata);
//TODO: Cache text location, that way we don't have the text alignment code here
if(isButtonDown || (isTogglable && toggle))
{
- g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);
+ g->fillrect(Position.X-1, Position.Y-1, Size.X+2, Size.Y+2, 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
@@ -116,7 +120,6 @@ void Button::Draw(const Point& screenPos)
void Button::OnMouseUnclick(int x, int y, unsigned int button)
{
- std::cout << "Unclick!" << std::endl;
if(button != 1)
{
return; //left click only!
@@ -160,6 +163,15 @@ void Button::OnMouseLeave(int x, int y)
void Button::DoAction()
{
std::cout << "Do action!"<<std::endl;
+ //if(actionCallback)
+ // (*(actionCallback))();
+ if(actionCallback)
+ actionCallback->ActionCallback(this);
+}
+
+void Button::SetActionCallback(ButtonAction * action)
+{
+ actionCallback = action;
}
Button::~Button()
diff --git a/src/interface/Button.h b/src/interface/Button.h
new file mode 100644
index 0000000..5f2d71f
--- /dev/null
+++ b/src/interface/Button.h
@@ -0,0 +1,60 @@
+/*
+ * Button.h
+ *
+ * Created on: Jan 8, 2012
+ * Author: Simon
+ */
+
+#ifndef BUTTON_H_
+#define BUTTON_H_
+
+#include <string>
+
+#include "Component.h"
+
+namespace ui
+{
+class Button;
+class ButtonAction
+{
+public:
+ virtual void ActionCallback(ui::Button * sender) {}
+};
+
+class Button : public Component
+{
+public:
+ Button(Window* parent_state, std::string buttonText);
+
+ Button(Point position, Point size, std::string buttonText);
+
+ Button(std::string buttonText);
+ virtual ~Button();
+
+ bool Toggleable;
+
+ std::string ButtonText;
+
+ virtual void OnMouseClick(int x, int y, unsigned int button);
+ virtual void OnMouseUnclick(int x, int y, unsigned int button);
+ //virtual void OnMouseUp(int x, int y, unsigned int button);
+
+ virtual void OnMouseEnter(int x, int y);
+ virtual void OnMouseLeave(int x, int y);
+
+ virtual void Draw(const Point& screenPos);
+
+ inline bool GetState() { return state; }
+ virtual void DoAction(); //action of button what ever it may be
+ void SetTogglable(bool isTogglable);
+ bool GetTogglable();
+ inline bool GetToggleState();
+ inline void SetToggleState(bool state);
+ void SetActionCallback(ButtonAction * action);
+
+protected:
+ bool isButtonDown, state, isMouseInside, isTogglable, toggle;
+ ButtonAction * actionCallback;
+};
+}
+#endif /* BUTTON_H_ */
diff --git a/src/interface/Component.cpp b/src/interface/Component.cpp
index 49ff7f8..75e9c40 100644
--- a/src/interface/Component.cpp
+++ b/src/interface/Component.cpp
@@ -2,12 +2,12 @@
#include "interface/Component.h"
#include "interface/Engine.h"
#include "interface/Point.h"
-#include "interface/State.h"
+#include "interface/Window.h"
#include "interface/Panel.h"
using namespace ui;
-Component::Component(State* parent_state):
+Component::Component(Window* parent_state):
parentstate_(parent_state),
_parent(NULL),
Position(Point(0,0)),
@@ -45,9 +45,9 @@ bool Component::IsFocused() const
return parentstate_->IsFocused(this);
}
-void Component::SetParentState(State* state)
+void Component::SetParentWindow(Window* window)
{
- parentstate_ = state;
+ parentstate_ = window;
}
void Component::SetParent(Panel* new_parent)
@@ -65,7 +65,7 @@ void Component::SetParent(Panel* new_parent)
_parent->RemoveChild(i, false);
// add ourself to the parent state
- GetParentState()->AddComponent(this);
+ GetParentWindow()->AddComponent(this);
//done in this loop.
break;
@@ -76,8 +76,8 @@ void Component::SetParent(Panel* new_parent)
else
{
// remove from parent state (if in parent state) and place in new parent
- if(GetParentState())
- GetParentState()->RemoveComponent(this);
+ if(GetParentWindow())
+ GetParentWindow()->RemoveComponent(this);
new_parent->children.push_back(this);
}
this->_parent = new_parent;
diff --git a/src/interface/Component.h b/src/interface/Component.h
new file mode 100644
index 0000000..578aba6
--- /dev/null
+++ b/src/interface/Component.h
@@ -0,0 +1,204 @@
+#pragma once
+
+#include "Point.h"
+#include "Window.h"
+#include "Platform.h"
+
+namespace ui
+{
+ class Window;
+ class Panel;
+
+ /* class Component
+ *
+ * An interactive UI component that can be added to a state or an XComponent*.
+ * *See sys::XComponent
+ */
+ class Component
+ {
+ public:
+ Component(Window* parent_state);
+ Component(Point position, Point size);
+ Component();
+ virtual ~Component();
+
+ void* UserData;
+ inline Window* const GetParentWindow() const { return parentstate_; }
+ bool IsFocused() const;
+
+ Point Position;
+ Point Size;
+ bool Locked;
+ bool Visible;
+
+ /* See the parent of this component.
+ * If new_parent is NULL, this component will have no parent. (THIS DOES NOT delete THE COMPONENT. See XComponent::RemoveChild)
+ */
+ void SetParentWindow(Window* window);
+ void SetParent(Panel* new_parent);
+
+ //Get the parent component.
+ inline Panel* const GetParent() const { return _parent; }
+
+ //UI functions:
+ /*
+ void Tick(float dt);
+ void Draw(const Point& screenPos);
+
+ void OnMouseHover(int localx, int localy);
+ void OnMouseMoved(int localx, int localy, int dx, int dy);
+ void OnMouseMovedInside(int localx, int localy, int dx, int dy);
+ void OnMouseEnter(int localx, int localy);
+ void OnMouseLeave(int localx, int localy);
+ void OnMouseDown(int x, int y, unsigned int button);
+ void OnMouseUp(int x, int y, unsigned int button);
+ void OnMouseClick(int localx, int localy, unsigned int button);
+ void OnMouseUnclick(int localx, int localy, unsigned int button);
+ void OnMouseWheel(int localx, int localy, int d);
+ void OnMouseWheelInside(int localx, int localy, int d);
+ void OnKeyPress(int key, bool shift, bool ctrl, bool alt);
+ void OnKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ */
+
+ ///
+ // Called: Every tick.
+ // Params:
+ // dt: The change in time.
+ ///
+ virtual void Tick(float dt);
+
+ ///
+ // Called: When ready to draw.
+ // Params:
+ // None
+ ///
+ virtual void Draw(const Point& screenPos);
+
+
+
+
+ ///
+ // Called: When the mouse is currently hovering over the item. (Called every tick)
+ // Params:
+ // localx: Local mouse X position.
+ // localy: Local mouse Y position.
+ ///
+ virtual void OnMouseHover(int localx, int localy);
+
+ ///
+ // Called: When the mouse moves.
+ // Params:
+ // localx: Local mouse X position.
+ // localy: Local mouse Y position.
+ // dx: Mouse X delta.
+ // dy: Mouse Y delta.
+ ///
+ virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
+
+ ///
+ // Called: When the mouse moves.
+ // Params:
+ // localx: Local mouse X position.
+ // localy: Local mouse Y position.
+ // dx: Mouse X delta.
+ // dy: Mouse Y delta.
+ ///
+ virtual void OnMouseMovedInside(int localx, int localy, int dx, int dy);
+
+ ///
+ // Called: When the mouse moves on top of the item.
+ // Params:
+ // localx: Local mouse X position.
+ // localy: Local mouse Y position.
+ // dx: Mouse X delta.
+ // dy: Mouse Y delta.
+ ///
+ virtual void OnMouseEnter(int localx, int localy);
+
+ ///
+ // Called: When the mouse leaves the item.
+ // Params:
+ // localx: Local mouse X position.
+ // localy: Local mouse Y position.
+ ///
+ virtual void OnMouseLeave(int localx, int localy);
+
+ ///
+ // Called: When a mouse button is pressed.
+ // Params:
+ // x: X position of the mouse.
+ // y: Y position of the mouse.
+ // button: The button that is being held down.
+ ///
+ virtual void OnMouseDown(int x, int y, unsigned button);
+
+ ///
+ // Called: When a mouse button is released.
+ // Params:
+ // x: X position of the mouse.
+ // y: Y position of the mouse.
+ // button: The button that is being released.
+ ///
+ virtual void OnMouseUp(int x, int y, unsigned button);
+
+ ///
+ // Called: When a mouse button is pressed on top of the item.
+ // Params:
+ // x: X position of the mouse.
+ // y: Y position of the mouse.
+ // button: The button that is being held down.
+ ///
+ virtual void OnMouseClick(int localx, int localy, unsigned button);
+
+ ///
+ // Called: When a mouse button is released on top of the item.
+ // Params:
+ // x: X position of the mouse.
+ // y: Y position of the mouse.
+ // button: The button that is being released.
+ ///
+ virtual void OnMouseUnclick(int localx, int localy, unsigned button);
+
+ ///
+ // Called: When the mouse wheel moves/changes.
+ // Params:
+ // localx: Local mouse X position.
+ // localy: Local mouse Y position.
+ // d: The mouse wheel movement value.
+ ///
+ virtual void OnMouseWheel(int localx, int localy, int d);
+
+ ///
+ // Called: When the mouse wheel moves/changes on top of the item.
+ // Params:
+ // localx: Local mouse X position.
+ // localy: Local mouse Y position.
+ // d: The mouse wheel movement value.
+ ///
+ virtual void OnMouseWheelInside(int localx, int localy, int d);
+
+ ///
+ // Called: When a key is pressed.
+ // Params:
+ // key: The value of the key that is being pressed.
+ // shift: Shift key is down.
+ // ctrl: Control key is down.
+ // alt: Alternate key is down.
+ ///
+ virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt);
+
+ ///
+ // Called: When a key is released.
+ // Params:
+ // key: The value of the key that is being released.
+ // shift: Shift key is released.
+ // ctrl: Control key is released.
+ // alt: Alternate key is released.
+ ///
+ virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt);
+
+ private:
+ Window* parentstate_;
+ Panel* _parent;
+ };
+}
diff --git a/src/interface/ControlFactory.cpp b/src/interface/ControlFactory.cpp
index 372ed31..7132499 100644
--- a/src/interface/ControlFactory.cpp
+++ b/src/interface/ControlFactory.cpp
@@ -50,10 +50,5 @@ ui::Panel * ControlFactory::MainMenu(int x, int y, int width, int height)
mainMenu->AddChild(tempButton); //Render options
currentX += 18;
- tempButton = new ui::Button(ui::Point(currentX, 1), ui::Point(16, height-2), "\x90"); //Pause
- tempButton->SetTogglable(true);
- mainMenu->AddChild(tempButton);
- currentX += 18;
-
return mainMenu;
}
diff --git a/src/interface/ControlFactory.h b/src/interface/ControlFactory.h
new file mode 100644
index 0000000..0f8ad61
--- /dev/null
+++ b/src/interface/ControlFactory.h
@@ -0,0 +1,14 @@
+#ifndef CONTROLFACTORY_H
+#define CONTROLFACTORY_H
+
+#include "Panel.h"
+#include "Engine.h"
+
+class ControlFactory
+{
+public:
+ static ui::Panel * MainMenu(int x, int y, int width, int height);
+
+};
+
+#endif // CONTROLFACTORY_H
diff --git a/src/interface/Engine.cpp b/src/interface/Engine.cpp
index 25f2038..55a2370 100644
--- a/src/interface/Engine.cpp
+++ b/src/interface/Engine.cpp
@@ -1,22 +1,25 @@
#include <iostream>
#include "Config.h"
+#include <stack>
+
#include "Global.h"
+#include "interface/Window.h"
#include "interface/Platform.h"
#include "interface/Engine.h"
-#include "interface/State.h"
#include "Graphics.h"
using namespace ui;
+using namespace std;
Engine::Engine():
state_(NULL),
- statequeued_(NULL),
mousex_(0),
mousey_(0),
mousexp_(0),
mouseyp_(0),
- FpsLimit(60.0f)
+ FpsLimit(60.0f),
+ windows(stack<Window*>())
{
}
@@ -40,7 +43,30 @@ void Engine::Exit()
running_ = false;
}
-void Engine::SetState(State * state)
+void Engine::ShowWindow(Window * window)
+{
+ if(state_)
+ {
+ windows.push(window);
+ }
+ state_ = window;
+ windows.push(window);
+}
+
+void Engine::CloseWindow()
+{
+ if(!windows.empty())
+ {
+ state_ = windows.top();
+ windows.pop();
+ }
+ else
+ {
+ state_ = NULL;
+ }
+}
+
+/*void Engine::SetState(State * state)
{
if(state_) //queue if currently in a state
statequeued_ = state;
@@ -50,7 +76,7 @@ void Engine::SetState(State * state)
if(state_)
state_->DoInitialized();
}
-}
+}*/
void Engine::SetSize(int width, int height)
{
@@ -63,7 +89,7 @@ void Engine::Tick(float dt)
if(state_ != NULL)
state_->DoTick(dt);
- if(statequeued_ != NULL)
+ /*if(statequeued_ != NULL)
{
if(state_ != NULL)
{
@@ -76,15 +102,15 @@ void Engine::Tick(float dt)
if(state_ != NULL)
state_->DoInitialized();
- }
+ }*/
}
void Engine::Draw()
{
if(state_)
state_->DoDraw();
- Global::Ref().g->Blit();
- Global::Ref().g->Clear();
+ g->Blit();
+ g->Clear();
}
void Engine::onKeyPress(int key, bool shift, bool ctrl, bool alt)
diff --git a/src/interface/Engine.h b/src/interface/Engine.h
new file mode 100644
index 0000000..7bf78f9
--- /dev/null
+++ b/src/interface/Engine.h
@@ -0,0 +1,71 @@
+#pragma once
+
+#include <stack>
+#include <SDL/SDL.h>
+#include "Singleton.h"
+#include "Platform.h"
+#include "Graphics.h"
+#include "Window.h"
+
+namespace ui
+{
+ class Window;
+
+ /* class Engine
+ *
+ * Controls the User Interface.
+ * Send user inputs to the Engine and the appropriate controls and components will interact.
+ */
+ class Engine: public Singleton<Engine>
+ {
+ public:
+ Engine();
+ ~Engine();
+
+ void ShowWindow(Window * window);
+ void CloseWindow();
+
+ void onMouseMove(int x, int y);
+ void onMouseClick(int x, int y, unsigned button);
+ void onMouseUnclick(int x, int y, unsigned button);
+ void onMouseWheel(int x, int y, int delta);
+ void onKeyPress(int key, bool shift, bool ctrl, bool alt);
+ void onKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ void onResize(int newWidth, int newHeight);
+ void onClose();
+
+ void Begin(int width, int height);
+ inline bool Running() { return running_; }
+ void Exit();
+
+ void Tick(float dt);
+ void Draw();
+
+ inline int GetMouseX() { return mousex_; }
+ inline int GetMouseY() { return mousey_; }
+ inline int GetWidth() { return width_; }
+ inline int GetHeight() { return height_; }
+
+ inline void SetSize(int width, int height);
+
+ //void SetState(Window* state);
+ //inline State* GetState() { return state_; }
+ inline Window* GetWindow() { return state_; }
+ float FpsLimit;
+ Graphics * g;
+ private:
+ std::stack<Window*> windows;
+ //Window* statequeued_;
+ Window* state_;
+
+ bool running_;
+
+ int mousex_;
+ int mousey_;
+ int mousexp_;
+ int mouseyp_;
+ int width_;
+ int height_;
+ };
+
+}
diff --git a/src/interface/Label.cpp b/src/interface/Label.cpp
index c77b6bf..cf09d4e 100644
--- a/src/interface/Label.cpp
+++ b/src/interface/Label.cpp
@@ -6,7 +6,7 @@
using namespace ui;
-Label::Label(State* parent_state, std::string labelText):
+Label::Label(Window* parent_state, std::string labelText):
Component(parent_state),
LabelText(labelText)
{
@@ -35,6 +35,6 @@ Label::~Label()
void Label::Draw(const Point& screenPos)
{
- Graphics * g = Global::Ref().g;
+ Graphics * g = 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/Label.h b/src/interface/Label.h
new file mode 100644
index 0000000..e56852e
--- /dev/null
+++ b/src/interface/Label.h
@@ -0,0 +1,26 @@
+#ifndef LABEL_H
+#define LABEL_H
+
+#include <string>
+
+#include "Component.h"
+
+namespace ui
+{
+ class Label : public Component
+ {
+ public:
+ Label(Window* parent_state, std::string labelText);
+
+ Label(Point position, Point size, std::string labelText);
+
+ Label(std::string labelText);
+ virtual ~Label();
+
+ std::string LabelText;
+
+ virtual void Draw(const Point& screenPos);
+ };
+}
+
+#endif // LABEL_H
diff --git a/src/interface/Panel.cpp b/src/interface/Panel.cpp
index e44663a..acfcf53 100644
--- a/src/interface/Panel.cpp
+++ b/src/interface/Panel.cpp
@@ -5,12 +5,12 @@
#include "interface/Panel.h"
#include "interface/Point.h"
-#include "interface/State.h"
+#include "interface/Window.h"
#include "interface/Component.h"
using namespace ui;
-Panel::Panel(State* parent_state):
+Panel::Panel(Window* parent_state):
Component(parent_state)
{
@@ -84,7 +84,7 @@ void Panel::Draw(const Point& screenPos)
// the component must be visible
if(children[i]->Visible)
{
- if(GetParentState()->AllowExclusiveDrawing)
+ if(GetParentWindow()->AllowExclusiveDrawing)
{
//who cares if the component is off the screen? draw anyway.
Point scrpos = screenPos + children[i]->Position;
@@ -143,7 +143,7 @@ void Panel::OnMouseClick(int localx, int localy, unsigned button)
localy < children[i]->Position.Y + children[i]->Size.Y )
{
childclicked = true;
- GetParentState()->FocusComponent(children[i]);
+ GetParentWindow()->FocusComponent(children[i]);
children[i]->OnMouseClick(localx - children[i]->Position.X, localy - children[i]->Position.Y, button);
break;
}
@@ -154,7 +154,7 @@ void Panel::OnMouseClick(int localx, int localy, unsigned button)
if(!childclicked)
{
XOnMouseClick(localx, localy, button);
- GetParentState()->FocusComponent(this);
+ GetParentWindow()->FocusComponent(this);
}
}
diff --git a/src/interface/Panel.h b/src/interface/Panel.h
new file mode 100644
index 0000000..51f52aa
--- /dev/null
+++ b/src/interface/Panel.h
@@ -0,0 +1,136 @@
+#pragma once
+#include <vector>
+//#include "Platform.h"
+
+#include "interface/Point.h"
+#include "interface/Window.h"
+#include "interface/Component.h"
+
+namespace ui
+{
+ /* class XComponent
+ *
+ * An eXtension of the Component class.
+ * Adds the ability to have child components.
+ *
+ * See sys::Component
+ */
+class Component;
+ class Panel : public Component
+ {
+ public:
+ friend class Component;
+
+ Panel(Window* parent_state);
+ Panel(Point position, Point size);
+ Panel();
+ virtual ~Panel();
+
+ /* Add a child component.
+ * Similar to XComponent::SetParent
+ *
+ * If the component is already parented, then this will become the new parent.
+ */
+ void AddChild(Component* c);
+
+ // Remove child from component. This DOES NOT free the component from memory.
+ void RemoveChild(Component* c);
+
+ // Remove child from component. This WILL free the component from memory unless told otherwise.
+ void RemoveChild(unsigned idx, bool freeMem = true);
+
+ //Grab the number of children this component owns.
+ int GetChildCount();
+
+ //Get child of this component by index.
+ Component* GetChild(unsigned idx);
+
+ void Tick(float dt);
+ void Draw(const Point& screenPos);
+
+ void OnMouseHover(int localx, int localy);
+ void OnMouseMoved(int localx, int localy, int dx, int dy);
+ void OnMouseMovedInside(int localx, int localy, int dx, int dy);
+ void OnMouseEnter(int localx, int localy);
+ void OnMouseLeave(int localx, int localy);
+ void OnMouseDown(int x, int y, unsigned button);
+ void OnMouseUp(int x, int y, unsigned button);
+ void OnMouseClick(int localx, int localy, unsigned button);
+ void OnMouseUnclick(int localx, int localy, unsigned button);
+ void OnMouseWheel(int localx, int localy, int d);
+ void OnMouseWheelInside(int localx, int localy, int d);
+ void OnKeyPress(int key, bool shift, bool ctrl, bool alt);
+ void OnKeyRelease(int key, bool shift, bool ctrl, bool alt);
+
+ protected:
+ // child components
+ std::vector<ui::Component*> children;
+
+ //UI functions:
+ /*
+ void XTick(float dt);
+ void XDraw(const Point& screenPos);
+
+ void XOnMouseHover(int localx, int localy);
+ void XOnMouseMoved(int localx, int localy, int dx, int dy);
+ void XOnMouseMovedInside(int localx, int localy, int dx, int dy);
+ void XOnMouseEnter(int localx, int localy);
+ void XOnMouseLeave(int localx, int localy);
+ void XOnMouseDown(int x, int y, unsigned int button);
+ void XOnMouseUp(int x, int y, unsigned int button);
+ void XOnMouseClick(int localx, int localy, unsigned int button);
+ void XOnMouseUnclick(int localx, int localy, unsigned int button);
+ void XOnMouseWheel(int localx, int localy, int d);
+ void XOnMouseWheelInside(int localx, int localy, int d);
+ void XOnKeyPress(int key, bool shift, bool ctrl, bool alt);
+ void XOnKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ */
+
+ // Overridable. Called by XComponent::Tick()
+ virtual void XTick(float dt);
+
+ // Overridable. Called by XComponent::Draw()
+ virtual void XDraw(const Point& screenPos);
+
+
+ // Overridable. Called by XComponent::XOnMouseHover()
+ virtual void XOnMouseHover(int localx, int localy);
+
+ // Overridable. Called by XComponent::OnMouseMoved()
+ virtual void XOnMouseMoved(int localx, int localy, int dx, int dy);
+
+ // Overridable. Called by XComponent::OnMouseMovedInside()
+ virtual void XOnMouseMovedInside(int localx, int localy, int dx, int dy);
+
+ // Overridable. Called by XComponent::OnMouseEnter()
+ virtual void XOnMouseEnter(int localx, int localy);
+
+ // Overridable. Called by XComponent::OnMouseLeave()
+ virtual void XOnMouseLeave(int localx, int localy);
+
+ // Overridable. Called by XComponent::OnMouseDown()
+ virtual void XOnMouseDown(int x, int y, unsigned button);
+
+ // Overridable. Called by XComponent::OnMouseUp()
+ virtual void XOnMouseUp(int x, int y, unsigned button);
+
+ // Overridable. Called by XComponent::OnMouseClick()
+ virtual void XOnMouseClick(int localx, int localy, unsigned button);
+
+ // Overridable. Called by XComponent::OnMouseUnclick()
+ virtual void XOnMouseUnclick(int localx, int localy, unsigned button);
+
+ // Overridable. Called by XComponent::OnMouseWheel()
+ virtual void XOnMouseWheel(int localx, int localy, int d);
+
+ // Overridable. Called by XComponent::OnMouseWheelInside()
+ virtual void XOnMouseWheelInside(int localx, int localy, int d);
+
+ // Overridable. Called by XComponent::OnKeyPress()
+ virtual void XOnKeyPress(int key, bool shift, bool ctrl, bool alt);
+
+ // Overridable. Called by XComponent::OnKeyRelease()
+ virtual void XOnKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ };
+
+}
diff --git a/src/interface/Platform.h b/src/interface/Platform.h
new file mode 100644
index 0000000..c57dca6
--- /dev/null
+++ b/src/interface/Platform.h
@@ -0,0 +1,108 @@
+#pragma once
+
+
+/* ***** Platform-ness ***** */
+
+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32_LEAN_AND_MEAN)
+# define IEF_PLATFORM_WIN32
+# ifndef WIN32_LEAN_AND_MEAN
+# define WIN32_LEAN_AND_MEAN
+# endif
+#elif defined(linux) || defined(_linux) || defined(__linux)
+# define IEF_PLATFORM_LINUX
+
+#elif defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh)
+# define IEF_PLATFORM_MACOSX
+
+//#elif defined(__FreeBSD__) || define(__FreeBSD_kernel__)
+//# define IEF_PLATFORM_FREEBSD
+
+#else
+# error Operating System not supported.
+#endif
+
+
+/* ***** Endian-ness ***** */
+
+#if defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || (defined(__MIPS__) && defined(__MISPEB__)) || defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || defined(__sparc__) || defined(__hppa__)
+# define IEF_ENDIAN_BIG
+
+#else
+# define IEF_ENDIAN_LITTLE
+#endif
+
+
+/* ***** Debug-ness ***** */
+
+#if !defined(NDEBUG) || defined(_DEBUG)
+# define IEF_DEBUG
+#endif
+
+
+/* ***** Primitive Types ***** */
+
+#ifndef NULL
+# define NULL 0
+#endif
+
+#include <climits>
+namespace sys
+{
+
+#if UCHAR_MAX == 0xFF //char
+ typedef signed char s8;
+ typedef unsigned char u8;
+#else
+# error No 8-Bit Integer supported.
+#endif
+#if USHRT_MAX == 0xFFFF //short
+ typedef signed short s16;
+ typedef unsigned short u16;
+#elif UINT_MAX == 0xFFFF
+ typedef signed int s16;
+ typedef unsigned int u16;
+#elif ULONG_MAX == 0xFFFF
+ typedef signed long s16;
+ typedef unsigned long u16;
+ #else
+ # error No 16-Bit Integer supported.
+ #endif
+ #if USHRT_MAX == 0xFFFFFFFF //int
+ typedef signed short s32;
+ typedef unsigned short u32;
+#elif UINT_MAX == 0xFFFFFFFF
+ typedef signed int s32;
+ typedef unsigned int u32;
+#elif ULONG_MAX == 0xFFFFFFFF
+ typedef signed long s32;
+ typedef unsigned long u32;
+ #else
+ # error No 32-Bit Integer supported.
+ #endif
+#if UINT_MAX == 0xFFFFFFFFFFFFFFFF //long
+ typedef signed int s64;
+ typedef unsigned int u64;
+#elif ULONG_MAX == 0xFFFFFFFFFFFFFFFF
+ typedef signed long s64;
+ typedef unsigned long u64;
+#elif ULLONG_MAX == 0xFFFFFFFFFFFFFFFF
+ typedef signed long long s64;
+ typedef unsigned long long u64;
+#else
+# pragma message("Warning: 64-bit not supported. s64 and u64 defined as 32-bit.")
+ typedef s32 s64;
+ typedef u32 u64;
+#endif
+//floating
+typedef float f32;
+typedef double f64;
+//misc
+typedef u8 byte;
+typedef u8 ubyte;
+typedef s8 sbyte;
+typedef s64 llong;
+typedef s64 sllong;
+typedef u64 ullong;
+typedef char* cstring;
+
+} //namespace sys
diff --git a/src/interface/Point.h b/src/interface/Point.h
new file mode 100644
index 0000000..0d0250c
--- /dev/null
+++ b/src/interface/Point.h
@@ -0,0 +1,136 @@
+#pragma once
+#include "Platform.h"
+
+namespace ui
+{
+
+//Lightweight 2D Int32/Float32 Point struct for UI
+struct Point
+{
+#if ENABLE_FLOAT_UI
+# define POINT_T float
+#else
+# define POINT_T int
+#endif
+
+ POINT_T X;
+ POINT_T Y;
+
+ Point(POINT_T x, POINT_T y)
+ : X(x)
+ , Y(y)
+ {
+ }
+
+ inline Point operator - () const
+ {
+ return Point(-X, -Y);
+ }
+
+ inline Point operator + (const Point& v) const
+ {
+ return Point(X + v.X, Y + v.Y);
+ }
+
+ inline Point operator - (const Point& v) const
+ {
+ return Point(X - v.X, Y - v.Y);
+ }
+
+ inline Point operator * (const Point& v) const
+ {
+ return Point(X * v.X, Y * v.Y);
+ }
+
+ inline Point operator * (int v) const
+ {
+ return Point(X * static_cast<POINT_T>(v), Y * static_cast<POINT_T>(v));
+ }
+
+ inline Point operator * (float v) const
+ {
+ return Point(X * static_cast<POINT_T>(v), Y * static_cast<POINT_T>(v));
+ }
+
+ inline Point operator / (const Point& v) const
+ {
+ return Point(X / v.X, Y / v.Y);
+ }
+
+ inline Point operator / (int v) const
+ {
+ return Point(X / static_cast<POINT_T>(v), Y / static_cast<POINT_T>(v));
+ }
+
+ inline Point operator / (float v) const
+ {
+ return Point(X / static_cast<POINT_T>(v), Y / static_cast<POINT_T>(v));
+ }
+
+ inline void operator += (const Point& v)
+ {
+ X += v.X;
+ Y += v.Y;
+ }
+
+ inline void operator -= (const Point& v)
+ {
+ X -= v.X;
+ Y -= v.Y;
+ }
+
+ inline void operator *= (const Point& v)
+ {
+ X *= v.X;
+ Y *= v.Y;
+ }
+
+ inline void operator *= (int v)
+ {
+ X *= static_cast<POINT_T>(v);
+ Y *= static_cast<POINT_T>(v);
+ }
+
+ inline void operator *= (float v)
+ {
+ X *= static_cast<POINT_T>(v);
+ Y *= static_cast<POINT_T>(v);
+ }
+
+ inline void operator /= (const Point& v)
+ {
+ X /= v.X;
+ Y /= v.Y;
+ }
+
+ inline void operator /= (int v)
+ {
+ X /= static_cast<POINT_T>(v);
+ Y /= static_cast<POINT_T>(v);
+ }
+
+ inline void operator /= (float v)
+ {
+ X /= static_cast<POINT_T>(v);
+ Y /= static_cast<POINT_T>(v);
+ }
+
+ inline bool operator == (const Point& v) const
+ {
+ return (X == v.X && Y == v.Y);
+ }
+
+ inline bool operator != (const Point& v) const
+ {
+ return (X != v.X || Y != v.Y);
+ }
+
+ inline void operator = (const Point& v)
+ {
+ X = v.X;
+ Y = v.Y;
+ }
+
+};
+
+}
diff --git a/src/interface/Sandbox.cpp b/src/interface/Sandbox.cpp
index a9760e7..9a858f8 100644
--- a/src/interface/Sandbox.cpp
+++ b/src/interface/Sandbox.cpp
@@ -16,6 +16,7 @@
#include "interface/Component.h"
#include "Renderer.h"
#include "Simulation.h"
+#include "Engine.h"
namespace ui {
@@ -60,7 +61,7 @@ void Sandbox::OnMouseUp(int localx, int localy, unsigned int button)
void Sandbox::Draw(const Point& screenPos)
{
- Graphics * g = Global::Ref().g;
+ Graphics * g = Engine::Ref().g;
if(!ren)
ren = new Renderer(g, sim);
ren->render_parts();
diff --git a/src/interface/Sandbox.h b/src/interface/Sandbox.h
new file mode 100644
index 0000000..fb4a668
--- /dev/null
+++ b/src/interface/Sandbox.h
@@ -0,0 +1,39 @@
+/*
+ * Sandbox.h
+ *
+ * Created on: Jan 8, 2012
+ * Author: Simon
+ */
+
+#ifndef SANDBOX_H_
+#define SANDBOX_H_
+
+#include <queue>
+#include "Point.h"
+#include "Component.h"
+#include "Simulation.h"
+#include "Renderer.h"
+
+namespace ui {
+
+class Sandbox: public ui::Component {
+private:
+ int lastCoordX, lastCoordY;
+ int activeElement;
+ std::queue<Point*> pointQueue;
+ bool isMouseDown;
+ Renderer * ren;
+ Simulation * sim;
+public:
+ Sandbox();
+ virtual Simulation * GetSimulation();
+ virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
+ virtual void OnMouseClick(int localx, int localy, unsigned int button);
+ virtual void OnMouseUp(int localx, int localy, unsigned int button);
+ virtual void Draw(const Point& screenPos);
+ virtual void Tick(float delta);
+ virtual ~Sandbox();
+};
+
+} /* namespace ui */
+#endif /* SANDBOX_H_ */
diff --git a/src/interface/State.cpp b/src/interface/Window.cpp
index 665e2e8..e28d34b 100644
--- a/src/interface/State.cpp
+++ b/src/interface/Window.cpp
@@ -1,30 +1,29 @@
-#include <vector>
-#include "interface/Component.h"
-#include "interface/Engine.h"
-#include "interface/State.h"
-//#include "Platform.h"
+#include "Window.h"
+#include "Component.h"
+#include "interface/Point.h"
using namespace ui;
-State::State()
-: UserData(NULL)
-, focusedComponent_(NULL)
+Window::Window(Point _position, Point _size):
+ Position(_position),
+ Size(_size),
+ focusedComponent_(NULL)
{
}
-State::~State()
+Window::~Window()
{
for(unsigned i = 0, sz = Components.size(); i < sz; ++i)
if( Components[i] )
delete Components[i];
}
-void State::AddComponent(Component* c)
+void Window::AddComponent(Component* c)
{
// TODO: do a check if component was already added?
- if(c->GetParentState()==NULL)
+ if(c->GetParentWindow()==NULL)
{
- c->SetParentState(this);
+ c->SetParentWindow(this);
Components.push_back(c);
}
else
@@ -33,17 +32,17 @@ void State::AddComponent(Component* c)
}
}
-unsigned State::GetComponentCount()
+unsigned Window::GetComponentCount()
{
return Components.size();
}
-Component* State::GetComponent(unsigned idx)
+Component* Window::GetComponent(unsigned idx)
{
return Components[idx];
}
-void State::RemoveComponent(Component* c)
+void Window::RemoveComponent(Component* c)
{
// remove component WITHOUT freeing it.
for(unsigned i = 0; i < Components.size(); ++i)
@@ -52,43 +51,43 @@ void State::RemoveComponent(Component* c)
if(Components[i] == c)
{
Components.erase(Components.begin() + i);
-
+
// we're done
return;
}
}
}
-void State::RemoveComponent(unsigned idx)
+void Window::RemoveComponent(unsigned idx)
{
// free component and remove it.
delete Components[idx];
Components.erase(Components.begin() + idx);
}
-bool State::IsFocused(const Component* c) const
+bool Window::IsFocused(const Component* c) const
{
return c == focusedComponent_;
}
-void State::FocusComponent(Component* c)
+void Window::FocusComponent(Component* c)
{
this->focusedComponent_ = c;
}
-void State::DoExit()
+void Window::DoExit()
{
OnExit();
}
-void State::DoInitialized()
+void Window::DoInitialized()
{
OnInitialized();
}
-void State::DoDraw()
+void Window::DoDraw()
{
//draw
for(int i = 0, sz = Components.size(); i < sz; ++i)
@@ -101,12 +100,12 @@ void State::DoDraw()
}
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() )
+ if( Components[i]->Position.X+Position.X + Components[i]->Size.X >= 0 &&
+ Components[i]->Position.Y+Position.Y + Components[i]->Size.Y >= 0 &&
+ Components[i]->Position.X+Position.X < ui::Engine::Ref().GetWidth() &&
+ Components[i]->Position.Y+Position.Y < ui::Engine::Ref().GetHeight() )
{
- Point scrpos(Components[i]->Position.X, Components[i]->Position.Y);
+ Point scrpos(Components[i]->Position.X + Position.X, Components[i]->Position.Y + Position.Y);
Components[i]->Draw( Point(scrpos) );
}
}
@@ -115,22 +114,22 @@ void State::DoDraw()
OnDraw();
}
-void State::DoTick(float dt)
+void Window::DoTick(float dt)
{
//on mouse hover
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 )
+ ui::Engine::Ref().GetMouseX() >= Components[i]->Position.X+Position.X &&
+ ui::Engine::Ref().GetMouseY() >= Components[i]->Position.Y+Position.Y &&
+ ui::Engine::Ref().GetMouseX() < Components[i]->Position.X+Position.X + Components[i]->Size.X &&
+ ui::Engine::Ref().GetMouseY() < Components[i]->Position.Y+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);
+ Components[i]->OnMouseHover(ui::Engine::Ref().GetMouseX() - (Components[i]->Position.X + Position.X), ui::Engine::Ref().GetMouseY() - (Components[i]->Position.Y + Position.Y));
break;
}
}
-
+
//tick
for(int i = 0, sz = Components.size(); i < sz; ++i)
{
@@ -140,7 +139,7 @@ void State::DoTick(float dt)
OnTick(dt);
}
-void State::DoKeyPress(int key, bool shift, bool ctrl, bool alt)
+void Window::DoKeyPress(int key, bool shift, bool ctrl, bool alt)
{
//on key press
if(focusedComponent_ != NULL)
@@ -152,7 +151,7 @@ void State::DoKeyPress(int key, bool shift, bool ctrl, bool alt)
OnKeyPress(key, shift, ctrl, alt);
}
-void State::DoKeyRelease(int key, bool shift, bool ctrl, bool alt)
+void Window::DoKeyRelease(int key, bool shift, bool ctrl, bool alt)
{
//on key unpress
if(focusedComponent_ != NULL)
@@ -164,7 +163,7 @@ void State::DoKeyRelease(int key, bool shift, bool ctrl, bool alt)
OnKeyRelease(key, shift, ctrl, alt);
}
-void State::DoMouseDown(int x, int y, unsigned button)
+void Window::DoMouseDown(int x, int y, unsigned button)
{
//on mouse click
bool clickState = false;
@@ -195,7 +194,7 @@ void State::DoMouseDown(int x, int y, unsigned button)
OnMouseDown(x, y, button);
}
-void State::DoMouseMove(int x, int y, int dx, int dy)
+void Window::DoMouseMove(int x, int y, int dx, int dy)
{
//on mouse move (if true, and inside)
for(int i = Components.size() - 1; i > -1 ; --i)
@@ -204,16 +203,16 @@ void State::DoMouseMove(int x, int y, int dx, int dy)
{
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 &&
@@ -234,7 +233,7 @@ void State::DoMouseMove(int x, int y, int dx, int dy)
{
Components[i]->OnMouseLeave(local.X, local.Y);
}
-
+
}
}
}
@@ -242,7 +241,7 @@ void State::DoMouseMove(int x, int y, int dx, int dy)
OnMouseMove(x, y, dx, dy);
}
-void State::DoMouseUp(int x, int y, unsigned button)
+void Window::DoMouseUp(int x, int y, unsigned button)
{
//on mouse unclick
for(int i = Components.size() - 1; i >= 0 ; --i)
@@ -267,7 +266,7 @@ void State::DoMouseUp(int x, int y, unsigned button)
OnMouseUp(x, y, button);
}
-void State::DoMouseWheel(int x, int y, int d)
+void Window::DoMouseWheel(int x, int y, int d)
{
//on mouse wheel focused
for(int i = Components.size() - 1; i >= 0 ; --i)
@@ -279,7 +278,7 @@ void State::DoMouseWheel(int x, int y, int d)
break;
}
}
-
+
//on mouse wheel
for(int i = Components.size() - 1; i >= 0 ; --i)
{
@@ -289,3 +288,4 @@ void State::DoMouseWheel(int x, int y, int d)
OnMouseWheel(x, y, d);
}
+
diff --git a/src/interface/Window.h b/src/interface/Window.h
new file mode 100644
index 0000000..581b91f
--- /dev/null
+++ b/src/interface/Window.h
@@ -0,0 +1,103 @@
+#ifndef WINDOW_H
+#define WINDOW_H
+
+#include <vector>
+#include "interface/Point.h"
+#include "Engine.h"
+
+namespace ui
+{
+
+enum ChromeStyle
+{
+ None, Title, Resizable
+};
+//class State;
+ class Engine;
+ class Component;
+
+ /* class State
+ *
+ * A UI state. Contains all components.
+ */
+ class Window
+ {
+ public:
+ Window(Point _position, Point _size);
+ virtual ~Window();
+
+ bool AllowExclusiveDrawing; //false will not call draw on objects outside of bounds
+
+ // Add Component to state
+ void AddComponent(Component* c);
+
+ // Get the number of components this state has.
+ unsigned GetComponentCount();
+
+ // Get component by index. (See GetComponentCount())
+ Component* GetComponent(unsigned idx);
+
+ // Remove a component from state. NOTE: This DOES NOT free component from memory.
+ void RemoveComponent(Component* c);
+
+ // Remove a component from state. NOTE: This WILL free component from memory.
+ void RemoveComponent(unsigned idx);
+
+ virtual void DoInitialized();
+ virtual void DoExit();
+ virtual void DoTick(float dt);
+ virtual void DoDraw();
+
+ virtual void DoMouseMove(int x, int y, int dx, int dy);
+ virtual void DoMouseDown(int x, int y, unsigned button);
+ virtual void DoMouseUp(int x, int y, unsigned button);
+ virtual void DoMouseWheel(int x, int y, int d);
+ virtual void DoKeyPress(int key, bool shift, bool ctrl, bool alt);
+ virtual void DoKeyRelease(int key, bool shift, bool ctrl, bool alt);
+
+ bool IsFocused(const Component* c) const;
+ void FocusComponent(Component* c);
+
+ void* UserData;
+
+ protected:
+ virtual void OnInitialized() {}
+ virtual void OnExit() {}
+ virtual void OnTick(float dt) {}
+ virtual void OnDraw() {}
+
+ virtual void OnMouseMove(int x, int y, int dx, int dy) {}
+ virtual void OnMouseDown(int x, int y, unsigned button) {}
+ virtual void OnMouseUp(int x, int y, unsigned button) {}
+ virtual void OnMouseWheel(int x, int y, int d) {}
+ virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt) {}
+ virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt) {}
+ std::vector<Component*> Components;
+ Component* focusedComponent_;
+
+ Point Position;
+ Point Size;
+ ChromeStyle chrome;
+
+ };
+
+
+/*class Window : public State
+{
+private:
+ ChromeStyle chrome;
+public:
+ Window(Point _position, Point _size);
+ Point Position;
+ Point Size;
+
+ virtual void DoTick(float dt);
+ virtual void DoDraw();
+
+ virtual void DoMouseMove(int x, int y, int dx, int dy);
+ virtual void DoMouseDown(int x, int y, unsigned button);
+ virtual void DoMouseUp(int x, int y, unsigned button);
+ virtual void DoMouseWheel(int x, int y, int d);
+};*/
+}
+#endif // WINDOW_H