-
Notifications
You must be signed in to change notification settings - Fork 60
Implement a better options menu #42
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
Draft
Polarite
wants to merge
13
commits into
Bixilon:master
Choose a base branch
from
Polarite:mistress
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4be5d62
WIP First draft of a... functioning settings menu and fixes to cloud …
Polarite 6ea4d88
Update AbstractButtonElement.kt
Polarite dbfd760
Simplify settings menus using Menu base class and fix cloud fullbright
Polarite 371dc9a
Added Language Settings, done most of the requested fixes.
Polarite f9f3be2
Fixes to cloud settings, Added controls settings, fixed slider elemen…
Polarite 6053339
Update copyright year
Polarite b41a86f
Make duplicate keybinds in controls change to red.
Polarite e2b3ad2
Fix bindingmanager getting confused when combination keys are used.
Polarite e6ed0a7
fix combination key holding in keyactionfilter.
Polarite 23df09e
Fixes to combination key displays and overriding keys if a combinatio…
Polarite 165e47e
Implemented a textbox element and a search system for controls just i…
Polarite b40e69d
language: add integrated language as fallback
Bixilon 0e7c250
option menu: button delegate
Bixilon 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
229 changes: 229 additions & 0 deletions
229
src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/input/slider/SliderElement.kt
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,229 @@ | ||
| /* | ||
| * Minosoft | ||
| * Copyright (C) 2020-2025 Moritz Zwerger | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| * | ||
| * This software is not affiliated with Mojang AB, the original developer of Minecraft. | ||
| */ | ||
|
|
||
| package de.bixilon.minosoft.gui.rendering.gui.elements.input.slider | ||
|
|
||
| import de.bixilon.kmath.vec.vec2.f.Vec2f | ||
| import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft | ||
| import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer | ||
| import de.bixilon.minosoft.gui.rendering.gui.elements.Element | ||
| import de.bixilon.minosoft.gui.rendering.gui.elements.primitive.AtlasImageElement | ||
| import de.bixilon.minosoft.gui.rendering.gui.elements.text.TextElement | ||
| import de.bixilon.minosoft.gui.rendering.gui.input.MouseCapturing | ||
| import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions.Companion.copy | ||
| import de.bixilon.minosoft.gui.rendering.gui.input.mouse.MouseActions | ||
| import de.bixilon.minosoft.gui.rendering.gui.input.mouse.MouseButtons | ||
| import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions | ||
| import de.bixilon.minosoft.gui.rendering.gui.mesh.consumer.GuiVertexConsumer | ||
| import de.bixilon.minosoft.gui.rendering.system.window.CursorShapes | ||
| import kotlin.math.roundToInt | ||
|
|
||
| class SliderElement( | ||
| guiRenderer: GUIRenderer, | ||
| private val label: String, | ||
| private val min: Float, | ||
| private val max: Float, | ||
| initialValue: Float, | ||
| private val onChange: (Float) -> Unit | ||
| ) : Element(guiRenderer), MouseCapturing { | ||
| private val buttonAtlas = guiRenderer.atlas[BUTTON_ATLAS] | ||
|
|
||
| var textElement: TextElement | ||
| private var isDragging = false | ||
| private var isHovered = false | ||
| private var isHandleHovered = false | ||
|
|
||
| var disabled: Boolean = false | ||
| set(value) { | ||
| if (field == value) return | ||
| field = value | ||
| cacheUpToDate = false | ||
| } | ||
|
|
||
| override val isCapturingMouse: Boolean get() = isDragging | ||
|
|
||
| var value: Float = initialValue.coerceIn(min, max) | ||
| set(value) { | ||
| val clamped = value.coerceIn(min, max) | ||
| if (field != clamped) { | ||
| field = clamped | ||
| updateText() | ||
| onChange(clamped) | ||
| cacheUpToDate = false | ||
| } | ||
| } | ||
|
|
||
| init { | ||
| textElement = TextElement(guiRenderer, getDisplayText(), background = null, parent = this) | ||
| updateText() | ||
| size = Vec2f(textElement.size.x + TEXT_PADDING * 2, SLIDER_HEIGHT) | ||
| } | ||
|
|
||
| private fun getDisplayText(): String { | ||
| return "$label: ${value.roundToInt()}" | ||
| } | ||
|
|
||
| private fun updateText() { | ||
| textElement.text = getDisplayText() | ||
| textElement.silentApply() | ||
| if (size.x == 0.0f) { | ||
| size = Vec2f(textElement.size.x + TEXT_PADDING * 2, SLIDER_HEIGHT) | ||
| } | ||
| } | ||
|
|
||
| override fun forceSilentApply() { | ||
| textElement.silentApply() | ||
| cacheUpToDate = false | ||
| } | ||
|
|
||
| override fun forceRender(offset: Vec2f, consumer: GuiVertexConsumer, options: GUIVertexOptions?) { | ||
| val size = size | ||
| val renderOptions = if (disabled) options.copy(alpha = 0.4f) else options | ||
|
|
||
| val trackTexture = buttonAtlas?.get("disabled") ?: guiRenderer.context.textures.whiteTexture | ||
| val trackBackground = AtlasImageElement(guiRenderer, trackTexture) | ||
| trackBackground.size = size | ||
| trackBackground.render(offset, consumer, renderOptions) | ||
|
|
||
| val normalizedValue = (value - min) / (max - min) | ||
| val handleWidth = HANDLE_WIDTH | ||
| val trackWidth = size.x - handleWidth | ||
| val handleX = trackWidth * normalizedValue | ||
|
|
||
| val handleTexture = if (disabled) { | ||
| buttonAtlas?.get("disabled") | ||
| } else if (isHandleHovered || isDragging) { | ||
| buttonAtlas?.get("hovered") | ||
| } else { | ||
| buttonAtlas?.get("normal") | ||
| } ?: guiRenderer.context.textures.whiteTexture | ||
|
|
||
| val slider = AtlasImageElement(guiRenderer, handleTexture) | ||
| slider.size = Vec2f(handleWidth, SLIDER_HEIGHT) | ||
| slider.render(offset + Vec2f(handleX, 0.0f), consumer, renderOptions) | ||
|
|
||
| val textSize = textElement.size | ||
| val textX = (size.x - textSize.x) / 2 | ||
| val textY = (size.y - textSize.y) / 2 | ||
| textElement.render(offset + Vec2f(textX, textY), consumer, renderOptions) | ||
| } | ||
|
|
||
| override fun onMouseAction(position: Vec2f, button: MouseButtons, action: MouseActions, count: Int): Boolean { | ||
| if (disabled) { | ||
| return true | ||
| } | ||
| if (button != MouseButtons.LEFT) { | ||
| return true | ||
| } | ||
|
|
||
| when (action) { | ||
| MouseActions.PRESS -> { | ||
| isDragging = true | ||
| updateValueFromPosition(position.x) | ||
| cacheUpToDate = false | ||
| } | ||
| MouseActions.RELEASE -> { | ||
| isDragging = false | ||
| context.window.resetCursor() | ||
| cacheUpToDate = false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| override fun onMouseMove(position: Vec2f, absolute: Vec2f): Boolean { | ||
| if (isDragging) { | ||
| updateValueFromPosition(position.x) | ||
| } | ||
|
|
||
| if (!isDragging) { | ||
| val normalizedValue = (value - min) / (max - min) | ||
| val handleWidth = HANDLE_WIDTH | ||
| val trackWidth = size.x - handleWidth | ||
| val handleX = trackWidth * normalizedValue | ||
|
|
||
| val wasHandleHovered = isHandleHovered | ||
| isHandleHovered = position.x >= handleX && position.x < handleX + handleWidth | ||
|
|
||
| if (wasHandleHovered != isHandleHovered) { | ||
| cacheUpToDate = false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| override fun onMouseEnter(position: Vec2f, absolute: Vec2f): Boolean { | ||
| isHovered = true | ||
| context.window.cursorShape = CursorShapes.HAND | ||
|
|
||
| val normalizedValue = (value - min) / (max - min) | ||
| val handleWidth = HANDLE_WIDTH | ||
| val trackWidth = size.x - handleWidth | ||
| val handleX = trackWidth * normalizedValue | ||
|
|
||
| isHandleHovered = position.x >= handleX && position.x < handleX + handleWidth | ||
| cacheUpToDate = false | ||
| return true | ||
| } | ||
|
|
||
| override fun onMouseLeave(): Boolean { | ||
| isHovered = false | ||
| isHandleHovered = false | ||
| if (!isDragging) { | ||
| context.window.resetCursor() | ||
| } | ||
| cacheUpToDate = false | ||
| return super.onMouseLeave() | ||
| } | ||
|
|
||
| override fun onMouseActionOutside(relativeX: Float, button: MouseButtons, action: MouseActions): Boolean { | ||
| if (!isDragging) return false | ||
|
|
||
| if (button == MouseButtons.LEFT && action == MouseActions.RELEASE) { | ||
| isDragging = false | ||
| context.window.resetCursor() | ||
| cacheUpToDate = false | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| override fun onMouseMoveOutside(relativeX: Float): Boolean { | ||
| if (!isDragging) return false | ||
| updateValueFromPosition(relativeX) | ||
| return true | ||
| } | ||
|
|
||
| private fun updateValueFromPosition(x: Float) { | ||
| val handleWidth = HANDLE_WIDTH | ||
| val trackWidth = size.x - handleWidth | ||
|
|
||
| if (trackWidth <= 0) { | ||
| value = min | ||
| return | ||
| } | ||
|
|
||
| val normalizedX = (x - handleWidth / 2) / trackWidth | ||
| value = min + normalizedX * (max - min) | ||
| } | ||
|
|
||
| companion object { | ||
| val BUTTON_ATLAS = minecraft("elements/button") | ||
| private const val TEXT_PADDING = 4.0f | ||
| private const val SLIDER_HEIGHT = 20.0f | ||
| private const val HANDLE_WIDTH = 8.0f | ||
| } | ||
| } | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the return value means that the event is consumed. Is it when not dragging?