Skip to content

Commit 35d0b44

Browse files
purebluezbrachy84
andauthored
Fix text alignment not working with TextFieldWidget (#150)
* do the fix * add test * horizontal * fix a few bugs * comment & bool for checking width --------- Co-authored-by: brachy84 <[email protected]>
1 parent a825c97 commit 35d0b44

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected void drawMeasuredLines(List<Line> measuredLines) {
9292
draw(measuredLine.text, x0, y0);
9393
y0 += (int) getFontHeight();
9494
}
95-
this.lastActualWidth = this.maxWidth > 0 ? Math.min(maxW, this.maxWidth) : maxW;
95+
this.lastActualWidth = maxW;
9696
this.lastActualHeight = measuredLines.size() * getFontHeight();
9797
this.lastTrimmedWidth = Math.max(0, this.lastActualWidth - this.scale);
9898
this.lastTrimmedHeight = Math.max(0, this.lastActualHeight - this.scale);
@@ -193,11 +193,11 @@ public List<String> wrapLine(String line) {
193193
return this.maxWidth > 0 ? getFontRenderer().listFormattedStringToWidth(line, (int) (this.maxWidth / this.scale)) : Collections.singletonList(line);
194194
}
195195

196-
public boolean wouldFit(List<String> text) {
196+
public boolean wouldFit(List<String> text, boolean shouldCheckWidth) {
197197
if (this.maxHeight > 0 && this.maxHeight < text.size() * getFontHeight() - this.scale) {
198198
return false;
199199
}
200-
if (this.maxWidth > 0) {
200+
if (this.maxWidth > 0 && shouldCheckWidth) {
201201
for (String line : text) {
202202
if (this.maxWidth < getFontRenderer().getStringWidth(line)) {
203203
return false;
@@ -240,7 +240,7 @@ protected int getStartX(float lineWidth) {
240240

241241
protected int getStartX(float maxWidth, float lineWidth) {
242242
if (this.alignment.x > 0 && maxWidth > 0) {
243-
return (int) (this.x + (maxWidth * this.alignment.x) - lineWidth * this.alignment.x);
243+
return Math.max(this.x, (int) (this.x + (maxWidth * this.alignment.x) - lineWidth * this.alignment.x));
244244
}
245245
return this.x;
246246
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
269269
.overlay(IKey.str("Button 2")))
270270
.child(new TextFieldWidget()
271271
.size(60, 18)
272-
.paddingTop(1)
272+
.setTextAlignment(Alignment.Center)
273273
.value(SyncHandlers.string(() -> this.value, val -> this.value = val))
274274
.margin(0, 2)
275275
.hintText("hint"))

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected void setupDrawText(ModularGuiContext context, WidgetTextFieldTheme wid
119119
this.renderer.setSimulate(false);
120120
this.renderer.setPos(getArea().getPadding().getLeft(), getArea().getPadding().getTop());
121121
this.renderer.setScale(this.scale);
122-
this.renderer.setAlignment(this.textAlignment, -1, getArea().paddedHeight());
122+
this.renderer.setAlignment(this.textAlignment, getArea().paddedWidth(), getArea().paddedHeight());
123123
}
124124

125125
protected void drawText(ModularGuiContext context, WidgetTextFieldTheme widgetTheme) {
@@ -226,7 +226,7 @@ public void onMouseDrag(int mouseButton, long timeSinceClick) {
226226
case Keyboard.KEY_ESCAPE:
227227
if (ModularUIConfig.escRestoreLastText) {
228228
this.handler.clear();
229-
this.handler.insert(this.lastText);
229+
this.handler.insert(this.lastText, canScrollHorizontally());
230230
}
231231
getContext().removeFocus();
232232
return Result.SUCCESS;
@@ -267,7 +267,7 @@ public void onMouseDrag(int mouseButton, long timeSinceClick) {
267267
this.handler.delete();
268268
}
269269
// paste copied text in marked text
270-
this.handler.insert(GuiScreen.getClipboardString().replace("§", ""));
270+
this.handler.insert(GuiScreen.getClipboardString().replace("§", ""), canScrollHorizontally());
271271
return Result.SUCCESS;
272272
} else if (GuiScreen.isKeyComboCtrlX(keyCode) && this.handler.hasTextMarked()) {
273273
// copy and delete copied text
@@ -283,12 +283,16 @@ public void onMouseDrag(int mouseButton, long timeSinceClick) {
283283
this.handler.delete();
284284
}
285285
// insert typed char
286-
this.handler.insert(String.valueOf(character));
286+
this.handler.insert(String.valueOf(character), canScrollHorizontally());
287287
return Result.SUCCESS;
288288
}
289289
return Result.STOP;
290290
}
291291

292+
public boolean canScrollHorizontally() {
293+
return getScrollArea().getScrollX() != null;
294+
}
295+
292296
public int getMaxLines() {
293297
return this.handler.getMaxLines();
294298
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,15 @@ public boolean test(String text) {
273273
return this.maxLines > 1 || ((this.pattern == null || this.pattern.matcher(text).matches()) && (this.maxCharacters < 0 || this.maxCharacters >= text.length()));
274274
}
275275

276-
public void insert(String text) {
277-
insert(Arrays.asList(text.split("\n")));
276+
public void insert(String text, boolean hasHorizontalScrolling) {
277+
insert(Arrays.asList(text.split("\n")), hasHorizontalScrolling);
278278
}
279279

280-
public void insert(List<String> text) {
280+
public void insert(List<String> text, boolean hasHorizontalScrolling) {
281281
List<String> copy = new ArrayList<>(this.text);
282282
Point point = insert(copy, text);
283-
if (point == null || copy.size() > this.maxLines || !this.renderer.wouldFit(copy)) return;
283+
// if we can scroll horizontally, we have virtually an infinite amount of space and don't need to check width
284+
if (point == null || copy.size() > this.maxLines || !this.renderer.wouldFit(copy, !hasHorizontalScrolling)) return;
284285
this.text.clear();
285286
this.text.addAll(copy);
286287
setCursor(point, true);
@@ -409,4 +410,4 @@ public GuiContext getGuiContext() {
409410
public void setGuiContext(GuiContext guiContext) {
410411
this.guiContext = guiContext;
411412
}
412-
}
413+
}

0 commit comments

Comments
 (0)