addded input box
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
package org.saturnclient.saturnclient.menus;
|
||||
|
||||
import org.saturnclient.ui.SaturnUi;
|
||||
import org.saturnclient.ui.widgets.SaturnButton;
|
||||
import org.saturnclient.ui.widgets.SaturnImageButton;
|
||||
import org.saturnclient.ui.widgets.SaturnInputBox;
|
||||
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
public class SettingsMenu extends SaturnUi {
|
||||
public boolean isOpen = false;
|
||||
public static Identifier searchTexture = Identifier.of("saturnclient", "textures/gui/components/search.png");
|
||||
public static Identifier closeTexture = Identifier.of("saturnclient", "textures/gui/components/close.png");
|
||||
public SaturnImageButton searchButton;
|
||||
public SaturnInputBox searchInputBox;
|
||||
|
||||
public SettingsMenu() {
|
||||
super(Text.literal("Settings"));
|
||||
@@ -19,17 +19,19 @@ public class SettingsMenu extends SaturnUi {
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
searchInputBox = new SaturnInputBox("Search", 10, 10, 100, 20);
|
||||
searchInputBox.visible = false;
|
||||
draw(searchInputBox);
|
||||
|
||||
searchButton = (SaturnImageButton) new SaturnImageButton(searchTexture, 10,
|
||||
10,
|
||||
() -> {
|
||||
isOpen = !isOpen;
|
||||
if (isOpen) {
|
||||
searchInputBox.visible = !searchInputBox.visible;
|
||||
if (searchInputBox.visible) {
|
||||
searchButton.sprite = closeTexture;
|
||||
draw(new SaturnButton("ABC", () -> {
|
||||
}).setWidth(30).setHeight(30).setX(70).setY(70));
|
||||
} else {
|
||||
searchInputBox.clearText();
|
||||
searchButton.sprite = searchTexture;
|
||||
widgets.remove(widgets.size() - 1);
|
||||
}
|
||||
}).setWidth(30).setHeight(30).setX(30).setY(30);
|
||||
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package org.saturnclient.ui.widgets;
|
||||
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.saturnclient.ui.SaturnWidget;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.font.TextRenderer;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.render.RenderLayer;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.ColorHelper;
|
||||
|
||||
public class SaturnInputBox extends SaturnWidget {
|
||||
public static final Identifier TEXTURE = Identifier.ofVanilla("widget/saturn_button");
|
||||
|
||||
public String text = "";
|
||||
public String placeholder = "";
|
||||
public int cursorPosition = 0;
|
||||
public boolean focused = false;
|
||||
private TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
|
||||
|
||||
public SaturnInputBox(String placeholder, int x, int y, int width, int height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.placeholder = placeholder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
focused = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void charTyped(char chr) {
|
||||
if (focused) {
|
||||
text = text.substring(0, cursorPosition) + chr
|
||||
+ text.substring(cursorPosition);
|
||||
|
||||
cursorPosition++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(int keyCode, int scanCode, int modifiers) {
|
||||
if (focused) {
|
||||
if (keyCode == GLFW.GLFW_KEY_BACKSPACE) {
|
||||
if (cursorPosition > 0) {
|
||||
text = text.substring(0, cursorPosition - 1) + text.substring(cursorPosition);
|
||||
cursorPosition--;
|
||||
}
|
||||
} else if (keyCode == GLFW.GLFW_KEY_LEFT) {
|
||||
if (cursorPosition > 0) {
|
||||
cursorPosition--;
|
||||
}
|
||||
} else if (keyCode == GLFW.GLFW_KEY_RIGHT) {
|
||||
if (cursorPosition < text.length()) {
|
||||
cursorPosition++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(DrawContext context, boolean hovering) {
|
||||
context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURE, this.x,
|
||||
this.y, this.width, this.height, ColorHelper.getWhite(this.alpha));
|
||||
|
||||
int scrollOffset = getScrollOffset();
|
||||
String visibleText = text.substring(scrollOffset);
|
||||
|
||||
int textColor = focused ? 0xFFFFFF : 0xAAAAAA;
|
||||
context.drawText(textRenderer, visibleText, x + 5, y + 5, textColor, false);
|
||||
|
||||
if (focused) {
|
||||
int cursorX = x + 5 + textRenderer.getWidth(visibleText.substring(0, cursorPosition - scrollOffset));
|
||||
context.fill(cursorX, y + 3, cursorX + 1, y + height - 3, 0xFFFFFFFF);
|
||||
}
|
||||
}
|
||||
|
||||
public void clearText() {
|
||||
text = "";
|
||||
cursorPosition = 0;
|
||||
}
|
||||
|
||||
private int getScrollOffset() {
|
||||
if (text.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int offset = 0;
|
||||
|
||||
if (cursorPosition == text.length()) {
|
||||
while (textRenderer.getWidth(text.substring(offset)) > width) {
|
||||
offset++;
|
||||
}
|
||||
} else {
|
||||
while (textRenderer.getWidth(text.substring(offset, cursorPosition)) > width) {
|
||||
offset++;
|
||||
}
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user