gui: wip margin, padding

This commit is contained in:
Bixilon 2021-09-06 13:26:08 +02:00
parent e680058a83
commit 061f2e0f39
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 99 additions and 6 deletions

View File

@ -16,7 +16,9 @@ package de.bixilon.minosoft.gui.rendering.gui.elements
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import de.bixilon.minosoft.gui.rendering.util.vec.Vec2Util.EMPTY import de.bixilon.minosoft.gui.rendering.util.vec.Vec2Util.EMPTY
import de.bixilon.minosoft.gui.rendering.util.vec.Vec2Util.MAX import de.bixilon.minosoft.gui.rendering.util.vec.Vec2Util.MAX
import de.bixilon.minosoft.gui.rendering.util.vec.Vec4Util.EMPTY
import glm_.vec2.Vec2i import glm_.vec2.Vec2i
import glm_.vec4.Vec4i
abstract class Element { abstract class Element {
open var parent: Element? = null open var parent: Element? = null
@ -26,6 +28,9 @@ abstract class Element {
open var prefMaxSize: Vec2i = Vec2i.MAX open var prefMaxSize: Vec2i = Vec2i.MAX
open var size: Vec2i = Vec2i() open var size: Vec2i = Vec2i()
open var margin: Vec4i = Vec4i.EMPTY
open var padding: Vec4i = Vec4i.EMPTY
open val maxSize: Vec2i open val maxSize: Vec2i
get() { get() {

View File

@ -15,6 +15,10 @@ package de.bixilon.minosoft.gui.rendering.gui.elements.layout
import de.bixilon.minosoft.gui.rendering.gui.elements.Element import de.bixilon.minosoft.gui.rendering.gui.elements.Element
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import de.bixilon.minosoft.gui.rendering.util.vec.Vec4Util.bottom
import de.bixilon.minosoft.gui.rendering.util.vec.Vec4Util.horizontal
import de.bixilon.minosoft.gui.rendering.util.vec.Vec4Util.left
import de.bixilon.minosoft.gui.rendering.util.vec.Vec4Util.top
import de.bixilon.minosoft.util.KUtil.synchronizedListOf import de.bixilon.minosoft.util.KUtil.synchronizedListOf
import glm_.vec2.Vec2i import glm_.vec2.Vec2i
@ -29,11 +33,12 @@ class RowLayout : Layout() {
var childYOffset = 0 var childYOffset = 0
var totalZ = 0 var totalZ = 0
for (child in children) { for (child in children) {
val childZ = child.render(Vec2i(offset.x, offset.y + childYOffset), z, consumer) childYOffset += padding.top + child.margin.top
val childZ = child.render(Vec2i(offset.x + padding.left + child.margin.left, offset.y + childYOffset), z, consumer)
if (totalZ < childZ) { if (totalZ < childZ) {
totalZ = childZ totalZ = childZ
} }
childYOffset += child.size.y childYOffset += child.size.y + child.margin.bottom + padding.bottom
} }
return totalZ return totalZ
@ -51,17 +56,23 @@ class RowLayout : Layout() {
override fun childChange(child: Element) { override fun childChange(child: Element) {
super.childChange(child) super.childChange(child)
// ToDo: Check max size
val size = Vec2i(0, 0) val size = Vec2i(0, 0)
val xPadding = padding.horizontal
size.y += padding.top
for (element in children) { for (element in children) {
size.y += element.margin.top
size.y += element.size.y size.y += element.size.y
size.y += element.margin.bottom
if (element.size.x > size.x) { val xSize = xPadding + element.size.x + element.margin.horizontal
size.x = element.size.x if (xSize > size.x) {
size.x = xSize
} }
// ToDo: Check max size
} }
size.y += padding.bottom
this.size = size this.size = size
} }

View File

@ -18,6 +18,7 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.font.renderer.ChatComponentRenderer import de.bixilon.minosoft.gui.rendering.font.renderer.ChatComponentRenderer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import glm_.vec2.Vec2i import glm_.vec2.Vec2i
import glm_.vec4.Vec4i
class TextElement( class TextElement(
private val renderWindow: RenderWindow, private val renderWindow: RenderWindow,
@ -50,6 +51,20 @@ class TextElement(
checkSize() checkSize()
} }
override var margin: Vec4i
get() = super.margin
set(value) {
super.margin = value
checkSize()
}
override var padding: Vec4i
get() = super.padding
set(value) {
super.padding = value
checkSize()
}
init { init {
textComponent = ChatComponent.of(text) textComponent = ChatComponent.of(text)
} }

View File

@ -32,6 +32,7 @@ import glm_.glm
import glm_.mat4x4.Mat4 import glm_.mat4x4.Mat4
import glm_.vec2.Vec2 import glm_.vec2.Vec2
import glm_.vec2.Vec2i import glm_.vec2.Vec2i
import glm_.vec4.Vec4i
class HUDRenderer( class HUDRenderer(
val connection: PlayConnection, val connection: PlayConnection,
@ -130,11 +131,14 @@ class HUDRenderer(
) )
text3.prefMaxSize = Vec2i(50, Int.MAX_VALUE) text3.prefMaxSize = Vec2i(50, Int.MAX_VALUE)
text4.prefMaxSize = Vec2i(50, Int.MAX_VALUE) text4.prefMaxSize = Vec2i(50, Int.MAX_VALUE)
text4.margin = Vec4i(10, 0, 5, 10)
// ToDo: size > maxSize // ToDo: size > maxSize
val layout = RowLayout() val layout = RowLayout()
layout.padding = Vec4i(4, 0, 0, 10)
layout += text1 layout += text1
layout += text2 layout += text2
layout += text3 layout += text3

View File

@ -0,0 +1,58 @@
/*
* Minosoft
* Copyright (C) 2021 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.util.vec
import glm_.vec4.Vec4
import glm_.vec4.Vec4i
object Vec4Util {
val Vec4.Companion.MIN: Vec4
get() = Vec4(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE)
val Vec4.Companion.EMPTY: Vec4
get() = Vec4(0.0f, 0.0f, 0.0f, 0.0f)
val Vec4.Companion.MAX: Vec4
get() = Vec4(Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE)
val Vec4i.Companion.MIN: Vec4i
get() = Vec4i(Int.MIN_VALUE, Int.MIN_VALUE, Int.MIN_VALUE, Int.MIN_VALUE)
val Vec4i.Companion.EMPTY: Vec4i
get() = Vec4i(0, 0, 0, 0)
val Vec4i.Companion.MAX: Vec4i
get() = Vec4i(Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE)
val Vec4i.top: Int
get() = this.x
val Vec4i.right: Int
get() = this.y
val Vec4i.bottom: Int
get() = this.z
val Vec4i.left: Int
get() = this.w
val Vec4i.horizontal: Int
get() = right + left
val Vec4i.vertical: Int
get() = top + bottom
}