-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI_DynamicButton.cpp
78 lines (62 loc) · 2.11 KB
/
GUI_DynamicButton.cpp
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
#include "GUI_Common.h"
#include "GUI_DynamicButton.h"
#include "GUI_LabelState.h"
#include "math.h"
GUI_DynamicButton::GUI_DynamicButton(string _text, RECT _rect, int _id, GUI_ElementStyle *_style)
: GUI_BaseElement(_rect, _id, _style)
{
swapState(new GUI_LabelState(this));
cState()->SetText(_text);
style = _style;
font = style->GetFont();
src = GUI_Looks::GetResource(this);
}
GUI_DynamicButton::~GUI_DynamicButton()
{
}
void GUI_DynamicButton::DrawMe(SURFHANDLE _tgt, int xoffset, int yoffset, RECT &drawablerect)
{
BLITDATA blitdata;
calculateBlitData(xoffset + rect.left, yoffset + rect.top, drawablerect, blitdata);
//width or height == 0 indicates that the element is completely outside its
//parents rect, so no need to draw.
if (blitdata.width > 0 && blitdata.height > 0)
{
oapiBlt(_tgt, src, &blitdata.tgtrect, &blitdata.srcrect, SURF_PREDEF_CK);
}
}
bool GUI_DynamicButton::ProcessMe(GUI_MOUSE_EVENT _event, int _x, int _y)
{
if (_event == LBUP)
{
return GUI_BaseElement::ProcessMe(_event, _x, _y);
}
return false;
}
bool GUI_DynamicButton::IsResourceCompatibleWith(GUI_BaseElement *element)
{
if (GUI_BaseElement::IsResourceCompatibleWith(element))
{
if (this == element || cState()->GetText() == ((GUI_DynamicButton*)element)->cState()->GetText()) return true;
}
return false;
}
//allocates the surface and draws the button on initialisation
GUI_ElementResource *GUI_DynamicButton::createResources()
{
assert(src == NULL && "Release old resource before creating it again!");
//allocate own surface and fill with background color
SURFHANDLE tgt = GUI_Draw::createElementBackground(style, width, height);
Sketchpad *skp = oapiGetSketchpad(tgt);
GUI_Draw::DrawRectangle(_R(0, 0, width, height), skp, style);
oapiReleaseSketchpad(skp);
//print text
font->Print(tgt, cState()->GetText(), width / 2, height / 2, _R(style->MarginLeft(), style->MarginTop(), width - style->MarginRight(), width - style->MarginBottom()),
false, T_CENTER, V_CENTER);
//assign new surface as source
return new GUI_ElementResource(tgt);
}
GUI_LabelState *GUI_DynamicButton::cState()
{
return (GUI_LabelState*)state;
}