screenshot delete button, element: check if key was consumed

This commit is contained in:
Bixilon 2022-02-17 13:42:20 +01:00
parent 0a86f61e85
commit 9fd1688b39
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
18 changed files with 193 additions and 99 deletions

View File

@ -0,0 +1,29 @@
/*
* Minosoft
* Copyright (C) 2022 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.data.text.events.click
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.input.mouse.MouseActions
import de.bixilon.minosoft.gui.rendering.gui.input.mouse.MouseButtons
import glm_.vec2.Vec2i
class ClickCallbackClickEvent(val callback: () -> Unit) : ClickEvent {
override fun onClick(guiRenderer: GUIRenderer, position: Vec2i, button: MouseButtons, action: MouseActions) {
if (button != MouseButtons.LEFT || action != MouseActions.PRESS) {
return
}
callback()
}
}

View File

@ -28,7 +28,7 @@ class CopyToClipboardClickEvent(
if (button != MouseButtons.LEFT || action != MouseActions.PRESS) {
return
}
if (guiRenderer.connection.profiles.gui.confirmation.copyToClipboard) {
if (!guiRenderer.connection.profiles.gui.confirmation.copyToClipboard) {
guiRenderer.renderWindow.window.clipboardText = text
return
}

View File

@ -35,7 +35,7 @@ class OpenFileClickEvent(
if (button != MouseButtons.LEFT || action != MouseActions.PRESS) {
return
}
if (guiRenderer.connection.profiles.gui.confirmation.openFile) {
if (!guiRenderer.connection.profiles.gui.confirmation.openFile) {
DesktopUtil.openFile(path)
return
}

View File

@ -38,7 +38,7 @@ class OpenURLClickEvent(
if (button != MouseButtons.LEFT || action != MouseActions.PRESS) {
return
}
if (guiRenderer.connection.profiles.gui.confirmation.openURL) {
if (!guiRenderer.connection.profiles.gui.confirmation.openURL) {
DesktopUtil.openURL(url)
return
}

View File

@ -29,7 +29,7 @@ class SendMessageClickEvent(
if (button != MouseButtons.LEFT || action != MouseActions.PRESS) {
return
}
if (guiRenderer.connection.profiles.gui.confirmation.sendMessage) {
if (!guiRenderer.connection.profiles.gui.confirmation.sendMessage) {
guiRenderer.connection.util.sendChatMessage(message)
return
}

View File

@ -113,42 +113,48 @@ open class ButtonElement(
cacheUpToDate = false
}
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions) {
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions): Boolean {
if (disabled) {
return
return true
}
if (button != MouseButtons.LEFT) {
return
return true
}
if (action != MouseActions.PRESS) {
return
return true
}
submit()
return true
}
override fun onKey(key: KeyCodes, type: KeyChangeTypes) {
override fun onKey(key: KeyCodes, type: KeyChangeTypes): Boolean {
if (!hovered) {
return
return true
}
if (disabled) {
return
return true
}
if (key != KeyCodes.KEY_ENTER) {
return
return true
}
if (type != KeyChangeTypes.PRESS) {
return
return true
}
submit()
return true
}
override fun onMouseEnter(position: Vec2i) {
override fun onMouseEnter(position: Vec2i): Boolean {
hovered = true
return true
}
override fun onMouseLeave() {
override fun onMouseLeave(): Boolean {
hovered = false
return true
}
open fun submit() {

View File

@ -171,12 +171,13 @@ open class TextElement(
renderInfo.currentLineNumber = 0
}
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions) {
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions): Boolean {
if (action != MouseActions.PRESS || button != MouseButtons.LEFT) {
return
return true
}
val text = getTextComponentAt(position) ?: return
val text = getTextComponentAt(position) ?: return false
text.clickEvent?.onClick(guiRenderer, position, button, action)
return true
}
private fun getTextComponentAt(position: Vec2i): TextComponent? {

View File

@ -92,8 +92,9 @@ open class TextFlowElement(
}
}
override fun onScroll(position: Vec2i, scrollOffset: Vec2d) {
override fun onScroll(position: Vec2i, scrollOffset: Vec2d): Boolean {
this.scrollOffset += scrollOffset.y.toInt()
return true
}
@Synchronized
@ -180,9 +181,10 @@ open class TextFlowElement(
}
}
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions) {
val pair = getAt(position) ?: return
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions): Boolean {
val pair = getAt(position) ?: return false
pair.first.textElement.onMouseAction(pair.second, button, action)
return true
}
private fun getAt(position: Vec2i): Pair<TextFlowLineElement, Vec2i>? {

View File

@ -96,7 +96,7 @@ class MarkTextElement(
super.forceRender(offset, consumer, options)
}
override fun onKey(key: KeyCodes, type: KeyChangeTypes) {
override fun onKey(key: KeyCodes, type: KeyChangeTypes): Boolean {
super.onKey(key, type)
val controlDown = guiRenderer.isKeyDown(ModifierKeys.CONTROL)
@ -104,7 +104,7 @@ class MarkTextElement(
when (key) {
KeyCodes.KEY_A -> {
if (!controlDown) {
return
return true
}
mark(0, chatComponent.message.length)
}
@ -114,8 +114,9 @@ class MarkTextElement(
}
}
KeyCodes.KEY_ESCAPE -> unmark()
else -> return
else -> return true
}
return true
}
fun copy() {

View File

@ -121,13 +121,14 @@ class TextInputElement(
onChange()
}
override fun onCharPress(char: Int) {
override fun onCharPress(char: Int): Boolean {
if (_value.length >= maxLength || !editable) {
return
return true
}
cursorTick = CURSOR_TICK_ON_ACTION
insert(char.toChar().toString())
forceApply()
return true
}
private fun mark(mark: Boolean, right: Boolean, modify: Int) {
@ -168,9 +169,9 @@ class TextInputElement(
textElement.unmark()
}
override fun onKey(key: KeyCodes, type: KeyChangeTypes) {
override fun onKey(key: KeyCodes, type: KeyChangeTypes): Boolean {
if (type == KeyChangeTypes.RELEASE) {
return
return true
}
val controlDown = guiRenderer.isKeyDown(ModifierKeys.CONTROL)
val shiftDown = guiRenderer.isKeyDown(ModifierKeys.SHIFT)
@ -189,12 +190,12 @@ class TextInputElement(
}
KeyCodes.KEY_BACKSPACE -> {
if (_value.isEmpty() || !editable) {
return
return true
}
if (textElement.marked) {
insert("")
} else if (_pointer == 0) {
return
return true
} else {
val delete = if (controlDown) calculateWordPointer(false) else -1
_value.delete(_pointer + delete, _pointer)
@ -204,12 +205,12 @@ class TextInputElement(
}
KeyCodes.KEY_DELETE -> {
if (_value.isEmpty() || !editable) {
return
return true
}
if (textElement.marked) {
insert("")
} else if (_pointer == _value.length) {
return
return true
} else {
val delete = if (controlDown) calculateWordPointer(true) else 1
_value.delete(_pointer, _pointer + delete)
@ -221,7 +222,7 @@ class TextInputElement(
if (!shiftDown) {
textElement.unmark()
}
return
return true
}
val modify = if (controlDown) {
calculateWordPointer(false)
@ -235,7 +236,7 @@ class TextInputElement(
if (!shiftDown && _pointer == _value.length) {
textElement.unmark()
}
return
return true
}
val modify = if (controlDown) {
calculateWordPointer(true)
@ -256,11 +257,13 @@ class TextInputElement(
else -> return textElement.onKey(key, type)
}
forceApply()
return true
}
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions) {
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions): Boolean {
if (action != MouseActions.PRESS) {
return
return true
}
val leftText = TextElement(guiRenderer, value, background = false)
leftText.prefMaxSize = Vec2i(position.x, size.y)
@ -282,6 +285,7 @@ class TextInputElement(
}
this._pointer = pointer
forceSilentApply()
return true
}
override fun onChildChange(child: Element) {

View File

@ -75,32 +75,36 @@ abstract class Menu(
}
}
override fun onMouseLeave() {
override fun onMouseLeave(): Boolean {
activeElement?.onMouseLeave()
activeElement = null
return true
}
override fun onMouseMove(position: Vec2i) {
override fun onMouseMove(position: Vec2i): Boolean {
val pair = getAt(position)
if (activeElement != pair?.first) {
activeElement?.onMouseLeave()
pair?.first?.onMouseEnter(pair.second)
activeElement = pair?.first
return
return true
}
pair?.first?.onMouseMove(pair.second)
return true
}
override fun onMouseEnter(position: Vec2i) {
override fun onMouseEnter(position: Vec2i): Boolean {
val pair = getAt(position)
pair?.first?.onMouseEnter(pair.second)
activeElement = pair?.first
return true
}
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions) {
val (element, delta) = getAt(position) ?: return
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions): Boolean {
val (element, delta) = getAt(position) ?: return true
element.onMouseAction(delta, button, action)
return true
}
override fun onChildChange(child: Element) {
@ -155,8 +159,7 @@ abstract class Menu(
super.tick()
}
override fun onKey(key: KeyCodes, type: KeyChangeTypes) {
super.onKey(key, type)
override fun onKey(key: KeyCodes, type: KeyChangeTypes): Boolean {
if (type != KeyChangeTypes.RELEASE && key == KeyCodes.KEY_TAB) {
var element: Element?
var initialIndex = elements.indexOf(activeElement)
@ -170,33 +173,36 @@ abstract class Menu(
index = 0
}
if (index == initialIndex) {
return
return true
}
element = elements.getOrNull(index) ?: return
element = elements.getOrNull(index) ?: return true
if (element.canFocus) {
break
}
}
if (element == null) {
return
return true
}
activeElement?.onMouseLeave()
element.onMouseEnter(Vec2i.EMPTY)
activeElement = element
return // no passthrough the key to current active element
return true // no passthrough the key to current active element
}
activeElement?.onKey(key, type)
return true
}
override fun onCharPress(char: Int) {
super.onCharPress(char)
override fun onCharPress(char: Int): Boolean {
activeElement?.onCharPress(char)
return true
}
override fun onScroll(position: Vec2i, scrollOffset: Vec2d) {
val (element, delta) = getAt(position) ?: return
override fun onScroll(position: Vec2i, scrollOffset: Vec2d): Boolean {
val (element, delta) = getAt(position) ?: return true
element.onScroll(delta, scrollOffset)
return true
}
private fun reset() {

View File

@ -0,0 +1,43 @@
/*
* Minosoft
* Copyright (C) 2022 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.gui.screen.menu.confirmation
import de.bixilon.kutil.concurrent.pool.DefaultThreadPool
import de.bixilon.minosoft.data.text.TextComponent
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.elements.input.button.ButtonElement
import java.io.File
class DeleteScreenshotDialog(
guiRenderer: GUIRenderer,
private val screenshot: File,
) : AbstractConfirmationMenu(
guiRenderer,
"Do you want to delete this screenshot?",
TextComponent(screenshot.name, color = VALUE_COLOR),
) {
override fun createButtons(): Array<ButtonElement> {
return arrayOf(
ButtonElement(guiRenderer, "§cSure, delete it") {
DefaultThreadPool += { screenshot.delete() }
close()
}
)
}
init {
initButtons()
}
}

View File

@ -118,31 +118,26 @@ class LayoutedGUIElement<T : LayoutedElement>(
}
// move out
this.lastPosition = INVALID_MOUSE_POSITION
elementLayout.onMouseLeave()
return true
return elementLayout.onMouseLeave()
}
val delta = position - offset
this.lastPosition = delta
if (lastPosition.isOutside(offset, size)) {
elementLayout.onMouseEnter(delta)
return true
return elementLayout.onMouseEnter(delta)
}
elementLayout.onMouseMove(delta)
return true
return elementLayout.onMouseMove(delta)
}
override fun onCharPress(char: Int): Boolean {
elementLayout.onCharPress(char)
return true
return elementLayout.onCharPress(char)
}
override fun onKeyPress(type: KeyChangeTypes, key: KeyCodes): Boolean {
val mouseButton = MouseButtons[key]
if (mouseButton == null) {
elementLayout.onKey(key, type)
return true
return elementLayout.onKey(key, type)
}
val position = lastPosition
@ -151,8 +146,7 @@ class LayoutedGUIElement<T : LayoutedElement>(
}
val mouseAction = MouseActions[type] ?: return false
elementLayout.onMouseAction(position, mouseButton, mouseAction)
return true
return elementLayout.onMouseAction(position, mouseButton, mouseAction)
}
override fun onScroll(scrollOffset: Vec2d): Boolean {
@ -160,8 +154,7 @@ class LayoutedGUIElement<T : LayoutedElement>(
if (lastPosition == INVALID_MOUSE_POSITION) {
return false
}
elementLayout.onScroll(position, scrollOffset)
return true
return elementLayout.onScroll(position, scrollOffset)
}
override fun onClose() {

View File

@ -34,12 +34,13 @@ abstract class AbstractChatElement(guiRenderer: GUIRenderer) : Element(guiRender
}
override fun onScroll(position: Vec2i, scrollOffset: Vec2d) {
override fun onScroll(position: Vec2i, scrollOffset: Vec2d): Boolean {
val size = messages.size
if (position.y > size.y || position.x > messages.size.x) {
return
return false
}
messages.onScroll(position, scrollOffset)
return true
}
override fun tick() {

View File

@ -133,11 +133,13 @@ class ChatElement(guiRenderer: GUIRenderer) : AbstractChatElement(guiRenderer) {
messages.onClose()
}
override fun onCharPress(char: Int) {
override fun onCharPress(char: Int): Boolean {
if (char == '§'.code) {
return input.onCharPress('&'.code)
input.onCharPress('&'.code)
} else {
input.onCharPress(char)
}
input.onCharPress(char)
return true
}
private fun submit() {
@ -154,23 +156,24 @@ class ChatElement(guiRenderer: GUIRenderer) : AbstractChatElement(guiRenderer) {
guiRenderer.gui.pop()
}
override fun onKey(key: KeyCodes, type: KeyChangeTypes) {
override fun onKey(key: KeyCodes, type: KeyChangeTypes): Boolean {
if (type != KeyChangeTypes.RELEASE) {
when (key) {
KeyCodes.KEY_ENTER -> {
return submit()
submit()
return true
}
KeyCodes.KEY_PAGE_UP -> {
messages.scrollOffset++
return
return true
}
KeyCodes.KEY_PAGE_DOWN -> {
messages.scrollOffset--
return
return true
}
KeyCodes.KEY_UP -> {
if (historyIndex <= 0) {
return
return true
}
val size = history.size
if (historyIndex > size) {
@ -183,11 +186,11 @@ class ChatElement(guiRenderer: GUIRenderer) : AbstractChatElement(guiRenderer) {
val size = history.size
historyIndex++
if (historyIndex > size) {
return
return true
}
if (historyIndex == size) {
input.value = ""
return
return true
}
input.value = history[historyIndex]
}
@ -195,11 +198,14 @@ class ChatElement(guiRenderer: GUIRenderer) : AbstractChatElement(guiRenderer) {
}
}
input.onKey(key, type)
return true
}
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions) {
val pair = getAt(position) ?: return
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions): Boolean {
val pair = getAt(position) ?: return false
pair.first.onMouseAction(pair.second, button, action)
return true
}
private fun getAt(position: Vec2i): Pair<Element, Vec2i>? {

View File

@ -82,9 +82,10 @@ class InternalChatElement(guiRenderer: GUIRenderer) : AbstractChatElement(guiRen
guiRenderer.gui.pop() // pop normal chat
}
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions) {
val pair = getAt(position) ?: return
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions): Boolean {
val pair = getAt(position) ?: return false
pair.first.onMouseAction(pair.second, button, action)
return true
}
private fun getAt(position: Vec2i): Pair<Element, Vec2i>? {

View File

@ -22,14 +22,14 @@ import glm_.vec2.Vec2i
interface InputElement {
fun onMouseMove(position: Vec2i) {}
fun onMouseEnter(position: Vec2i) {}
fun onMouseLeave() {}
fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions) {}
fun onScroll(position: Vec2i, scrollOffset: Vec2d) {}
fun onMouseMove(position: Vec2i) = false
fun onMouseEnter(position: Vec2i) = false
fun onMouseLeave() = false
fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions) = false
fun onScroll(position: Vec2i, scrollOffset: Vec2d) = false
fun onKey(key: KeyCodes, type: KeyChangeTypes) {}
fun onCharPress(char: Int) {}
fun onKey(key: KeyCodes, type: KeyChangeTypes) = false
fun onCharPress(char: Int) = false
// ToDo: drag
}

View File

@ -22,9 +22,12 @@ import de.bixilon.kutil.time.TimeUtil
import de.bixilon.minosoft.data.text.BaseComponent
import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.TextComponent
import de.bixilon.minosoft.data.text.events.click.ClickCallbackClickEvent
import de.bixilon.minosoft.data.text.events.click.OpenFileClickEvent
import de.bixilon.minosoft.data.text.events.hover.TextHoverEvent
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.gui.screen.menu.confirmation.DeleteScreenshotDialog
import de.bixilon.minosoft.gui.rendering.system.base.PixelTypes
import de.bixilon.minosoft.terminal.RunConfiguration
import glm_.vec2.Vec2i
@ -73,22 +76,20 @@ class ScreenshotTaker(
ImageIO.write(bufferedImage, "png", file)
renderWindow.connection.util.sendDebugMessage(BaseComponent(
"§aScreenshot saved to ",
"§aScreenshot saved: ",
TextComponent(file.name).apply {
color = ChatColors.WHITE
underline()
clickEvent = OpenFileClickEvent(file.slashPath)
hoverEvent = TextHoverEvent("Click to open")
},
// "\n",
// TextComponent("[DELETE]").apply {
// color = ChatColors.RED
// bold()
// clickEvent = ClickEvent(ClickEvent.ClickEventActions.OPEN_CONFIRMATION, {
// TODO()
// })
// hoverEvent = ClickEvent(ClickEvent.HoverEventActions.SHOW_TEXT, "Click to delete screenshot")
// },
" ",
TextComponent("[DELETE]").apply {
color = ChatColors.RED
bold()
clickEvent = ClickCallbackClickEvent { DeleteScreenshotDialog(renderWindow.renderer[GUIRenderer] ?: return@ClickCallbackClickEvent, file).open() }
hoverEvent = TextHoverEvent("Click to delete screenshot")
},
))
} catch (exception: Exception) {
exception.fail()