-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLButton.cpp
126 lines (104 loc) · 2.04 KB
/
LButton.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
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
#include "LButton.h"
LButton::LButton()
{
mPosition.x = 0;
mPosition.y = 0;
currentSprite = BUTTON_SPRITE_MOUSE_OUT;
}
void LButton::SetPosition(int x, int y)
{
mPosition.x = x;
mPosition.y = y;
}
void LButton::SetDimensions(int width, int height)
{
buttonWidth = width;
buttonHeight = height;
//buttonImageTexture.SetTextureScale()
}
void LButton::SetScale(float value)
{
buttonImageSprite.SetTextureScale(value);
}
int LButton::GetWidth()
{
return buttonWidth;
}
int LButton::GetHeight()
{
return buttonHeight;
}
void LButton::SetButtonSpriteTexture(SDL_Renderer* renderer, const char* path, int spriteCount)
{
buttonImageSprite.LoadSpriteTexture(renderer, path, spriteCount);
}
void LButton::HandleEvent(SDL_Event* e)
{
if (e->type == SDL_MOUSEMOTION || e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP)
{
int x, y;
SDL_GetMouseState(&x, &y);
// Check is mouse is in the button
bool isInside = true;
// Mouse is left of button
if (x < mPosition.x)
{
isInside = false;
}
else if (x > mPosition.x + buttonWidth)
{
isInside = false;
}
else if (y < mPosition.y)
{
isInside = false;
}
else if (y > mPosition.y + buttonHeight)
{
isInside = false;
}
if (!isInside)
{
currentSprite = BUTTON_SPRITE_MOUSE_OUT;
}
else
{
switch (e->type)
{
case SDL_MOUSEMOTION:
currentSprite = BUTTON_SPRITE_MOUSE_OVER_MOTION;
break;
case SDL_MOUSEBUTTONDOWN:
currentSprite = BUTTON_SPRITE_MOUSE_DOWN;
case SDL_MOUSEBUTTONUP:
currentSprite = BUTTON_SPRITE_MOUSE_UP;
default:
break;
}
}
}
}
Sprite* LButton::GetButtonSprite()
{
return &buttonImageSprite;
}
void LButton::Start()
{
}
void LButton::Update()
{
}
void LButton::Destroy()
{
}
void LButton::SetTextureClip(int index, SDL_Rect* rect)
{
buttonImageSprite.SetClip(index, rect);
}
void LButton::Render()
{
if(buttonImageSprite.GetClipCount() > 0)
buttonImageSprite.Render(mPosition.x, mPosition.y, buttonImageSprite.GetClip(currentSprite));
else
printf("Sprite sheet on button has no clips!");
}