-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Graphics Settings modal with live name-label tuning #4065
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| import { html, LitElement } from "lit"; | ||
| import { customElement, property, query, state } from "lit/decorators.js"; | ||
| import { crazyGamesSDK } from "src/client/CrazyGamesSDK"; | ||
| import { PauseGameIntentEvent } from "src/client/Transport"; | ||
| import { assetUrl } from "../../../core/AssetUrls"; | ||
| import { EventBus } from "../../../core/EventBus"; | ||
| import { UserSettings } from "../../../core/game/UserSettings"; | ||
| import { Controller } from "../../Controller"; | ||
| import { translateText } from "../../Utils"; | ||
| import type { GraphicsOverrides } from "../../render/gl"; | ||
| import renderDefaults from "../../render/gl/render-settings.json"; | ||
|
|
||
| const settingsIcon = assetUrl("images/SettingIconWhite.svg"); | ||
|
|
||
| const NAME_SCALE_MIN = 0.2; | ||
| const NAME_SCALE_MAX = 1.5; | ||
| const NAME_SCALE_STEP = 0.05; | ||
|
|
||
| const NAME_CULL_MIN = 0; | ||
| const NAME_CULL_MAX = 0.05; | ||
| const NAME_CULL_STEP = 0.001; | ||
|
|
||
| export class ShowGraphicsSettingsModalEvent { | ||
| constructor( | ||
| public readonly isVisible: boolean = true, | ||
| public readonly shouldPause: boolean = false, | ||
| public readonly isPaused: boolean = false, | ||
| ) {} | ||
| } | ||
|
|
||
| @customElement("graphics-settings-modal") | ||
| export class GraphicsSettingsModal extends LitElement implements Controller { | ||
| public eventBus: EventBus; | ||
| public userSettings: UserSettings; | ||
|
|
||
| @state() | ||
| private isVisible: boolean = false; | ||
|
|
||
| @query(".modal-overlay") | ||
| private modalOverlay!: HTMLElement; | ||
|
|
||
| @property({ type: Boolean }) | ||
| shouldPause = false; | ||
|
|
||
| @property({ type: Boolean }) | ||
| wasPausedWhenOpened = false; | ||
|
|
||
| init() { | ||
| this.eventBus.on(ShowGraphicsSettingsModalEvent, (event) => { | ||
| this.isVisible = event.isVisible; | ||
| this.shouldPause = event.shouldPause; | ||
| this.wasPausedWhenOpened = event.isPaused; | ||
| this.pauseGame(true); | ||
| this.requestUpdate(); | ||
| }); | ||
| } | ||
|
|
||
| private pauseGame(pause: boolean) { | ||
| if (this.shouldPause && !this.wasPausedWhenOpened) { | ||
| if (pause) { | ||
| crazyGamesSDK.gameplayStop(); | ||
| } else { | ||
| crazyGamesSDK.gameplayStart(); | ||
| } | ||
| this.eventBus.emit(new PauseGameIntentEvent(pause)); | ||
| } | ||
| } | ||
|
|
||
| createRenderRoot() { | ||
| return this; | ||
| } | ||
|
|
||
| connectedCallback() { | ||
| super.connectedCallback(); | ||
| window.addEventListener("click", this.handleOutsideClick, true); | ||
| window.addEventListener("keydown", this.handleKeyDown); | ||
| } | ||
|
|
||
| disconnectedCallback() { | ||
| window.removeEventListener("click", this.handleOutsideClick, true); | ||
| window.removeEventListener("keydown", this.handleKeyDown); | ||
| super.disconnectedCallback(); | ||
| } | ||
|
|
||
| private handleOutsideClick = (event: MouseEvent) => { | ||
| if ( | ||
| this.isVisible && | ||
| this.modalOverlay && | ||
| event.target === this.modalOverlay | ||
| ) { | ||
| this.closeModal(); | ||
| } | ||
| }; | ||
|
|
||
| private handleKeyDown = (event: KeyboardEvent) => { | ||
| if (this.isVisible && event.key === "Escape") { | ||
| this.closeModal(); | ||
| } | ||
| }; | ||
|
|
||
| public closeModal() { | ||
| this.isVisible = false; | ||
| this.requestUpdate(); | ||
| this.pauseGame(false); | ||
| } | ||
|
|
||
| private currentNameScale(): number { | ||
| return ( | ||
| this.userSettings.graphicsOverrides().name?.nameScaleFactor ?? | ||
| renderDefaults.name.nameScaleFactor | ||
| ); | ||
| } | ||
|
|
||
| private currentNameCull(): number { | ||
| return ( | ||
| this.userSettings.graphicsOverrides().name?.cullThreshold ?? | ||
| renderDefaults.name.cullThreshold | ||
| ); | ||
| } | ||
|
|
||
| private patchName(patch: Partial<GraphicsOverrides["name"]>) { | ||
| const current = this.userSettings.graphicsOverrides(); | ||
| this.userSettings.setGraphicsOverrides({ | ||
| ...current, | ||
| name: { ...current.name, ...patch }, | ||
| }); | ||
| this.requestUpdate(); | ||
| } | ||
|
|
||
| private onNameScaleChange(event: Event) { | ||
| const value = parseFloat((event.target as HTMLInputElement).value); | ||
| this.patchName({ nameScaleFactor: value }); | ||
| } | ||
|
|
||
| private onNameCullChange(event: Event) { | ||
| const value = parseFloat((event.target as HTMLInputElement).value); | ||
| this.patchName({ cullThreshold: value }); | ||
| } | ||
|
|
||
| private onResetClick() { | ||
| this.userSettings.setGraphicsOverrides({}); | ||
| this.requestUpdate(); | ||
| } | ||
|
|
||
| render() { | ||
| if (!this.isVisible) return null; | ||
|
|
||
| const nameScale = this.currentNameScale(); | ||
| const nameCull = this.currentNameCull(); | ||
|
|
||
| return html` | ||
| <div | ||
| class="modal-overlay fixed inset-0 bg-black/60 backdrop-blur-xs z-2000 flex items-center justify-center p-4" | ||
| @contextmenu=${(e: Event) => e.preventDefault()} | ||
| > | ||
| <div | ||
| class="bg-slate-800 border border-slate-600 rounded-lg max-w-md w-full max-h-[80vh] overflow-y-auto" | ||
| > | ||
| <div | ||
| class="flex items-center justify-between p-4 border-b border-slate-600" | ||
| > | ||
| <div class="flex items-center gap-2"> | ||
| <img | ||
| src=${settingsIcon} | ||
| alt="graphicsSettings" | ||
| width="24" | ||
|
evanpelle marked this conversation as resolved.
|
||
| height="24" | ||
| class="align-middle" | ||
| /> | ||
| <h2 class="text-xl font-semibold text-white"> | ||
| ${translateText("graphics_setting.title")} | ||
| </h2> | ||
| </div> | ||
| <button | ||
| class="text-slate-400 hover:text-white text-2xl font-bold leading-none" | ||
| @click=${this.closeModal} | ||
| > | ||
| × | ||
| </button> | ||
| </div> | ||
|
|
||
| <div class="p-4 flex flex-col gap-3"> | ||
| <div | ||
| class="px-3 py-1 text-xs font-semibold text-slate-400 uppercase tracking-wider" | ||
| > | ||
| ${translateText("graphics_setting.section_name_labels")} | ||
| </div> | ||
|
|
||
| <div | ||
| class="flex gap-3 items-center w-full text-left p-3 hover:bg-slate-700 rounded-sm text-white transition-colors" | ||
| > | ||
| <div class="flex-1"> | ||
| <div class="font-medium"> | ||
| ${translateText("graphics_setting.name_scale_label")} | ||
| </div> | ||
| <input | ||
| type="range" | ||
| min=${NAME_SCALE_MIN} | ||
| max=${NAME_SCALE_MAX} | ||
| step=${NAME_SCALE_STEP} | ||
| .value=${String(nameScale)} | ||
| @input=${this.onNameScaleChange} | ||
| class="w-full border border-slate-500 rounded-lg" | ||
| /> | ||
| </div> | ||
| <div class="text-sm text-slate-400 w-12 text-right"> | ||
| ${nameScale.toFixed(2)} | ||
| </div> | ||
| </div> | ||
|
|
||
| <div | ||
| class="flex gap-3 items-center w-full text-left p-3 hover:bg-slate-700 rounded-sm text-white transition-colors" | ||
| > | ||
| <div class="flex-1"> | ||
| <div class="font-medium"> | ||
| ${translateText("graphics_setting.name_cull_label")} | ||
| </div> | ||
| <div class="text-sm text-slate-400"> | ||
| ${translateText("graphics_setting.name_cull_desc")} | ||
| </div> | ||
| <input | ||
| type="range" | ||
| min=${NAME_CULL_MIN} | ||
| max=${NAME_CULL_MAX} | ||
| step=${NAME_CULL_STEP} | ||
| .value=${String(nameCull)} | ||
| @input=${this.onNameCullChange} | ||
| class="w-full border border-slate-500 rounded-lg" | ||
| /> | ||
| </div> | ||
| <div class="text-sm text-slate-400 w-12 text-right"> | ||
| ${nameCull.toFixed(3)} | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="border-t border-slate-600 pt-3 mt-4"> | ||
| <button | ||
| class="flex gap-3 items-center w-full text-left p-3 hover:bg-slate-700 rounded-sm text-white transition-colors" | ||
| @click=${this.onResetClick} | ||
| > | ||
| <div class="flex-1"> | ||
| <div class="font-medium"> | ||
| ${translateText("graphics_setting.reset_label")} | ||
| </div> | ||
| <div class="text-sm text-slate-400"> | ||
| ${translateText("graphics_setting.reset_desc")} | ||
| </div> | ||
| </div> | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| `; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.