Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.typho.pnegative.utils;
- import javafx.geometry.Point2D;
- import javafx.geometry.Rectangle2D;
- import javafx.geometry.VPos;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.input.KeyCode;
- import javafx.scene.input.KeyEvent;
- import javafx.scene.text.Font;
- import javafx.scene.text.TextAlignment;
- import net.typho.pnegative.PNegative;
- import net.typho.pnegative.Palette;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Locale;
- public class TextBox implements SelectableUIElement {
- private Box box;
- private List<String> text;
- private Font font;
- private int cursorPos;
- public TextBox(Box box, Font font) {
- this.box = box;
- this.font = font;
- text = new ArrayList<>();
- cursorPos = 0;
- }
- @Override
- public void draw(GraphicsContext gc) {
- Rectangle2D bounds = box.get(PNegative.uiScale);
- gc.setFill(Palette.a4());
- gc.fillRect(bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), bounds.getHeight());
- gc.setFill(Palette.a5());
- gc.fillRect(bounds.getMinX() + PNegative.uiScale, bounds.getMinY() + PNegative.uiScale, bounds.getWidth() - PNegative.uiScale * 2, bounds.getHeight() - PNegative.uiScale * 2);
- StringBuilder text = new StringBuilder();
- for (String s : this.text) {
- text.append(s);
- }
- gc.setFill(Palette.b2());
- gc.setFont(new Font(font.getName(), font.getSize() * PNegative.uiScale));
- gc.setTextAlign(TextAlignment.LEFT);
- gc.setTextBaseline(VPos.CENTER);
- gc.fillText(String.valueOf(text), bounds.getMinX() + PNegative.uiScale * 2, (bounds.getMinY() + bounds.getHeight() / 2));
- }
- @Override
- public boolean contains(Point2D point) {
- return box.get(PNegative.uiScale).contains(point);
- }
- @Override
- public void handle(KeyEvent event) {
- switch (event.getCode()) {
- case ENTER:
- PNegative.selectedItem = null;
- return;
- case LEFT:
- if (cursorPos - 1 >= 0) {
- cursorPos -= 1;
- }
- return;
- case RIGHT:
- if (cursorPos + 1 <= text.size() - 1) {
- cursorPos += 1;
- }
- return;
- case BACK_SPACE:
- if (cursorPos - 1 >= 0) {
- text.remove(cursorPos - 1);
- cursorPos -= 1;
- }
- return;
- }
- int c = event.getCode().getCode();
- if (c >= 33 && c <= 126) {
- if (event.isShiftDown()) {
- text.add(cursorPos, Character.toString(Character.toUpperCase(c)));
- } else {
- text.add(cursorPos, Character.toString(Character.toLowerCase(c)));
- }
- cursorPos += 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment