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 glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.backend.InputTracker;
import ru.windcorp.optica.client.graphics.flat.RenderTarget;
import ru.windcorp.optica.client.graphics.gui.event.ChildAddedEvent;
@ -54,7 +55,7 @@ public class Component extends Named {
private boolean valid = false;
private Size preferredSize = null;
private Vec2i preferredSize = null;
private Object layoutHint = null;
private Layout layout = null;
@ -185,8 +186,8 @@ public class Component extends Named {
return this;
}
public Component setSize(Size size) {
return setSize(size.width, size.height);
public Component setSize(Vec2i size) {
return setSize(size.x, size.y);
}
public synchronized Component setBounds(int x, int y, int width, int height) {
@ -195,8 +196,8 @@ public class Component extends Named {
return this;
}
public Component setBounds(int x, int y, Size size) {
return setBounds(x, y, size.width, size.height);
public Component setBounds(int x, int y, Vec2i size) {
return setBounds(x, y, size.x, size.y);
}
public boolean isValid() {
@ -235,7 +236,7 @@ public class Component extends Named {
}
}
public synchronized Size getPreferredSize() {
public synchronized Vec2i getPreferredSize() {
if (preferredSize != null) {
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;
return this;
}
public Component setPreferredSize(int width, int height) {
return setPreferredSize(new Size(width, height));
return setPreferredSize(new Vec2i(width, height));
}
public Layout getLayout() {

View File

@ -19,6 +19,7 @@ package ru.windcorp.optica.client.graphics.gui;
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.flat.RenderTarget;
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 final int color;
public DebugComponent(String name, Size size, int color) {
public DebugComponent(String name, Vec2i size, int color) {
super(name);
this.color = color;
@ -68,7 +69,7 @@ public class LayerTestGUI extends GUILayer {
public LayerTestGUI() {
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;
import glm.vec._2.i.Vec2i;
public interface Layout {
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.min;
import glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.gui.Component;
import ru.windcorp.optica.client.graphics.gui.Layout;
import ru.windcorp.optica.client.graphics.gui.Size;
public class LayoutAlign implements Layout {
@ -47,19 +47,19 @@ public class LayoutAlign implements Layout {
public void layout(Component c) {
c.getChildren().forEach(child -> {
Size size = child.getPreferredSize();
Vec2i size = child.getPreferredSize();
int cWidth = c.getWidth() - 2 * margin;
int cHeight = c.getHeight() - 2 * margin;
size.width = min(size.width, cWidth);
size.height = min(size.height, cHeight);
size.x = min(size.x, cWidth);
size.y = min(size.y, cHeight);
child.setBounds(
c.getX() +
(int) ((cWidth - size.width) * alignX) + margin,
(int) ((cWidth - size.x) * alignX) + margin,
c.getY() +
(int) ((cHeight - size.height) * alignY) + margin,
(int) ((cHeight - size.y) * alignY) + margin,
size
);
@ -67,18 +67,18 @@ public class LayoutAlign implements Layout {
}
@Override
public Size calculatePreferredSize(Component c) {
Size result = new Size(0, 0);
public Vec2i calculatePreferredSize(Component c) {
Vec2i result = new Vec2i(0, 0);
c.getChildren().stream()
.map(child -> child.getPreferredSize())
.forEach(size -> {
result.width = max(size.width, result.width);
result.height = max(size.height, result.height);
result.x = max(size.x, result.x);
result.y = max(size.y, result.y);
});
result.width += 2 * margin;
result.height += 2 * margin;
result.x += 2 * margin;
result.y += 2 * margin;
return result;
}

View File

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

View File

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

View File

@ -19,9 +19,9 @@ package ru.windcorp.optica.client.graphics.gui.layout;
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.Layout;
import ru.windcorp.optica.client.graphics.gui.Size;
public class LayoutGrid implements Layout {
@ -30,10 +30,10 @@ public class LayoutGrid implements Layout {
int[] rows = new int[10];
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");
columns = update(columns, column, size.width);
rows = update(rows, row, size.height);
columns = update(columns, column, size.x);
rows = update(rows, row, size.y);
}
private int[] update(int[] array, int index, int value) {
@ -48,19 +48,19 @@ public class LayoutGrid implements Layout {
return array;
}
Size getBounds() {
Vec2i getBounds() {
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) {
if (columns[i] != 0) {
result.width += columns[i] + gap;
result.x += columns[i] + gap;
}
}
for (int i = 0; i < rows.length; ++i) {
if (rows[i] != 0) {
result.height += rows[i] + gap;
result.y += rows[i] + gap;
}
}
@ -138,7 +138,7 @@ public class LayoutGrid implements Layout {
}
@Override
public Size calculatePreferredSize(Component c) {
public Vec2i calculatePreferredSize(Component c) {
synchronized (c.getChildren()) {
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 glm.vec._2.i.Vec2i;
import ru.windcorp.optica.client.graphics.gui.Component;
import ru.windcorp.optica.client.graphics.gui.Layout;
import ru.windcorp.optica.client.graphics.gui.Size;
public class LayoutHorizontal implements Layout {
@ -50,7 +50,7 @@ public class LayoutHorizontal implements Layout {
synchronized (c.getChildren()) {
for (Component child : c.getChildren()) {
width = child.getPreferredSize().width;
width = child.getPreferredSize().x;
child.setBounds(x, y, width, c.getHeight() - 2 * margin);
x += gap + width;
@ -59,25 +59,25 @@ public class LayoutHorizontal implements Layout {
}
@Override
public Size calculatePreferredSize(Component c) {
Size size = new Size(0, 0);
Size childPreferredSize;
public Vec2i calculatePreferredSize(Component c) {
Vec2i size = new Vec2i(0, 0);
Vec2i childPreferredSize;
synchronized (c.getChildren()) {
for (int i = 0; i < c.getChildren().size(); ++i) {
childPreferredSize = c.getChild(i).getPreferredSize();
if (i > 0) {
size.width += gap;
size.x += gap;
}
size.height = max(size.height, childPreferredSize.height);
size.width += childPreferredSize.width;
size.y = max(size.y, childPreferredSize.y);
size.x += childPreferredSize.x;
}
}
size.width += 2 * margin;
size.height += 2 * margin;
size.x += 2 * margin;
size.y += 2 * margin;
return size;
}

View File

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