Skip to content

Commit 4c8231e

Browse files
committed
add getWrappedDrawable() to IIcon
1 parent 1b356ef commit 4c8231e

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

src/main/java/com/cleanroommc/modularui/api/drawable/IIcon.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
import com.cleanroommc.modularui.drawable.InteractableIcon;
55
import com.cleanroommc.modularui.widget.sizer.Box;
66

7+
import org.jetbrains.annotations.Nullable;
8+
79
/**
810
* A {@link IDrawable} with a fixed size.
911
*/
1012
public interface IIcon extends IDrawable {
1113

14+
/**
15+
* @return the drawable this icon wraps or null if it doesn't wrap anything
16+
*/
17+
@Nullable IDrawable getWrappedDrawable();
18+
1219
/**
1320
* @return width of this icon or 0 if the width should be dynamic
1421
*/
@@ -24,6 +31,15 @@ public interface IIcon extends IDrawable {
2431
*/
2532
Box getMargin();
2633

34+
default IDrawable getRootDrawable() {
35+
IDrawable drawable = this;
36+
while (drawable instanceof IIcon icon) {
37+
drawable = icon.getWrappedDrawable();
38+
if (drawable == null) return icon;
39+
}
40+
return drawable;
41+
}
42+
2743
default HoverableIcon asHoverable() {
2844
return new HoverableIcon(this);
2945
}

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

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

3+
import com.cleanroommc.modularui.api.drawable.IDrawable;
34
import com.cleanroommc.modularui.api.drawable.IIcon;
45
import com.cleanroommc.modularui.screen.viewport.GuiContext;
56
import com.cleanroommc.modularui.theme.WidgetTheme;
@@ -13,6 +14,11 @@ public DelegateIcon(IIcon icon) {
1314
this.icon = icon;
1415
}
1516

17+
@Override
18+
public IIcon getWrappedDrawable() {
19+
return icon;
20+
}
21+
1622
@Override
1723
public int getWidth() {
1824
return this.icon.getWidth();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public Icon(IDrawable drawable) {
2929
this.drawable = drawable;
3030
}
3131

32+
@Override
33+
public IDrawable getWrappedDrawable() {
34+
return drawable;
35+
}
36+
3237
@Override
3338
public int getWidth() {
3439
return this.width;
@@ -44,6 +49,10 @@ public Box getMargin() {
4449
return this.margin;
4550
}
4651

52+
public int getColor() {
53+
return color;
54+
}
55+
4756
@SideOnly(Side.CLIENT)
4857
@Override
4958
public void draw(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme) {

src/main/java/com/cleanroommc/modularui/drawable/text/KeyIcon.java

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

33
import com.cleanroommc.modularui.api.MCHelper;
4+
import com.cleanroommc.modularui.api.drawable.IDrawable;
45
import com.cleanroommc.modularui.api.drawable.IIcon;
56
import com.cleanroommc.modularui.api.drawable.IKey;
67
import com.cleanroommc.modularui.screen.viewport.GuiContext;
@@ -27,6 +28,11 @@ public FontRenderer getFontRenderer() {
2728
return this.overrideFontRenderer != null ? this.overrideFontRenderer : MCHelper.getFontRenderer();
2829
}
2930

31+
@Override
32+
public IKey getWrappedDrawable() {
33+
return key;
34+
}
35+
3036
@Override
3137
public int getWidth() {
3238
return getFontRenderer().getStringWidth(key.get()) + this.margin.horizontal();
@@ -42,6 +48,10 @@ public Box getMargin() {
4248
return null;
4349
}
4450

51+
public IKey getKey() {
52+
return key;
53+
}
54+
4555
@Override
4656
public void draw(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme) {
4757
int w = getWidth(), h = getHeight();

src/main/java/com/cleanroommc/modularui/drawable/text/TextIcon.java

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

3+
import com.cleanroommc.modularui.api.drawable.IDrawable;
34
import com.cleanroommc.modularui.api.drawable.IIcon;
45
import com.cleanroommc.modularui.screen.viewport.GuiContext;
56
import com.cleanroommc.modularui.theme.WidgetTheme;
@@ -9,6 +10,8 @@
910
import net.minecraftforge.fml.relauncher.Side;
1011
import net.minecraftforge.fml.relauncher.SideOnly;
1112

13+
import org.jetbrains.annotations.Nullable;
14+
1215
public class TextIcon implements IIcon {
1316

1417
private final String text;
@@ -34,6 +37,11 @@ public void draw(GuiContext context, int x, int y, int width, int height, Widget
3437
TextRenderer.SHARED.drawSimple(this.text);
3538
}
3639

40+
@Override
41+
public @Nullable IDrawable getWrappedDrawable() {
42+
return null;
43+
}
44+
3745
@Override
3846
public int getWidth() {
3947
return this.width;

0 commit comments

Comments
 (0)