Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improved full screen #464

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/webgal/src/UI/Menu/Options/Display/Display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export function Display() {
<div className={styles.Options_main_content_half}>
<NormalOption key="fullScreen" title={t('fullScreen.title')}>
<NormalButton
textList={t('fullScreen.options.yes', 'fullScreen.options.no')}
textList={t('fullScreen.options.on', 'fullScreen.options.off')}
functionList={[
() => {
dispatch(setOptionData({ key: 'fullScreen', value: fullScreenOption.yes }));
dispatch(setOptionData({ key: 'fullScreen', value: fullScreenOption.on }));
setStorage();
},
() => {
dispatch(setOptionData({ key: 'fullScreen', value: fullScreenOption.no }));
dispatch(setOptionData({ key: 'fullScreen', value: fullScreenOption.off }));
setStorage();
},
]}
Expand Down
2 changes: 1 addition & 1 deletion packages/webgal/src/UI/Title/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Title: FC = () => {
onClick={() => {
playBgm(GUIState.titleBgm);
dispatch(setVisibility({ component: 'isEnterGame', visibility: true }));
if (fullScreen === fullScreenOption.yes) document.documentElement.requestFullscreen();
if (fullScreen === fullScreenOption.on) document.documentElement.requestFullscreen();
}}
onMouseEnter={playSeEnter}
/>
Expand Down
27 changes: 24 additions & 3 deletions packages/webgal/src/hooks/useFullScreen.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
import { setStorage } from '@/Core/controller/storage/storageController';
import { RootState } from '@/store/store';
import { fullScreenOption } from '@/store/userDataInterface';
import { setOptionData } from '@/store/userDataReducer';
import { useEffect } from 'react';
import { useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';

export function useFullScreen() {
const userDataState = useSelector((state: RootState) => state.userData);
const GUIState = useSelector((state: RootState) => state.GUI);
const dispatch = useDispatch();
const fullScreen = userDataState.optionData.fullScreen;
const isEnterGame = GUIState.isEnterGame;
let currentWindowHeight = window.innerHeight;

useEffect(() => {
switch (fullScreen) {
case fullScreenOption.yes: {
case fullScreenOption.on: {
if (isEnterGame) {
document.documentElement.requestFullscreen();
};
break;
}
case fullScreenOption.no: {
case fullScreenOption.off: {
if (document.fullscreenElement) {
document.exitFullscreen();
};
break;
}
}
}, [fullScreen]);

/**
* 通过窗口高度变化判断是否退出全屏,并更改全屏状态
*/
useEffect(() => {
const isExitingFullScreen = () => {
if (fullScreen === fullScreenOption.on && isEnterGame && currentWindowHeight >= window.innerHeight) {
dispatch(setOptionData({ key: 'fullScreen', value: fullScreenOption.off }));
setStorage();
}
currentWindowHeight = window.innerHeight;
};
window.addEventListener('resize', isExitingFullScreen);
return () => {
window.removeEventListener('resize', isExitingFullScreen);
};
}, [fullScreen, currentWindowHeight]);
};
35 changes: 33 additions & 2 deletions packages/webgal/src/hooks/useHotkey.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { useGenSyncRef } from '@/hooks/useGenSyncRef';
import { RootState } from '@/store/store';
import { useMounted, useUnMounted, useUpdated } from '@/hooks/useLifeCycle';
import { useCallback, useRef } from 'react';
import { useCallback, useEffect, useRef } from 'react';
import { componentsVisibility, MenuPanelTag } from '@/store/guiInterface';
import { setVisibility } from '@/store/GUIReducer';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { startFast, stopAll, stopFast } from '@/Core/controller/gamePlay/fastSkip';
import { nextSentence } from '@/Core/controller/gamePlay/nextSentence';
import styles from '@/UI/Backlog/backlog.module.scss';
import throttle from 'lodash/throttle';
import { fastSaveGame } from '@/Core/controller/storage/fastSaveLoad';
import { WebGAL } from '@/Core/WebGAL';
import { setOptionData } from '@/store/userDataReducer';
import { setStorage } from '@/Core/controller/storage/storageController';
import { fullScreenOption } from '@/store/userDataInterface';

// options备用
export interface HotKeyType {
Expand All @@ -36,6 +39,7 @@ export function useHotkey(opt?: HotKeyType) {
usePanic();
useFastSaveBeforeUnloadPage();
useSpaceAndEnter();
useToggleFullScreen();
}

/**
Expand Down Expand Up @@ -364,3 +368,30 @@ function hasScrollToBottom(dom: Element) {
const { scrollTop, clientHeight, scrollHeight } = dom;
return scrollTop === 0;
}

/**
* F11 进入全屏
*/
function useToggleFullScreen() {
const userDataState = useSelector((state: RootState) => state.userData);
const dispatch = useDispatch();
const fullScreen = userDataState.optionData.fullScreen;
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.code === 'F11') {
e.preventDefault();
if (fullScreen !== fullScreenOption.on) {
dispatch(setOptionData({ key: 'fullScreen', value: fullScreenOption.on }));
setStorage();
} else {
dispatch(setOptionData({ key: 'fullScreen', value: fullScreenOption.off }));
setStorage();
}
}
};
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [fullScreen]);
}
4 changes: 2 additions & 2 deletions packages/webgal/src/store/userDataInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export enum voiceOption {
}

export enum fullScreenOption {
yes,
no,
on,
off,
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/webgal/src/store/userDataReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const initialOptionSet: IOptionData = {
textboxOpacity: 75,
language: language.zhCn,
voiceInterruption: voiceOption.yes,
fullScreen: fullScreenOption.no,
fullScreen: fullScreenOption.off,
};

// 初始化用户数据
Expand Down
6 changes: 3 additions & 3 deletions packages/webgal/src/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ const en = {
title: 'Display',
options: {
fullScreen: {
title: 'Auto Full Screen',
title: 'Full Screen',
options: {
yes: 'YES',
no: 'NO',
on: 'ON',
off: 'OFF',
},
},
textSpeed: {
Expand Down
6 changes: 3 additions & 3 deletions packages/webgal/src/translations/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ const zhCn = {
title: '显示',
options: {
fullScreen: {
title: '自动全屏',
title: '全屏模式',
options: {
yes: '',
no: '',
on: '开启',
off: '关闭',
},
},
textSpeed: {
Expand Down
Loading