Skip to content

Commit

Permalink
Support OSD locking
Browse files Browse the repository at this point in the history
  • Loading branch information
xfangfang committed Nov 22, 2023
1 parent 5a863ff commit 129b6b6
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 25 deletions.
1 change: 1 addition & 0 deletions resources/svg/player-lock.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions resources/svg/player-unlock.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions resources/xml/views/video_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,28 @@
positionType="absolute"
visibility="invisible"/>

<brls:Box
width="50"
height="50"
alignItems="center"
backgroundColor="#303030A0"
focusable="true"
focusUp="video/osd/lock/box"
focusDown="video/osd/lock/box"
focusRight="video/osd/lock/box"
focusLeft="video/osd/lock/box"
cornerRadius="8"
positionTop="45%"
positionRight="20"
positionType="absolute"
id="video/osd/lock/box">
<SVGImage
id="video/osd/lock/icon"
detachedX="13"
detachedY="13"
height="24"
width="24"
SVG="@res/svg/player-unlock.svg"/>
</brls:Box>

</brls:Box>
9 changes: 9 additions & 0 deletions wiliwili/include/view/video_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class VideoView : public brls::Box {

void onOSDStateChanged(bool state);

void toggleOSDLock();

void toggleDanmaku();

void toggleOSD();
Expand All @@ -94,6 +96,8 @@ class VideoView : public brls::Box {

void hideVideoSpeedButton();

void hideOSDLockButton();

void disableCloseOnEndOfFile();

void disableBottomBar();
Expand Down Expand Up @@ -262,12 +266,17 @@ class VideoView : public brls::Box {
BRLS_BIND(brls::Label, hintLabel, "video/osd/hint/label");
BRLS_BIND(brls::Box, hintBox, "video/osd/hint/box");
BRLS_BIND(VideoProfile, videoProfile, "video/profile");
BRLS_BIND(brls::Box, osdLockBox, "video/osd/lock/box");
BRLS_BIND(SVGImage, osdLockIcon, "video/osd/lock/icon");


// OSD
time_t osdLastShowTime = 0;
const time_t OSD_SHOW_TIME = 5; //默认显示五秒
OSDState osd_state = OSDState::HIDDEN;
bool is_osd_shown = false;
bool is_osd_lock = false;
bool hide_lock_button = false;
bool is_seeking = false;
int64_t seeking_range = 0;
size_t seeking_iter = 0;
Expand Down
2 changes: 2 additions & 0 deletions wiliwili/source/activity/player_base_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ void BasePlayerActivity::setCommonData() {

if (brls::Application::ORIGINAL_WINDOW_HEIGHT < 720)
video->hideStatusLabel();

video->hideOSDLockButton();
}

void BasePlayerActivity::showShareDialog(const std::string& link) {
Expand Down
103 changes: 79 additions & 24 deletions wiliwili/source/view/video_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ VideoView::VideoView() {
static bool ignoreSpeed = false;
switch (status.state) {
case brls::GestureState::UNSURE: {
if (isLiveMode) break;
if (isLiveMode || is_osd_lock) break;
// 长按加速
if (fabs(mpvCore->getSpeed() - 1) > 10e-2) {
ignoreSpeed = true;
Expand Down Expand Up @@ -188,6 +188,12 @@ VideoView::VideoView() {
static int click_state = ClickState::IDLE;
static int64_t press_time = 0;

// 当 osd 锁定时直接切换 osd 即可
if (is_osd_lock) {
this->toggleOSD();
break;
}

int CHECK_TIME = 200000;
static size_t tap_iter = 0;
switch (click_state) {
Expand Down Expand Up @@ -447,6 +453,14 @@ VideoView::VideoView() {
this->btnCastIcon->getParent()->addGestureRecognizer(
new brls::TapGestureRecognizer(this->btnCastIcon->getParent()));

/// OSD 锁定按钮
this->osdLockBox->registerClickAction([this](...) {
this->toggleOSDLock();
return true;
});
this->osdLockBox->addGestureRecognizer(
new brls::TapGestureRecognizer(this->osdLockBox));

this->refreshDanmakuIcon();

this->registerAction(
Expand Down Expand Up @@ -536,15 +550,16 @@ VideoView::~VideoView() {
void VideoView::draw(NVGcontext* vg, float x, float y, float width,
float height, Style style, FrameContext* ctx) {
if (!mpvCore->isValid()) return;
float alpha = this->getAlpha();
float alpha = this->getAlpha();
time_t current = wiliwili::unix_time();
bool drawOSD = current < this->osdLastShowTime;
bool drawOSD = current < this->osdLastShowTime;

// draw video
mpvCore->draw(brls::Rect(x, y, width, height), alpha);

// draw highlight progress
if (HIGHLIGHT_PROGRESS_BAR && !drawOSD) {
// OSD绘制时,进度条也包含了高能进度条,避免太杂乱仅在不显示 OSD 时绘制
if (HIGHLIGHT_PROGRESS_BAR && (!drawOSD || is_osd_lock)) {
drawHighlightProgress(vg, x, y + height, width, alpha);
}

Expand All @@ -571,34 +586,42 @@ void VideoView::draw(NVGcontext* vg, float x, float y, float width,
this->onOSDStateChanged(true);
}

// draw highlight progress
auto sliderRect = osdSlider->getFrame();
drawHighlightProgress(vg, sliderRect.getMinX() + 30,
sliderRect.getMinY() + 25,
sliderRect.getWidth() - 60, alpha);

osdTopBox->setVisibility(brls::Visibility::VISIBLE);
osdBottomBox->setVisibility(brls::Visibility::VISIBLE);
osdBottomBox->frame(ctx);
osdTopBox->frame(ctx);

// draw subtitle (upon osd)
if (showSubtitleSetting)
SubtitleCore::instance().drawSubtitle(vg, x, y, width, height - 120,
getAlpha());
// 当 osd 锁定时,只显示锁定按钮
if (!is_osd_lock) {
// draw highlight progress
auto sliderRect = osdSlider->getFrame();
drawHighlightProgress(vg, sliderRect.getMinX() + 30,
sliderRect.getMinY() + 25,
sliderRect.getWidth() - 60, alpha);

// draw osd
osdTopBox->setVisibility(brls::Visibility::VISIBLE);
osdBottomBox->setVisibility(brls::Visibility::VISIBLE);
osdBottomBox->frame(ctx);
osdTopBox->frame(ctx);
}
// draw osd lock button
if (!hide_lock_button) {
osdLockBox->setVisibility(brls::Visibility::VISIBLE);
osdLockBox->frame(ctx);
}
} else {
if (is_osd_shown) {
is_osd_shown = false;
this->onOSDStateChanged(false);
}
osdTopBox->setVisibility(brls::Visibility::INVISIBLE);
osdBottomBox->setVisibility(brls::Visibility::INVISIBLE);

// draw subtitle (without osd)
if (showSubtitleSetting)
SubtitleCore::instance().drawSubtitle(vg, x, y, width, height,
getAlpha());
osdLockBox->setVisibility(brls::Visibility::INVISIBLE);
}

// draw subtitle
// 在正常显示 osd 时,将字幕向上偏移,避免被 OSD 挡住
if (showSubtitleSetting)
SubtitleCore::instance().drawSubtitle(
vg, x, y, width,
height - ((drawOSD && !is_osd_lock) ? 120.0f : 0.0f), alpha);

if (current > this->hintLastShowTime &&
this->hintBox->getVisibility() == brls::Visibility::VISIBLE) {
this->clearHint();
Expand Down Expand Up @@ -801,6 +824,29 @@ void VideoView::onOSDStateChanged(bool state) {
}
}

void VideoView::toggleOSDLock() {
is_osd_lock = !is_osd_lock;
this->osdLockIcon->setImageFromSVGRes(
is_osd_lock ? "svg/player-lock.svg" : "svg/player-unlock.svg");
if (is_osd_lock) {
osdTopBox->setVisibility(brls::Visibility::GONE);
osdBottomBox->setVisibility(brls::Visibility::GONE);
// 锁定时上下按键不可用
osdLockBox->setCustomNavigationRoute(FocusDirection::UP,
"video/osd/lock/box");
osdLockBox->setCustomNavigationRoute(FocusDirection::DOWN,
"video/osd/lock/box");
} else {
// 如果在锁定时,osdLockBox 获取到了焦点,通过点击进入了非锁定状态
// 避免上下按键不可用,手动设置上下按键的导航路线
osdLockBox->setCustomNavigationRoute(FocusDirection::UP,
"video/osd/setting");
osdLockBox->setCustomNavigationRoute(FocusDirection::DOWN,
"video/osd/toggle");
}
this->showOSD();
}

void VideoView::toggleDanmaku() {
if (DanmakuCore::DANMAKU_ON) {
DanmakuCore::DANMAKU_ON = false;
Expand Down Expand Up @@ -855,6 +901,11 @@ void VideoView::hideVideoSpeedButton() {
videoSpeed->getParent()->setVisibility(brls::Visibility::GONE);
}

void VideoView::hideOSDLockButton() {
osdLockBox->setVisibility(brls::Visibility::INVISIBLE);
hide_lock_button = true;
}

void VideoView::hideStatusLabel() {
leftStatusLabel->setVisibility(brls::Visibility::GONE);
centerStatusLabel->setVisibility(brls::Visibility::GONE);
Expand Down Expand Up @@ -1297,6 +1348,10 @@ void VideoView::unRegisterMpvEvent() {

void VideoView::onChildFocusGained(View* directChild, View* focusedView) {
Box::onChildFocusGained(directChild, focusedView);
if (is_osd_lock) {
brls::Application::giveFocus(this->osdLockBox);
return;
}
// 只有在全屏显示OSD时允许OSD组件获取焦点
if (this->isFullscreen() && isOSDShown()) {
// 当弹幕按钮隐藏时不可获取焦点
Expand Down

0 comments on commit 129b6b6

Please sign in to comment.