summaryrefslogtreecommitdiff
path: root/src/interface
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-08 17:39:03 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-08 17:39:03 (GMT)
commitb0ea52690ba56a0d0602ad8674b7e5ab2ba3e778 (patch)
tree7d72e0509f4d2643d3be837a337d088ca5949c73 /src/interface
downloadpowder-b0ea52690ba56a0d0602ad8674b7e5ab2ba3e778.zip
powder-b0ea52690ba56a0d0602ad8674b7e5ab2ba3e778.tar.gz
Initial
Diffstat (limited to 'src/interface')
-rw-r--r--src/interface/Button.cpp127
-rw-r--r--src/interface/Component.cpp89
-rw-r--r--src/interface/Panel.cpp23
-rw-r--r--src/interface/Sandbox.cpp57
-rw-r--r--src/interface/State.cpp232
-rw-r--r--src/interface/Window.cpp23
6 files changed, 551 insertions, 0 deletions
diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp
new file mode 100644
index 0000000..a357c36
--- /dev/null
+++ b/src/interface/Button.cpp
@@ -0,0 +1,127 @@
+/*
+ * Button.cpp
+ *
+ * Created on: Jan 8, 2012
+ * Author: Simon
+ */
+
+#include <iostream>
+
+#include "interface/Button.h"
+#include "Graphics.h"
+
+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)
+{
+
+}
+
+void Button::Draw(void* userdata)
+{
+ Graphics * 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);
+ }
+ 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);
+ }
+ /*sf::RenderWindow* rw = reinterpret_cast<sf::RenderWindow*>(userdata); //it better be a RenderWindow or so help your god
+
+ //Draw component here
+ sf::Text textGraphic(ButtonText);
+ textGraphic.SetCharacterSize(11);
+ if(isButtonDown)
+ textGraphic.SetColor(sf::Color::Black);
+ else
+ textGraphic.SetColor(sf::Color::White);
+ sf::FloatRect tempRect = textGraphic.GetRect();
+ textGraphic.SetPosition(ceil(X + Width/2 - tempRect.Width/2), ceil(Y + Height/2 - tempRect.Height/2));
+
+ if(isMouseInside)
+ {
+ if(isButtonDown)
+ rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black));
+ else
+ rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::Black, 2.f, sf::Color::White));
+ }
+ else
+ {
+ if(isButtonDown)
+ rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black));
+ else
+ rw->Draw(sf::Shape::Rectangle(X+1, Y+1, Width-2, Width-2, sf::Color::Black, 1.f, sf::Color::White));
+ }
+
+ rw->Draw(textGraphic);*/
+}
+
+void Button::OnMouseUnclick(int x, int y, unsigned int button)
+{
+ if(button != 1)
+ {
+ return; //left click only!
+ }
+
+ if(isButtonDown)
+ {
+ if(state)
+ {
+ state = false;
+ }
+ else
+ {
+ if(Toggleable)
+ {
+ state = true;
+ }
+ 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!
+
+ isButtonDown = false;
+}
+
+void Button::OnMouseClick(int x, int y, unsigned int button)
+{
+ if(button != 1) return; //left click only!
+
+ isButtonDown = true;
+}
+
+void Button::OnMouseEnter(int x, int y, int dx, int dy)
+{
+ isMouseInside = true;
+}
+
+void Button::OnMouseLeave(int x, int y, int dx, int dy)
+{
+ isMouseInside = false;
+}
+
+void Button::DoAction()
+{
+ std::cout << "Do action!"<<std::endl;
+}
+
+} /* namespace ui */
diff --git a/src/interface/Component.cpp b/src/interface/Component.cpp
new file mode 100644
index 0000000..48a329b
--- /dev/null
+++ b/src/interface/Component.cpp
@@ -0,0 +1,89 @@
+/*
+ * Component.cpp
+ *
+ * Created on: Jan 8, 2012
+ * Author: Simon
+ */
+
+#include "interface/Component.h"
+
+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()
+{
+}
+
+void Component::Draw(void* userdata)
+{
+}
+
+void Component::Tick(float dt)
+{
+}
+
+void Component::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
+{
+}
+
+void Component::OnKeyRelease(int key, bool shift, bool ctrl, bool alt)
+{
+}
+
+void Component::OnMouseEnter(int localx, int localy, int dx, int dy)
+{
+}
+
+void Component::OnMouseLeave(int localx, int localy, int dx, int dy)
+{
+}
+
+void Component::OnMouseClick(int localx, int localy, unsigned int button)
+{
+}
+
+void Component::OnMouseUnclick(int localx, int localy, unsigned int button)
+{
+}
+
+void Component::OnMouseDown(int localx, int localy, unsigned int button)
+{
+}
+
+void Component::OnMouseHover(int localx, int localy)
+{
+}
+
+void Component::OnMouseMoved(int localx, int localy, int dx, int dy)
+{
+}
+
+void Component::OnMouseMovedInside(int localx, int localy, int dx, int dy)
+{
+}
+
+void Component::OnMouseUp(int localx, int localy, unsigned int button)
+{
+}
+
+void Component::OnMouseWheel(int localx, int localy, int d)
+{
+}
+
+void Component::OnMouseWheelInside(int localx, int localy, int d)
+{
+}
+
+void Component::OnMouseWheelFocused(int localx, int localy, int d)
+{
+}
+} /* namespace ui */
diff --git a/src/interface/Panel.cpp b/src/interface/Panel.cpp
new file mode 100644
index 0000000..164bfa3
--- /dev/null
+++ b/src/interface/Panel.cpp
@@ -0,0 +1,23 @@
+/*
+ * Panel.cpp
+ *
+ * Created on: Jan 8, 2012
+ * Author: Simon
+ */
+
+#include "interface/Panel.h"
+
+namespace ui {
+
+Panel::Panel(int x, int y, int width, int height):
+ Component(x, y, width, height)
+{
+ // TODO Auto-generated constructor stub
+
+}
+
+Panel::~Panel() {
+ // TODO Auto-generated destructor stub
+}
+
+} /* namespace ui */
diff --git a/src/interface/Sandbox.cpp b/src/interface/Sandbox.cpp
new file mode 100644
index 0000000..c33571a
--- /dev/null
+++ b/src/interface/Sandbox.cpp
@@ -0,0 +1,57 @@
+/*
+ * Sandbox.cpp
+ *
+ * Created on: Jan 8, 2012
+ * Author: Simon
+ */
+
+#include "Config.h"
+
+#include "interface/Sandbox.h"
+#include "interface/Component.h"
+#include "Renderer.h"
+
+namespace ui {
+
+Sandbox::Sandbox():
+ Component(0, 0, XRES, YRES)
+{
+ sim = new Simulation();
+}
+
+void Sandbox::OnMouseMovedInside(int localx, int localy, int dx, int dy)
+{
+ if(isMouseDown)
+ {
+ sim->create_parts(localx, localy, 20, 20, 1, 0);
+ }
+}
+
+void Sandbox::OnMouseDown(int localx, int localy, unsigned int button)
+{
+ isMouseDown = true;
+}
+
+void Sandbox::OnMouseUp(int localx, int localy, unsigned int button)
+{
+ isMouseDown = false;
+}
+
+void Sandbox::Draw(void* userdata)
+{
+ Graphics * g = reinterpret_cast<Graphics*>(userdata);
+ if(!ren)
+ ren = new Renderer(g, sim);
+ ren->render_parts();
+}
+
+void Sandbox::Tick(float delta)
+{
+ sim->update_particles();
+}
+
+Sandbox::~Sandbox() {
+ // TODO Auto-generated destructor stub
+}
+
+} /* namespace ui */
diff --git a/src/interface/State.cpp b/src/interface/State.cpp
new file mode 100644
index 0000000..e069e8f
--- /dev/null
+++ b/src/interface/State.cpp
@@ -0,0 +1,232 @@
+/*
+ * State.cpp
+ *
+ * Created on: Jan 8, 2012
+ * Author: Simon
+ */
+
+#include <vector>
+#include <iostream>
+#include <cstring>
+
+#include "interface/State.h"
+
+namespace ui {
+
+State::State(int w, int h) :
+ mouseX(0),
+ mouseY(0),
+ mouseXP(0),
+ mouseYP(0),
+ width(w),
+ height(h)
+{
+}
+
+State::~State()
+{
+ //Components.~vector(); // just in case // devnote : Nope.jpg Nate :3 -frankbro
+}
+
+void State::Add(Component* child)
+{
+ Components.push_back(child);
+ child->Parent = this;
+}
+
+void State::Remove(Component* child)
+{
+ for(int i = 0; i < Components.size(); i++)
+ if(Components[i] == child)
+ {
+ Components.erase(Components.begin() + i);
+ break;
+ }
+}
+
+void State::Draw(void* userdata)
+{
+ //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);
+ }
+ }
+ }
+}
+
+void State::Tick(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);
+ }
+ break;
+ }
+
+ //tick
+ for(int i = 0; i < Components.size(); i++)
+ Components[i]->Tick(dt);
+}
+
+void State::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
+{
+ //on key press
+ if(focusedComponent_ != NULL)
+ if(focusedComponent_->Enabled)
+ focusedComponent_->OnKeyPress(key, shift, ctrl, alt);
+}
+
+void State::OnKeyRelease(int key, bool shift, bool ctrl, bool alt)
+{
+ //on key unpress
+ if(focusedComponent_ != NULL)
+ if(focusedComponent_->Enabled)
+ focusedComponent_->OnKeyRelease(key, shift, ctrl, alt);
+}
+
+void State::OnMouseDown(int x, int y, unsigned int 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)
+ {
+ Components[i]->OnMouseClick(x - Components[i]->X, y - Components[i]->Y, button);
+ this->focusedComponent_ = Components[i]; //set this component as the focused component
+ break;
+ }
+
+ //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);
+}
+
+void State::OnMouseMove(int x, int y)
+{
+ //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)
+ {
+ 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.
+ }
+ }
+ // end of for loop here
+
+ //set the previous mouse coords
+ mouseXP = x;
+ mouseYP = y;
+}
+
+void State::OnMouseUp(int x, int y, unsigned int 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)
+ {
+ Components[i]->OnMouseUnclick(x - Components[i]->X, y - Components[i]->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);
+}
+
+void State::OnMouseWheel(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)
+ {
+ if(Components[i]->Enabled)
+ Components[i]->OnMouseWheelInside(x - Components[i]->X, y - Components[i]->Y, d);
+ break; //found top-most component under mouse
+ }
+
+ //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);
+}
+
+
+} /* namespace ui */
diff --git a/src/interface/Window.cpp b/src/interface/Window.cpp
new file mode 100644
index 0000000..624bf9a
--- /dev/null
+++ b/src/interface/Window.cpp
@@ -0,0 +1,23 @@
+/*
+ * 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 */