1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#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;
if(actionCallback)
delete actionCallback;
if(save)
delete save;
}
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 = new 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 */
|