summaryrefslogtreecommitdiff
path: root/src/interface/SaveButton.cpp
diff options
context:
space:
mode:
authorSimon 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)
commit44639a6423c03552a3c0faafab27ef8f395f73a6 (patch)
tree1a4fc49a56060759fcbec6f18e9159cf126e8606 /src/interface/SaveButton.cpp
parent4a60b97c700c2f1843b7e99313554cb89fb5da4e (diff)
downloadpowder-44639a6423c03552a3c0faafab27ef8f395f73a6.zip
powder-44639a6423c03552a3c0faafab27ef8f395f73a6.tar.gz
Some folder changes, started search and client
Diffstat (limited to 'src/interface/SaveButton.cpp')
-rw-r--r--src/interface/SaveButton.cpp157
1 files changed, 157 insertions, 0 deletions
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 */