Skip to content

Commit 732eb6d

Browse files
committed
add hint text to text fields
1 parent 8cb2261 commit 732eb6d

File tree

8 files changed

+131
-75
lines changed

8 files changed

+131
-75
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,26 @@ protected void draw(String text, float x, float y) {
232232
GlStateManager.enableBlend();
233233
}
234234

235+
public int getColor() {
236+
return color;
237+
}
238+
239+
public float getScale() {
240+
return scale;
241+
}
242+
243+
public Alignment getAlignment() {
244+
return alignment;
245+
}
246+
247+
public int getX() {
248+
return x;
249+
}
250+
251+
public int getY() {
252+
return y;
253+
}
254+
235255
public float getFontHeight() {
236256
return getFontRenderer().FONT_HEIGHT * this.scale;
237257
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,13 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager,
177177
.child(new TextFieldWidget()
178178
.size(60, 20)
179179
.value(SyncHandlers.string(() -> this.value, val -> this.value = val))
180-
.margin(0, 3))
180+
.margin(0, 3)
181+
.hintText("hint"))
181182
.child(new TextFieldWidget()
182183
.size(60, 20)
183184
.value(SyncHandlers.doubleNumber(() -> this.doubleValue, val -> this.doubleValue = val))
184-
.setNumbersDouble(Function.identity()))
185+
.setNumbersDouble(Function.identity())
186+
.hintText("number"))
185187
.child(IKey.str("Test string").asWidget().padding(2).debugName("test string"))
186188
.child(IKey.EMPTY.asWidget().debugName("Empty Ikey")))
187189
.child(new Column()

src/main/java/com/cleanroommc/modularui/theme/ThemeAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private ThemeAPI() {
3535
registerWidgetTheme(Theme.BUTTON, new WidgetTheme(GuiTextures.MC_BUTTON, GuiTextures.MC_BUTTON_HOVERED, Color.WHITE.main, Color.WHITE.main, true), WidgetTheme::new);
3636
registerWidgetTheme(Theme.ITEM_SLOT, new WidgetSlotTheme(GuiTextures.SLOT_ITEM, Color.withAlpha(Color.WHITE.main, 0x60)), WidgetSlotTheme::new);
3737
registerWidgetTheme(Theme.FLUID_SLOT, new WidgetSlotTheme(GuiTextures.SLOT_FLUID, Color.withAlpha(Color.WHITE.main, 0x60)), WidgetSlotTheme::new);
38-
registerWidgetTheme(Theme.TEXT_FIELD, new WidgetTextFieldTheme(0xFF2F72A8), (parent, json, fallback) -> new WidgetTextFieldTheme(parent, fallback, json));
38+
registerWidgetTheme(Theme.TEXT_FIELD, new WidgetTextFieldTheme(0xFF2F72A8, 0xFF5F5F5F), (parent, json, fallback) -> new WidgetTextFieldTheme(parent, fallback, json));
3939
registerWidgetTheme(Theme.TOGGLE_BUTTON, new WidgetThemeSelectable(GuiTextures.MC_BUTTON, GuiTextures.MC_BUTTON_HOVERED, Color.WHITE.main, Color.WHITE.main, true,
4040
GuiTextures.MC_BUTTON_DISABLED, IDrawable.NONE, Color.WHITE.main, Color.WHITE.main, true), WidgetThemeSelectable::new);
4141
}

src/main/java/com/cleanroommc/modularui/theme/WidgetTextFieldTheme.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@
99
public class WidgetTextFieldTheme extends WidgetTheme {
1010

1111
private final int markedColor;
12+
private final int hintColor;
1213

13-
public WidgetTextFieldTheme(int markedColor) {
14+
public WidgetTextFieldTheme(int markedColor, int hintColor) {
1415
super(GuiTextures.DISPLAY_SMALL, null, Color.WHITE.main, Color.WHITE.main, false);
1516
this.markedColor = markedColor;
17+
this.hintColor = hintColor;
1618
}
1719

1820
public WidgetTextFieldTheme(WidgetTheme parent, JsonObject fallback, JsonObject json) {
1921
super(parent, json, fallback);
2022
this.markedColor = JsonHelper.getColorWithFallback(json, fallback, ((WidgetTextFieldTheme) parent).getMarkedColor(), "markedColor");
23+
this.hintColor = JsonHelper.getColorWithFallback(json, fallback, ((WidgetTextFieldTheme) parent).getHintColor(), "hintColor");
2124
}
2225

2326
public int getMarkedColor() {
2427
return this.markedColor;
2528
}
29+
30+
public int getHintColor() {
31+
return hintColor;
32+
}
2633
}

src/main/java/com/cleanroommc/modularui/widgets/textfield/BaseTextFieldWidget.java

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.cleanroommc.modularui.widget.AbstractScrollWidget;
1313
import com.cleanroommc.modularui.widget.scroll.HorizontalScrollData;
1414
import com.cleanroommc.modularui.widget.scroll.ScrollData;
15-
1615
import com.cleanroommc.modularui.widgets.VoidWidget;
1716

1817
import net.minecraft.client.gui.GuiScreen;
@@ -53,7 +52,10 @@ public class BaseTextFieldWidget<W extends BaseTextFieldWidget<W>> extends Abstr
5352
protected boolean focusOnGuiOpen;
5453
private int cursorTimer;
5554

56-
protected boolean changedTextColor = false;
55+
protected Integer textColor;
56+
protected Integer markedColor;
57+
protected String hintText = null;
58+
protected Integer hintTextColor;
5759

5860
public BaseTextFieldWidget() {
5961
super(new HorizontalScrollData(), null);
@@ -76,9 +78,6 @@ public boolean isChildValid(VoidWidget child) {
7678
public void onInit() {
7779
super.onInit();
7880
this.handler.setGuiContext(getContext());
79-
if (!this.changedTextColor) {
80-
this.renderer.setColor(getWidgetTheme(getContext().getTheme()).getTextColor());
81-
}
8281
}
8382

8483
@Override
@@ -102,17 +101,33 @@ public void onUpdate() {
102101
@Override
103102
public void preDraw(ModularGuiContext context, boolean transformed) {
104103
if (transformed) {
105-
drawText(context);
104+
WidgetTextFieldTheme widgetTheme = (WidgetTextFieldTheme) getWidgetTheme(context.getTheme());
105+
this.renderer.setColor(this.textColor != null ? this.textColor : widgetTheme.getTextColor());
106+
this.renderer.setCursorColor(this.textColor != null ? this.textColor : widgetTheme.getTextColor());
107+
this.renderer.setMarkedColor(this.markedColor != null ? this.markedColor : widgetTheme.getMarkedColor());
108+
setupDrawText(context, widgetTheme);
109+
drawText(context, widgetTheme);
106110
} else {
107111
Stencil.apply(1, 1, getArea().w() - 2, getArea().h() - 2, context);
108112
}
109113
}
110114

111-
public void drawText(ModularGuiContext context) {
115+
protected void setupDrawText(ModularGuiContext context, WidgetTextFieldTheme widgetTheme) {
112116
this.renderer.setSimulate(false);
113117
this.renderer.setScale(this.scale);
114118
this.renderer.setAlignment(this.textAlignment, -2, getArea().height);
115-
this.renderer.draw(this.handler.getText());
119+
}
120+
121+
protected void drawText(ModularGuiContext context, WidgetTextFieldTheme widgetTheme) {
122+
if (this.handler.isTextEmpty() && this.hintText != null) {
123+
int c = this.renderer.getColor();
124+
int hintColor = this.hintTextColor != null ? this.hintTextColor : widgetTheme.getHintColor();
125+
this.renderer.setColor(hintColor);
126+
this.renderer.draw(Collections.singletonList(this.hintText));
127+
this.renderer.setColor(c);
128+
} else {
129+
this.renderer.draw(this.handler.getText());
130+
}
116131
getScrollArea().getScrollX().setScrollSize(Math.max(0, (int) (this.renderer.getLastWidth() + 0.5f)));
117132
}
118133

@@ -290,8 +305,12 @@ public W setScrollBar(@Nullable ScrollBar scrollBar) {
290305
}*/
291306

292307
public W setTextColor(int color) {
293-
this.renderer.setColor(color);
294-
this.changedTextColor = true;
308+
this.textColor = color;
309+
return getThis();
310+
}
311+
312+
public W setMarkedColor(int color) {
313+
this.markedColor = color;
295314
return getThis();
296315
}
297316

@@ -300,6 +319,23 @@ public W setFocusOnGuiOpen(boolean focusOnGuiOpen) {
300319
return getThis();
301320
}
302321

322+
/**
323+
* Sets a constant hint text. The hint is displayed in a less noticeable color when the field is empty.
324+
* The color is by default obtained from the current them, but can be overriden with {@link #hintColor(int)}.
325+
*
326+
* @param hint hint text to display
327+
* @return this
328+
*/
329+
public W hintText(String hint) {
330+
this.hintText = hint;
331+
return getThis();
332+
}
333+
334+
public W hintColor(int color) {
335+
this.hintTextColor = color;
336+
return getThis();
337+
}
338+
303339
public static char getDecimalSeparator() {
304340
return format.getDecimalFormatSymbols().getDecimalSeparator();
305341
}

src/main/java/com/cleanroommc/modularui/widgets/textfield/TextFieldHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ public List<String> getText() {
234234
return this.text;
235235
}
236236

237+
public boolean isTextEmpty() {
238+
if (this.text.isEmpty()) return true;
239+
for (String line : this.text) {
240+
if (!line.isEmpty()) return false;
241+
}
242+
return true;
243+
}
244+
237245
public void onChanged() {
238246
this.textFieldWidget.markTooltipDirty();
239247
}

src/main/java/com/cleanroommc/modularui/widgets/textfield/TextFieldRenderer.java

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class TextFieldRenderer extends TextRenderer {
1919

2020
protected final TextFieldHandler handler;
2121
protected int markedColor = 0x2F72A8;
22+
protected int cursorColor = 0xFFFFFFFF;
2223
protected boolean renderCursor = false;
2324

2425
public TextFieldRenderer(TextFieldHandler handler) {
@@ -37,55 +38,56 @@ public void setMarkedColor(int markedColor) {
3738
this.markedColor = markedColor;
3839
}
3940

41+
public void setCursorColor(int cursorColor) {
42+
this.cursorColor = cursorColor;
43+
}
44+
4045
@Override
4146
protected void drawMeasuredLines(List<Line> measuredLines) {
42-
drawCursors(measuredLines);
47+
drawMarked(measuredLines);
4348
super.drawMeasuredLines(measuredLines);
49+
// draw cursor
50+
if (this.renderCursor) {
51+
Point main = this.handler.getMainCursor();
52+
Point2D.Float start = getPosOf(measuredLines, main);
53+
if (this.handler.getText().get(main.y).isEmpty()) {
54+
start.x += 0.7f;
55+
}
56+
drawCursor(start.x, start.y);
57+
}
4458
}
4559

4660
@Override
4761
public List<String> wrapLine(String line) {
4862
return Collections.singletonList(line);
4963
}
5064

51-
protected void drawCursors(List<Line> measuredLines) {
52-
if (!this.simulate) {
53-
Point2D.Float start;
54-
if (this.handler.hasTextMarked()) {
55-
start = getPosOf(measuredLines, this.handler.getStartCursor());
56-
// render Marked
57-
Point2D.Float end = getPosOf(measuredLines, this.handler.getEndCursor());
58-
59-
if (start.y == end.y) {
60-
drawMarked(start.y, start.x, end.x);
61-
} else {
62-
int min = this.handler.getStartCursor().y;
63-
int max = this.handler.getEndCursor().y;
64-
Line line = measuredLines.get(min);
65-
int startX = getStartX(line.getWidth());
66-
drawMarked(start.y, start.x, startX + line.getWidth());
67-
start.y += getFontHeight();
68-
if (max - min > 1) {
69-
for (int i = min + 1; i < max; i++) {
70-
line = measuredLines.get(i);
71-
startX = getStartX(line.getWidth());
72-
drawMarked(start.y, startX, startX + line.getWidth());
73-
start.y += getFontHeight();
74-
}
65+
protected void drawMarked(List<Line> measuredLines) {
66+
if (!this.simulate && this.handler.hasTextMarked()) {
67+
Point2D.Float start = getPosOf(measuredLines, this.handler.getStartCursor());
68+
// render Marked
69+
Point2D.Float end = getPosOf(measuredLines, this.handler.getEndCursor());
70+
71+
if (start.y == end.y) {
72+
drawMarked(start.y, start.x, end.x);
73+
} else {
74+
int min = this.handler.getStartCursor().y;
75+
int max = this.handler.getEndCursor().y;
76+
Line line = measuredLines.get(min);
77+
int startX = getStartX(line.getWidth());
78+
drawMarked(start.y, start.x, startX + line.getWidth());
79+
start.y += getFontHeight();
80+
if (max - min > 1) {
81+
for (int i = min + 1; i < max; i++) {
82+
line = measuredLines.get(i);
83+
startX = getStartX(line.getWidth());
84+
drawMarked(start.y, startX, startX + line.getWidth());
85+
start.y += getFontHeight();
7586
}
76-
line = measuredLines.get(max);
77-
startX = getStartX(line.getWidth());
78-
drawMarked(start.y, startX, end.x);
79-
}
80-
}
81-
// draw cursor
82-
Point main = this.handler.getMainCursor();
83-
start = getPosOf(measuredLines, main);
84-
if (this.renderCursor) {
85-
if (this.handler.getText().get(main.y).isEmpty()) {
86-
start.x += 0.7f;
8787
}
88-
drawCursor(start.x, start.y);
88+
line = measuredLines.get(max);
89+
startX = getStartX(line.getWidth());
90+
drawMarked(start.y, startX, end.x);
8991
}
9092
}
9193
}
@@ -158,10 +160,10 @@ private void drawCursor(float x0, float y0) {
158160
y0 = (y0 - 1) / this.scale;
159161
float x1 = x0 + 0.6f;
160162
float y1 = y0 + 9;
161-
float red = Color.getRedF(this.color);
162-
float green = Color.getGreenF(this.color);
163-
float blue = Color.getBlueF(this.color);
164-
float alpha = Color.getAlphaF(this.color);
163+
float red = Color.getRedF(this.cursorColor);
164+
float green = Color.getGreenF(this.cursorColor);
165+
float blue = Color.getBlueF(this.cursorColor);
166+
float alpha = Color.getAlphaF(this.cursorColor);
165167
if (alpha == 0)
166168
alpha = 1f;
167169
Tessellator tessellator = Tessellator.getInstance();

src/main/java/com/cleanroommc/modularui/widgets/textfield/TextFieldWidget.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public class TextFieldWidget extends BaseTextFieldWidget<TextFieldWidget> {
3232
private String mathFailMessage = null;
3333
private double defaultNumber = 0;
3434

35-
protected boolean changedMarkedColor = false;
36-
3735
public double parse(String num) {
3836
ParseResult result = MathUtils.parseExpression(num, this.defaultNumber, true);
3937
double value = result.getResult();
@@ -58,9 +56,6 @@ public void onInit() {
5856
if (!hasTooltip()) {
5957
tooltipBuilder(tooltip -> tooltip.addLine(IKey.str(getText())));
6058
}
61-
if (!this.changedMarkedColor) {
62-
this.renderer.setMarkedColor(getMarkedColor());
63-
}
6459
}
6560

6661
public int getMarkedColor() {
@@ -96,13 +91,11 @@ public void onUpdate() {
9691
}
9792

9893
@Override
99-
public void drawText(ModularGuiContext context) {
94+
protected void setupDrawText(ModularGuiContext context, WidgetTextFieldTheme widgetTheme) {
10095
this.renderer.setSimulate(false);
10196
this.renderer.setPos(getArea().getPadding().left, 0);
10297
this.renderer.setScale(this.scale);
10398
this.renderer.setAlignment(this.textAlignment, -1, getArea().height);
104-
this.renderer.draw(this.handler.getText());
105-
getScrollData().setScrollSize(Math.max(0, (int) this.renderer.getLastWidth()));
10699
}
107100

108101
@Override
@@ -173,18 +166,6 @@ public TextFieldWidget setPattern(Pattern pattern) {
173166
return this;
174167
}
175168

176-
public TextFieldWidget setTextColor(int textColor) {
177-
this.renderer.setColor(textColor);
178-
this.changedTextColor = true;
179-
return this;
180-
}
181-
182-
public TextFieldWidget setMarkedColor(int color) {
183-
this.renderer.setMarkedColor(color);
184-
this.changedMarkedColor = true;
185-
return this;
186-
}
187-
188169
public TextFieldWidget setValidator(Function<String, String> validator) {
189170
this.validator = validator;
190171
return this;

0 commit comments

Comments
 (0)