-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathIDrawable.java
More file actions
168 lines (145 loc) · 4.98 KB
/
IDrawable.java
File metadata and controls
168 lines (145 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.cleanroommc.modularui.api.drawable;
import com.cleanroommc.modularui.drawable.DrawableArray;
import com.cleanroommc.modularui.drawable.Icon;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.widget.Widget;
import com.cleanroommc.modularui.widget.sizer.Area;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.Nullable;
/**
* An object which can be drawn. This is mainly used for backgrounds and overlays in
* {@link com.cleanroommc.modularui.api.widget.IWidget}.
*/
public interface IDrawable {
/**
* Draws this drawable at the given position with the given size.
*
* @param context current context to draw with
* @param x x position
* @param y y position
* @param width draw width
* @param height draw height
*/
@SideOnly(Side.CLIENT)
void draw(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme);
/**
* @deprecated use {@link #draw(GuiContext, int, int, int, int, WidgetTheme)}
*/
@SideOnly(Side.CLIENT)
@Deprecated
default void draw(GuiContext context, int x, int y, int width, int height) {
draw(context, x, y, width, height, WidgetTheme.getDefault());
}
/**
* @deprecated use {@link #drawAtZero(GuiContext, int, int, WidgetTheme)}
*/
@SideOnly(Side.CLIENT)
@Deprecated
default void drawAtZero(GuiContext context, int width, int height) {
drawAtZero(context, width, height, WidgetTheme.getDefault());
}
/**
* Draws this drawable at the current (0|0) with the given size.
*
* @param context gui context
* @param width draw width
* @param height draw height
* @param widgetTheme
*/
@SideOnly(Side.CLIENT)
default void drawAtZero(GuiContext context, int width, int height, WidgetTheme widgetTheme) {
draw(context, 0, 0, width, height, widgetTheme);
}
/**
* @deprecated use {@link #draw(GuiContext, Area, WidgetTheme)}
*/
@SideOnly(Side.CLIENT)
@Deprecated
default void draw(GuiContext context, Area area) {
draw(context, area.x, area.y, area.width, area.height, WidgetTheme.getDefault());
}
/**
* Draws this drawable in a given area.
*
* @param context current context to draw with
* @param area draw area
*/
@SideOnly(Side.CLIENT)
default void draw(GuiContext context, Area area, WidgetTheme widgetTheme) {
draw(context, area.x, area.y, area.width, area.height, widgetTheme);
}
/**
* @deprecated use {@link #drawAtZero(GuiContext, Area, WidgetTheme)}
*/
@Deprecated
@SideOnly(Side.CLIENT)
default void drawAtZero(GuiContext context, Area area) {
draw(context, 0, 0, area.width, area.height, WidgetTheme.getDefault());
}
/**
* Draws this drawable at the current (0|0) with the given area's size.
*
* @param context gui context
* @param area draw area
*/
@SideOnly(Side.CLIENT)
default void drawAtZero(GuiContext context, Area area, WidgetTheme widgetTheme) {
draw(context, 0, 0, area.width, area.height, widgetTheme);
}
/**
* @return if theme color can be applied on this drawable
*/
default boolean canApplyTheme() {
return false;
}
/**
* @return a widget with this drawable as a background
*/
default Widget<?> asWidget() {
return new DrawableWidget(this);
}
/**
* @return this drawable as an icon
*/
default Icon asIcon() {
return new Icon(this);
}
/**
* Reads extra json data after this drawable is created.
*
* @param json json to read from
*/
default void loadFromJson(JsonObject json) {}
/**
* An empty drawable. Does nothing.
*/
IDrawable EMPTY = (context, x, y, width, height, widgetTheme) -> {};
/**
* An empty drawable used to mark hover textures as "should not be used"!
*/
IDrawable NONE = (context, x, y, width, height, widgetTheme) -> {};
static boolean isVisible(@Nullable IDrawable drawable) {
if (drawable == null || drawable == EMPTY || drawable == NONE) return false;
if (drawable instanceof DrawableArray array) {
return array.getDrawables().length > 0;
}
return true;
}
/**
* A widget wrapping a drawable. The drawable is drawn between the background and the overlay.
*/
class DrawableWidget extends Widget<DrawableWidget> {
private final IDrawable drawable;
public DrawableWidget(IDrawable drawable) {
this.drawable = drawable;
}
@SideOnly(Side.CLIENT)
@Override
public void draw(GuiContext context, WidgetTheme widgetTheme) {
this.drawable.drawAtZero(context, getArea(), widgetTheme);
}
}
}