-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cursor.cpp
90 lines (74 loc) · 2.48 KB
/
Cursor.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
#include "Cursor.h"
using namespace std;
Cursor::Cursor(int x, int y, unsigned width, unsigned height, int valueMin, int valueMax){
_x = x;
_y = y;
_width = width;
_height = height;
_valueMin = valueMin;
_valueMax = valueMax;
_value = _valueMin;
_activated = false;
_text.setFont(*TextureLoader::getFont());
_text.setCharacterSize(8);
_text.setString(std::to_string(_value));
_text.setPosition(_x+_width/2-_text.getLocalBounds().width/2,_y);
_rsBar.setSize(sf::Vector2f(_width,_height/6));
_rsBar.setPosition(_x, _y+_height/2-_rsBar.getSize().y/2);
_rsBar.setFillColor(sf::Color(100,100,100));
_rsCursor.setSize(sf::Vector2f(8,_height));
_rsCursor.setPosition(_x+(_value-_valueMin)/(_valueMax-_valueMin)*_width,_y);
_rsCursor.setFillColor(sf::Color(0,0,0));
}
Cursor::~Cursor(){}
sf::Vector2i Cursor::getPosition(){
return sf::Vector2i(_x,_y);
}
sf::Vector2i Cursor::getSize(){
return sf::Vector2i(_width,_height);
}
void Cursor::setPosition(int x, int y){
_x = x;
_y = y;
_rsBar.setPosition(_x, _y+_height/2-_rsBar.getSize().y/2);
_rsCursor.setPosition(_x+(_value-_valueMin)/(float)(_valueMax-_valueMin)*_width,_y);
_text.setPosition(_x+_width/2-_text.getLocalBounds().width/2,_y);
}
void Cursor::setValue(int value){
_value = value;
if(_value < _valueMin){
_value = _valueMin;
}
if(_value > _valueMax){
_value = _valueMax;
}
_rsCursor.setPosition(_x+(_value-_valueMin)/(float)(_valueMax-_valueMin)*_width,_y);
_text.setString(std::to_string(_value));
}
void Cursor::setValue(sf::Vector2i &mousePos){
int x = mousePos.x - _x;
int value = round((x/(float)_width) * (_valueMax-_valueMin)) + _valueMin;
setValue(value);
}
bool Cursor::mouseIsOnCursor(sf::Vector2i &mousePos){
if(mousePos.x >= _rsCursor.getPosition().x && mousePos.x <= _rsCursor.getPosition().x+_rsCursor.getLocalBounds().width){
if(mousePos.y >= _rsCursor.getPosition().y && mousePos.y <= _rsCursor.getPosition().y+_rsCursor.getLocalBounds().height){
return true;
}
}
return false;
}
int Cursor::getValue(){
return _value;
}
bool Cursor::isActivated(){
return _activated;
}
void Cursor::setActivated(bool act){
_activated = act;
}
void Cursor::draw(sf::RenderWindow &window){
window.draw(_rsBar);
window.draw(_rsCursor);
window.draw(_text);
}