use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.
the class AbstractGuiTextArea method mouseClick.
@Override
public boolean mouseClick(ReadablePoint position, int button) {
if (getContainer() != null) {
getContainer().convertFor(this, (Point) (position = new Point(position)));
}
boolean hovering = isMouseHovering(position);
if (hovering && isFocused() && button == 0) {
updateCurrentOffset();
int mouseX = position.getX() - BORDER;
int mouseY = position.getY() - BORDER;
TextRenderer fontRenderer = MCVer.getFontRenderer();
int textY = clamp(mouseY / (fontRenderer.fontHeight + LINE_SPACING) + currentYOffset, 0, text.length - 1);
if (cursorY != textY) {
currentXOffset = 0;
}
String line = text[textY].substring(currentXOffset);
int textX = fontRenderer.trimToWidth(line, mouseX).length() + currentXOffset;
setCursorPosition(textX, textY);
}
setFocused(hovering);
return hovering;
}
use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.
the class AbstractGuiTextArea method updateCurrentOffset.
private void updateCurrentOffset() {
currentXOffset = Math.min(currentXOffset, cursorX);
String line = text[cursorY].substring(currentXOffset, cursorX);
TextRenderer fontRenderer = MCVer.getFontRenderer();
int currentWidth = fontRenderer.getWidth(line);
if (currentWidth > size.getWidth() - BORDER * 2) {
currentXOffset = cursorX - fontRenderer.trimToWidth(line, size.getWidth() - BORDER * 2, true).length();
}
currentYOffset = Math.min(currentYOffset, cursorY);
int lineHeight = MCVer.getFontRenderer().fontHeight + LINE_SPACING;
int contentHeight = size.getHeight() - BORDER * 2;
int maxLines = contentHeight / lineHeight;
if (cursorY - currentYOffset >= maxLines) {
currentYOffset = cursorY - maxLines + 1;
}
}
use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.
the class AbstractGuiLabel method draw.
@Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
super.draw(renderer, size, renderInfo);
TextRenderer fontRenderer = MCVer.getFontRenderer();
// #if MC>=11600
List<String> lines = fontRenderer.getTextHandler().wrapLines(new LiteralText(text), size.getWidth(), Style.EMPTY).stream().map(it -> it.visit(Optional::of)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
// #else
// $$ @SuppressWarnings("unchecked")
// $$ List<String> lines = fontRenderer.wrapStringToWidthAsList(text, size.getWidth());
// #endif
int y = 0;
for (String line : lines) {
renderer.drawString(0, y, isEnabled() ? color : disabledColor, line);
y += fontRenderer.fontHeight;
}
}
use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.
the class AbstractGuiTextField method updateCurrentOffset.
/**
* Update current text offset to make sure the cursor is always visible.
*/
private void updateCurrentOffset() {
currentOffset = Math.min(currentOffset, cursorPos);
String line = text.substring(currentOffset, cursorPos);
TextRenderer fontRenderer = MCVer.getFontRenderer();
int currentWidth = fontRenderer.getWidth(line);
if (currentWidth > size.getWidth() - 2 * BORDER) {
currentOffset = cursorPos - fontRenderer.trimToWidth(line, size.getWidth() - 2 * BORDER, true).length();
}
}
use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.
the class AbstractGuiTextField method draw.
@Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
this.size = size;
updateCurrentOffset();
super.draw(renderer, size, renderInfo);
int width = size.getWidth(), height = size.getHeight();
TextRenderer fontRenderer = MCVer.getFontRenderer();
int posY = height / 2 - fontRenderer.fontHeight / 2;
// Draw black rect once pixel smaller than gray rect
renderer.drawRect(0, 0, width, height, isFocused() ? ReadableColor.WHITE : BORDER_COLOR);
renderer.drawRect(1, 1, width - 2, height - 2, ReadableColor.BLACK);
if (text.isEmpty() && !isFocused() && !Strings.isNullOrEmpty(hint)) {
// Draw hint
String text = fontRenderer.trimToWidth(hint, width - 2 * BORDER);
renderer.drawString(BORDER, posY, textColorDisabled, text);
} else {
// Draw text
String renderText = text.substring(currentOffset);
renderText = fontRenderer.trimToWidth(renderText, width - 2 * BORDER);
ReadableColor color = isEnabled() ? textColorEnabled : textColorDisabled;
int lineEnd = renderer.drawString(BORDER, height / 2 - fontRenderer.fontHeight / 2, color, renderText);
// Draw selection
int from = getSelectionFrom();
int to = getSelectionTo();
String leftStr = renderText.substring(0, clamp(from - currentOffset, 0, renderText.length()));
String rightStr = renderText.substring(clamp(to - currentOffset, 0, renderText.length()));
int left = BORDER + fontRenderer.getWidth(leftStr);
int right = lineEnd - fontRenderer.getWidth(rightStr) - 1;
renderer.invertColors(right, height - 2, left, 2);
// Draw cursor
if (blinkCursorTick / 6 % 2 == 0 && focused) {
String beforeCursor = renderText.substring(0, cursorPos - currentOffset);
int posX = BORDER + fontRenderer.getWidth(beforeCursor);
if (cursorPos == text.length()) {
renderer.drawString(posX, posY, CURSOR_COLOR, "_", true);
} else {
renderer.drawRect(posX, posY - 1, 1, 1 + fontRenderer.fontHeight, CURSOR_COLOR);
}
}
}
}
Aggregations