├── src ├── util │ └── ClipboardUtil.java ├── api │ ├── IScreenBasic.java │ └── Widget.java ├── ImperadorLabel.java ├── ImperadorButton.java └── ImperadorEntryBox.java └── README.md /src/util/ClipboardUtil.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import java.awt.*; 4 | import java.awt.datatransfer.*; 5 | import java.io.IOException; 6 | 7 | /** 8 | * @author SrRina 9 | * @since 28/08/2021 at 19:05 10 | **/ 11 | public class ClipboardUtil { 12 | public static String get() { 13 | final java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 14 | final Transferable content = clipboard.getContents(null); 15 | 16 | if (content != null && content.isDataFlavorSupported(DataFlavor.stringFlavor)) { 17 | try { 18 | return content.getTransferData(DataFlavor.stringFlavor).toString(); 19 | } catch (UnsupportedFlavorException | IOException exc) { 20 | return null; 21 | } 22 | } 23 | 24 | return null; 25 | } 26 | 27 | public static void set(final String string) { 28 | final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 29 | 30 | clipboard.setContents(new StringSelection(string), null); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/api/IScreenBasic.java: -------------------------------------------------------------------------------- 1 | package api; 2 | 3 | import me.rina.turok.util.TurokRect; 4 | 5 | /** 6 | * @author SrRina 7 | * @since 17/11/20 at 11:12am 8 | */ 9 | public interface IScreenBasic { 10 | boolean isEnabled(); 11 | 12 | /* 13 | * Get rect instance. 14 | */ 15 | TurokRect getRect(); 16 | 17 | /* 18 | * Verify if booleans focus; 19 | */ 20 | boolean verifyFocus(int mx, int my); 21 | 22 | /* 23 | * Event opened on screen. 24 | */ 25 | void onScreenOpened(); 26 | void onCustomScreenOpened(); 27 | 28 | /* 29 | * Event closed on screen. 30 | */ 31 | void onScreenClosed(); 32 | void onCustomScreenClosed(); 33 | 34 | /* 35 | * Keyboard input. 36 | */ 37 | void onKeyboardPressed(char c, int keyCode); 38 | void onCustomKeyboardPressed(char c, int keyCode); 39 | 40 | /* 41 | * Mouse clicks down. 42 | */ 43 | void onMouseClicked(int button); 44 | void onCustomMouseClicked(int button); 45 | 46 | /* 47 | * Mouse clicks up. 48 | */ 49 | void onMouseReleased(int button); 50 | void onCustomMouseReleased(int button); 51 | 52 | /* 53 | * Render ticks. 54 | */ 55 | void onRender(); 56 | void onCustomRender(); 57 | 58 | /* 59 | * Saving!! 60 | */ 61 | void onSave(); 62 | void onLoad(); 63 | } -------------------------------------------------------------------------------- /src/api/Widget.java: -------------------------------------------------------------------------------- 1 | package api; 2 | 3 | import me.rina.turok.util.TurokRect; 4 | 5 | /** 6 | * @author SrRina 7 | * @since 17/11/20 at 11:12am 8 | */ 9 | public class Widget implements IScreenBasic { 10 | public TurokRect rect; 11 | 12 | public Widget(String tag) { 13 | this.rect = new TurokRect(tag, 0, 0); 14 | } 15 | 16 | @Override 17 | public boolean isEnabled() { 18 | return false; 19 | } 20 | 21 | @Override 22 | public TurokRect getRect() { 23 | return this.rect; 24 | } 25 | 26 | @Override 27 | public boolean verifyFocus(int mx, int my) { 28 | return false; 29 | } 30 | 31 | @Override 32 | public void onScreenOpened() { 33 | 34 | } 35 | 36 | @Override 37 | public void onCustomScreenOpened() { 38 | 39 | } 40 | 41 | @Override 42 | public void onScreenClosed() { 43 | 44 | } 45 | 46 | @Override 47 | public void onCustomScreenClosed() { 48 | 49 | } 50 | 51 | @Override 52 | public void onKeyboardPressed(char charCode, int keyCode) { 53 | 54 | } 55 | 56 | @Override 57 | public void onCustomKeyboardPressed(char charCode, int keyCode) { 58 | 59 | } 60 | 61 | @Override 62 | public void onMouseClicked(int button) { 63 | 64 | } 65 | 66 | @Override 67 | public void onCustomMouseClicked(int button) { 68 | 69 | } 70 | 71 | @Override 72 | public void onMouseReleased(int button) { 73 | 74 | } 75 | 76 | @Override 77 | public void onCustomMouseReleased(int button) { 78 | 79 | } 80 | 81 | @Override 82 | public void onRender() { 83 | 84 | } 85 | 86 | @Override 87 | public void onCustomRender() { 88 | 89 | } 90 | 91 | @Override 92 | public void onSave() { 93 | 94 | } 95 | 96 | @Override 97 | public void onLoad() { 98 | 99 | } 100 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Imperador-Widgets 2 | 3 | What is this? Imperador widgets is an entry box source code, it has copy | paste | delete + words | backward + words | select | scroll | type 4 | 5 | How to use it? This was developed into Minecraft context, but you can look the code and use it how you want. 6 | 7 | --- 8 | 9 | For you implement in your game/client/project, remember, the code use OpenGL, the scissor! 10 | You can replace the all render methods on the code with your own classes! 11 | 12 | # Configuration 13 | 14 | ```java 15 | final ImperadorEntryBox entry = new ImperadorEntryBox(new TurokFont(new Font(...)), "default_text"); 16 | 17 | entry.scissor(); // For you sync automatically the space. 18 | // Or you can edit the scissor! 19 | entry.getScissor(); // Its a TurokRect class! 20 | // The scissor works for label, button & entry. 21 | 22 | // For you set the size of entry/label/button! 23 | entry.getRect(); 24 | 25 | // The label/button have center, left & right methods; 26 | label.center(); 27 | label.left(int offset); 28 | label.right(int offset); 29 | // Or edit. 30 | label.setOffsetX(int v); 31 | 32 | // You can modify the string offset Y! 33 | label.setOffsetY(int v); 34 | 35 | // The entry box have a Save entry! 36 | final String typing = entry.getText(); // The current typed! 37 | final String last = entry.getSave(); // The last text before unfocus! 38 | 39 | ``` 40 | 41 | # Events 42 | 43 | ```java 44 | final TurokMouse mouse = new TurokMouse(mousePositionX, mousePositionY); 45 | 46 | // For entry box, you always need update the scroll! 47 | entry.doMouseScroll(mouse); 48 | 49 | // For you sync the mouse over of entry/button; 50 | button.doMouseOver(mouse); 51 | 52 | // The event click for button & entry is the mouseClicked(int mx, int my, int button) (Minecraft GUI); 53 | button.onMouseClicked(button); 54 | entry.onMouseClicked(button); 55 | // Remember do onMouseReleased in mouseReleased (Minecraft GUI); 56 | entry.doSetIndexAB(button); // For sync the entry box selections. 57 | 58 | // For entry box keytyped, the keyTyped method in (Minecraft GUI); 59 | entry.onKeyboardPressed(char character, int key); 60 | 61 | ``` 62 | -------------------------------------------------------------------------------- /src/ImperadorLabel.java: -------------------------------------------------------------------------------- 1 | // package 2 | 3 | import me.rina.turok.render.font.TurokFont; 4 | import me.rina.turok.render.font.management.TurokFontManager; 5 | import me.rina.turok.render.opengl.TurokGL; 6 | import me.rina.turok.render.opengl.TurokRenderGL; 7 | import me.rina.turok.render.opengl.TurokShaderGL; 8 | import me.rina.turok.util.TurokRect; 9 | import org.lwjgl.opengl.GL11; 10 | import api.Widget; 11 | 12 | import java.awt.*; 13 | 14 | /** 15 | * @author SrRina 16 | * @since 13/08/2021 at 00:58 17 | **/ 18 | public class ImperadorLabel extends Widget { 19 | private String text; 20 | 21 | public int[] background = new int[] {0, 0, 0, 0}; 22 | public int[] string = new int[] {255, 255, 255, 255}; 23 | 24 | private boolean isShadow; 25 | private boolean isRendering; 26 | 27 | private float offsetX; 28 | private float offsetY; 29 | 30 | private float offsetW; 31 | private float offsetH; 32 | 33 | private TurokFont font; 34 | private final TurokRect scissor = new TurokRect(0, 0); 35 | 36 | public ImperadorLabel(TurokFont font, String text) { 37 | super("Imperador:Label"); 38 | 39 | this.font = font; 40 | this.text = text; 41 | 42 | this.setShadow(true); 43 | this.setOffsetY(3f); 44 | } 45 | 46 | public TurokRect getScissor() { 47 | return scissor; 48 | } 49 | 50 | public void setFont(TurokFont font) { 51 | this.font = font; 52 | } 53 | 54 | public TurokFont getFont() { 55 | return font; 56 | } 57 | 58 | public void setText(String text) { 59 | this.text = text; 60 | } 61 | 62 | public String getText() { 63 | return text; 64 | } 65 | 66 | public void setShadow(boolean shadow) { 67 | this.isShadow = shadow; 68 | } 69 | 70 | public void setRendering(boolean rendering) { 71 | isRendering = rendering; 72 | } 73 | 74 | public boolean isRendering() { 75 | return isRendering; 76 | } 77 | 78 | public boolean isShadow() { 79 | return isShadow; 80 | } 81 | 82 | public void setOffsetX(float offsetX) { 83 | this.offsetX = offsetX; 84 | } 85 | 86 | public float getOffsetX() { 87 | return offsetX; 88 | } 89 | 90 | public void setOffsetY(float offsetY) { 91 | this.offsetY = offsetY; 92 | } 93 | 94 | public float getOffsetY() { 95 | return offsetY; 96 | } 97 | 98 | public void setOffsetW(float offsetW) { 99 | this.offsetW = offsetW; 100 | } 101 | 102 | public float getOffsetW() { 103 | return offsetW; 104 | } 105 | 106 | public void setOffsetH(float offsetH) { 107 | this.offsetH = offsetH; 108 | } 109 | 110 | public float getOffsetH() { 111 | return offsetH; 112 | } 113 | 114 | public void scissor() { 115 | this.scissor.copy(this.rect); 116 | } 117 | 118 | public void center() { 119 | this.offsetX = (this.rect.getWidth() / 2f) - (TurokFontManager.getStringWidth(this.font, this.text) / 2f); 120 | } 121 | 122 | public void left(float difference) { 123 | this.offsetX = difference; 124 | } 125 | 126 | public void right(float difference) { 127 | this.offsetX = this.rect.getWidth() - TurokFontManager.getStringWidth(this.font, this.text) - difference; 128 | } 129 | 130 | @Override 131 | public void onScreenClosed() { 132 | 133 | } 134 | 135 | @Override 136 | public void onCustomScreenClosed() { 137 | 138 | } 139 | 140 | @Override 141 | public void onScreenOpened() { 142 | 143 | } 144 | 145 | @Override 146 | public void onCustomScreenOpened() { 147 | 148 | } 149 | 150 | @Override 151 | public void onKeyboardPressed(char character, int key) { 152 | 153 | } 154 | 155 | @Override 156 | public void onCustomKeyboardPressed(char character, int key) { 157 | 158 | } 159 | 160 | @Override 161 | public void onMouseReleased(int button) { 162 | 163 | } 164 | 165 | @Override 166 | public void onCustomMouseReleased(int button) { 167 | 168 | } 169 | 170 | @Override 171 | public void onMouseClicked(int button) { 172 | 173 | } 174 | 175 | @Override 176 | public void onCustomMouseClicked(int button) { 177 | 178 | } 179 | 180 | @Override 181 | public void onRender() { 182 | if (this.isRendering()) { 183 | return; 184 | } 185 | 186 | final boolean flag = !GL11.glIsEnabled(GL11.GL_SCISSOR_TEST); 187 | 188 | TurokGL.color(TurokGL.arrayColorToColorClass(this.background)); 189 | TurokRenderGL.drawSolidRect(this.rect); 190 | 191 | if (flag) { 192 | GL11.glEnable(GL11.GL_SCISSOR_TEST); 193 | } 194 | 195 | final Color color = TurokGL.arrayColorToColorClass(this.string); 196 | 197 | TurokShaderGL.drawScissor(this.scissor); 198 | TurokFontManager.render(this.font, this.text, this.rect.getX() + this.getOffsetX(), this.rect.getY() + this.getOffsetY(), this.isShadow(), color); 199 | 200 | if (flag) { 201 | GL11.glDisable(GL11.GL_SCISSOR_TEST); 202 | } 203 | } 204 | 205 | @Override 206 | public void onCustomRender() { 207 | 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /src/ImperadorButton.java: -------------------------------------------------------------------------------- 1 | // package 2 | 3 | import me.rina.turok.hardware.mouse.TurokMouse; 4 | import me.rina.turok.render.font.TurokFont; 5 | import me.rina.turok.render.font.management.TurokFontManager; 6 | import me.rina.turok.render.opengl.TurokGL; 7 | import me.rina.turok.render.opengl.TurokRenderGL; 8 | import me.rina.turok.render.opengl.TurokShaderGL; 9 | import me.rina.turok.util.TurokMath; 10 | import me.rina.turok.util.TurokRect; 11 | import org.lwjgl.opengl.GL11; 12 | import api.Widget; 13 | 14 | import java.awt.*; 15 | 16 | /** 17 | * @author SrRina 18 | * @since 13/08/2021 at 20:38 19 | **/ 20 | public class ImperadorButton extends Widget { 21 | private String text; 22 | 23 | public int[] string = new int[] {255, 255, 255, 255}; 24 | public int[] pressed = new int[] {255, 255, 255, 150}; 25 | public int[] highlight = new int[] {255, 255, 255, 200}; 26 | public int[] outline = new int[] {255, 255, 255, 200}; 27 | public int[] background = new int[] {0, 0, 0, 0}; 28 | 29 | public float pressedAlpha; 30 | public float lastTickPressedAlpha; 31 | 32 | public float highlightAlpha; 33 | public float lastTickHighlightAlpha; 34 | 35 | public float outlineAlpha; 36 | public float lastTickOutlineAlpha; 37 | 38 | private float partialTicks = 1f; 39 | 40 | private boolean isShadow; 41 | private boolean isRendering; 42 | private boolean isToDrawOutline; 43 | 44 | private boolean isPressed; 45 | private boolean isReleased; 46 | private boolean isMouseOver; 47 | 48 | private float offsetX; 49 | private float offsetY; 50 | 51 | private float offsetW; 52 | private float offsetH; 53 | 54 | private TurokFont font; 55 | private final TurokRect scissor = new TurokRect(0, 0); 56 | 57 | public ImperadorButton(TurokFont font, String text) { 58 | super("Imperador:Button"); 59 | 60 | this.font = font; 61 | this.text = text; 62 | 63 | this.setIsShadow(true); 64 | } 65 | 66 | public TurokRect getScissor() { 67 | return scissor; 68 | } 69 | 70 | public void setPartialTicks(float partialTicks) { 71 | this.partialTicks = partialTicks; 72 | } 73 | 74 | public float getPartialTicks() { 75 | return partialTicks; 76 | } 77 | 78 | public void setFont(TurokFont font) { 79 | this.font = font; 80 | } 81 | 82 | public TurokFont getFont() { 83 | return font; 84 | } 85 | 86 | public void setText(String text) { 87 | this.text = text; 88 | } 89 | 90 | public String getText() { 91 | return text; 92 | } 93 | 94 | public void setMouseOver(boolean isMouseOver) { 95 | this.isMouseOver = isMouseOver; 96 | } 97 | 98 | public boolean isMouseOver() { 99 | return isMouseOver; 100 | } 101 | 102 | public void setIsShadow(boolean isShadow) { 103 | this.isShadow = isShadow; 104 | } 105 | 106 | public boolean getIsShadow() { 107 | return isShadow; 108 | } 109 | 110 | public void setRendering(boolean rendering) { 111 | isRendering = rendering; 112 | } 113 | 114 | public boolean isRendering() { 115 | return isRendering; 116 | } 117 | 118 | public void setPressed(boolean pressed) { 119 | this.isPressed = pressed; 120 | } 121 | 122 | public boolean isPressed() { 123 | return isPressed; 124 | } 125 | 126 | public void setReleased(boolean released) { 127 | isReleased = released; 128 | } 129 | 130 | public boolean isReleased() { 131 | return isReleased; 132 | } 133 | 134 | public void setToDrawOutline(boolean toDrawOutline) { 135 | isToDrawOutline = toDrawOutline; 136 | } 137 | 138 | public boolean isToDrawOutline() { 139 | return isToDrawOutline; 140 | } 141 | 142 | public void setOffsetX(float offsetX) { 143 | this.offsetX = offsetX; 144 | } 145 | 146 | public float getOffsetX() { 147 | return offsetX; 148 | } 149 | 150 | public void setOffsetY(float offsetY) { 151 | this.offsetY = offsetY; 152 | } 153 | 154 | public float getOffsetY() { 155 | return offsetY; 156 | } 157 | 158 | public void setOffsetW(float offsetW) { 159 | this.offsetW = offsetW; 160 | } 161 | 162 | public float getOffsetW() { 163 | return offsetW; 164 | } 165 | 166 | public void setOffsetH(float offsetH) { 167 | this.offsetH = offsetH; 168 | } 169 | 170 | public float getOffsetH() { 171 | return offsetH; 172 | } 173 | 174 | public void scissor() { 175 | this.scissor.copy(this.rect); 176 | } 177 | 178 | public void center() { 179 | this.offsetX = (this.rect.getWidth() / 2f) - (TurokFontManager.getStringWidth(this.font, this.text) / 2f); 180 | } 181 | 182 | public void left(float difference) { 183 | this.offsetX = difference; 184 | } 185 | 186 | public void right(float difference) { 187 | this.offsetX = this.rect.getWidth() - TurokFontManager.getStringWidth(this.font, this.text) - difference; 188 | } 189 | 190 | public void doMouseOver(TurokMouse mouse) { 191 | this.isMouseOver = this.rect.collideWithMouse(mouse) && this.isRendering(); 192 | } 193 | 194 | @Override 195 | public void onScreenClosed() { 196 | 197 | } 198 | 199 | @Override 200 | public void onCustomScreenClosed() { 201 | 202 | } 203 | 204 | @Override 205 | public void onScreenOpened() { 206 | 207 | } 208 | 209 | @Override 210 | public void onCustomScreenOpened() { 211 | 212 | } 213 | 214 | @Override 215 | public void onKeyboardPressed(char character, int key) { 216 | 217 | } 218 | 219 | @Override 220 | public void onCustomKeyboardPressed(char character, int key) { 221 | 222 | } 223 | 224 | @Override 225 | public void onMouseReleased(int button) { 226 | if (!this.isRendering()) { 227 | return; 228 | } 229 | 230 | if (this.isPressed()) { 231 | this.setReleased(this.isMouseOver()); 232 | this.setPressed(false); 233 | } 234 | } 235 | 236 | @Override 237 | public void onCustomMouseReleased(int button) { 238 | 239 | } 240 | 241 | @Override 242 | public void onMouseClicked(int button) { 243 | if (!this.isRendering()) { 244 | return; 245 | } 246 | 247 | if (this.isMouseOver() && (button == 0 || button == 1)) { 248 | this.setPressed(true); 249 | } 250 | } 251 | 252 | @Override 253 | public void onCustomMouseClicked(int button) { 254 | 255 | } 256 | 257 | @Override 258 | public void onRender() { 259 | if (!this.isRendering()) { 260 | return; 261 | } 262 | 263 | this.highlightAlpha = this.isMouseOver() ? this.highlight[3] : 0f; 264 | this.pressedAlpha = this.isPressed() ? this.pressed[3] : 0f; 265 | this.outlineAlpha = this.outline[3]; 266 | 267 | this.lastTickHighlightAlpha = TurokMath.lerp(this.lastTickHighlightAlpha, this.highlightAlpha, this.getPartialTicks()); 268 | this.lastTickPressedAlpha = TurokMath.lerp(this.lastTickPressedAlpha, this.pressedAlpha, this.getPartialTicks()); 269 | this.lastTickOutlineAlpha = TurokMath.lerp(this.lastTickOutlineAlpha, this.outlineAlpha, this.getPartialTicks()); 270 | 271 | final boolean flag = !GL11.glIsEnabled(GL11.GL_SCISSOR_TEST); 272 | 273 | TurokGL.color(TurokGL.arrayColorToColorClass(this.background)); 274 | TurokRenderGL.drawSolidRect(this.rect); 275 | 276 | if (this.lastTickOutlineAlpha != 0f && this.isToDrawOutline()) { 277 | TurokGL.color(this.outline[0], this.outline[1], this.outline[2], this.lastTickOutlineAlpha); 278 | TurokRenderGL.drawOutlineRect(this.rect); 279 | } 280 | 281 | if (this.lastTickHighlightAlpha != 0f) { 282 | TurokGL.color(this.highlight[0], this.highlight[1], this.highlight[2], this.lastTickHighlightAlpha); 283 | TurokRenderGL.drawSolidRect(this.rect); 284 | } 285 | 286 | if (this.lastTickPressedAlpha != 0f) { 287 | TurokGL.color(this.pressed[0], this.pressed[1], this.pressed[2], this.lastTickPressedAlpha); 288 | TurokRenderGL.drawSolidRect(this.rect); 289 | } 290 | 291 | if (flag) { 292 | GL11.glEnable(GL11.GL_SCISSOR_TEST); 293 | } 294 | 295 | final Color color = TurokGL.arrayColorToColorClass(this.string); 296 | 297 | TurokShaderGL.drawScissor(this.scissor); 298 | TurokFontManager.render(this.font, this.text, this.rect.getX() + this.getOffsetX(), this.rect.getY() + this.getOffsetY(), this.getIsShadow(), color); 299 | 300 | if (flag) { 301 | GL11.glDisable(GL11.GL_SCISSOR_TEST); 302 | } 303 | } 304 | 305 | @Override 306 | public void onCustomRender() { 307 | 308 | } 309 | } -------------------------------------------------------------------------------- /src/ImperadorEntryBox.java: -------------------------------------------------------------------------------- 1 | // package 2 | 3 | import me.rina.turok.hardware.mouse.TurokMouse; 4 | import me.rina.turok.render.font.TurokFont; 5 | import me.rina.turok.render.font.management.TurokFontManager; 6 | import me.rina.turok.render.opengl.TurokGL; 7 | import me.rina.turok.render.opengl.TurokRenderGL; 8 | import me.rina.turok.render.opengl.TurokShaderGL; 9 | import me.rina.turok.util.TurokMath; 10 | import me.rina.turok.util.TurokRect; 11 | import me.rina.turok.util.TurokTick; 12 | import net.minecraft.util.ChatAllowedCharacters; 13 | import org.apache.commons.lang3.CharUtils; 14 | import org.apache.commons.lang3.StringUtils; 15 | import org.lwjgl.input.Keyboard; 16 | import org.lwjgl.opengl.GL11; 17 | import api.Widget; 18 | import util.ClipboardUtil; 19 | 20 | import java.awt.*; 21 | 22 | /** 23 | * @author SrRina 24 | * @since 16/08/2021 at 14:50 25 | **/ 26 | public class ImperadorEntryBox extends Widget { 27 | private String text; 28 | private String save; 29 | 30 | private int indexA; 31 | private int indexB; 32 | 33 | public int[] string = new int[] {255, 255, 255, 255}; 34 | public int[] pressed = new int[] {255, 255, 255, 150}; 35 | public int[] focused = new int[] {255, 255, 255, 255}; 36 | public int[] outline = new int[] {255, 255, 255, 200}; 37 | public int[] background = new int[] {0, 0, 0, 0}; 38 | public int[] background_selected = new int[] {0, 0, 255, 100}; 39 | 40 | public float pressedAlpha; 41 | public float lastTickPressedAlpha; 42 | 43 | public float focusedAlpha; 44 | public float lastTickFocusedAlpha; 45 | 46 | public float outlineAlpha; 47 | public float lastTickOutlineAlpha; 48 | 49 | private float kerning = 0f; 50 | private float partialTicks = 1f; 51 | 52 | private boolean isShadow; 53 | private boolean isRendering; 54 | 55 | private boolean isToDrawOutline; 56 | private boolean isFocused; 57 | 58 | private boolean isSplitRendering; 59 | private boolean isMouseOver; 60 | 61 | private boolean isPressed; 62 | private boolean isReleased; 63 | 64 | private boolean isDragging; 65 | 66 | private float offsetX; 67 | private float offsetY; 68 | 69 | private float offsetW; 70 | private float offsetH; 71 | 72 | private float scroll; 73 | private TurokFont font; 74 | 75 | protected int lastIndexCurrent; 76 | protected float lastSize; 77 | 78 | private final TurokTick splitTick = new TurokTick(); 79 | private final TurokTick pressTick = new TurokTick(); 80 | 81 | private final TurokRect scissor = new TurokRect(0, 0); 82 | 83 | public ImperadorEntryBox(TurokFont font, String text) { 84 | super("Imperador:Entry:Box"); 85 | 86 | this.font = font; 87 | this.text = text; 88 | 89 | this.setIsShadow(true); 90 | } 91 | 92 | public TurokRect getScissor() { 93 | return scissor; 94 | } 95 | 96 | public void setKerning(float kerning) { 97 | this.kerning = kerning; 98 | } 99 | 100 | public float getKerning() { 101 | return kerning; 102 | } 103 | 104 | public void setPartialTicks(float partialTicks) { 105 | this.partialTicks = partialTicks; 106 | } 107 | 108 | public float getPartialTicks() { 109 | return partialTicks; 110 | } 111 | 112 | public void setFont(TurokFont font) { 113 | this.font = font; 114 | } 115 | 116 | public TurokFont getFont() { 117 | return font; 118 | } 119 | 120 | public void setText(String text) { 121 | this.text = text; 122 | } 123 | 124 | public String getText() { 125 | return text; 126 | } 127 | 128 | public boolean isEmpty() { 129 | if (this.text.isEmpty()) { 130 | return true; 131 | } 132 | 133 | boolean empty = true; 134 | 135 | for (String c : this.text.split("")) { 136 | if (!c.equals(" ")) { 137 | empty = false; 138 | 139 | break; 140 | } 141 | } 142 | 143 | return empty; 144 | } 145 | 146 | public void setSave(String save) { 147 | this.save = save; 148 | } 149 | 150 | public String getSave() { 151 | return save; 152 | } 153 | 154 | public void setMouseOver(boolean isMouseOver) { 155 | this.isMouseOver = isMouseOver; 156 | } 157 | 158 | public boolean isMouseOver() { 159 | return isMouseOver; 160 | } 161 | 162 | public void setIsShadow(boolean isShadow) { 163 | this.isShadow = isShadow; 164 | } 165 | 166 | public boolean getIsShadow() { 167 | return isShadow; 168 | } 169 | 170 | public void setSplitRendering(boolean splitRendering) { 171 | this.isSplitRendering = splitRendering; 172 | } 173 | 174 | public boolean isSplitRendering() { 175 | return isSplitRendering; 176 | } 177 | 178 | public void setRendering(boolean rendering) { 179 | isRendering = rendering; 180 | } 181 | 182 | public boolean isRendering() { 183 | return isRendering; 184 | } 185 | 186 | public void setPressed(boolean pressed) { 187 | this.isPressed = pressed; 188 | } 189 | 190 | public boolean isPressed() { 191 | return isPressed; 192 | } 193 | 194 | public void setReleased(boolean released) { 195 | isReleased = released; 196 | } 197 | 198 | public boolean isReleased() { 199 | return isReleased; 200 | } 201 | 202 | public void setToDrawOutline(boolean toDrawOutline) { 203 | isToDrawOutline = toDrawOutline; 204 | } 205 | 206 | public boolean isToDrawOutline() { 207 | return isToDrawOutline; 208 | } 209 | 210 | public void setFocused(boolean focused) { 211 | isFocused = focused; 212 | } 213 | 214 | public boolean isFocused() { 215 | return isFocused; 216 | } 217 | 218 | public void setDragging(boolean dragging) { 219 | isDragging = dragging; 220 | } 221 | 222 | public boolean isDragging() { 223 | return isDragging; 224 | } 225 | 226 | public void setOffsetX(float offsetX) { 227 | this.offsetX = offsetX; 228 | } 229 | 230 | public float getOffsetX() { 231 | return offsetX; 232 | } 233 | 234 | public void setOffsetY(float offsetY) { 235 | this.offsetY = offsetY; 236 | } 237 | 238 | public float getOffsetY() { 239 | return offsetY; 240 | } 241 | 242 | public void setOffsetW(float offsetW) { 243 | this.offsetW = offsetW; 244 | } 245 | 246 | public float getOffsetW() { 247 | return offsetW; 248 | } 249 | 250 | public void setOffsetH(float offsetH) { 251 | this.offsetH = offsetH; 252 | } 253 | 254 | public float getOffsetH() { 255 | return offsetH; 256 | } 257 | 258 | public void setIndexA(int indexA) { 259 | this.indexA = indexA; 260 | } 261 | 262 | public int getIndexA() { 263 | return indexA; 264 | } 265 | 266 | public void setIndexB(int indexB) { 267 | this.indexB = indexB; 268 | } 269 | 270 | public int getIndexB() { 271 | return indexB; 272 | } 273 | 274 | public float getLastSize() { 275 | return lastSize; 276 | } 277 | 278 | public void scissor() { 279 | this.scissor.copy(this.rect); 280 | } 281 | 282 | public void center() { 283 | this.offsetX = (this.rect.getWidth() / 2f) - (TurokFontManager.getStringWidth(this.getFont(), this.text) / 2f); 284 | } 285 | 286 | public void left(float difference) { 287 | this.offsetX = difference; 288 | } 289 | 290 | public void right(float difference) { 291 | this.offsetX = this.rect.getWidth() - TurokFontManager.getStringWidth(this.getFont(), this.text) - difference; 292 | } 293 | 294 | public int getIndexByPosition(float x) { 295 | int index = this.text.isEmpty() ? 0 : this.text.length(); 296 | int i = 0; 297 | 298 | float size = 0f; 299 | 300 | for (String c : this.text.split("")) { 301 | float w = TurokFontManager.getStringWidth(this.getFont(), c); 302 | 303 | if (this.isCollidingWithMouse((int) x, 0, size, w + this.getKerning())) { 304 | index = i; 305 | 306 | break; 307 | } 308 | 309 | size += w + this.getKerning(); 310 | i++; 311 | } 312 | 313 | return TurokMath.clamp(index, 0, this.text.length()); 314 | } 315 | 316 | public float getSizeByIndex(int index) { 317 | if (index == -1) { 318 | return 0; 319 | } 320 | 321 | int i = -1; 322 | float size = 0f; 323 | 324 | for (String c : this.text.split("")) { 325 | float w = TurokFontManager.getStringWidth(this.getFont(), c); 326 | 327 | if (i == index) { 328 | break; 329 | } 330 | 331 | size += w + this.getKerning(); 332 | i++; 333 | } 334 | 335 | return size; 336 | } 337 | 338 | public void doMouseOver(TurokMouse mouse) { 339 | this.isMouseOver = this.rect.collideWithMouse(mouse) && this.isRendering(); 340 | } 341 | 342 | public boolean isCollidingWithMouse(int mouseX, int mouseY, float s, float size) { 343 | return mouseX >= this.rect.getX() + this.offsetX + s && mouseX <= this.rect.getX() + this.offsetX + s + size; 344 | } 345 | 346 | public void doSetIndexAB(TurokMouse mouse) { 347 | if (!this.isMouseOver()) { 348 | return; 349 | } 350 | 351 | this.offsetW = TurokMath.clamp(mouse.getX(), this.rect.getX() + this.offsetX, this.rect.getX() + this.rect.getWidth()); 352 | this.offsetH = this.getIndexByPosition(this.offsetW); 353 | 354 | this.splitTick.reset(); 355 | 356 | int index = 0; 357 | 358 | if (this.text.isEmpty()) { 359 | this.setIndexA(index); 360 | this.setIndexB(index); 361 | 362 | return; 363 | } 364 | 365 | index = this.getIndexByPosition(mouse.getX()); 366 | 367 | if (index == -1) { 368 | index = this.text.length(); 369 | } 370 | 371 | this.setIndexA(index); 372 | this.setIndexB(index); 373 | } 374 | 375 | public void doMouseScroll(TurokMouse mouse) { 376 | float m = (this.rect.getWidth() - 2f) - TurokMath.min(this.lastSize, this.rect.getWidth()); 377 | float x = TurokMath.clamp(mouse.getX(), this.rect.getX() + this.offsetX, this.rect.getX() + this.rect.getWidth()); 378 | 379 | float size = 2f + this.getSizeByIndex(this.indexA); 380 | 381 | if (2f + this.lastSize < this.rect.getWidth()) { 382 | this.scroll = 2f; 383 | } 384 | 385 | if ((this.rect.getX() + this.offsetX + size <= this.rect.getX() + 2f || this.rect.getX() + this.offsetX + size >= this.rect.getX() + this.rect.getWidth()) && !this.isDragging()) { 386 | float value = this.rect.getWidth() - TurokMath.min(size, this.rect.getWidth()); 387 | 388 | this.scroll = value + (value >= 0 ? 2f : 0f); 389 | } 390 | 391 | if (this.scroll >= 2f) { 392 | this.scroll = 2f; 393 | } else if (this.scroll <= m) { 394 | this.scroll = m; 395 | } 396 | 397 | this.offsetX = TurokMath.lerp(this.offsetX, this.scroll, this.getPartialTicks()); 398 | 399 | if (!this.isDragging()) { 400 | this.lastIndexCurrent = -1; 401 | 402 | this.offsetW = x; 403 | this.offsetH = -1; 404 | 405 | return; 406 | } 407 | 408 | if (this.offsetH == -1) { 409 | return; 410 | } 411 | 412 | // If you did not know, the split is not rendered when you drag. 413 | this.splitTick.reset(); 414 | 415 | int indexCurrent = this.getIndexByPosition(x); 416 | 417 | if (indexCurrent != -1) { 418 | this.lastIndexCurrent = indexCurrent; 419 | } 420 | 421 | // Without the 250ms delay, the double click for select all never will works right. 422 | if (this.pressTick.isPassedMS(250)) { 423 | if (this.lastIndexCurrent > this.offsetH) { 424 | this.indexA = (int) this.offsetH; 425 | this.indexB = this.lastIndexCurrent; 426 | } else { 427 | this.indexA = this.lastIndexCurrent; 428 | this.indexB = (int) this.offsetH; 429 | } 430 | } 431 | 432 | if (mouse.getX() >= this.rect.getX() + this.rect.getWidth() - 1f && this.lastSize + 2f >= this.rect.getWidth()) { 433 | final float speed = (this.rect.getX() + this.rect.getWidth() - 1f) - mouse.getX(); 434 | 435 | this.scroll -= TurokMath.sqrt(speed * speed); 436 | } 437 | 438 | if (mouse.getX() <= this.rect.getX() + 1f) { 439 | final float speed = this.rect.getX() - mouse.getX(); 440 | 441 | this.scroll += TurokMath.sqrt(speed * speed); 442 | } 443 | } 444 | 445 | public float size() { 446 | if (this.text.isEmpty()) { 447 | return -1f; 448 | } 449 | 450 | float s = 0f; 451 | 452 | for (int i = 0; i < this.text.length(); i++) { 453 | final String c = CharUtils.toString(this.text.charAt(i)); 454 | final float w = TurokFontManager.getStringWidth(this.getFont(), c); 455 | 456 | s += w + this.getKerning(); 457 | } 458 | 459 | return s; 460 | } 461 | 462 | public void split(float k) { 463 | if (!this.splitTick.isPassedMS(500)) { 464 | this.setSplitRendering(true); 465 | } 466 | 467 | if (this.splitTick.isPassedMS(500)) { 468 | this.setSplitRendering(false); 469 | } 470 | 471 | if (this.splitTick.isPassedMS(1000)) { 472 | this.splitTick.reset(); 473 | } 474 | 475 | if (this.isSplitRendering()) { 476 | TurokGL.color(TurokGL.arrayColorToColorClass(this.string)); 477 | TurokRenderGL.drawSolidRect(this.rect.getX() + this.offsetX + k, this.rect.getY() + 1f, 1f, this.rect.getHeight() - 2f); 478 | } 479 | } 480 | 481 | @Override 482 | public void onScreenClosed() { 483 | 484 | } 485 | 486 | @Override 487 | public void onCustomScreenClosed() { 488 | 489 | } 490 | 491 | @Override 492 | public void onScreenOpened() { 493 | 494 | } 495 | 496 | @Override 497 | public void onCustomScreenOpened() { 498 | 499 | } 500 | 501 | @Override 502 | public void onKeyboardPressed(char character, int key) { 503 | try { 504 | if (this.isFocused()) { 505 | String cache = this.text; 506 | int a = this.indexA; 507 | 508 | switch (key) { 509 | case Keyboard.KEY_ESCAPE: { 510 | this.setFocused(false); 511 | 512 | break; 513 | } 514 | 515 | case Keyboard.KEY_RETURN: { 516 | this.setFocused(false); 517 | 518 | break; 519 | } 520 | 521 | case Keyboard.KEY_LEFT: { 522 | a--; 523 | 524 | break; 525 | } 526 | 527 | case Keyboard.KEY_RIGHT: { 528 | a++; 529 | 530 | break; 531 | } 532 | 533 | case Keyboard.KEY_BACK: { 534 | if (this.getIndexA() != this.getIndexB()) { 535 | String affectedBySubString = cache.substring(0, this.indexA) + cache.substring(this.indexB, this.text.length());; 536 | 537 | cache = affectedBySubString; 538 | 539 | this.indexB = this.indexA; 540 | 541 | break; 542 | } 543 | 544 | String memory; 545 | int skip = 0; 546 | 547 | if (Keyboard.isKeyDown(Keyboard.KEY_RCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_LCONTROL)) { 548 | memory = cache.substring(0, this.indexA); 549 | 550 | if (memory.length() != 0) { 551 | boolean foundFirstSpace = false; 552 | boolean foundLetter = false; 553 | 554 | int index = memory.length() - 1; 555 | 556 | for (int i = 0; i < memory.length(); i++) { 557 | index = memory.length() - 1 - i; 558 | 559 | String c = CharUtils.toString(memory.charAt(index)); 560 | 561 | if (!c.equals(" ")) { 562 | foundLetter = true; 563 | } 564 | 565 | if (c.equals(" ")) { 566 | if (foundLetter) { 567 | break; 568 | } 569 | 570 | if (foundFirstSpace) { 571 | break; 572 | } 573 | 574 | foundFirstSpace = true; 575 | } 576 | } 577 | 578 | for (String c : memory.substring(index, memory.length()).split("")) { 579 | skip++; 580 | } 581 | 582 | memory = memory.substring(0, index); 583 | } 584 | } else { 585 | memory = StringUtils.chop(cache.substring(0, this.indexA)); 586 | skip = 1; 587 | } 588 | 589 | cache = memory + cache.substring(this.indexA, this.text.length()); 590 | a = a - skip; 591 | 592 | break; 593 | } 594 | 595 | case Keyboard.KEY_DELETE: { 596 | if (this.getIndexA() != this.getIndexB()) { 597 | String affectedBySubString = cache.substring(0, this.indexA) + cache.substring(this.indexB, this.text.length()); 598 | 599 | cache = affectedBySubString; 600 | 601 | this.indexB = this.indexA; 602 | 603 | break; 604 | } 605 | 606 | String memory = cache.substring(this.indexA, this.text.length()); 607 | 608 | if (memory.length() != 0) { 609 | if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL)) { 610 | int i = 0; 611 | 612 | boolean foundFirstSpace = false; 613 | boolean foundLetter = false; 614 | 615 | for (String s : memory.split("")) { 616 | if (!s.equals(" ")) { 617 | foundLetter = true; 618 | } 619 | 620 | if (s.equals(" ")) { 621 | if (foundLetter) { 622 | break; 623 | } 624 | 625 | if (foundFirstSpace) { 626 | break; 627 | } 628 | 629 | foundFirstSpace = true; 630 | } 631 | 632 | i++; 633 | } 634 | 635 | memory = memory.substring(i, memory.length()); 636 | } else { 637 | memory = memory.substring(1); 638 | } 639 | 640 | cache = cache.substring(0, this.indexA) + memory; 641 | } 642 | 643 | break; 644 | } 645 | 646 | default: { 647 | if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL)) { 648 | if (key == Keyboard.KEY_A) { 649 | this.setIndexA(0); 650 | this.setIndexB(cache.length()); 651 | 652 | a = 0; 653 | } 654 | 655 | final String copy = ClipboardUtil.get(); 656 | 657 | if (key == Keyboard.KEY_V && copy != null) { 658 | for (String i : copy.split("")) { 659 | a++; 660 | } 661 | 662 | if (this.getIndexA() != this.getIndexB()) { 663 | cache = cache.substring(0, this.indexA) + cache.substring(this.indexB, cache.length()); 664 | 665 | this.indexB = this.indexA; 666 | } 667 | 668 | cache = cache.substring(0, this.indexA) + copy + cache.substring(this.indexA, cache.length()); 669 | } 670 | 671 | if (key == Keyboard.KEY_C && this.getIndexA() != this.getIndexB()) { 672 | ClipboardUtil.set(cache.substring(this.indexA, this.indexB)); 673 | } 674 | 675 | if (key == Keyboard.KEY_X && this.getIndexA() != this.getIndexB()) { 676 | ClipboardUtil.set(cache.substring(this.indexA, this.indexB)); 677 | 678 | cache = cache.substring(0, this.indexA) + cache.substring(this.indexB, this.text.length()); 679 | 680 | this.indexB = this.indexA; 681 | } 682 | 683 | break; 684 | } 685 | 686 | if (ChatAllowedCharacters.isAllowedCharacter(character)) { 687 | if (this.getIndexA() != this.getIndexB()) { 688 | cache = cache.substring(0, this.indexA) + cache.substring(this.indexB, cache.length()); 689 | 690 | this.indexB = this.indexA; 691 | } 692 | 693 | cache = cache.substring(0, this.indexA) + CharUtils.toString(character) + cache.substring(this.indexA, cache.length()); 694 | a++; 695 | } 696 | 697 | break; 698 | } 699 | } 700 | 701 | if (!cache.equals(this.text)) { 702 | this.text = cache; 703 | this.splitTick.reset(); 704 | } 705 | 706 | if (a != this.indexA) { 707 | this.indexA = TurokMath.clamp(a, 0, this.text.length()); 708 | this.indexB = this.indexA; 709 | 710 | this.splitTick.reset(); 711 | } 712 | } 713 | } catch (StringIndexOutOfBoundsException exc) { 714 | // Actually, THE CODE RUN VERY CLEAN NO PROBLEMS, BUT FOR SOME REASON, ITS EXPLODE! 715 | } 716 | } 717 | 718 | @Override 719 | public void onCustomKeyboardPressed(char character, int key) { 720 | 721 | } 722 | 723 | @Override 724 | public void onMouseReleased(int button) { 725 | if (!this.isRendering()) { 726 | return; 727 | } 728 | 729 | if (this.isPressed()) { 730 | this.setReleased(this.isMouseOver()); 731 | 732 | this.setDragging(false); 733 | this.setPressed(false); 734 | } 735 | } 736 | 737 | @Override 738 | public void onCustomMouseReleased(int button) { 739 | 740 | } 741 | 742 | @Override 743 | public void onMouseClicked(int button) { 744 | if (!this.isRendering()) { 745 | return; 746 | } 747 | 748 | if (!this.isMouseOver() && this.isFocused()) { 749 | this.setFocused(false); 750 | } 751 | 752 | if (this.isMouseOver() && (button == 0 || button == 1)) { 753 | this.splitTick.reset(); 754 | this.setFocused(this.isMouseOver()); 755 | 756 | if (this.pressTick.isPassedMS(500)) { 757 | this.pressTick.reset(); 758 | } else { 759 | this.setIndexA(0); 760 | this.setIndexB(this.text.length()); 761 | } 762 | 763 | this.setPressed(true); 764 | this.setDragging(true); 765 | } 766 | } 767 | 768 | @Override 769 | public void onCustomMouseClicked(int button) { 770 | 771 | } 772 | 773 | @Override 774 | public void onRender() { 775 | if (!this.isRendering()) { 776 | return; 777 | } 778 | 779 | this.focusedAlpha = this.isFocused() ? this.focused[3] : 0f; 780 | this.pressedAlpha = this.isPressed() ? this.pressed[3] : 0f; 781 | this.outlineAlpha = this.outline[3]; 782 | 783 | this.lastTickFocusedAlpha = TurokMath.lerp(this.lastTickFocusedAlpha, this.focusedAlpha, this.getPartialTicks()); 784 | this.lastTickPressedAlpha = TurokMath.lerp(this.lastTickPressedAlpha, this.pressedAlpha, this.getPartialTicks()); 785 | this.lastTickOutlineAlpha = TurokMath.lerp(this.lastTickOutlineAlpha, this.outlineAlpha, this.getPartialTicks()); 786 | 787 | final boolean flag = !GL11.glIsEnabled(GL11.GL_SCISSOR_TEST); 788 | 789 | TurokGL.color(TurokGL.arrayColorToColorClass(this.background)); 790 | TurokRenderGL.drawSolidRect(this.rect); 791 | 792 | if (this.lastTickOutlineAlpha != 0f && this.isToDrawOutline()) { 793 | TurokGL.color(this.outline[0], this.outline[1], this.outline[2], this.lastTickOutlineAlpha); 794 | TurokRenderGL.drawOutlineRect(this.rect); 795 | } 796 | 797 | if (this.lastTickFocusedAlpha != 0f) { 798 | TurokGL.color(this.focused[0], this.focused[1], this.focused[2], this.lastTickFocusedAlpha); 799 | TurokRenderGL.drawSolidRect(this.rect); 800 | } 801 | 802 | if (this.lastTickPressedAlpha != 0f) { 803 | TurokGL.color(this.pressed[0], this.pressed[1], this.pressed[2], this.lastTickPressedAlpha); 804 | TurokRenderGL.drawSolidRect(this.rect); 805 | } 806 | 807 | if (flag) { 808 | GL11.glEnable(GL11.GL_SCISSOR_TEST); 809 | } 810 | 811 | if (!this.isFocused()) { 812 | this.save = this.text; 813 | } 814 | 815 | if (this.isFocused()) { 816 | this.lastSize = (int) this.size(); 817 | } 818 | 819 | final Color color = TurokGL.arrayColorToColorClass(this.string); 820 | 821 | TurokShaderGL.drawScissor(this.scissor); 822 | 823 | float l = 0; 824 | float k = 0; 825 | 826 | int tl = this.text.isEmpty() ? -1 : this.text.length(); 827 | 828 | int i = 0; 829 | int j = 0; 830 | 831 | for (String c : this.text.split("")) { 832 | j++; 833 | 834 | if (tl == -1) { 835 | if (this.isFocused()) this.split(0f); 836 | 837 | continue; 838 | } 839 | 840 | float w = TurokFontManager.getStringWidth(this.getFont(), c); 841 | boolean r = true; 842 | 843 | if (j > this.getIndexA() && j <= this.getIndexB() && this.getIndexB() != this.getIndexA() && this.isFocused()) { 844 | TurokGL.color(TurokGL.arrayColorToColorClass(this.background_selected)); 845 | TurokRenderGL.drawSolidRect(this.rect.getX() + this.offsetX + k, this.rect.getY() + this.offsetY / 2, w + this.getKerning(), this.rect.getHeight() - (this.offsetY / 2 * 2)); 846 | 847 | l = k + w + this.getKerning(); 848 | r = false; 849 | } 850 | 851 | TurokFontManager.render(this.getFont(), c, this.rect.getX() + this.offsetX + k, this.rect.getY() + this.offsetY, this.getIsShadow(), color); 852 | 853 | if (r && i == this.getIndexA() && this.isFocused()) { 854 | this.split(k); 855 | } 856 | 857 | k += w + this.getKerning(); 858 | i++; 859 | } 860 | 861 | if (this.indexA == tl && this.isFocused()) { 862 | this.split(k); 863 | } 864 | 865 | if (flag) { 866 | GL11.glDisable(GL11.GL_SCISSOR_TEST); 867 | } 868 | } 869 | 870 | @Override 871 | public void onCustomRender() { 872 | 873 | } 874 | } 875 | --------------------------------------------------------------------------------