Add panel children sorting

This commit is contained in:
DBotThePony 2023-07-15 19:38:53 +07:00
parent c71a1ab0d6
commit d855234808
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 55 additions and 14 deletions

View File

@ -65,15 +65,15 @@ public class FlexGridPanel<S extends Screen> extends EditablePanel<S> {
float min_width = getWidth();
// "финальная" ширина этой панели
float this_width = getWidth() - getDockPadding().left() - getDockPadding().right();
float this_width = getWidth() - getDockPadding().getLeft() - getDockPadding().getRight();
if (this_width <= 0) {
return;
}
for (var child : children) {
min_width = Math.min(min_width, child.getWidth() + child.getDockMargin().left() + child.getDockMargin().right());
desired_width += child.getWidth() + child.getDockMargin().left() + child.getDockMargin().right();
min_width = Math.min(min_width, child.getWidth() + child.getDockMargin().getLeft() + child.getDockMargin().getRight());
desired_width += child.getWidth() + child.getDockMargin().getLeft() + child.getDockMargin().getRight();
}
int rows = 1;
@ -108,7 +108,7 @@ public class FlexGridPanel<S extends Screen> extends EditablePanel<S> {
for (int i = index; i < children.size(); i++) {
var child = children.get(i);
var gain_width = child.getWidth() + child.getDockMargin().left() + child.getDockMargin().right();
var gain_width = child.getWidth() + child.getDockMargin().getLeft() + child.getDockMargin().getRight();
index = i;
@ -121,7 +121,7 @@ public class FlexGridPanel<S extends Screen> extends EditablePanel<S> {
break;
}
max_height = Math.max(max_height, child.getHeight() + child.getDockMargin().top() + child.getDockMargin().bottom());
max_height = Math.max(max_height, child.getHeight() + child.getDockMargin().getTop() + child.getDockMargin().getBottom());
total_width += gain_width;
}
@ -142,7 +142,7 @@ public class FlexGridPanel<S extends Screen> extends EditablePanel<S> {
this_width /= 2;
// определение точки по середине по высоте
float h_middle = (getHeight() - getDockPadding().bottom() - getDockPadding().top() - total_height) / 2;
float h_middle = (getHeight() - getDockPadding().getBottom() - getDockPadding().getTop() - total_height) / 2;
// OverdriveThatMatters.LOGGER.info("Общая высота {}, рядов {}, середина {}", total_height, rows, h_middle);
@ -154,7 +154,7 @@ public class FlexGridPanel<S extends Screen> extends EditablePanel<S> {
for (int i = index; i < children.size(); i++) {
var child = children.get(i);
var gain_width = child.getWidth() + child.getDockMargin().left() + child.getDockMargin().right();
var gain_width = child.getWidth() + child.getDockMargin().getLeft() + child.getDockMargin().getRight();
until = i;
@ -162,7 +162,7 @@ public class FlexGridPanel<S extends Screen> extends EditablePanel<S> {
break;
}
max_height = Math.max(max_height, child.getHeight() + child.getDockMargin().top() + child.getDockMargin().bottom());
max_height = Math.max(max_height, child.getHeight() + child.getDockMargin().getTop() + child.getDockMargin().getBottom());
total_width += gain_width;
}
@ -176,8 +176,8 @@ public class FlexGridPanel<S extends Screen> extends EditablePanel<S> {
for (int i = index; i <= until; i++) {
var child = children.get(i);
child.setPos((int) (this_width - total_width + accumulate_width + child.getDockMargin().left()), (int) (h_middle + max_height - child.getHeight() / 2));
accumulate_width += child.getWidth() + child.getDockMargin().left() + child.getDockMargin().right();
child.setPos((int) (this_width - total_width + accumulate_width + child.getDockMargin().getLeft()), (int) (h_middle + max_height - child.getHeight() / 2));
accumulate_width += child.getWidth() + child.getDockMargin().getLeft() + child.getDockMargin().getRight();
}
h_middle += max_height * 2;

View File

@ -29,11 +29,9 @@ import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.roundToInt
@JvmRecord
data class ScreenPos(val x: Float, val y: Float)
@JvmRecord
data class DockProperty @JvmOverloads constructor(val left: Float = 0f, val top: Float = 0f, val right: Float = 0f, val bottom: Float = 0f) {
data class DockProperty(val left: Float = 0f, val top: Float = 0f, val right: Float = 0f, val bottom: Float = 0f) {
val isEmpty get() = left == 0f && right == 0f && top == 0f && bottom == 0f
companion object {
@ -73,7 +71,7 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
width: Float = 10f,
height: Float = 10f,
) {
) : Comparable<EditablePanel<*>> {
// layout engine does not support navigation using keyboard
// fuck off
val listener: GuiEventListener = object : GuiEventListener {
@ -140,6 +138,18 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
}
}
var childrenOrder = 0
set(value) {
if (field != value) {
field = value
parent?.sortChildren()
}
}
override fun compareTo(other: EditablePanel<*>): Int {
return childrenOrder.compareTo(other.childrenOrder)
}
var parent: EditablePanel<*>? = null
set(value) {
if (field === value)
@ -670,9 +680,17 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
this.parent = parent
}
var childrenSortingInvalidated = false
private set
fun invalidateChildrenSorting() {
childrenSortingInvalidated = true
}
private fun onParent(child: EditablePanel<*>) {
if (childrenInternal.contains(child)) throw IllegalStateException("Already containing $child")
childrenInternal.add(child)
invalidateChildrenSorting()
if (child.visible) {
visibleChildrenInternal.add(child)
@ -691,6 +709,7 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
val indexOf = childrenInternal.indexOf(child)
if (indexOf == -1) throw IllegalStateException("Already not containing $child")
childrenInternal.removeAt(indexOf)
invalidateChildrenSorting()
if (child.visible) {
visibleChildrenInternal.remove(child)
@ -702,6 +721,18 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
}
}
private fun sortChildren() {
childrenSortingInvalidated = false
childrenInternal.sort()
val old = ArrayList(visibleChildrenInternal)
visibleChildrenInternal.sort()
if (old != visibleChildrenInternal) {
invalidateLayout()
}
}
protected open fun innerRender(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {}
protected open fun innerRenderPost(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {}
protected open fun innerRenderTooltips(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float): Boolean {
@ -731,6 +762,9 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
}
fun performLayoutIfNeeded() {
if (childrenSortingInvalidated)
sortChildren()
if (layoutInvalidated) {
performLayout()
} else if (boundsInvalidated) {
@ -1267,7 +1301,14 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
return findHierarchicalFocus() != null
}
/**
* Called when [hasFocusedChildren] changes
*/
protected open fun onHierarchicalFocusChanged() {}
/**
* Called when [isFocusedThis] changes
*/
protected open fun onFocusChanged() {}
fun isEverFocused(): Boolean {