Skip to content

Commit 7b99a78

Browse files
committed
Make the slider have a background (it changes color!)
Fix a small error in isolateWithFullAlpha and add javadocs
1 parent 95ad952 commit 7b99a78

File tree

4 files changed

+107
-68
lines changed

4 files changed

+107
-68
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package gregtech.api.mui.drawable;
2+
3+
import com.cleanroommc.modularui.api.drawable.IDrawable;
4+
import com.cleanroommc.modularui.drawable.GuiDraw;
5+
import com.cleanroommc.modularui.screen.viewport.GuiContext;
6+
import com.cleanroommc.modularui.theme.WidgetTheme;
7+
import com.cleanroommc.modularui.utils.Color;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
import java.util.function.IntSupplier;
11+
12+
public class DynamicColorRectangle implements IDrawable {
13+
14+
@NotNull
15+
private final IntSupplier colorSupplier;
16+
17+
public DynamicColorRectangle(@NotNull IntSupplier colorSupplier) {
18+
this.colorSupplier = colorSupplier;
19+
}
20+
21+
@Override
22+
public void draw(GuiContext context, int x0, int y0, int width, int height, WidgetTheme widgetTheme) {
23+
if (canApplyTheme()) {
24+
Color.setGlColor(widgetTheme.getColor());
25+
} else {
26+
Color.setGlColorOpaque(Color.WHITE.main);
27+
}
28+
29+
GuiDraw.drawRect(x0, y0, width, height, colorSupplier.getAsInt());
30+
}
31+
}

src/main/java/gregtech/api/util/ColorUtil.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,51 @@ public enum ARGBHelper {
4646
this.shift = shift;
4747
}
4848

49-
public @Range(from = 0, to = 0xFF) int getFromInt(int sourceARGB) {
50-
return (sourceARGB >> shift) & 0xFF;
49+
/**
50+
* Isolate this channel as an integer from 0 to 255. <br/>
51+
* Example: {@code GREEN.isolateAndShift(0xDEADBEEF)} will return {@code 0xBE} or {@code 190}.
52+
*/
53+
public @Range(from = 0, to = 0xFF) int isolateAndShift(int value) {
54+
return (value >> shift) & 0xFF;
5155
}
5256

53-
public int setInInt(int originalARGB, @Range(from = 0, to = 0xFF) int replacementColor) {
54-
return (originalARGB & invertedOverlay) | (replacementColor << shift);
57+
/**
58+
* Remove the other two colors from the integer encoded ARGB and set the alpha to 255. <br/>
59+
* Will always return {@code 0xFF000000} if called on {@link #ALPHA}. <br/>
60+
* Unlike {@link #isolateAndShift(int)}, this will not be between 0 and 255. <br/>
61+
* Example: {@code GREEN.isolateWithFullAlpha(0xDEADBEEF)} will return {@code 0xFF00BE00} or {@code 4278238720}.
62+
*/
63+
public int isolateWithFullAlpha(int value) {
64+
return (value & overlay) | 0xFF000000;
5565
}
5666

57-
public int setInInt(@Range(from = 0, to = 0xFF) int color) {
58-
return color << shift;
67+
/**
68+
* Set the value of this channel in an integer encoded ARGB value.
69+
*/
70+
public int replace(int originalARGB, @Range(from = 0, to = 0xFF) int value) {
71+
return (originalARGB & invertedOverlay) | (value << shift);
5972
}
6073

61-
public int addInInt(int originalARGB, @Range(from = 0, to = 0xFF) int addingColor) {
62-
return setInInt(originalARGB, (getFromInt(originalARGB) + addingColor) & 0xFF);
74+
/**
75+
* The same as {@link #replace(int, int)} but will just return the value shifted to this channel.
76+
*/
77+
public int replace(@Range(from = 0, to = 0xFF) int value) {
78+
return value << shift;
6379
}
6480

65-
public int subtractFromInt(int originalARGB, @Range(from = 0, to = 0xFF) int subtractingColor) {
66-
return setInInt(originalARGB, (getFromInt(originalARGB) - subtractingColor) & 0xFF);
81+
/**
82+
* Add a value to this channel's value. Can overflow in this channel, but will not affect the other channels.
83+
*/
84+
public int add(int originalARGB, @Range(from = 0, to = 0xFF) int value) {
85+
return replace(originalARGB, (isolateAndShift(originalARGB) + value) & 0xFF);
86+
}
87+
88+
/**
89+
* Subtract a value from this channel's value. Can underflow in this channel, but will not affect the other
90+
* channels.
91+
*/
92+
public int subtract(int originalARGB, @Range(from = 0, to = 0xFF) int value) {
93+
return replace(originalARGB, (isolateAndShift(originalARGB) - value) & 0xFF);
6794
}
6895
}
6996
}

src/main/java/gregtech/common/items/behaviors/spray/CreativeSprayBehavior.java

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import gregtech.api.items.metaitem.stats.IMouseEventHandler;
88
import gregtech.api.mui.GTGuiTextures;
99
import gregtech.api.mui.GTGuis;
10+
import gregtech.api.mui.drawable.DynamicColorRectangle;
1011
import gregtech.api.mui.factory.MetaItemGuiFactory;
1112
import gregtech.api.mui.sync.PagedWidgetSyncHandler;
1213
import gregtech.api.util.GTUtility;
@@ -144,66 +145,43 @@ public ModularPanel buildUI(HandGuiData guiData, PanelSyncManager guiSyncManager
144145
.addPage(Flow.column()
145146
.widthRel(1.0f)
146147
.heightRel(1.0f)
147-
.child(Flow.row()
148-
.widthRel(1.0f)
149-
.coverChildrenHeight()
150-
.child(new TextFieldWidget()
151-
.width(30)
152-
.setNumbers(0, 255)
153-
.value(createRGBIntValue(ARGBHelper.RED, rgbColorSync,
154-
usesRGBSync::getBoolValue)))
155-
.child(new SliderWidget()
156-
.width(132)
157-
.bounds(0.0D, 255.0d)
158-
.value(createRGBDoubleValue(ARGBHelper.RED, rgbColorSync,
159-
usesRGBSync::getBoolValue))))
160-
.child(Flow.row()
161-
.widthRel(1.0f)
162-
.coverChildrenHeight()
163-
.child(new TextFieldWidget()
164-
.width(30)
165-
.setNumbers(0, 255)
166-
.value(createRGBIntValue(ARGBHelper.GREEN, rgbColorSync,
167-
usesRGBSync::getBoolValue)))
168-
.child(new SliderWidget()
169-
.width(132)
170-
.bounds(0.0D, 255.0d)
171-
.value(createRGBDoubleValue(ARGBHelper.GREEN, rgbColorSync,
172-
usesRGBSync::getBoolValue))))
173-
.child(Flow.row()
174-
.widthRel(1.0f)
175-
.coverChildrenHeight()
176-
.child(new TextFieldWidget()
177-
.width(30)
178-
.setNumbers(0, 255)
179-
.value(createRGBIntValue(ARGBHelper.BLUE, rgbColorSync,
180-
usesRGBSync::getBoolValue)))
181-
.child(new SliderWidget()
182-
.width(132)
183-
.bounds(0.0D, 255.0d)
184-
.value(createRGBDoubleValue(ARGBHelper.BLUE, rgbColorSync,
185-
usesRGBSync::getBoolValue))))));
148+
.child(createColorRow(ARGBHelper.RED, rgbColorSync, usesRGBSync::getBoolValue))
149+
.child(createColorRow(ARGBHelper.GREEN, rgbColorSync, usesRGBSync::getBoolValue))
150+
.child(createColorRow(ARGBHelper.BLUE, rgbColorSync, usesRGBSync::getBoolValue))));
186151
}
187152

