-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIClickable.h
48 lines (35 loc) · 1.2 KB
/
IClickable.h
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
#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
#include <iostream>
#include "helper/InputHelper.h"
class IClickable {
protected:
Time entered;
Time left;
virtual bool checkBounds(RenderWindow& window) = 0;
void onEnter() {
InputHelper& inputHelper = InputHelper::instance();
entered = inputHelper.elapsedTime();
}
void onLeave() {
InputHelper& inputHelper = InputHelper::instance();
left = inputHelper.elapsedTime();
}
bool isMouseHeld(RenderWindow& window) {
return Mouse::isButtonPressed(Mouse::Left) && isMouseOver(window);
}
bool isMouseClickHeld(RenderWindow& window) {
InputHelper& inputHelper = InputHelper::instance();
std::cout << isMouseHeld(window) << " " << inputHelper.pressedAt(Mouse::Left).asSeconds() << " " << entered.asSeconds() << std::endl;
return isMouseHeld(window) && inputHelper.pressedAt(Mouse::Left) >= entered;
}
public:
bool isMouseOver(RenderWindow& window) {
bool bounded = checkBounds(window);
if (bounded && left >= entered)
onEnter();
else if (!bounded && left <= entered)
onLeave();
return bounded;
}
};