diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-19 13:44:59 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-19 13:44:59 (GMT) |
| commit | 44639a6423c03552a3c0faafab27ef8f395f73a6 (patch) | |
| tree | 1a4fc49a56060759fcbec6f18e9159cf126e8606 /src/interface | |
| parent | 4a60b97c700c2f1843b7e99313554cb89fb5da4e (diff) | |
| download | powder-44639a6423c03552a3c0faafab27ef8f395f73a6.zip powder-44639a6423c03552a3c0faafab27ef8f395f73a6.tar.gz | |
Some folder changes, started search and client
Diffstat (limited to 'src/interface')
| -rw-r--r-- | src/interface/Button.cpp | 64 | ||||
| -rw-r--r-- | src/interface/Button.h | 12 | ||||
| -rw-r--r-- | src/interface/Engine.cpp | 3 | ||||
| -rw-r--r-- | src/interface/Sandbox.cpp | 101 | ||||
| -rw-r--r-- | src/interface/Sandbox.h | 39 | ||||
| -rw-r--r-- | src/interface/SaveButton.cpp | 157 | ||||
| -rw-r--r-- | src/interface/SaveButton.h | 50 |
7 files changed, 271 insertions, 155 deletions
diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp index a3f76b9..affa75d 100644 --- a/src/interface/Button.cpp +++ b/src/interface/Button.cpp @@ -11,6 +11,7 @@ #include "Graphics.h" #include "Global.h" #include "Engine.h" +#include "Misc.h" namespace ui { @@ -20,9 +21,12 @@ Button::Button(Window* parent_state, std::string buttonText): isMouseInside(false), isButtonDown(false), isTogglable(false), - actionCallback(NULL) + actionCallback(NULL), + textPosition(ui::Point(0, 0)), + textVAlign(AlignMiddle), + textHAlign(AlignCentre) { - + TextPosition(); } Button::Button(Point position, Point size, std::string buttonText): @@ -31,9 +35,12 @@ Button::Button(Point position, Point size, std::string buttonText): isMouseInside(false), isButtonDown(false), isTogglable(false), - actionCallback(NULL) + actionCallback(NULL), + textPosition(ui::Point(0, 0)), + textVAlign(AlignMiddle), + textHAlign(AlignCentre) { - + TextPosition(); } Button::Button(std::string buttonText): @@ -42,9 +49,48 @@ Button::Button(std::string buttonText): isMouseInside(false), isButtonDown(false), isTogglable(false), - actionCallback(NULL) + actionCallback(NULL), + textPosition(ui::Point(0, 0)), + textVAlign(AlignMiddle), + textHAlign(AlignCentre) { + TextPosition(); +} + +void Button::TextPosition() +{ + //Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2 + switch(textVAlign) + { + case AlignTop: + textPosition.Y = 3; + break; + case AlignMiddle: + textPosition.Y = (Size.Y-10)/2; + break; + case AlignBottom: + textPosition.Y = Size.Y-11; + break; + } + switch(textHAlign) + { + case AlignLeft: + textPosition.X = 3; + break; + case AlignCentre: + textPosition.X = (Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2; + break; + case AlignRight: + textPosition.X = (Size.X-Graphics::textwidth((char *)ButtonText.c_str()))-2; + break; + } +} + +void Button::SetText(std::string buttonText) +{ + ButtonText = buttonText; + TextPosition(); } void Button::SetTogglable(bool togglable) @@ -68,25 +114,21 @@ inline void Button::SetToggleState(bool state) toggle = state; } - - void Button::Draw(const Point& screenPos) { 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-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); + g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, 0, 0, 0, 255); } else { if(isMouseInside) 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); + g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, 255, 255, 255, 255); } /*sf::RenderWindow* rw = reinterpret_cast<sf::RenderWindow*>(userdata); //it better be a RenderWindow or so help your god diff --git a/src/interface/Button.h b/src/interface/Button.h index 5f2d71f..aabca91 100644 --- a/src/interface/Button.h +++ b/src/interface/Button.h @@ -9,7 +9,7 @@ #define BUTTON_H_ #include <string> - +#include "Misc.h" #include "Component.h" namespace ui @@ -51,10 +51,18 @@ public: inline bool GetToggleState(); inline void SetToggleState(bool state); void SetActionCallback(ButtonAction * action); - + void TextPosition(); + void SetText(std::string buttonText); + HorizontalAlignment GetHAlignment() { return textHAlign; } + VerticalAlignment GetVAlignment() { return textVAlign; } + void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); } protected: bool isButtonDown, state, isMouseInside, isTogglable, toggle; ButtonAction * actionCallback; + ui::Point textPosition; + HorizontalAlignment textHAlign; + VerticalAlignment textVAlign; + }; } #endif /* BUTTON_H_ */ diff --git a/src/interface/Engine.cpp b/src/interface/Engine.cpp index 55a2370..130fc8e 100644 --- a/src/interface/Engine.cpp +++ b/src/interface/Engine.cpp @@ -47,10 +47,9 @@ void Engine::ShowWindow(Window * window) { if(state_) { - windows.push(window); + windows.push(state_); } state_ = window; - windows.push(window); } void Engine::CloseWindow() diff --git a/src/interface/Sandbox.cpp b/src/interface/Sandbox.cpp deleted file mode 100644 index 9a858f8..0000000 --- a/src/interface/Sandbox.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Sandbox.cpp - * - * Created on: Jan 8, 2012 - * Author: Simon - */ - -#include <iostream> -#include <queue> - -#include "Config.h" -#include "Global.h" - -#include "interface/Point.h" -#include "interface/Sandbox.h" -#include "interface/Component.h" -#include "Renderer.h" -#include "Simulation.h" -#include "Engine.h" - -namespace ui { - -Sandbox::Sandbox(): - Component(Point(0, 0), Point(XRES, YRES)), - pointQueue(std::queue<Point*>()), - ren(NULL), - isMouseDown(false), - activeElement(1) -{ - sim = new Simulation(); -} - -Simulation * Sandbox::GetSimulation() -{ - return sim; -} - -void Sandbox::OnMouseMoved(int localx, int localy, int dx, int dy) -{ - if(isMouseDown) - { - pointQueue.push(new Point(localx-dx, localy-dy)); - pointQueue.push(new Point(localx, localy)); - } -} - -void Sandbox::OnMouseClick(int localx, int localy, unsigned int button) -{ - isMouseDown = true; - pointQueue.push(new Point(localx, localy)); -} - -void Sandbox::OnMouseUp(int localx, int localy, unsigned int button) -{ - if(isMouseDown) - { - isMouseDown = false; - pointQueue.push(new Point(localx, localy)); - } -} - -void Sandbox::Draw(const Point& screenPos) -{ - Graphics * g = Engine::Ref().g; - if(!ren) - ren = new Renderer(g, sim); - ren->render_parts(); -} - -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, 1, 1, activeElement, 0); - delete sPoint; - } - else - { - sim->create_parts(fPoint->X, fPoint->Y, 1, 1, activeElement, 0); - } - sPoint = fPoint; - } - if(sPoint) - delete sPoint; - } - sim->update_particles(); - sim->sys_pause = 1; -} - -Sandbox::~Sandbox() { - // TODO Auto-generated destructor stub -} - -} /* namespace ui */ diff --git a/src/interface/Sandbox.h b/src/interface/Sandbox.h deleted file mode 100644 index fb4a668..0000000 --- a/src/interface/Sandbox.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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/SaveButton.cpp b/src/interface/SaveButton.cpp new file mode 100644 index 0000000..18f3281 --- /dev/null +++ b/src/interface/SaveButton.cpp @@ -0,0 +1,157 @@ +#include <iostream> + +#include "SaveButton.h" +#include "search/Save.h" +#include "Graphics.h" +#include "Global.h" +#include "Engine.h" +#include "client/Client.h" + +namespace ui { + +SaveButton::SaveButton(Window* parent_state, Save save): + Component(parent_state), + save(save), + thumbnail(NULL), + isMouseInside(false), + isButtonDown(false), + actionCallback(NULL) +{ + +} + +SaveButton::SaveButton(Point position, Point size, Save save): + Component(position, size), + save(save), + thumbnail(NULL), + isMouseInside(false), + isButtonDown(false), + actionCallback(NULL) +{ + +} + +SaveButton::SaveButton(Save save): + Component(), + save(save), + thumbnail(NULL), + isMouseInside(false), + isButtonDown(false), + actionCallback(NULL) +{ + +} + +SaveButton::~SaveButton() +{ + if(thumbnail) + delete thumbnail; +} + +void SaveButton::Tick(float dt) +{ + Thumbnail * tempThumb; + float scaleFactorY = 1.0f, scaleFactorX = 1.0f; + if(!thumbnail) + { + tempThumb = Client::Ref().GetThumbnail(save.GetID(), 0); + if(tempThumb) + { + thumbnail = tempThumb; //Store a local copy of the thumbnail + if(thumbnail->Data) + { + if(thumbnail->Size.Y > (Size.Y-25)) + { + scaleFactorY = ((float)(Size.Y-25))/((float)thumbnail->Size.Y); + } + if(thumbnail->Size.X > Size.X) + { + scaleFactorX = ((float)Size.X)/((float)thumbnail->Size.X); + } + if(scaleFactorY < 1.0f || scaleFactorX < 1.0f) + { + float scaleFactor = scaleFactorY < scaleFactorX ? scaleFactorY : scaleFactorX; + pixel * thumbData = thumbnail->Data; + thumbnail->Data = Graphics::resample_img(thumbData, thumbnail->Size.X, thumbnail->Size.Y, thumbnail->Size.X * scaleFactor, thumbnail->Size.Y * scaleFactor); + thumbnail->Size.X *= scaleFactor; + thumbnail->Size.Y *= scaleFactor; + free(thumbData); + } + } + } + } +} + +void SaveButton::Draw(const Point& screenPos) +{ + Graphics * g = ui::Engine::Ref().g; + float scaleFactor; + + if(thumbnail) + { + g->draw_image(thumbnail->Data, screenPos.X+(Size.X-thumbnail->Size.X)/2, screenPos.Y+((Size.Y-25)-thumbnail->Size.Y)/2, thumbnail->Size.X, thumbnail->Size.Y, 255); + g->drawrect(screenPos.X+(Size.X-thumbnail->Size.X)/2, screenPos.Y+((Size.Y-25)-thumbnail->Size.Y)/2, thumbnail->Size.X, thumbnail->Size.Y, 180, 180, 180, 255); + } + else + { + scaleFactor = (Size.Y-25)/((float)YRES); + g->drawrect(screenPos.X+(Size.X-((float)XRES)*scaleFactor)/2, screenPos.Y+((Size.Y-21)-((float)YRES)*scaleFactor)/2, ((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor, 180, 180, 180, 255); + } + + if(isMouseInside) + { + g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255); + g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save.name.c_str()))/2, screenPos.Y+Size.Y - 21, save.name, 255, 255, 255, 255); + g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save.userName.c_str()))/2, screenPos.Y+Size.Y - 10, save.userName, 200, 230, 255, 255); + } + else + { + g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save.name.c_str()))/2, screenPos.Y+Size.Y - 21, save.name, 180, 180, 180, 255); + g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save.userName.c_str()))/2, screenPos.Y+Size.Y - 10, save.userName, 100, 130, 160, 255); + } +} + +void SaveButton::OnMouseUnclick(int x, int y, unsigned int button) +{ + if(button != 1) + { + return; //left click only! + } + + if(isButtonDown) + { + DoAction(); + } + + isButtonDown = false; +} + +void SaveButton::OnMouseClick(int x, int y, unsigned int button) +{ + if(button != 1) return; //left click only! + isButtonDown = true; +} + +void SaveButton::OnMouseEnter(int x, int y) +{ + isMouseInside = true; +} + +void SaveButton::OnMouseLeave(int x, int y) +{ + isMouseInside = false; +} + +void SaveButton::DoAction() +{ + std::cout << "Do action!" << std::endl; + if(actionCallback) + actionCallback->ActionCallback(this); +} + +void SaveButton::SetActionCallback(SaveButtonAction * action) +{ + actionCallback = action; +} + +} /* namespace ui */ diff --git a/src/interface/SaveButton.h b/src/interface/SaveButton.h new file mode 100644 index 0000000..8834d8a --- /dev/null +++ b/src/interface/SaveButton.h @@ -0,0 +1,50 @@ +#ifndef SAVEBUTTON_H_ +#define SAVEBUTTON_H_ + +#include <string> + +#include "Component.h" +#include "search/Save.h" +#include "Graphics.h" +#include "search/Thumbnail.h" + +namespace ui +{ +class SaveButton; +class SaveButtonAction +{ +public: + virtual void ActionCallback(ui::SaveButton * sender) {} +}; + +class SaveButton : public Component +{ + Save save; + Thumbnail * thumbnail; +public: + SaveButton(Window* parent_state, Save save); + + SaveButton(Point position, Point size, Save save); + + SaveButton(Save save); + virtual ~SaveButton(); + + virtual void OnMouseClick(int x, int y, unsigned int button); + virtual void OnMouseUnclick(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); + virtual void Tick(float dt); + + inline bool GetState() { return state; } + virtual void DoAction(); + void SetActionCallback(SaveButtonAction * action); +protected: + bool isButtonDown, state, isMouseInside; + SaveButtonAction * actionCallback; +}; +} +#endif /* BUTTON_H_ */ + |
