The_Typholorian

https://www.reddit.com/r/javahelp/comments/18w1akm/capitalize_non_az_characters_javafx_custom_text/

Jan 1st, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package net.typho.pnegative.utils;
  2.  
  3. import javafx.geometry.Point2D;
  4. import javafx.geometry.Rectangle2D;
  5. import javafx.geometry.VPos;
  6. import javafx.scene.canvas.GraphicsContext;
  7. import javafx.scene.input.KeyCode;
  8. import javafx.scene.input.KeyEvent;
  9. import javafx.scene.text.Font;
  10. import javafx.scene.text.TextAlignment;
  11. import net.typho.pnegative.PNegative;
  12. import net.typho.pnegative.Palette;
  13.  
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.Locale;
  17.  
  18. public class TextBox implements SelectableUIElement {
  19.     private Box box;
  20.     private List<String> text;
  21.     private Font font;
  22.     private int cursorPos;
  23.  
  24.     public TextBox(Box box, Font font) {
  25.         this.box = box;
  26.         this.font = font;
  27.         text = new ArrayList<>();
  28.         cursorPos = 0;
  29.     }
  30.  
  31.     @Override
  32.     public void draw(GraphicsContext gc) {
  33.         Rectangle2D bounds = box.get(PNegative.uiScale);
  34.  
  35.         gc.setFill(Palette.a4());
  36.         gc.fillRect(bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), bounds.getHeight());
  37.  
  38.         gc.setFill(Palette.a5());
  39.         gc.fillRect(bounds.getMinX() + PNegative.uiScale, bounds.getMinY() + PNegative.uiScale, bounds.getWidth() - PNegative.uiScale * 2, bounds.getHeight() - PNegative.uiScale * 2);
  40.  
  41.         StringBuilder text = new StringBuilder();
  42.  
  43.         for (String s : this.text) {
  44.             text.append(s);
  45.         }
  46.  
  47.         gc.setFill(Palette.b2());
  48.         gc.setFont(new Font(font.getName(), font.getSize() * PNegative.uiScale));
  49.         gc.setTextAlign(TextAlignment.LEFT);
  50.         gc.setTextBaseline(VPos.CENTER);
  51.         gc.fillText(String.valueOf(text), bounds.getMinX() + PNegative.uiScale * 2, (bounds.getMinY() + bounds.getHeight() / 2));
  52.     }
  53.  
  54.     @Override
  55.     public boolean contains(Point2D point) {
  56.         return box.get(PNegative.uiScale).contains(point);
  57.     }
  58.  
  59.     @Override
  60.     public void handle(KeyEvent event) {
  61.         switch (event.getCode()) {
  62.             case ENTER:
  63.                 PNegative.selectedItem = null;
  64.                 return;
  65.             case LEFT:
  66.                 if (cursorPos - 1 >= 0) {
  67.                     cursorPos -= 1;
  68.                 }
  69.                 return;
  70.             case RIGHT:
  71.                 if (cursorPos + 1 <= text.size() - 1) {
  72.                     cursorPos += 1;
  73.                 }
  74.                 return;
  75.             case BACK_SPACE:
  76.                 if (cursorPos - 1 >= 0) {
  77.                     text.remove(cursorPos - 1);
  78.                     cursorPos -= 1;
  79.                 }
  80.                 return;
  81.         }
  82.  
  83.         int c = event.getCode().getCode();
  84.  
  85.         if (c >= 33 && c <= 126) {
  86.             if (event.isShiftDown()) {
  87.                 text.add(cursorPos, Character.toString(Character.toUpperCase(c)));
  88.             } else {
  89.                 text.add(cursorPos, Character.toString(Character.toLowerCase(c)));
  90.             }
  91.  
  92.             cursorPos += 1;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment