-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathTextFieldWidget.java
More file actions
247 lines (215 loc) · 8.07 KB
/
TextFieldWidget.java
File metadata and controls
247 lines (215 loc) · 8.07 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package com.cleanroommc.modularui.widgets.textfield;
import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.api.ITheme;
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.api.value.IStringValue;
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
import com.cleanroommc.modularui.theme.WidgetTextFieldTheme;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.MathUtils;
import com.cleanroommc.modularui.utils.ParseResult;
import com.cleanroommc.modularui.value.StringValue;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import com.cleanroommc.modularui.value.sync.ValueSyncHandler;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.text.ParsePosition;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Pattern;
/**
* Text input widget with one line only. Can be synced between client and server. Can handle text validation.
*/
public class TextFieldWidget extends BaseTextFieldWidget<TextFieldWidget> {
private IStringValue<?> stringValue;
private Function<String, String> validator = val -> val;
private boolean numbers = false;
private String mathFailMessage = null;
private double defaultNumber = 0;
public double parse(String num) {
ParseResult result = MathUtils.parseExpression(num, this.defaultNumber, true);
double value = result.getResult();
if (result.isFailure()) {
this.mathFailMessage = result.getError();
ModularUI.LOGGER.error("Math expression error in {}: {}", this, this.mathFailMessage);
}
return value;
}
public IStringValue<?> createMathFailMessageValue() {
return new StringValue.Dynamic(() -> this.mathFailMessage, val -> this.mathFailMessage = val);
}
@Override
public void onInit() {
super.onInit();
if (this.stringValue == null) {
this.stringValue = new StringValue("");
}
setText(this.stringValue.getStringValue());
if (!hasTooltip()) {
tooltipBuilder(tooltip -> tooltip.addLine(IKey.str(getText())));
}
}
public int getMarkedColor() {
WidgetTheme theme = getWidgetTheme(getContext().getTheme());
if (theme instanceof WidgetTextFieldTheme textFieldTheme) {
return textFieldTheme.getMarkedColor();
}
return ITheme.getDefault().getTextFieldTheme().getMarkedColor();
}
@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
if (syncHandler instanceof IStringValue<?> iStringValue && syncHandler instanceof ValueSyncHandler<?> valueSyncHandler) {
this.stringValue = iStringValue;
valueSyncHandler.setChangeListener(() -> {
markTooltipDirty();
setText(this.stringValue.getValue().toString());
});
return true;
}
return false;
}
@Override
public void onUpdate() {
super.onUpdate();
if (!isFocused()) {
String s = this.stringValue.getStringValue();
if (!getText().equals(s)) {
setText(s);
}
}
}
@Override
protected void setupDrawText(ModularGuiContext context, WidgetTextFieldTheme widgetTheme) {
this.renderer.setSimulate(false);
this.renderer.setPos(getArea().getPadding().left, 0);
this.renderer.setScale(this.scale);
this.renderer.setAlignment(this.textAlignment, -1, getArea().height);
}
@Override
public void drawForeground(ModularGuiContext context) {
if (hasTooltip() && getScrollData().isScrollBarActive(getScrollArea()) && isHoveringFor(getTooltip().getShowUpTimer())) {
getTooltip().draw(getContext());
}
}
@NotNull
public String getText() {
if (this.handler.getText().isEmpty()) {
return "";
}
if (this.handler.getText().size() > 1) {
throw new IllegalStateException("TextFieldWidget can only have one line!");
}
return this.handler.getText().get(0);
}
public void setText(@NotNull String text) {
if (this.handler.getText().isEmpty()) {
this.handler.getText().add(text);
} else {
this.handler.getText().set(0, text);
}
}
@Override
public void onFocus(ModularGuiContext context) {
super.onFocus(context);
Point main = this.handler.getMainCursor();
if (main.x == 0) {
this.handler.setCursor(main.y, getText().length(), true, true);
}
}
@Override
public void onRemoveFocus(ModularGuiContext context) {
super.onRemoveFocus(context);
if (this.handler.getText().isEmpty()) {
this.handler.getText().add(this.validator.apply(""));
} else if (this.handler.getText().size() == 1) {
this.handler.getText().set(0, this.validator.apply(this.handler.getText().get(0)));
markTooltipDirty();
} else {
throw new IllegalStateException("TextFieldWidget can only have one line!");
}
this.stringValue.setStringValue(this.numbers ? format.parse(getText(), new ParsePosition(0)).toString() : getText());
}
@Override
public boolean canHover() {
return true;
}
public String getMathFailMessage() {
return mathFailMessage;
}
public TextFieldWidget setMaxLength(int maxLength) {
this.handler.setMaxCharacters(maxLength);
return this;
}
public TextFieldWidget setPattern(Pattern pattern) {
this.handler.setPattern(pattern);
return this;
}
public TextFieldWidget setValidator(Function<String, String> validator) {
this.validator = validator;
return this;
}
public TextFieldWidget setNumbersLong(Function<Long, Long> validator) {
this.numbers = true;
setValidator(val -> {
long num;
if (val.isEmpty()) {
num = (long) this.defaultNumber;
} else {
num = (long) parse(val);
}
return format.format(validator.apply(num));
});
return this;
}
public TextFieldWidget setNumbers(Function<Integer, Integer> validator) {
this.numbers = true;
return setValidator(val -> {
int num;
if (val.isEmpty()) {
num = (int) this.defaultNumber;
} else {
num = (int) parse(val);
}
return format.format(validator.apply(num));
});
}
public TextFieldWidget setNumbersDouble(Function<Double, Double> validator) {
this.numbers = true;
return setValidator(val -> {
double num;
if (val.isEmpty()) {
num = this.defaultNumber;
} else {
num = parse(val);
}
return format.format(validator.apply(num));
});
}
public TextFieldWidget setNumbers(Supplier<Integer> min, Supplier<Integer> max) {
return setNumbers(val -> Math.min(max.get(), Math.max(min.get(), val)));
}
public TextFieldWidget setNumbersLong(Supplier<Long> min, Supplier<Long> max) {
return setNumbersLong(val -> Math.min(max.get(), Math.max(min.get(), val)));
}
public TextFieldWidget setNumbers(int min, int max) {
return setNumbers(val -> Math.min(max, Math.max(min, val)));
}
public TextFieldWidget setNumbers() {
return setNumbers(Integer.MIN_VALUE, Integer.MAX_VALUE);
}
public TextFieldWidget setDefaultNumber(double defaultNumber) {
this.defaultNumber = defaultNumber;
return this;
}
@ApiStatus.Experimental
public TextFieldWidget setFormatAsInteger(boolean formatAsInteger) {
this.renderer.setFormatAsInteger(formatAsInteger);
return getThis();
}
public TextFieldWidget value(IStringValue<?> stringValue) {
this.stringValue = stringValue;
setValue(stringValue);
return this;
}
}