188-
private static IntValue.Dynamic createRGBIntValue(@NotNull ARGBHelper helper, @NotNull IntSyncValue argbSync,
189-
@NotNull BooleanSupplier allowSetting) {
190-
return new IntValue.Dynamic(() -> helper.getFromInt(argbSync.getIntValue()),
191-
newSingleColor -> {
192-
if (allowSetting.getAsBoolean()) {
193-
argbSync.setIntValue(helper.setInInt(argbSync.getIntValue(), newSingleColor));
194-
}
195-
});
196-
}
197-
198-
private static DoubleValue.Dynamic createRGBDoubleValue(@NotNull ARGBHelper helper,
199-
@NotNull IntSyncValue argbSync,
200-
@NotNull BooleanSupplier allowSetting) {
201-
return new DoubleValue.Dynamic(() -> helper.getFromInt(argbSync.getIntValue()),
202-
newSingleColor -> {
203-
if (allowSetting.getAsBoolean()) {
204-
argbSync.setIntValue(helper.setInInt(argbSync.getIntValue(), (int) newSingleColor));
205-
}
206-
});
153+
private static Flow createColorRow(@NotNull ARGBHelper helper, @NotNull IntSyncValue rgbColorSync,
154+
@NotNull BooleanSupplier allowSetting) {
155+
return Flow.row()
156+
.widthRel(1.0f)
157+
.coverChildrenHeight()
158+
.child(new TextFieldWidget()
159+
.width(30)
160+
.setNumbers(0, 255)
161+
.value(new IntValue.Dynamic(() -> helper.isolateAndShift(rgbColorSync.getIntValue()),
162+
colorDigit -> {
163+
if (allowSetting.getAsBoolean()) {
164+
int newColor = helper.replace(rgbColorSync.getIntValue(), colorDigit);
165+
rgbColorSync.setIntValue(newColor);
166+
}
167+
})))
168+
.child(new SliderWidget()
169+
.width(132)
170+
.bounds(0.0D, 255.0d)
171+
.value(new DoubleValue.Dynamic(
172+
() -> (double) helper.isolateAndShift(rgbColorSync.getIntValue()),
173+
colorDigit -> {
174+
if (allowSetting.getAsBoolean()) {
175+
int newColor = helper.replace(rgbColorSync.getIntValue(), (int) colorDigit);
176+
rgbColorSync.setIntValue(newColor);
177+
}
178+
}))
179+
.background(
180+
new DynamicColorRectangle(() -> helper.isolateWithFullAlpha(rgbColorSync.getIntValue()))
181+
.asIcon()
182+
.margin(4, 0)
183+
.height(8))
184+
.addTooltipLine(IKey.lang("metaitem.spray.creative.tip." + helper.toString().toLowerCase())));
207185
}
208186

209187
@Override

src/main/resources/assets/gregtech/lang/en_us.lang

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ metaitem.spray.creative.name=Creative Spray Can (%s)
334334
metaitem.spray.creative.name_base=Creative Spray Can
335335
metaitem.spray.creative.mode.normal=Normal Mode
336336
metaitem.spray.creative.mode.rgb=RGB Mode
337+
metaitem.spray.creative.tip.red=Red
338+
metaitem.spray.creative.tip.green=Blue
339+
metaitem.spray.creative.tip.blue=Green
337340
metaitem.spray.creative.solvent=Solvent
338341
metaitem.spray.creative.white=White
339342
metaitem.spray.creative.orange=Orange

0 commit comments

Comments
 (0)