2012-01-19 07:44:59 -06:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "SaveButton.h"
|
2012-06-07 08:23:26 -05:00
|
|
|
#include "client/SaveInfo.h"
|
2012-07-06 10:06:26 -05:00
|
|
|
#include "graphics/Graphics.h"
|
2012-01-19 07:44:59 -06:00
|
|
|
#include "Engine.h"
|
2012-07-31 13:49:08 -05:00
|
|
|
#include "client/ThumbnailBroker.h"
|
2012-04-03 11:08:56 -05:00
|
|
|
#include "simulation/SaveRenderer.h"
|
2012-01-19 07:44:59 -06:00
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2012-06-07 08:23:26 -05:00
|
|
|
SaveButton::SaveButton(Point position, Point size, SaveInfo * save):
|
2012-01-19 07:44:59 -06:00
|
|
|
Component(position, size),
|
2012-06-07 08:23:26 -05:00
|
|
|
file(NULL),
|
2012-01-19 07:44:59 -06:00
|
|
|
save(save),
|
|
|
|
thumbnail(NULL),
|
|
|
|
isMouseInside(false),
|
|
|
|
isButtonDown(false),
|
2012-01-29 11:12:35 -06:00
|
|
|
actionCallback(NULL),
|
2012-04-06 18:45:24 -05:00
|
|
|
voteColour(255, 0, 0),
|
|
|
|
selectable(false),
|
|
|
|
selected(false)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
2012-04-19 09:22:18 -05:00
|
|
|
if(save)
|
|
|
|
{
|
|
|
|
if(save->votesUp==0)
|
|
|
|
voteRatio = 0.0f;
|
|
|
|
else if(save->votesDown==0)
|
|
|
|
voteRatio = 1.0f;
|
|
|
|
else
|
|
|
|
voteRatio = 1.0f-(float)(((float)(save->votesDown))/((float)(save->votesUp)));
|
|
|
|
if(voteRatio < 0.0f)
|
|
|
|
voteRatio = 0.0f;
|
|
|
|
if(voteRatio > 1.0f) //Not possible, but just in case the server were to give a negative value or something
|
|
|
|
voteRatio = 1.0f;
|
2012-01-19 07:44:59 -06:00
|
|
|
|
|
|
|
|
2012-04-19 09:22:18 -05:00
|
|
|
voteColour.Red = (1.0f-voteRatio)*255;
|
|
|
|
voteColour.Green = voteRatio*255;
|
|
|
|
}
|
2012-04-22 11:13:43 -05:00
|
|
|
|
|
|
|
if(save)
|
|
|
|
{
|
|
|
|
name = save->name;
|
|
|
|
if(Graphics::textwidth((char *)name.c_str()) > Size.X)
|
|
|
|
{
|
|
|
|
int position = Graphics::textwidthx((char *)name.c_str(), Size.X - 22);
|
|
|
|
name = name.erase(position, name.length()-position);
|
|
|
|
name += "...";
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 08:23:26 -05:00
|
|
|
SaveButton::SaveButton(Point position, Point size, SaveFile * file):
|
|
|
|
Component(position, size),
|
|
|
|
save(NULL),
|
|
|
|
file(file),
|
|
|
|
thumbnail(NULL),
|
|
|
|
isMouseInside(false),
|
|
|
|
isButtonDown(false),
|
|
|
|
actionCallback(NULL),
|
|
|
|
voteColour(255, 0, 0),
|
|
|
|
selectable(false),
|
2012-07-27 14:06:17 -05:00
|
|
|
selected(false),
|
2012-07-31 13:49:08 -05:00
|
|
|
wantsDraw(false),
|
|
|
|
waitingForThumb(false)
|
2012-06-07 08:23:26 -05:00
|
|
|
{
|
|
|
|
if(file)
|
|
|
|
{
|
2012-07-28 19:33:28 -05:00
|
|
|
name = file->GetDisplayName();
|
2012-06-07 08:23:26 -05:00
|
|
|
if(Graphics::textwidth((char *)name.c_str()) > Size.X)
|
|
|
|
{
|
|
|
|
int position = Graphics::textwidthx((char *)name.c_str(), Size.X - 22);
|
|
|
|
name = name.erase(position, name.length()-position);
|
|
|
|
name += "...";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
SaveButton::~SaveButton()
|
|
|
|
{
|
2012-07-31 13:49:08 -05:00
|
|
|
ThumbnailBroker::Ref().DetachThumbnailListener(this);
|
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
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-06-07 08:23:26 -05:00
|
|
|
if(file)
|
|
|
|
delete file;
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
|
2012-07-31 13:49:08 -05:00
|
|
|
void SaveButton::OnThumbnailReady(Thumbnail * thumb)
|
|
|
|
{
|
|
|
|
if(thumb)
|
|
|
|
{
|
|
|
|
if(thumbnail)
|
|
|
|
delete thumbnail;
|
|
|
|
thumbnail = thumb;
|
|
|
|
waitingForThumb = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
void SaveButton::Tick(float dt)
|
|
|
|
{
|
2012-07-31 13:49:08 -05:00
|
|
|
if(!thumbnail && !waitingForThumb)
|
|
|
|
{
|
|
|
|
if(save)
|
|
|
|
{
|
|
|
|
if(save->GetGameSave())
|
|
|
|
{
|
|
|
|
waitingForThumb = true;
|
|
|
|
ThumbnailBroker::Ref().RenderThumbnail(save->GetGameSave(), Size.X-3, Size.Y-25, this);
|
|
|
|
}
|
|
|
|
else if(save->GetID())
|
|
|
|
{
|
|
|
|
waitingForThumb = true;
|
|
|
|
ThumbnailBroker::Ref().RetrieveThumbnail(save->GetID() , Size.X-3, Size.Y-25, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(file && file->GetGameSave())
|
|
|
|
{
|
|
|
|
waitingForThumb = true;
|
|
|
|
ThumbnailBroker::Ref().RenderThumbnail(file->GetGameSave(), Size.X-3, Size.Y-25, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*Thumbnail * tempThumb;
|
2012-01-19 07:44:59 -06:00
|
|
|
float scaleFactorY = 1.0f, scaleFactorX = 1.0f;
|
2012-07-31 13:49:08 -05:00
|
|
|
if(!thumbnail)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
2012-06-07 08:23:26 -05:00
|
|
|
if(save)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
2012-06-07 08:23:26 -05:00
|
|
|
if(!save->GetGameSave() && save->GetID())
|
|
|
|
{
|
|
|
|
tempThumb = Client::Ref().GetThumbnail(save->GetID(), 0);
|
|
|
|
if(tempThumb)
|
|
|
|
{
|
|
|
|
thumbnail = new Thumbnail(*tempThumb); //Store a local copy of the thumbnail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(save->GetGameSave())
|
|
|
|
{
|
|
|
|
thumbnail = SaveRenderer::Ref().Render(save->GetGameSave());
|
|
|
|
}
|
|
|
|
else
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
2012-06-07 08:23:26 -05:00
|
|
|
thumbnail = NULL;
|
2012-04-03 08:07:39 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-07 08:23:26 -05:00
|
|
|
if(file)
|
2012-04-03 08:07:39 -05:00
|
|
|
{
|
2012-07-27 14:06:17 -05:00
|
|
|
if(file->GetThumbnail())
|
|
|
|
{
|
|
|
|
thumbnail = new Thumbnail(*file->GetThumbnail());
|
|
|
|
}
|
|
|
|
else if(file->GetGameSave())
|
2012-06-07 08:23:26 -05:00
|
|
|
{
|
|
|
|
thumbnail = SaveRenderer::Ref().Render(file->GetGameSave());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
thumbnail = NULL;
|
|
|
|
}
|
2012-04-03 08:07:39 -05:00
|
|
|
}
|
|
|
|
if(thumbnail && thumbnail->Data)
|
|
|
|
{
|
|
|
|
if(thumbnail->Size.Y > (Size.Y-25))
|
|
|
|
{
|
|
|
|
scaleFactorY = ((float)(Size.Y-25))/((float)thumbnail->Size.Y);
|
|
|
|
}
|
|
|
|
if(thumbnail->Size.X > Size.X-3)
|
|
|
|
{
|
|
|
|
scaleFactorX = ((float)Size.X-3)/((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);
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
}
|
2012-07-31 13:49:08 -05:00
|
|
|
}*/
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void SaveButton::Draw(const Point& screenPos)
|
|
|
|
{
|
|
|
|
Graphics * g = ui::Engine::Ref().g;
|
|
|
|
float scaleFactor;
|
2012-01-29 11:12:35 -06:00
|
|
|
ui::Point thumbBoxSize(0, 0);
|
2012-01-19 07:44:59 -06:00
|
|
|
|
2012-07-27 14:06:17 -05:00
|
|
|
wantsDraw = true;
|
|
|
|
|
2012-04-06 18:45:24 -05:00
|
|
|
if(selected && selectable)
|
|
|
|
{
|
|
|
|
g->fillrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 100, 170, 255, 100);
|
|
|
|
}
|
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
if(thumbnail)
|
|
|
|
{
|
2012-01-29 11:12:35 -06:00
|
|
|
thumbBoxSize = ui::Point(thumbnail->Size.X, thumbnail->Size.Y);
|
2012-06-07 08:23:26 -05:00
|
|
|
if(save && save->id)
|
2012-02-11 11:04:39 -06:00
|
|
|
g->draw_image(thumbnail->Data, screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbnail->Size.X, thumbnail->Size.Y, 255);
|
2012-01-29 11:12:35 -06:00
|
|
|
else
|
2012-02-11 11:04:39 -06:00
|
|
|
g->draw_image(thumbnail->Data, screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbnail->Size.X, thumbnail->Size.Y, 255);
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scaleFactor = (Size.Y-25)/((float)YRES);
|
2012-01-29 11:12:35 -06:00
|
|
|
thumbBoxSize = ui::Point(((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor);
|
|
|
|
}
|
2012-04-19 09:22:18 -05:00
|
|
|
if(save)
|
2012-01-29 11:12:35 -06:00
|
|
|
{
|
2012-04-19 09:22:18 -05:00
|
|
|
if(save->id)
|
|
|
|
{
|
|
|
|
if(isMouseInside)
|
|
|
|
g->drawrect(screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 210, 230, 255, 255);
|
|
|
|
else
|
|
|
|
g->drawrect(screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 180, 180, 180, 255);
|
2012-04-22 11:13:43 -05:00
|
|
|
g->drawrect(screenPos.X-4+thumbBoxSize.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, 7, thumbBoxSize.Y, 180, 180, 180, 255);
|
2012-04-19 09:22:18 -05:00
|
|
|
|
2012-08-12 16:32:57 -05:00
|
|
|
int voteBar = std::max(10.0f, ((float)(thumbBoxSize.Y-4))*voteRatio);
|
2012-04-22 11:13:43 -05:00
|
|
|
g->fillrect(1+screenPos.X-3+thumbBoxSize.X+(Size.X-thumbBoxSize.X)/2, (screenPos.Y-2)+(thumbBoxSize.Y-voteBar)+(Size.Y-21-thumbBoxSize.Y)/2, 3, voteBar, voteColour.Red, voteColour.Green, voteColour.Blue, 255);
|
2012-04-19 09:22:18 -05:00
|
|
|
}
|
2012-02-11 11:11:07 -06:00
|
|
|
else
|
2012-04-19 09:22:18 -05:00
|
|
|
{
|
|
|
|
if(isMouseInside)
|
|
|
|
g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 210, 230, 255, 255);
|
|
|
|
else
|
|
|
|
g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 180, 180, 180, 255);
|
|
|
|
}
|
2012-01-29 11:12:35 -06:00
|
|
|
|
2012-02-11 11:11:07 -06:00
|
|
|
if(isMouseInside)
|
2012-04-19 09:22:18 -05:00
|
|
|
{
|
|
|
|
//g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255);
|
2012-04-22 11:13:43 -05:00
|
|
|
g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)name.c_str()))/2, screenPos.Y+Size.Y - 21, name, 255, 255, 255, 255);
|
2012-04-19 09:22:18 -05:00
|
|
|
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-02-11 11:11:07 -06:00
|
|
|
else
|
2012-04-19 09:22:18 -05:00
|
|
|
{
|
2012-04-22 11:13:43 -05:00
|
|
|
g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)name.c_str()))/2, screenPos.Y+Size.Y - 21, name, 180, 180, 180, 255);
|
2012-04-19 09:22:18 -05:00
|
|
|
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
|
|
|
}
|
2012-06-07 08:23:26 -05:00
|
|
|
if(file)
|
|
|
|
{
|
|
|
|
if(isMouseInside)
|
|
|
|
g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 210, 230, 255, 255);
|
|
|
|
else
|
|
|
|
g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 180, 180, 180, 255);
|
|
|
|
|
|
|
|
if(isMouseInside)
|
|
|
|
{
|
|
|
|
g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)name.c_str()))/2, screenPos.Y+Size.Y - 21, name, 255, 255, 255, 255);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)name.c_str()))/2, screenPos.Y+Size.Y - 21, name, 180, 180, 180, 255);
|
|
|
|
}
|
|
|
|
}
|
2012-04-06 18:45:24 -05:00
|
|
|
|
|
|
|
if(isMouseInside && selectable)
|
|
|
|
{
|
|
|
|
g->clearrect(screenPos.X+(Size.X-20), screenPos.Y+6, 14, 14);
|
|
|
|
g->drawrect(screenPos.X+(Size.X-20), screenPos.Y+6, 14, 14, 255, 255, 255, 255);
|
|
|
|
if(selected)
|
|
|
|
g->fillrect(screenPos.X+(Size.X-18), screenPos.Y+8, 10, 10, 255, 255, 255, 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!
|
|
|
|
}
|
|
|
|
|
2012-04-06 18:45:24 -05:00
|
|
|
if(x>=Size.X-20 && y>=6 && y<=20 && x<=Size.X-6 && selectable)
|
|
|
|
{
|
|
|
|
selected = !selected;
|
|
|
|
DoSelection();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
if(isButtonDown)
|
|
|
|
{
|
|
|
|
DoAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
isButtonDown = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveButton::OnMouseClick(int x, int y, unsigned int button)
|
|
|
|
{
|
2012-04-06 18:45:24 -05:00
|
|
|
if(button !=1 && selectable)
|
|
|
|
{
|
|
|
|
selected = !selected;
|
|
|
|
DoSelection();
|
|
|
|
}
|
2012-01-19 07:44:59 -06:00
|
|
|
if(button != 1) return; //left click only!
|
2012-04-06 18:45:24 -05:00
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-04-06 18:45:24 -05:00
|
|
|
void SaveButton::DoSelection()
|
|
|
|
{
|
|
|
|
if(selectable)
|
|
|
|
actionCallback->SelectedCallback(this);
|
|
|
|
}
|
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
void SaveButton::SetActionCallback(SaveButtonAction * action)
|
|
|
|
{
|
|
|
|
actionCallback = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ui */
|