forked from Aloshi/EmulationStation
-
Notifications
You must be signed in to change notification settings - Fork 341
/
VideoComponent.cpp
358 lines (308 loc) · 8.14 KB
/
VideoComponent.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "components/VideoComponent.h"
#include "resources/ResourceManager.h"
#include "utils/FileSystemUtil.h"
#include "PowerSaver.h"
#include "ThemeData.h"
#include "Window.h"
#include <SDL_timer.h>
#define FADE_TIME_MS 200
std::string getTitlePath() {
std::string titleFolder = getTitleFolder();
return titleFolder + "last_title.srt";
}
std::string getTitleFolder() {
std::string home = Utils::FileSystem::getHomePath();
return home + "/.emulationstation/tmp/";
}
void writeSubtitle(const char* gameName, const char* systemName, bool always)
{
FILE* file = fopen(getTitlePath().c_str(), "w");
int end = (int)(Settings::getInstance()->getInt("ScreenSaverSwapVideoTimeout") / (1000));
if (always) {
fprintf(file, "1\n00:00:01,000 --> 00:00:%d,000\n", end);
}
else
{
fprintf(file, "1\n00:00:01,000 --> 00:00:08,000\n");
}
fprintf(file, "%s\n", gameName);
fprintf(file, "<i>%s</i>\n\n", systemName);
if (!always) {
if (end > 12)
{
fprintf(file, "2\n00:00:%d,000 --> 00:00:%d,000\n%s\n<i>%s</i>\n", end-4, end, gameName, systemName);
}
}
fflush(file);
fclose(file);
file = NULL;
}
void VideoComponent::setScreensaverMode(bool isScreensaver)
{
mScreensaverMode = isScreensaver;
}
VideoComponent::VideoComponent(Window* window) :
GuiComponent(window),
mStaticImage(window),
mVideoHeight(0),
mVideoWidth(0),
mStartDelayed(false),
mIsPlaying(false),
mShowing(false),
mScreensaverActive(false),
mDisable(false),
mScreensaverMode(false),
mTargetIsMax(false),
mTargetSize(0, 0)
{
// Setup the default configuration
mConfig.showSnapshotDelay = false;
mConfig.showSnapshotNoVideo = false;
mConfig.startDelay = 0;
if (mWindow->getGuiStackSize() > 1) {
topWindow(false);
}
std::string path = getTitleFolder();
if(!Utils::FileSystem::exists(path))
Utils::FileSystem::createDirectory(path);
}
VideoComponent::~VideoComponent()
{
// Stop any currently running video
stopVideo();
// Delete subtitle file, if existing
remove(getTitlePath().c_str());
}
void VideoComponent::onOriginChanged()
{
// Update the embeded static image
mStaticImage.setOrigin(mOrigin);
}
void VideoComponent::onPositionChanged()
{
// Update the embeded static image
mStaticImage.setPosition(mPosition);
}
void VideoComponent::onSizeChanged()
{
// Update the embeded static image
mStaticImage.onSizeChanged();
}
bool VideoComponent::setVideo(std::string path)
{
// Convert the path into a generic format
std::string fullPath = path.empty() ? path : Utils::FileSystem::getAbsolutePath(path);
// Check that it's changed
if (fullPath == mVideoPath)
return !path.empty();
// Store the path
mVideoPath = fullPath;
// If the file exists then set the new video
if (!fullPath.empty() && ResourceManager::getInstance()->fileExists(fullPath))
{
// Return true to show that we are going to attempt to play a video
return true;
}
// Return false to show that no video will be displayed
return false;
}
void VideoComponent::setImage(std::string path)
{
// Check that the image has changed
if (path == mStaticImagePath)
return;
mStaticImage.setImage(path);
mFadeIn = 0.0f;
mStaticImagePath = path;
}
void VideoComponent::setDefaultVideo()
{
setVideo(mConfig.defaultVideoPath);
}
void VideoComponent::setOpacity(unsigned char opacity)
{
mOpacity = opacity;
// Update the embeded static image
mStaticImage.setOpacity(opacity);
}
void VideoComponent::render(const Transform4x4f& parentTrans)
{
if (!isVisible())
return;
Transform4x4f trans = parentTrans * getTransform();
GuiComponent::renderChildren(trans);
Renderer::setMatrix(trans);
// Handle the case where the video is delayed
handleStartDelay();
// Handle looping of the video
handleLooping();
}
void VideoComponent::renderSnapshot(const Transform4x4f& parentTrans)
{
// This is the case where the video is not currently being displayed. Work out
// if we need to display a static image
if ((mConfig.showSnapshotNoVideo && mVideoPath.empty()) || (mStartDelayed && mConfig.showSnapshotDelay))
{
// Display the static image instead
mStaticImage.setOpacity((unsigned char)(mFadeIn * 255.0f));
mStaticImage.render(parentTrans);
}
}
void VideoComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties)
{
using namespace ThemeFlags;
GuiComponent::applyTheme(theme, view, element, (properties ^ SIZE) | ((properties & (SIZE | POSITION)) ? ORIGIN : 0));
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "video");
if(!elem)
return;
Vector2f scale = getParent() ? getParent()->getSize() : Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
if(properties & ThemeFlags::SIZE)
{
if(elem->has("size"))
setResize(elem->get<Vector2f>("size") * scale);
else if(elem->has("maxSize"))
setMaxSize(elem->get<Vector2f>("maxSize") * scale);
}
if(elem->has("default"))
mConfig.defaultVideoPath = elem->get<std::string>("default");
if((properties & ThemeFlags::DELAY) && elem->has("delay"))
mConfig.startDelay = (unsigned)(elem->get<float>("delay") * 1000.0f);
if (elem->has("showSnapshotNoVideo"))
mConfig.showSnapshotNoVideo = elem->get<bool>("showSnapshotNoVideo");
if (elem->has("showSnapshotDelay"))
mConfig.showSnapshotDelay = elem->get<bool>("showSnapshotDelay");
}
std::vector<HelpPrompt> VideoComponent::getHelpPrompts()
{
std::vector<HelpPrompt> ret;
ret.push_back(HelpPrompt("a", "select"));
return ret;
}
void VideoComponent::handleStartDelay()
{
// Only play if any delay has timed out
if (mStartDelayed)
{
if (mStartTime > SDL_GetTicks())
{
// Timeout not yet completed
return;
}
// Completed
mStartDelayed = false;
// Clear the playing flag so startVideo works
mIsPlaying = false;
startVideo();
}
}
void VideoComponent::handleLooping()
{
}
void VideoComponent::startVideoWithDelay()
{
// If not playing then either start the video or initiate the delay
if (!mIsPlaying)
{
// Set the video that we are going to be playing so we don't attempt to restart it
mPlayingVideoPath = mVideoPath;
if (mConfig.startDelay == 0 || PowerSaver::getMode() == PowerSaver::INSTANT)
{
// No delay. Just start the video
mStartDelayed = false;
startVideo();
}
else
{
// Configure the start delay
mStartDelayed = true;
mFadeIn = 0.0f;
mStartTime = SDL_GetTicks() + mConfig.startDelay;
}
mIsPlaying = true;
}
}
void VideoComponent::update(int deltaTime)
{
manageState();
// If the video start is delayed and there is less than the fade time then set the image fade
// accordingly
if (mStartDelayed)
{
Uint32 ticks = SDL_GetTicks();
if (mStartTime > ticks)
{
Uint32 diff = mStartTime - ticks;
if (diff < FADE_TIME_MS)
{
mFadeIn = (float)diff / (float)FADE_TIME_MS;
return;
}
}
}
// If the fade in is less than 1 then increment it
if (mFadeIn < 1.0f)
{
mFadeIn += deltaTime / (float)FADE_TIME_MS;
if (mFadeIn > 1.0f)
mFadeIn = 1.0f;
}
GuiComponent::update(deltaTime);
}
void VideoComponent::manageState()
{
// We will only show if the component is on display and the screensaver
// is not active and the component is visible
bool show = mShowing && !mScreensaverActive && !mDisable && mVisible;
// See if we're already playing
if (mIsPlaying)
{
// If we are not on display then stop the video from playing
if (!show)
{
stopVideo();
}
else
{
if (mVideoPath != mPlayingVideoPath)
{
// Path changed. Stop the video. We will start it again below because
// mIsPlaying will be modified by stopVideo to be false
stopVideo();
}
}
}
// Need to recheck variable rather than 'else' because it may be modified above
if (!mIsPlaying)
{
// If we are on display then see if we should start the video
if (show && !mVideoPath.empty())
{
startVideoWithDelay();
}
}
}
void VideoComponent::onShow()
{
mShowing = true;
manageState();
}
void VideoComponent::onHide()
{
mShowing = false;
manageState();
}
void VideoComponent::onScreenSaverActivate()
{
mScreensaverActive = true;
manageState();
}
void VideoComponent::onScreenSaverDeactivate()
{
mScreensaverActive = false;
manageState();
}
void VideoComponent::topWindow(bool isTop)
{
mDisable = !isTop;
manageState();
}