forked from kidanger/vpv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
149 lines (126 loc) · 3.79 KB
/
Player.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
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
#include <cmath>
#include <algorithm>
#include <numeric>
#include <limits>
#include <SFML/Window/Event.hpp>
#include "imgui.h"
#include "Player.hpp"
#include "Sequence.hpp"
#include "globals.hpp"
Player::Player() {
static int id = 0;
id++;
ID = "Player " + std::to_string(id);
frame = 1;
minFrame = 1;
maxFrame = std::numeric_limits<int>::max();
currentMinFrame = 1;
currentMaxFrame = maxFrame;
opened = true;
}
void Player::update()
{
frameAccumulator += frameClock.restart();
int oldframe = frame;
if (playing) {
while (frameAccumulator.asSeconds() > 1 / fabsf(fps)) {
frame += fps >= 0 ? 1 : -1;
frameAccumulator -= sf::seconds(1 / fabsf(fps));
checkBounds();
}
} else {
frameAccumulator = sf::seconds(0);
}
int index = std::find(gPlayers.begin(), gPlayers.end(), this) - gPlayers.begin();
bool isKeyFocused = !ImGui::GetIO().WantCaptureKeyboard && index <= 9 &&
ImGui::IsKeyPressed(sf::Keyboard::Num1 + index) && ImGui::IsKeyDown(sf::Keyboard::LAlt);
if (isKeyFocused)
opened = true;
if (!opened) {
return;
}
if (!ImGui::Begin(ID.c_str(), &opened, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse))
{
ImGui::End();
return;
}
if (isKeyFocused) {
ImGui::SetWindowFocus();
}
if (ImGui::IsWindowFocused()) {
checkShortcuts();
}
displaySettings();
checkBounds();
ImGui::End();
}
void Player::displaySettings()
{
if (ImGui::Button("<")) {
frame--;
playing = 0;
}
ImGui::SameLine(); ImGui::ShowHelpMarker("Previous frame (left)");
ImGui::SameLine();
ImGui::Checkbox("Play", &playing);
ImGui::SameLine(); ImGui::ShowHelpMarker("Toggle playback (p)");
ImGui::SameLine();
if (ImGui::Button(">")) {
frame++;
playing = 0;
}
ImGui::SameLine(); ImGui::ShowHelpMarker("Next frame (right)");
ImGui::SameLine();
ImGui::Checkbox("Looping", &looping);
ImGui::SameLine(); ImGui::ShowHelpMarker("Loops when at the end of the sequence");
if (ImGui::SliderInt("Frame", &frame, currentMinFrame, currentMaxFrame)) {
playing = 0;
}
ImGui::SliderFloat("FPS", &fps, -100.f, 100.f, "%.2f frames/s");
ImGui::SameLine(); ImGui::ShowHelpMarker("Change the Frame Per Second rate");
ImGui::DragIntRange2("Bounds", ¤tMinFrame, ¤tMaxFrame, 1.f, minFrame, maxFrame);
ImGui::SameLine(); ImGui::ShowHelpMarker("Change the bounds of the playback");
}
void Player::checkShortcuts()
{
if (!ImGui::GetIO().WantCaptureKeyboard && ImGui::IsKeyPressed(sf::Keyboard::P, false)) {
playing = !playing;
}
if (!ImGui::GetIO().WantCaptureKeyboard && ImGui::IsKeyPressed(sf::Keyboard::Left)) {
frame--;
checkBounds();
}
if (!ImGui::GetIO().WantCaptureKeyboard && ImGui::IsKeyPressed(sf::Keyboard::Right)) {
frame++;
checkBounds();
}
}
void Player::checkBounds()
{
currentMaxFrame = std::min(currentMaxFrame, maxFrame);
currentMinFrame = std::max(currentMinFrame, minFrame);
currentMaxFrame = std::max(currentMaxFrame, currentMinFrame);
currentMinFrame = std::min(currentMinFrame, currentMaxFrame);
if (frame > currentMaxFrame) {
if (looping)
frame = currentMinFrame;
else
frame = currentMaxFrame;
}
if (frame < currentMinFrame) {
if (looping)
frame = currentMaxFrame;
else
frame = currentMinFrame;
}
}
void Player::reconfigureBounds()
{
minFrame = 1;
maxFrame = std::numeric_limits<int>::max();
for (auto seq : gSequences) {
if (seq->player == this)
maxFrame = fmin(maxFrame, seq->filenames.size());
}
checkBounds();
}