Skip to content

Commit 01d6fb7

Browse files
committed
Close video on Escape button
1 parent b1acef2 commit 01d6fb7

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

src/engine/Input.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ namespace Engine
9292
UI_HOME,
9393
UI_END,
9494

95+
Video_Close,
96+
9597
Count
9698
};
9799

src/engine/PlatformGLFW.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ int32_t PlatformGLFW::run(int argc, char** argv)
257257
bindMouseButton(GLFW_MOUSE_BUTTON_RIGHT, ActionType::UI_Close, false);
258258
bindMouseAxis(MouseAxis::ScrollY, ActionType::UI_Mousewheel, false);
259259

260+
bindKey(GLFW_KEY_ESCAPE, ActionType::Video_Close, false);
261+
260262
// // special keys test
261263
// bindKey(GLFW_KEY_LEFT_SHIFT, ActionType::PlayerForward, true);
262264
// bindKey(GLFW_KEY_RIGHT_SHIFT, ActionType::PlayerBackward, true);

src/media/Video.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ using AVFrameRefPtr = std::unique_ptr<AVFrame, deleteAVFrameRef>;
6868
}
6969

7070
namespace Media {
71-
class Video
71+
class Video: public std::enable_shared_from_this<Video>
7272
{
7373
public:
7474
Video(VideoPlayer &player, Engine::BaseEngine &engine, const std::string &fileName_)
@@ -99,6 +99,18 @@ namespace Media {
9999

100100
bool init(const uint16_t width, const uint16_t height)
101101
{
102+
actions.push_back(Engine::Input::RegisterAction(Engine::ActionType::Video_Close,
103+
[videoPtr = weak_from_this()](bool triggered, float /*intensity*/){
104+
auto video = videoPtr.lock();
105+
if (!video) {
106+
LogWarn() << "Video already finished, the action should not be triggered";
107+
return;
108+
}
109+
110+
if (triggered)
111+
video->stop = true;
112+
}));
113+
102114
av_init_packet(&packet);
103115
packet.data = nullptr;
104116
packet.size = 0;
@@ -309,6 +321,8 @@ namespace Media {
309321
assert(!videoFrames.empty());
310322
assert(videoFrames.front());
311323

324+
if (stop)
325+
return false;
312326
if (currentTime >= videoFrames.front()->pts) {
313327
AVFrameRefPtr frame;
314328
videoFrames.front().swap(frame);
@@ -344,6 +358,8 @@ namespace Media {
344358
AVPacket packet = {};
345359
Handle::TextureHandle texture;
346360
UI::ImageView *view;
361+
bool stop = false;
362+
std::vector<Engine::ManagedActionBinding> actions;
347363
};
348364
}
349365
#endif
@@ -364,7 +380,7 @@ void Media::VideoPlayer::play(std::string fileName)
364380
if (!Utils::endsWith(Utils::toUpper(fileName), ".BIK"))
365381
fileName += ".bik";
366382

367-
currentVideo = std::make_unique<Video>(*this, engine, fileName);
383+
currentVideo = std::make_shared<Video>(*this, engine, fileName);
368384
#else
369385
LogWarn() << "No libavcodec support compiled, won't play" << fileName;
370386
#endif

src/media/Video.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ namespace Media {
2525

2626
private:
2727
Engine::BaseEngine &engine;
28-
std::unique_ptr<Video> currentVideo;
28+
std::shared_ptr<Video> currentVideo;
2929
};
3030
}

src/ui/Hud.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ void UI::Hud::onInputAction(Engine::ActionType action)
254254
if (!m_pLoadingScreen->isHidden())
255255
return;
256256

257+
if (m_Engine.getVideoPlayer().active())
258+
return;
259+
257260
if (m_Engine.getConsole().isOpen())
258261
{
259262
if (action == ActionType::UI_Close || action == ActionType::UI_ToggleConsole)

0 commit comments

Comments
 (0)