Skip to content

Commit

Permalink
Close video on Escape button
Browse files Browse the repository at this point in the history
  • Loading branch information
mplucinski committed Jun 2, 2018
1 parent b1acef2 commit 01d6fb7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/engine/Input.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ namespace Engine
UI_HOME,
UI_END,

Video_Close,

Count
};

Expand Down
2 changes: 2 additions & 0 deletions src/engine/PlatformGLFW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ int32_t PlatformGLFW::run(int argc, char** argv)
bindMouseButton(GLFW_MOUSE_BUTTON_RIGHT, ActionType::UI_Close, false);
bindMouseAxis(MouseAxis::ScrollY, ActionType::UI_Mousewheel, false);

bindKey(GLFW_KEY_ESCAPE, ActionType::Video_Close, false);

// // special keys test
// bindKey(GLFW_KEY_LEFT_SHIFT, ActionType::PlayerForward, true);
// bindKey(GLFW_KEY_RIGHT_SHIFT, ActionType::PlayerBackward, true);
Expand Down
20 changes: 18 additions & 2 deletions src/media/Video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ using AVFrameRefPtr = std::unique_ptr<AVFrame, deleteAVFrameRef>;
}

namespace Media {
class Video
class Video: public std::enable_shared_from_this<Video>
{
public:
Video(VideoPlayer &player, Engine::BaseEngine &engine, const std::string &fileName_)
Expand Down Expand Up @@ -99,6 +99,18 @@ namespace Media {

bool init(const uint16_t width, const uint16_t height)
{
actions.push_back(Engine::Input::RegisterAction(Engine::ActionType::Video_Close,
[videoPtr = weak_from_this()](bool triggered, float /*intensity*/){
auto video = videoPtr.lock();
if (!video) {
LogWarn() << "Video already finished, the action should not be triggered";
return;
}

if (triggered)
video->stop = true;
}));

av_init_packet(&packet);
packet.data = nullptr;
packet.size = 0;
Expand Down Expand Up @@ -309,6 +321,8 @@ namespace Media {
assert(!videoFrames.empty());
assert(videoFrames.front());

if (stop)
return false;
if (currentTime >= videoFrames.front()->pts) {
AVFrameRefPtr frame;
videoFrames.front().swap(frame);
Expand Down Expand Up @@ -344,6 +358,8 @@ namespace Media {
AVPacket packet = {};
Handle::TextureHandle texture;
UI::ImageView *view;
bool stop = false;
std::vector<Engine::ManagedActionBinding> actions;
};
}
#endif
Expand All @@ -364,7 +380,7 @@ void Media::VideoPlayer::play(std::string fileName)
if (!Utils::endsWith(Utils::toUpper(fileName), ".BIK"))
fileName += ".bik";

currentVideo = std::make_unique<Video>(*this, engine, fileName);
currentVideo = std::make_shared<Video>(*this, engine, fileName);
#else
LogWarn() << "No libavcodec support compiled, won't play" << fileName;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/media/Video.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ namespace Media {

private:
Engine::BaseEngine &engine;
std::unique_ptr<Video> currentVideo;
std::shared_ptr<Video> currentVideo;
};
}
3 changes: 3 additions & 0 deletions src/ui/Hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ void UI::Hud::onInputAction(Engine::ActionType action)
if (!m_pLoadingScreen->isHidden())
return;

if (m_Engine.getVideoPlayer().active())
return;

if (m_Engine.getConsole().isOpen())
{
if (action == ActionType::UI_Close || action == ActionType::UI_ToggleConsole)
Expand Down

0 comments on commit 01d6fb7

Please sign in to comment.