Replaced .gui.Size with Vec2i

This commit is contained in:
OLEGSHA 2020-08-05 11:55:50 +03:00
parent 2abffff843
commit b9dc25096f
10 changed files with 91 additions and 116 deletions

View File

@ -25,6 +25,7 @@ import org.lwjgl.glfw.GLFW;
import com.google.common.eventbus.EventBus; import com.google.common.eventbus.EventBus;
import glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.backend.InputTracker; import ru.windcorp.optica.client.graphics.backend.InputTracker;
import ru.windcorp.optica.client.graphics.flat.RenderTarget; import ru.windcorp.optica.client.graphics.flat.RenderTarget;
import ru.windcorp.optica.client.graphics.gui.event.ChildAddedEvent; import ru.windcorp.optica.client.graphics.gui.event.ChildAddedEvent;
@ -54,7 +55,7 @@ public class Component extends Named {
private boolean valid = false; private boolean valid = false;
private Size preferredSize = null; private Vec2i preferredSize = null;
private Object layoutHint = null; private Object layoutHint = null;
private Layout layout = null; private Layout layout = null;
@ -185,8 +186,8 @@ public class Component extends Named {
return this; return this;
} }
public Component setSize(Size size) { public Component setSize(Vec2i size) {
return setSize(size.width, size.height); return setSize(size.x, size.y);
} }
public synchronized Component setBounds(int x, int y, int width, int height) { public synchronized Component setBounds(int x, int y, int width, int height) {
@ -195,8 +196,8 @@ public class Component extends Named {
return this; return this;
} }
public Component setBounds(int x, int y, Size size) { public Component setBounds(int x, int y, Vec2i size) {
return setBounds(x, y, size.width, size.height); return setBounds(x, y, size.x, size.y);
} }
public boolean isValid() { public boolean isValid() {
@ -235,7 +236,7 @@ public class Component extends Named {
} }
} }
public synchronized Size getPreferredSize() { public synchronized Vec2i getPreferredSize() {
if (preferredSize != null) { if (preferredSize != null) {
return preferredSize; return preferredSize;
} }
@ -248,16 +249,16 @@ public class Component extends Named {
} }
} }
return new Size(0, 0); return new Vec2i(0, 0);
} }
public synchronized Component setPreferredSize(Size preferredSize) { public synchronized Component setPreferredSize(Vec2i preferredSize) {
this.preferredSize = preferredSize; this.preferredSize = preferredSize;
return this; return this;
} }
public Component setPreferredSize(int width, int height) { public Component setPreferredSize(int width, int height) {
return setPreferredSize(new Size(width, height)); return setPreferredSize(new Vec2i(width, height));
} }
public Layout getLayout() { public Layout getLayout() {

View File

@ -19,6 +19,7 @@ package ru.windcorp.optica.client.graphics.gui;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.Colors; import ru.windcorp.optica.client.graphics.Colors;
import ru.windcorp.optica.client.graphics.flat.RenderTarget; import ru.windcorp.optica.client.graphics.flat.RenderTarget;
import ru.windcorp.optica.client.graphics.gui.event.HoverEvent; import ru.windcorp.optica.client.graphics.gui.event.HoverEvent;
@ -30,7 +31,7 @@ public class LayerTestGUI extends GUILayer {
private static class DebugComponent extends Component { private static class DebugComponent extends Component {
private final int color; private final int color;
public DebugComponent(String name, Size size, int color) { public DebugComponent(String name, Vec2i size, int color) {
super(name); super(name);
this.color = color; this.color = color;
@ -68,7 +69,7 @@ public class LayerTestGUI extends GUILayer {
public LayerTestGUI() { public LayerTestGUI() {
super("LayerTestGui", new LayoutAlign(1, 0.75, 5)); super("LayerTestGui", new LayoutAlign(1, 0.75, 5));
getRoot().addChild(new DebugComponent("Alex", new Size(200, 100), 0x44FF44)); getRoot().addChild(new DebugComponent("Alex", new Vec2i(200, 100), 0x44FF44));
} }
} }

View File

@ -17,10 +17,12 @@
*******************************************************************************/ *******************************************************************************/
package ru.windcorp.optica.client.graphics.gui; package ru.windcorp.optica.client.graphics.gui;
import glm.vec._2.i.Vec2i;
public interface Layout { public interface Layout {
public void layout(Component c); public void layout(Component c);
public Size calculatePreferredSize(Component c); public Vec2i calculatePreferredSize(Component c);
} }

View File

@ -1,29 +0,0 @@
/*******************************************************************************
* Optica
* Copyright (C) 2020 Wind Corporation
*
* 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/>.
*******************************************************************************/
package ru.windcorp.optica.client.graphics.gui;
public class Size {
public int width, height;
public Size(int width, int height) {
this.width = width;
this.height = height;
}
}

View File

@ -20,9 +20,9 @@ package ru.windcorp.optica.client.graphics.gui.layout;
import static java.lang.Math.max; import static java.lang.Math.max;
import static java.lang.Math.min; import static java.lang.Math.min;
import glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.gui.Component; import ru.windcorp.optica.client.graphics.gui.Component;
import ru.windcorp.optica.client.graphics.gui.Layout; import ru.windcorp.optica.client.graphics.gui.Layout;
import ru.windcorp.optica.client.graphics.gui.Size;
public class LayoutAlign implements Layout { public class LayoutAlign implements Layout {
@ -47,19 +47,19 @@ public class LayoutAlign implements Layout {
public void layout(Component c) { public void layout(Component c) {
c.getChildren().forEach(child -> { c.getChildren().forEach(child -> {
Size size = child.getPreferredSize(); Vec2i size = child.getPreferredSize();
int cWidth = c.getWidth() - 2 * margin; int cWidth = c.getWidth() - 2 * margin;
int cHeight = c.getHeight() - 2 * margin; int cHeight = c.getHeight() - 2 * margin;
size.width = min(size.width, cWidth); size.x = min(size.x, cWidth);
size.height = min(size.height, cHeight); size.y = min(size.y, cHeight);
child.setBounds( child.setBounds(
c.getX() + c.getX() +
(int) ((cWidth - size.width) * alignX) + margin, (int) ((cWidth - size.x) * alignX) + margin,
c.getY() + c.getY() +
(int) ((cHeight - size.height) * alignY) + margin, (int) ((cHeight - size.y) * alignY) + margin,
size size
); );
@ -67,18 +67,18 @@ public class LayoutAlign implements Layout {
} }
@Override @Override
public Size calculatePreferredSize(Component c) { public Vec2i calculatePreferredSize(Component c) {
Size result = new Size(0, 0); Vec2i result = new Vec2i(0, 0);
c.getChildren().stream() c.getChildren().stream()
.map(child -> child.getPreferredSize()) .map(child -> child.getPreferredSize())
.forEach(size -> { .forEach(size -> {
result.width = max(size.width, result.width); result.x = max(size.x, result.x);
result.height = max(size.height, result.height); result.y = max(size.y, result.y);
}); });
result.width += 2 * margin; result.x += 2 * margin;
result.height += 2 * margin; result.y += 2 * margin;
return result; return result;
} }

View File

@ -19,9 +19,9 @@ package ru.windcorp.optica.client.graphics.gui.layout;
import static java.lang.Math.max; import static java.lang.Math.max;
import glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.gui.Component; import ru.windcorp.optica.client.graphics.gui.Component;
import ru.windcorp.optica.client.graphics.gui.Layout; import ru.windcorp.optica.client.graphics.gui.Layout;
import ru.windcorp.optica.client.graphics.gui.Size;
public class LayoutBorderHorizontal implements Layout { public class LayoutBorderHorizontal implements Layout {
@ -43,25 +43,25 @@ public class LayoutBorderHorizontal implements Layout {
public void layout(Component c) { public void layout(Component c) {
int left = 0, right = 0; int left = 0, right = 0;
Size childSize; Vec2i childSize;
synchronized (c.getChildren()) { synchronized (c.getChildren()) {
for (Component child : c.getChildren()) { for (Component child : c.getChildren()) {
if (child.getLayoutHint() == LEFT) { if (child.getLayoutHint() == LEFT) {
childSize = child.getPreferredSize(); childSize = child.getPreferredSize();
left = childSize.width + margin; left = childSize.x + margin;
child.setBounds( child.setBounds(
c.getX(), c.getX(),
c.getY(), c.getY(),
childSize.width, childSize.x,
c.getHeight()); c.getHeight());
} else if (child.getLayoutHint() == RIGHT) { } else if (child.getLayoutHint() == RIGHT) {
childSize = child.getPreferredSize(); childSize = child.getPreferredSize();
right = childSize.width + margin; right = childSize.x + margin;
child.setBounds( child.setBounds(
c.getX() + c.getWidth() - childSize.width, c.getX() + c.getWidth() - childSize.x,
c.getY(), c.getY(),
childSize.width, childSize.x,
c.getHeight()); c.getHeight());
} }
} }
@ -80,11 +80,11 @@ public class LayoutBorderHorizontal implements Layout {
} }
@Override @Override
public Size calculatePreferredSize(Component c) { public Vec2i calculatePreferredSize(Component c) {
Size result = new Size(0, 0); Vec2i result = new Vec2i(0, 0);
int left = 0, right = 0; int left = 0, right = 0;
Size childSize; Vec2i childSize;
synchronized (c.getChildren()) { synchronized (c.getChildren()) {
for (Component child : c.getChildren()) { for (Component child : c.getChildren()) {
@ -92,22 +92,22 @@ public class LayoutBorderHorizontal implements Layout {
if (child.getLayoutHint() instanceof String) { if (child.getLayoutHint() instanceof String) {
if (child.getLayoutHint() == LEFT) { if (child.getLayoutHint() == LEFT) {
left = max(left, childSize.width + margin); left = max(left, childSize.x + margin);
result.height = max(result.height, childSize.height); result.y = max(result.y, childSize.y);
continue; continue;
} else if (child.getLayoutHint() == RIGHT) { } else if (child.getLayoutHint() == RIGHT) {
right = max(right, childSize.width + margin); right = max(right, childSize.x + margin);
result.height = max(result.height, childSize.height); result.y = max(result.y, childSize.y);
continue; continue;
} }
} }
result.width = max(result.width, childSize.width); result.x = max(result.x, childSize.x);
result.height = max(result.height, childSize.height); result.y = max(result.y, childSize.y);
} }
} }
result.width += left + right; result.x += left + right;
return result; return result;
} }

View File

@ -19,9 +19,9 @@ package ru.windcorp.optica.client.graphics.gui.layout;
import static java.lang.Math.max; import static java.lang.Math.max;
import glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.gui.Component; import ru.windcorp.optica.client.graphics.gui.Component;
import ru.windcorp.optica.client.graphics.gui.Layout; import ru.windcorp.optica.client.graphics.gui.Layout;
import ru.windcorp.optica.client.graphics.gui.Size;
public class LayoutBorderVertical implements Layout { public class LayoutBorderVertical implements Layout {
@ -44,25 +44,25 @@ public class LayoutBorderVertical implements Layout {
public void layout(Component c) { public void layout(Component c) {
int top = 0, bottom = 0; int top = 0, bottom = 0;
Size childSize; Vec2i childSize;
synchronized (c.getChildren()) { synchronized (c.getChildren()) {
for (Component child : c.getChildren()) { for (Component child : c.getChildren()) {
if (child.getLayoutHint() == UP) { if (child.getLayoutHint() == UP) {
childSize = child.getPreferredSize(); childSize = child.getPreferredSize();
top = childSize.height + margin; top = childSize.y + margin;
child.setBounds( child.setBounds(
c.getX(), c.getX(),
c.getY(), c.getY(),
c.getWidth(), c.getWidth(),
childSize.height); childSize.y);
} else if (child.getLayoutHint() == DOWN) { } else if (child.getLayoutHint() == DOWN) {
childSize = child.getPreferredSize(); childSize = child.getPreferredSize();
bottom = childSize.height + margin; bottom = childSize.y + margin;
child.setBounds( child.setBounds(
c.getX(), c.getX(),
c.getY() + c.getHeight() - childSize.height, c.getY() + c.getHeight() - childSize.y,
c.getWidth(), childSize.height); c.getWidth(), childSize.y);
} }
} }
@ -80,11 +80,11 @@ public class LayoutBorderVertical implements Layout {
} }
@Override @Override
public Size calculatePreferredSize(Component c) { public Vec2i calculatePreferredSize(Component c) {
Size result = new Size(0, 0); Vec2i result = new Vec2i(0, 0);
int up = 0, down = 0; int up = 0, down = 0;
Size childSize; Vec2i childSize;
synchronized (c.getChildren()) { synchronized (c.getChildren()) {
for (Component child : c.getChildren()) { for (Component child : c.getChildren()) {
@ -92,22 +92,22 @@ public class LayoutBorderVertical implements Layout {
if (child.getLayoutHint() instanceof String) { if (child.getLayoutHint() instanceof String) {
if (child.getLayoutHint() == UP) { if (child.getLayoutHint() == UP) {
up = max(up, childSize.height + margin); up = max(up, childSize.y + margin);
result.width = max(result.width, childSize.width); result.x = max(result.x, childSize.x);
continue; continue;
} else if (child.getLayoutHint() == DOWN) { } else if (child.getLayoutHint() == DOWN) {
down = max(down, childSize.height + margin); down = max(down, childSize.y + margin);
result.width = max(result.width, childSize.width); result.x = max(result.x, childSize.x);
continue; continue;
} }
} }
result.width = max(result.width, childSize.width); result.x = max(result.x, childSize.x);
result.height = max(result.height, childSize.height); result.y = max(result.y, childSize.y);
} }
} }
result.height += up + down; result.y += up + down;
return result; return result;
} }

View File

@ -19,9 +19,9 @@ package ru.windcorp.optica.client.graphics.gui.layout;
import java.util.Arrays; import java.util.Arrays;
import glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.gui.Component; import ru.windcorp.optica.client.graphics.gui.Component;
import ru.windcorp.optica.client.graphics.gui.Layout; import ru.windcorp.optica.client.graphics.gui.Layout;
import ru.windcorp.optica.client.graphics.gui.Size;
public class LayoutGrid implements Layout { public class LayoutGrid implements Layout {
@ -30,10 +30,10 @@ public class LayoutGrid implements Layout {
int[] rows = new int[10]; int[] rows = new int[10];
boolean isSummed = false; boolean isSummed = false;
void add(int column, int row, Size size) { void add(int column, int row, Vec2i size) {
if (isSummed) throw new IllegalStateException("Already summed"); if (isSummed) throw new IllegalStateException("Already summed");
columns = update(columns, column, size.width); columns = update(columns, column, size.x);
rows = update(rows, row, size.height); rows = update(rows, row, size.y);
} }
private int[] update(int[] array, int index, int value) { private int[] update(int[] array, int index, int value) {
@ -48,19 +48,19 @@ public class LayoutGrid implements Layout {
return array; return array;
} }
Size getBounds() { Vec2i getBounds() {
if (isSummed) throw new IllegalStateException("Already summed"); if (isSummed) throw new IllegalStateException("Already summed");
Size result = new Size(2*margin - gap, 2*margin - gap); Vec2i result = new Vec2i(2*margin - gap, 2*margin - gap);
for (int i = 0; i < columns.length; ++i) { for (int i = 0; i < columns.length; ++i) {
if (columns[i] != 0) { if (columns[i] != 0) {
result.width += columns[i] + gap; result.x += columns[i] + gap;
} }
} }
for (int i = 0; i < rows.length; ++i) { for (int i = 0; i < rows.length; ++i) {
if (rows[i] != 0) { if (rows[i] != 0) {
result.height += rows[i] + gap; result.y += rows[i] + gap;
} }
} }
@ -138,7 +138,7 @@ public class LayoutGrid implements Layout {
} }
@Override @Override
public Size calculatePreferredSize(Component c) { public Vec2i calculatePreferredSize(Component c) {
synchronized (c.getChildren()) { synchronized (c.getChildren()) {
return calculateGrid(c).getBounds(); return calculateGrid(c).getBounds();
} }

View File

@ -19,9 +19,9 @@ package ru.windcorp.optica.client.graphics.gui.layout;
import static java.lang.Math.max; import static java.lang.Math.max;
import glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.gui.Component; import ru.windcorp.optica.client.graphics.gui.Component;
import ru.windcorp.optica.client.graphics.gui.Layout; import ru.windcorp.optica.client.graphics.gui.Layout;
import ru.windcorp.optica.client.graphics.gui.Size;
public class LayoutHorizontal implements Layout { public class LayoutHorizontal implements Layout {
@ -50,7 +50,7 @@ public class LayoutHorizontal implements Layout {
synchronized (c.getChildren()) { synchronized (c.getChildren()) {
for (Component child : c.getChildren()) { for (Component child : c.getChildren()) {
width = child.getPreferredSize().width; width = child.getPreferredSize().x;
child.setBounds(x, y, width, c.getHeight() - 2 * margin); child.setBounds(x, y, width, c.getHeight() - 2 * margin);
x += gap + width; x += gap + width;
@ -59,25 +59,25 @@ public class LayoutHorizontal implements Layout {
} }
@Override @Override
public Size calculatePreferredSize(Component c) { public Vec2i calculatePreferredSize(Component c) {
Size size = new Size(0, 0); Vec2i size = new Vec2i(0, 0);
Size childPreferredSize; Vec2i childPreferredSize;
synchronized (c.getChildren()) { synchronized (c.getChildren()) {
for (int i = 0; i < c.getChildren().size(); ++i) { for (int i = 0; i < c.getChildren().size(); ++i) {
childPreferredSize = c.getChild(i).getPreferredSize(); childPreferredSize = c.getChild(i).getPreferredSize();
if (i > 0) { if (i > 0) {
size.width += gap; size.x += gap;
} }
size.height = max(size.height, childPreferredSize.height); size.y = max(size.y, childPreferredSize.y);
size.width += childPreferredSize.width; size.x += childPreferredSize.x;
} }
} }
size.width += 2 * margin; size.x += 2 * margin;
size.height += 2 * margin; size.y += 2 * margin;
return size; return size;
} }

View File

@ -19,9 +19,9 @@ package ru.windcorp.optica.client.graphics.gui.layout;
import static java.lang.Math.max; import static java.lang.Math.max;
import glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.gui.Component; import ru.windcorp.optica.client.graphics.gui.Component;
import ru.windcorp.optica.client.graphics.gui.Layout; import ru.windcorp.optica.client.graphics.gui.Layout;
import ru.windcorp.optica.client.graphics.gui.Size;
public class LayoutVertical implements Layout { public class LayoutVertical implements Layout {
@ -50,7 +50,7 @@ public class LayoutVertical implements Layout {
synchronized (c.getChildren()) { synchronized (c.getChildren()) {
for (Component child : c.getChildren()) { for (Component child : c.getChildren()) {
height = child.getPreferredSize().height; height = child.getPreferredSize().y;
child.setBounds(x, y, c.getWidth() - 2 * margin, height); child.setBounds(x, y, c.getWidth() - 2 * margin, height);
y += gap + height; y += gap + height;
@ -59,25 +59,25 @@ public class LayoutVertical implements Layout {
} }
@Override @Override
public Size calculatePreferredSize(Component c) { public Vec2i calculatePreferredSize(Component c) {
Size size = new Size(0, 0); Vec2i size = new Vec2i(0, 0);
Size childPreferredSize; Vec2i childPreferredSize;
synchronized (c.getChildren()) { synchronized (c.getChildren()) {
for (int i = 0; i < c.getChildren().size(); ++i) { for (int i = 0; i < c.getChildren().size(); ++i) {
childPreferredSize = c.getChild(i).getPreferredSize(); childPreferredSize = c.getChild(i).getPreferredSize();
if (i > 0) { if (i > 0) {
size.height += gap; size.y += gap;
} }
size.width = max(size.width, childPreferredSize.width); size.x = max(size.x, childPreferredSize.x);
size.height += childPreferredSize.height; size.y += childPreferredSize.y;
} }
} }
size.width += 2 * margin; size.x += 2 * margin;
size.height += 2 * margin; size.y += 2 * margin;
return size; return size;
} }