-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolumeSlider.cpp
More file actions
57 lines (43 loc) · 1.93 KB
/
Copy pathVolumeSlider.cpp
File metadata and controls
57 lines (43 loc) · 1.93 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
#include "VolumeSlider.h"
VolumeSlider::VolumeSlider(MusicPlayer* musicPlayer, sf::Vector2f pos, sf::Vector2f sz)
: player(musicPlayer), position(pos), size(sz) {
background.setSize(size);
background.setFillColor(sf::Color(150, 150, 150));
background.setPosition(position);
fill.setSize({ size.x * (player->getVolume() / 100.0f), size.y });
fill.setFillColor(sf::Color(100, 200, 100));
fill.setPosition(position);
}
void VolumeSlider::handleEvent(const sf::Event& event, MusicPlayer& player) {
if (event.type == sf::Event::MouseButtonPressed &&
event.mouseButton.button == sf::Mouse::Left) {
sf::Vector2f mousePos(event.mouseButton.x, event.mouseButton.y);
// Ïðîâåðêà ïîïàäàíèÿ â îáùèé ôîí ïîëçóíêà
if (background.getGlobalBounds().contains(mousePos)) {
isDragging = true;
// Óñòàíîâêà ãðîìêîñòè è çàïîëíåííîé ÷àñòè
float relativeX = mousePos.x - background.getPosition().x;
float ratio = std::clamp(relativeX / background.getSize().x, 0.f, 1.f);
fill.setSize({ background.getSize().x * ratio, background.getSize().y });
player.setVolume(ratio * 100);
}
}
if (event.type == sf::Event::MouseButtonReleased &&
event.mouseButton.button == sf::Mouse::Left) {
isDragging = false;
}
if (event.type == sf::Event::MouseMoved && isDragging) {
sf::Vector2f mousePos(event.mouseMove.x, event.mouseMove.y);
float relativeX = mousePos.x - background.getPosition().x;
float ratio = std::clamp(relativeX / background.getSize().x, 0.f, 1.f);
fill.setSize({ background.getSize().x * ratio, background.getSize().y });
player.setVolume(ratio * 100);
}
}
void VolumeSlider::update() {
fill.setSize({ size.x * (player->getVolume() / 100.0f), size.y });
}
void VolumeSlider::draw(sf::RenderWindow& window) {
window.draw(background);
window.draw(fill);
}