diff options
Diffstat (limited to 'src/QuickMedia.cpp')
-rw-r--r-- | src/QuickMedia.cpp | 105 |
1 files changed, 71 insertions, 34 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 4fa5f35..0a34508 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -17,6 +17,7 @@ const sf::Color front_color(43, 45, 47); const sf::Color back_color(33, 35, 37); +const int DOUBLE_CLICK_TIME = 500; namespace QuickMedia { Program::Program() : @@ -319,49 +320,80 @@ namespace QuickMedia { search_bar->onTextUpdateCallback = nullptr; search_bar->onTextSubmitCallback = nullptr; - watched_videos.insert(content_url); - std::unique_ptr<VideoPlayer> video_player = nullptr; - try { - printf("Play video: %s\n", content_url.c_str()); - video_player.reset(new VideoPlayer(&window, window_size.x, window_size.y, content_url.c_str())); - } catch(VideoInitializationException &e) { - show_notification("Video player", "Failed to create video player", Urgency::CRITICAL); - video_player = nullptr; - } + // This variable is needed because calling play_video is not possible in onPlaybackEndedCallback + bool play_next_video = false; - std::vector<std::unique_ptr<BodyItem>> related_media = current_plugin->get_related_media(content_url); - bool reload = false; - - if(video_player) { - video_player->onPlaybackEndedCallback = [this, &related_media, &video_player, &reload]() { - std::string new_video_url; - // Find video that hasn't been played before in this video session - for(auto it = related_media.begin(), end = related_media.end(); it != end; ++it) { - if(watched_videos.find((*it)->url) == watched_videos.end()) { - new_video_url = (*it)->url; - related_media.erase(it); - break; - } + auto onPlaybackEndedCallback = [this, &play_next_video]() { + std::string new_video_url; + std::vector<std::unique_ptr<BodyItem>> related_media = current_plugin->get_related_media(content_url); + // Find video that hasn't been played before in this video session + for(auto it = related_media.begin(), end = related_media.end(); it != end; ++it) { + if(watched_videos.find((*it)->url) == watched_videos.end()) { + new_video_url = (*it)->url; + break; } + } - // If there are no videos to play, then dont play any... - if(new_video_url.empty()) - return; + // If there are no videos to play, then dont play any... + if(new_video_url.empty()) { + show_notification("Video player", "No more related videos to play"); + return; + } - content_url = std::move(new_video_url); - related_media = current_plugin->get_related_media(content_url); - // TODO: This doesn't seem to work correctly right now, it causes video to become black when changing video (context reset bug). - //video_player->load_file(video_url); - reload = true; - }; - } + content_url = std::move(new_video_url); + play_next_video = true; + // TODO: This doesn't seem to work correctly right now, it causes video to become black when changing video (context reset bug). + //video_player->load_file(video_url); + }; + + std::unique_ptr<VideoPlayer> video_player = nullptr; + auto play_video = [this, &video_player, &onPlaybackEndedCallback]() { + watched_videos.insert(content_url); + try { + printf("Play video: %s\n", content_url.c_str()); + video_player.reset(new VideoPlayer(&window, window_size.x, window_size.y, content_url.c_str())); + video_player->onPlaybackEndedCallback = onPlaybackEndedCallback; + } catch(VideoInitializationException &e) { + show_notification("Video player", "Failed to create video player", Urgency::CRITICAL); + video_player = nullptr; + } + }; + play_video(); - sf::Clock resize_timer; + sf::Clock time_since_last_left_click; + int left_click_counter; + bool video_is_fullscreen = false; sf::Event event; - while (current_page == Page::VIDEO_CONTENT && !reload) { + auto on_doubleclick = [this, &video_is_fullscreen]() { + if(video_is_fullscreen) { + window.create(sf::VideoMode::getDesktopMode(), "QuickMedia", sf::Style::Default); + } else { + window.create(sf::VideoMode::getDesktopMode(), "QuickMedia", sf::Style::Fullscreen); + } + window.setVerticalSyncEnabled(true); + video_is_fullscreen = !video_is_fullscreen; + }; + + while (current_page == Page::VIDEO_CONTENT) { + if(play_next_video) { + play_next_video = false; + play_video(); + } + while (window.pollEvent(event)) { base_event_handler(event, Page::SEARCH_SUGGESTION); + if(event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) { + if(time_since_last_left_click.restart().asMilliseconds() <= DOUBLE_CLICK_TIME) { + if(++left_click_counter == 2) { + on_doubleclick(); + left_click_counter = 0; + } + } else { + left_click_counter = 1; + } + } + if(video_player) { if(event.type == sf::Event::Resized) video_player->resize(window_size); @@ -374,6 +406,11 @@ namespace QuickMedia { video_player->draw(window); window.display(); } + + if(video_is_fullscreen) { + window.create(sf::VideoMode::getDesktopMode(), "QuickMedia", sf::Style::Default); + window.setVerticalSyncEnabled(true); + } } enum class TrackMediaType { |