Advertisement
AMisspelledUsernaem

UI

Feb 21st, 2025 (edited)
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.27 KB | None | 0 0
  1. local UDim2 = require("UDim2")
  2. local Enum  = require("Enum")
  3. local InputService = require("InputService")
  4. local Signal = require("Signal")
  5.  
  6. local focusedTextbox = nil
  7.  
  8. local UI = {}
  9.  
  10. UI.__index = UI
  11.  
  12. UI.Windows = {}
  13. UI.Focused = Signal.new()
  14. UI.FocusLost = Signal.new()
  15.  
  16. function UI.new()
  17.     local self = {
  18.         Size = UDim2.fromOffset(0, 0),
  19.         Position = UDim2.fromOffset(0, 0),
  20.         AnchorPoint = {X = .5, Y = .5},
  21.         TextXAlign = Enum.TextXAlign.Center,
  22.         TextYAlign = Enum.TextYAlign.Center,
  23.         Text = "",
  24.         BackgroundColor = colors.white,
  25.         TextColor = colors.black,
  26.  
  27.         TextEditable = false,
  28.         MultiLine = false,
  29.  
  30.         Focused = Signal.new(),
  31.         FocusLost = Signal.new(),
  32.  
  33.         Clicked = Signal.new()
  34.     }
  35.  
  36.     return setmetatable(self, UI)
  37. end
  38.  
  39. function UI:Init(optionalParent)
  40.     if (self.Window) then
  41.         printError("UI already initialized")
  42.         return
  43.     end
  44.  
  45.     local terminalSizeX, terminalSizeY = term.getSize()
  46.     local sizeX = self.Size.X.Offset + terminalSizeX * self.Size.X.Scale
  47.     local sizeY = self.Size.Y.Offset + terminalSizeY * self.Size.Y.Scale
  48.     local positionX = (self.Position.X.Offset + terminalSizeX * self.Position.X.Scale) - sizeX * self.AnchorPoint.X
  49.     local positionY = (self.Position.Y.Offset + terminalSizeY * self.Position.Y.Scale) - sizeY * self.AnchorPoint.Y
  50.  
  51.     local newUI = window.create(optionalParent or term.current(), positionX, positionY, sizeX, sizeY)
  52.     UI.Windows[newUI] = self
  53.     self.Window = newUI
  54.     self:Update()
  55. end
  56.  
  57. function UI:Update()
  58.     if (not self) then
  59.         return printError("Self not assigned")
  60.     end
  61.     if (not self.Window) then
  62.         return printError("Missing Window")
  63.     end
  64.  
  65.     local terminalSizeX, terminalSizeY = term.getSize()
  66.     local sizeX = self.Size.X.Offset + (terminalSizeX + 1) * self.Size.X.Scale
  67.     local sizeY = self.Size.Y.Offset + (terminalSizeY + 1) * self.Size.Y.Scale
  68.     local positionX = (self.Position.X.Offset + terminalSizeX * self.Position.X.Scale) - sizeX * self.AnchorPoint.X
  69.     local positionY = (self.Position.Y.Offset + terminalSizeY * self.Position.Y.Scale) - sizeY * self.AnchorPoint.Y
  70.  
  71.     self.Window.reposition(positionX, positionY, sizeX, sizeY)
  72.  
  73.     local cursorX = 0 do
  74.         if (self.TextXAlign == Enum.TextXAlign.Center) then
  75.             cursorX = (sizeX / 2) - (#self.Text / 2) + 1
  76.         elseif (self.TextXAlign == Enum.TextXAlign.Right) then
  77.             cursorX = sizeX - #self.Text
  78.         end
  79.     end
  80.     local cursorY = 0 do
  81.         if (self.TextYAlign == Enum.TextYAlign.Center) then
  82.             cursorY = (sizeY / 2) + 1
  83.         elseif (self.TextYAlign == Enum.TextYAlign.Botom) then
  84.             cursorY = sizeY
  85.         end
  86.     end
  87.  
  88.     self.Window.setBackgroundColor(self.BackgroundColor)
  89.     self.Window.setTextColor(self.TextColor)
  90.     self.Window.clear()
  91.     self.Window.setCursorPos(cursorX, cursorY)
  92.     self.Window.write(self.Text)
  93. end
  94.  
  95. function UI:CleanUp()
  96.     self.Window.setVisible(false)
  97.     self.Window.clear()
  98.     UI.Windows[self.Window] = nil
  99.  
  100.     for _, signal in next, {UI.FocusLost, UI.Focused, UI.Clicked} do
  101.         for _, c in next, signal.__Connections do
  102.             c:Disconnect()
  103.         end
  104.     end
  105.  
  106.     for k in next, self do
  107.         self[k] = nil
  108.     end
  109. end
  110.  
  111. function UI.UpdateAll()
  112.     term.clear()
  113.     for _, ui in next, UI.Windows do
  114.         UI.Update(ui)
  115.     end
  116. end
  117.  
  118. local function appendCharacter(char)
  119.     if (not focusedTextbox) then
  120.         return
  121.     end
  122.     focusedTextbox.Text = focusedTextbox.Text .. char
  123.     UI.Update(focusedTextbox)
  124. end
  125.  
  126. InputService.MouseDown:Connect(function (mouseKey, X, Y)
  127.     if (focusedTextbox) then
  128.         UI.FocusLost:Fire(false)
  129.         focusedTextbox = nil
  130.         return
  131.     end
  132.     for uiWindow, ui in next, UI.Windows do
  133.         local sizeX, sizeY = uiWindow.getSize()
  134.         local positionX, positionY = uiWindow.getPosition()
  135.         if (mouseKey == Enum.MouseButton.Left and X > positionX and X < positionX + sizeX and Y > positionY and Y < positionY + sizeY) then
  136.             if (ui.TextEditable) then
  137.                 focusedTextbox = ui
  138.                 UI.Focused:Fire(ui)
  139.                 ui.Focused:Fire()
  140.                 uiWindow.setCursorBlink(true)
  141.             else
  142.                 ui.Clicked:Fire(X, Y)
  143.             end
  144.             return
  145.         end
  146.     end
  147. end)
  148.  
  149. InputService.Writing:Connect(appendCharacter)
  150. InputService.Paste:Connect(appendCharacter)
  151.  
  152. InputService.KeyDown:Connect(function (keyCode)
  153.     if (not focusedTextbox) then
  154.         return
  155.     end
  156.  
  157.     local key = keys.getName(keyCode)
  158.     if (key == "enter") then
  159.         if (focusedTextbox.MultiLine) then
  160.             appendCharacter("\n")
  161.         else
  162.             UI.FocusLost:Fire(true)
  163.             focusedTextbox = nil
  164.             return
  165.         end
  166.     elseif (key == "backspace") then
  167.         focusedTextbox.Text = string.sub(focusedTextbox.Text, 1, -2)
  168.     end
  169.  
  170.     UI.Update(focusedTextbox)
  171. end)
  172.  
  173. UI.FocusLost:Connect(function (enterPressed)
  174.     if (not focusedTextbox or not focusedTextbox.Window) then
  175.         return
  176.     end
  177.     focusedTextbox.Window.setCursorBlink(false)
  178.     focusedTextbox.FocusLost:Fire(enterPressed)
  179. end)
  180.  
  181. return UI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement