Skip to content

Commit b94603e

Browse files
committed
aspectRatio for Icon
1 parent b480bcc commit b94603e

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/main/java/com/cleanroommc/modularui/drawable/Icon.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Icon implements IIcon, IJsonSerializable {
2121

2222
private final IDrawable drawable;
2323
private int width = 0, height = 0;
24+
private float aspectRatio = 0;
2425
private Alignment alignment = Alignment.Center;
2526
private final Box margin = new Box();
2627

@@ -55,13 +56,37 @@ public void draw(GuiContext context, int x, int y, int width, int height, Widget
5556
y += this.margin.getTop();
5657
width -= this.margin.horizontal();
5758
height -= this.margin.vertical();
58-
if (this.width > 0) {
59-
x += (int) (width * this.alignment.x - this.width * this.alignment.x);
60-
width = this.width;
59+
int frameWidth = width;
60+
int frameHeight = height;
61+
if (this.width > 0) width = this.width;
62+
if (this.height > 0) height = this.height;
63+
if (this.aspectRatio > 0) {
64+
if (this.width <= 0) {
65+
if (this.height <= 0) {
66+
// width and height is unset, so adjust width or height so that one of them takes the full space
67+
float w = width, h = height;
68+
float properW = this.aspectRatio * h;
69+
if (w > properW) {
70+
width = (int) properW;
71+
} else if (w < properW) {
72+
height = (int) (w / this.aspectRatio);
73+
}
74+
} else {
75+
// height is set, so adjust width to height
76+
float properW = this.aspectRatio * height;
77+
width = (int) properW;
78+
}
79+
} else if (this.height <= 0) {
80+
// width is set, so adjust height to width
81+
height = (int) (width / this.aspectRatio);
82+
}
6183
}
62-
if (this.height > 0) {
63-
y += (int) (height * this.alignment.y - this.height * this.alignment.y);
64-
height = this.height;
84+
// apply alignment
85+
if (width != frameWidth) {
86+
x += (int) (frameWidth * this.alignment.x - width * this.alignment.x);
87+
}
88+
if (height != frameHeight) {
89+
y += (int) (frameHeight * this.alignment.y - height * this.alignment.y);
6590
}
6691
this.drawable.draw(context, x, y, width, height, widgetTheme);
6792
}
@@ -96,6 +121,11 @@ public Icon size(int size) {
96121
return width(size).height(size);
97122
}
98123

124+
public Icon aspectRatio(float aspectRatio) {
125+
this.aspectRatio = aspectRatio;
126+
return this;
127+
}
128+
99129
public Icon alignment(Alignment alignment) {
100130
this.alignment = alignment;
101131
return this;
@@ -148,6 +178,7 @@ public void loadFromJson(JsonObject json) {
148178
this.height = (json.has("autoHeight") || json.has("autoSize")) &&
149179
JsonHelper.getBoolean(json, true, "autoHeight", "autoSize") ? 0 :
150180
JsonHelper.getInt(json, 0, "height", "h", "size");
181+
this.aspectRatio = JsonHelper.getFloat(json, 0, "aspectRatio");
151182
this.alignment = JsonHelper.deserialize(json, Alignment.class, Alignment.Center, "alignment", "align");
152183
this.margin.fromJson(json);
153184
}
@@ -161,6 +192,7 @@ public boolean saveToJson(JsonObject json) {
161192
json.add("drawable", JsonHelper.serialize(this.drawable));
162193
json.addProperty("width", this.width);
163194
json.addProperty("height", this.height);
195+
json.addProperty("aspectRatio", this.aspectRatio);
164196
json.add("alignment", JsonHelper.serialize(this.alignment));
165197
this.margin.toJson(json);
166198
return true;

src/main/java/com/cleanroommc/modularui/test/TestGuis.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,32 @@ public static ModularPanel buildCollapseDisabledChildrenUI() {
544544
.yLim(-1.2f, 1.2f)
545545
.xTickFinder(MathUtils.PI_HALF, 1)
546546
.yTickFinder(0.2f, 1)
547+
.graphAspectRatio(16 / 9f)
547548
.plot(x, y));
548549
}
549550

551+
public static @NotNull ModularPanel buildAspectRatioUI() {
552+
return new ModularPanel("aspect_ratio")
553+
.coverChildren()
554+
.padding(10)
555+
.child(new Row()
556+
.childPadding(10)
557+
.coverChildren()
558+
.child(new Rectangle().color(Color.BLUE_ACCENT.main)
559+
.asIcon().aspectRatio(4f / 3)
560+
.asWidget().size(80)
561+
.overlay(IKey.str("4:3 Free")))
562+
.child(new Rectangle().color(Color.RED_ACCENT.main)
563+
.asIcon().aspectRatio(4f / 3).width(70)
564+
.asWidget().size(80)
565+
.overlay(IKey.str("4:3 | width = 70")))
566+
.child(new Rectangle().color(Color.LIGHT_GREEN.main)
567+
.asIcon().aspectRatio(4f / 3).height(45).alignment(Alignment.BottomRight)
568+
.asWidget().size(80)
569+
.overlay(IKey.str("4:3 | height = 45\nBottom Right"))))
570+
.overlay();
571+
}
572+
550573
private static class TestPanel extends ModularPanel {
551574

552575
public TestPanel(String name) {

0 commit comments

Comments
 (0)