-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserInterface.h
More file actions
216 lines (167 loc) · 4.91 KB
/
userInterface.h
File metadata and controls
216 lines (167 loc) · 4.91 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#pragma once
#include "SFML/Graphics.hpp"
#include "box2d/box2d.h"
#include "cameraBox2DtoSFML.h"
#include "coreEvents.h"
#include "scene.h"
#include <iostream>
/*
Drawable
|
--GUInterface --------------------------
/ | | \
GUiButton GUiFishka GUiSprite GUiText
/ \ |
GUiButtonChekc GUiButtonNew GUiLabel
*/
namespace gamecore {
class Scene;
class GUInterface;
// Òèïû ïîëüçîâàòåëüñêèõ ãðàôè÷åñêèõ ýëåìåíòîâ
enum class UiType
{
Fishka = 0,
Button,
PlaceHolder,
Sensor,
Joint,
Sprite,
Text,
Label,
};
// Âñïîìîãàòåëüíàÿ ñòðóêòóðà äëÿ õðàíåíèÿ ïîëüçîâàòåëüñêîé èíôîðìàöèè â îáúåêòàõ box2d
struct B2BodyUserDataD
{
UiType itemType;
GUInterface* itemPointer;
int cash;
bool isDrawable;
};
/// <summary>
/// Áàçîâûé êëàññ äëÿ âñåõ ãðàôè÷åñêèõ ýëåìåíòîâ èãðû
/// </summary>
class GUInterface : public sf::Drawable
{
public:
GUInterface(const UiType guiType);
GUInterface(sf::Texture* texture, const UiType guiType);
GUInterface(sf::Texture* texture, sf::IntRect rect, const int id, const UiType guiType);
const UiType type;
virtual void SetPosition(const sf::Vector2f& pos);
virtual void SetRotation(const sf::Angle& angle);
virtual const sf::IntRect& GetRect() const;
virtual sf::Sprite& GetSprite();
virtual void draw(sf::RenderTarget& target, const sf::RenderStates& states) const override;
virtual ~GUInterface() = default;
protected:
sf::Sprite sprite_;
sf::Texture* texture_;
sf::IntRect rect_;
int id_ = -1;
protected:
const bool Contains(const int x, const int y) const;
};
/// <summary>
/// Ïðåäñòàâëÿåò ðîäèòåëüñêèé êëàññ äëÿ âñåõ ãðàôè÷åñêèõ èíòåðôåéñîô - êíîïêà
/// </summary>
class GUiButton : public GUInterface
{
public:
GUiButton(sf::Texture* texture, const sf::IntRect rect, const int id);
virtual void MouseDown(const sf::Event::MouseButtonEvent& downPos, Scene* sc) = 0;
virtual void MouseHover(const sf::Event::MouseMoveEvent& movPos) = 0;
protected:
sf::Color defaultColor_;
sf::Color hoverColor_;
bool mouseHover_ = false;
};
/// <summary>
/// Ïðåäñòàâëÿåò èãðîâóþ ôèøêó
/// </summary>
class GUiFishka final : public GUInterface
{
public:
GUiFishka(sf::Texture* texture, const sf::IntRect rect, const int id);
const int GetID() const;
const sf::Vector2f GetScale() const;
void SetScale(const sf::Vector2f& scale);
const sf::Vector2f origin;
private:
const sf::Vector2f defaultScale_{ .88f, .88f };
};
class GUiSprite final : public GUInterface
{
public:
GUiSprite(sf::Texture* texture);
void SetScale(const sf::Vector2f& scale);
};
/// <summary>
/// Êëàññ ðåàëèçóåò èíòåðôåéñ ïðîñòîãî òåêñòîâîãî ñîîáùåíèÿ
/// </summary>
class GUiText : public GUInterface
{
public:
GUiText(sf::Font* font, const int id);
GUiText(sf::Font* font, const UiType guiType, const int id);
void SetPosition(const sf::Vector2f& pos) override;
void SetString(const sf::String& string);
void SetCharacterSize(const size_t size);
void SetFillColor(const sf::Color& color);
void SetRotation(const sf::Angle& angle) override;
void draw(sf::RenderTarget& target, const sf::RenderStates& states) const override;
sf::Text& GetText();
sf::Text& GetText() const;
const sf::Color& GetColor() const;
~GUiText();
private:
using GUInterface::GetRect;
using GUInterface::GetSprite;
protected:
sf::Font* font_;
sf::Text* text_;
bool mouseHover_ = false;
const bool Contains(const int x, const int y) const;
};
/// <summary>
/// Êëàññ ðåàëèçóåò èíòåðôåéñ òåêñòîâîãî ñîîáùåíèÿ êîòîðîå ìîæíî ùåëêàòü è íàâîäèòüñÿ ìûøêîé íà íåãî
/// </summary>
class GUiLabel final : public GUiText
{
public:
GUiLabel(sf::Font* font, const int labelId);
template <typename Fn>
void MouseDown(const sf::Event::MouseButtonEvent& downPos, Fn callBackFn);
void MouseHover(const sf::Event::MouseMoveEvent& movPos);
private:
bool mouseHover_ = false;
int labelId_;
};
template<typename Fn>
inline void GUiLabel::MouseDown(const sf::Event::MouseButtonEvent& downPos, Fn callBackFn)
{
if (downPos.button == sf::Mouse::Button::Left && Contains(downPos.x, downPos.y))
{
callBackFn->MousePushCallBack(labelId_);
}
}
/// <summary>
/// Ðåàëèçàöèÿ ýëåìåíòà óïðàâëåíèÿ: êíîïêà Check
/// </summary>
class GUiButtonChekc final : public GUiButton
{
public:
GUiButtonChekc(sf::Texture* texture, const sf::IntRect rect, const int id);
void MouseDown(const sf::Event::MouseButtonEvent& downPos, Scene* ms) override;
void MouseHover(const sf::Event::MouseMoveEvent& movPos) override;
};
/// <summary>
/// Ðåàëèçàöèÿ ýëåìåíòà óïðàâëåíèÿ: êíîïêà New
/// </summary>
class GUiButtonNew final : public GUiButton
{
public:
GUiButtonNew(sf::Texture* texture, const sf::IntRect rect, const int id);
void MouseDown(const sf::Event::MouseButtonEvent& downPos, Scene* ms) override;
void MouseHover(const sf::Event::MouseMoveEvent& movPos) override;
};
} // namespace gamecore