forked from MCJack123/PrimeUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcenterLabel.lua
More file actions
26 lines (25 loc) · 1.13 KB
/
Copy pathcenterLabel.lua
File metadata and controls
26 lines (25 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
local PrimeUI = require "util" -- DO NOT COPY THIS LINE
local expect = require "cc.expect".expect -- DO NOT COPY THIS LINE
-- Start copying below this line. --
--- Draws a line of text, centering it inside a box horizontally.
---@param win window The window to draw on
---@param x number The X position of the left side of the box
---@param y number The Y position of the box
---@param width number The width of the box to draw in
---@param text string The text to draw
---@param fgColor color|nil The color of the text (defaults to white)
---@param bgColor color|nil The color of the background (defaults to black)
function PrimeUI.centerLabel(win, x, y, width, text, fgColor, bgColor)
expect(1, win, "table")
expect(2, x, "number")
expect(3, y, "number")
expect(4, width, "number")
expect(5, text, "string")
fgColor = expect(6, fgColor, "number", "nil") or colors.white
bgColor = expect(7, bgColor, "number", "nil") or colors.black
assert(#text <= width, "string is too long")
win.setCursorPos(x + math.floor((width - #text) / 2), y)
win.setTextColor(fgColor)
win.setBackgroundColor(bgColor)
win.write(text)
end