2012-01-19 07:44:59 -06:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "SaveButton.h"
|
|
|
|
#include "search/Save.h"
|
|
|
|
#include "Graphics.h"
|
|
|
|
#include "Global.h"
|
|
|
|
#include "Engine.h"
|
|
|
|
#include "client/Client.h"
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2012-01-20 18:17:42 -06:00
|
|
|
SaveButton::SaveButton(Window* parent_state, Save * save):
|
2012-01-19 07:44:59 -06:00
|
|
|
Component(parent_state),
|
|
|
|
save(save),
|
|
|
|
thumbnail(NULL),
|
|
|
|
isMouseInside(false),
|
|
|
|
isButtonDown(false),
|
|
|
|
actionCallback(NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-01-20 18:17:42 -06:00
|
|
|
SaveButton::SaveButton(Point position, Point size, Save * save):
|
2012-01-19 07:44:59 -06:00
|
|
|
Component(position, size),
|
|
|
|
save(save),
|
|
|
|
thumbnail(NULL),
|
|
|
|
isMouseInside(false),
|
|
|
|
isButtonDown(false),
|
|
|
|
actionCallback(NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-01-20 18:17:42 -06:00
|
|
|
SaveButton::SaveButton(Save * save):
|
2012-01-19 07:44:59 -06:00
|
|
|
Component(),
|
|
|
|
save(save),
|
|
|
|
thumbnail(NULL),
|
|
|
|
isMouseInside(false),
|
|
|
|
isButtonDown(false),
|
|
|
|
actionCallback(NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveButton::~SaveButton()
|
|
|
|
{
|
|
|
|
if(thumbnail)
|
|
|
|
delete thumbnail;
|
2012-01-20 17:52:19 -06:00
|
|
|
if(actionCallback)
|
|
|
|
delete actionCallback;
|
2012-01-20 18:17:42 -06:00
|
|
|
if(save)
|
|
|
|
delete save;
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void SaveButton::Tick(float dt)
|
|
|
|
{
|
|
|
|
Thumbnail * tempThumb;
|
|
|
|
float scaleFactorY = 1.0f, scaleFactorX = 1.0f;
|
|
|
|
if(!thumbnail)
|
|
|
|
{
|
2012-01-20 18:17:42 -06:00
|
|
|
tempThumb = Client::Ref().GetThumbnail(save->GetID(), 0);
|
2012-01-19 07:44:59 -06:00
|
|
|
if(tempThumb)
|
|
|
|
{
|
2012-01-21 12:51:28 -06:00
|
|
|
thumbnail = new Thumbnail(*tempThumb); //Store a local copy of the thumbnail
|
2012-01-19 07:44:59 -06:00
|
|
|
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);
|
2012-01-20 18:17:42 -06:00
|
|
|
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);
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-20 18:17:42 -06:00
|
|
|
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);
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
if(actionCallback)
|
|
|
|
actionCallback->ActionCallback(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveButton::SetActionCallback(SaveButtonAction * action)
|
|
|
|
{
|
|
|
|
actionCallback = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ui */
|