Skip to content

Commit 666744f

Browse files
committed
ability to override color on UITexture without theme
1 parent 0ead83b commit 666744f

6 files changed

Lines changed: 113 additions & 11 deletions

File tree

src/main/java/com/cleanroommc/modularui/ClientProxy.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ void preInit(FMLPreInitializationEvent event) {
9494
} catch (IOException | LWJGLException e) {
9595
throw new RuntimeException(e);
9696
} catch (Throwable e) {
97-
ModularUI.LOGGER.info("Custom Cursors failed to load. This is likely because an incompatible LWJGL version was used like with CleanroomLoader.");
98-
// TODO: proper lwjgl 3 support
99-
// currently it seems this is not even triggered and the cursors are created correctly, but when the cursors are set nothing happens
97+
ModularUI.LOGGER.info("Custom Cursors failed to load.");
98+
// lwjgl3: currently it seems this is not even triggered and the cursors are created correctly, but when the cursors are set nothing happens
10099
}
101100
}
102101

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,14 @@ public boolean saveToJson(JsonObject json) {
179179
json.addProperty("tiled", this.tiled);
180180
return true;
181181
}
182+
183+
@Override
184+
protected AdaptableUITexture copy() {
185+
return new AdaptableUITexture(location, u0, v0, u1, v1, colorType, nonOpaque, imageWidth, imageHeight, bl, bt, br, bb, tiled);
186+
}
187+
188+
@Override
189+
public AdaptableUITexture withColorOverride(int color) {
190+
return (AdaptableUITexture) super.withColorOverride(color);
191+
}
182192
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.cleanroommc.modularui.drawable;
2+
3+
import com.cleanroommc.modularui.api.drawable.IDrawable;
4+
import com.cleanroommc.modularui.screen.viewport.GuiContext;
5+
import com.cleanroommc.modularui.theme.WidgetTheme;
6+
import com.cleanroommc.modularui.widget.Widget;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
10+
11+
public class DelegateDrawable implements IDrawable {
12+
13+
@NotNull
14+
private IDrawable drawable;
15+
16+
public DelegateDrawable(@Nullable IDrawable drawable) {
17+
setDrawable(drawable);
18+
}
19+
20+
// protected, so subclasses can define mutability
21+
protected void setDrawable(@Nullable IDrawable drawable) {
22+
this.drawable = drawable != null ? drawable : IDrawable.EMPTY;
23+
}
24+
25+
@NotNull
26+
public IDrawable getWrappedDrawable() {
27+
return drawable;
28+
}
29+
30+
@Override
31+
public void draw(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme) {
32+
this.drawable.draw(context, x, y, width, height, widgetTheme);
33+
}
34+
35+
@Override
36+
public boolean canApplyTheme() {
37+
return this.drawable.canApplyTheme();
38+
}
39+
40+
@Override
41+
public void applyColor(int themeColor) {
42+
this.drawable.applyColor(themeColor);
43+
}
44+
45+
@Override
46+
public int getDefaultWidth() {
47+
return this.drawable.getDefaultWidth();
48+
}
49+
50+
@Override
51+
public int getDefaultHeight() {
52+
return this.drawable.getDefaultHeight();
53+
}
54+
55+
@Override
56+
public Widget<?> asWidget() {
57+
return this.drawable.asWidget();
58+
}
59+
60+
@Override
61+
public Icon asIcon() {
62+
return this.drawable.asIcon();
63+
}
64+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.cleanroommc.modularui.drawable;
22

3-
import com.cleanroommc.modularui.api.drawable.IDrawable;
43
import com.cleanroommc.modularui.api.drawable.IIcon;
54
import com.cleanroommc.modularui.screen.viewport.GuiContext;
65
import com.cleanroommc.modularui.theme.WidgetTheme;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.cleanroommc.modularui.drawable;
22

3-
import com.google.gson.JsonObject;
4-
53
import net.minecraft.util.ResourceLocation;
64

5+
import com.google.gson.JsonObject;
6+
77
public class TiledUITexture extends UITexture {
88

99
private final int imageWidth, imageHeight;
@@ -35,4 +35,14 @@ public boolean saveToJson(JsonObject json) {
3535
}
3636
return true;
3737
}
38+
39+
@Override
40+
protected TiledUITexture copy() {
41+
return new TiledUITexture(location, u0, v0, u1, v1, imageWidth, imageHeight, colorType, nonOpaque);
42+
}
43+
44+
@Override
45+
public TiledUITexture withColorOverride(int color) {
46+
return (TiledUITexture) super.withColorOverride(color);
47+
}
3848
}

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ static UITexture icon(String name, int x, int y) {
4848
@Nullable public final ColorType colorType;
4949
public final boolean nonOpaque;
5050

51+
private int colorOverride = 0;
52+
5153
/**
5254
* Creates a drawable texture
5355
*
@@ -164,11 +166,7 @@ public void drawSubArea(float x, float y, float width, float height, float uStar
164166
}
165167

166168
public void drawSubArea(float x, float y, float width, float height, float uStart, float vStart, float uEnd, float vEnd, WidgetTheme widgetTheme) {
167-
if (canApplyTheme()) {
168-
Color.setGlColor(widgetTheme.getColor());
169-
} else {
170-
Color.setGlColorOpaque(Color.WHITE.main);
171-
}
169+
applyColor(this.colorType != null ? this.colorType.getColor(widgetTheme) : ColorType.DEFAULT.getColor(widgetTheme));
172170
GuiDraw.drawTexture(this.location, x, y, x + width, y + height, lerpU(uStart), lerpV(vStart), lerpU(uEnd), lerpV(vEnd), this.nonOpaque);
173171
}
174172

@@ -177,6 +175,15 @@ public boolean canApplyTheme() {
177175
return colorType != null;
178176
}
179177

178+
@Override
179+
public void applyColor(int themeColor) {
180+
if (this.colorOverride != 0) {
181+
Color.setGlColor(this.colorOverride);
182+
} else {
183+
IDrawable.super.applyColor(themeColor);
184+
}
185+
}
186+
180187
public static UITexture parseFromJson(JsonObject json) {
181188
String name = JsonHelper.getString(json, null, "name", "id");
182189
if (name != null) {
@@ -218,6 +225,8 @@ public static UITexture parseFromJson(JsonObject json) {
218225
} else if (JsonHelper.getBoolean(json, false, "canApplyTheme")) {
219226
builder.canApplyTheme();
220227
}
228+
UITexture uiTexture = builder.build();
229+
uiTexture.colorOverride = JsonHelper.getColor(json, 0, "colorOverride");
221230
return builder.build();
222231
}
223232

@@ -234,9 +243,20 @@ public boolean saveToJson(JsonObject json) {
234243
json.addProperty("u1", this.u1);
235244
json.addProperty("v1", this.v1);
236245
if (this.colorType != null) json.addProperty("colorType", this.colorType.getName());
246+
json.addProperty("colorOverride", this.colorOverride);
237247
return true;
238248
}
239249

250+
protected UITexture copy() {
251+
return new UITexture(this.location, this.u0, this.v0, this.u1, this.v1, this.colorType);
252+
}
253+
254+
public UITexture withColorOverride(int color) {
255+
UITexture t = copy();
256+
t.colorOverride = color;
257+
return t;
258+
}
259+
240260
private static int defaultImageWidth = 16, defaultImageHeight = 16;
241261

242262
public static void setDefaultImageSize(int w, int h) {

0 commit comments

Comments
 (0)