-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIButton.cpp
More file actions
52 lines (43 loc) · 1.45 KB
/
Copy pathUIButton.cpp
File metadata and controls
52 lines (43 loc) · 1.45 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
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <functional>
#include <iostream>
#include "UIButton.h"
#include "GameEvents.h"
#include "InputEvents.h"
#include "GameObject.h"
#include "Window.h"
#include "UIHelper.h"
using namespace std;
using namespace sf;
UIButton::UIButton(Vector2f position, Vector2f size, string string, Color color) {
//The button background
GetBody().setSize(size);
GetBody().setOrigin(GetBody().getSize() / 2.0f);
GetBody().setPosition(position);
GetBody().setFillColor(color);
//The button text
text.setPosition(GetBody().getPosition());
text.setFillColor(Color::White);
text.setCharacterSize(buttonCharacterSize);
text.setFont(GetFont());
text.setString(string);
text.setOrigin(text.getLocalBounds().left + text.getLocalBounds().width / 2.0f, text.getLocalBounds().top + text.getLocalBounds().height / 2.0f);
MouseUpEvent[Id] = [this](auto mouseButton, auto mousePosition) { OnMouseUp(mouseButton, mousePosition); };
AddDrawable(GetBody(), 1);
AddDrawable(text, 0);
}
UIButton::~UIButton() {
OnClickedEvent.clear();
MouseUpEvent.erase(Id);
RemoveDrawable(GetBody(), 1);
RemoveDrawable(text, 0);
}
//Dispatch OnClickedEvent when we are clicked
void UIButton::OnMouseUp(Mouse::Button mouseButton, Vector2i mousePosition) {
if (mouseButton != Mouse::Button::Left) { return; }
if (!GetBody().getGlobalBounds().contains((Vector2f)mousePosition)) { return; }
for (auto const& e : OnClickedEvent) {
e.second();
}
}