API.lua 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. --[[
  2. MIT License
  3. Copyright (c) 2019 Mitchell Davis <coding.jackalope@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. --]]
  20. if SLAB_PATH == nil then
  21. SLAB_PATH = (...):match("(.-)[^%.]+$")
  22. end
  23. local Button = require(SLAB_PATH .. '.Internal.UI.Button')
  24. local CheckBox = require(SLAB_PATH .. '.Internal.UI.CheckBox')
  25. local ColorPicker = require(SLAB_PATH .. '.Internal.UI.ColorPicker')
  26. local ComboBox = require(SLAB_PATH .. '.Internal.UI.ComboBox')
  27. local Cursor = require(SLAB_PATH .. '.Internal.Core.Cursor')
  28. local Dialog = require(SLAB_PATH .. '.Internal.UI.Dialog')
  29. local DrawCommands = require(SLAB_PATH .. '.Internal.Core.DrawCommands')
  30. local Image = require(SLAB_PATH .. '.Internal.UI.Image')
  31. local Input = require(SLAB_PATH .. '.Internal.UI.Input')
  32. local Keyboard = require(SLAB_PATH .. '.Internal.Input.Keyboard')
  33. local ListBox = require(SLAB_PATH .. '.Internal.UI.ListBox')
  34. local Mouse = require(SLAB_PATH .. '.Internal.Input.Mouse')
  35. local Menu = require(SLAB_PATH .. '.Internal.UI.Menu')
  36. local MenuState = require(SLAB_PATH .. '.Internal.UI.MenuState')
  37. local MenuBar = require(SLAB_PATH .. '.Internal.UI.MenuBar')
  38. local Region = require(SLAB_PATH .. '.Internal.UI.Region')
  39. local Separator = require(SLAB_PATH .. '.Internal.UI.Separator')
  40. local Shape = require(SLAB_PATH .. '.Internal.UI.Shape')
  41. local Stats = require(SLAB_PATH .. '.Internal.Core.Stats')
  42. local Style = require(SLAB_PATH .. '.Style')
  43. local Text = require(SLAB_PATH .. '.Internal.UI.Text')
  44. local Tree = require(SLAB_PATH .. '.Internal.UI.Tree')
  45. local Window = require(SLAB_PATH .. '.Internal.UI.Window')
  46. --[[
  47. Slab
  48. Slab is an immediate mode GUI toolkit for the Love 2D framework. This library is designed to
  49. allow users to easily add this library to their existing Love 2D projects and quickly create
  50. tools to enable them to iterate on their ideas quickly. The user should be able to utilize this
  51. library with minimal integration steps and is completely written in Lua and utilizes
  52. the Love 2D API. No compiled binaries are required and the user will have access to the source
  53. so that they may make adjustments that meet the needs of their own projects and tools. Refer
  54. to main.lua and SlabTest.lua for example usage of this library.
  55. Supported Version: 11.2.0
  56. API:
  57. Initialize
  58. GetVersion
  59. GetLoveVersion
  60. Update
  61. Draw
  62. GetStyle
  63. Window:
  64. BeginWindow
  65. EndWindow
  66. GetWindowPosition
  67. GetWindowSize
  68. GetWindowContentSize
  69. GetWindowActiveSize
  70. BeginColumn
  71. EndColumn
  72. Menu:
  73. BeginMainMenuBar
  74. EndMainMenuBar
  75. BeginMenuBar
  76. EndMenuBar
  77. BeginMenu
  78. EndMenu
  79. BeginContextMenuItem
  80. BeginContextMenuWindow
  81. EndContextMenu
  82. MenuItem
  83. MenuItemChecked
  84. Separator
  85. Button
  86. RadioButton
  87. Text
  88. TextSelectable
  89. Textf
  90. CheckBox
  91. Input
  92. GetInputText
  93. GetInputNumber
  94. GetInputCursorPos
  95. IsInputFocused
  96. SetInputFocus
  97. SetInputCursorPos
  98. SetInputCursorPosLine
  99. BeginTree
  100. EndTree
  101. BeginComboBox
  102. EndComboBox
  103. Image
  104. Cursor:
  105. SameLine
  106. NewLine
  107. SetCursorPos
  108. GetCursorPos
  109. Properties
  110. ListBox:
  111. BeginListBox
  112. EndListBox
  113. BeginListBoxItem
  114. IsListBoxItemClicked
  115. EndListBoxItem
  116. Dialog:
  117. OpenDialog
  118. BeginDialog
  119. EndDialog
  120. CloseDialog
  121. MessageBox
  122. FileDialog
  123. Mouse:
  124. IsMouseDown
  125. IsMouseClicked
  126. IsMouseReleased
  127. IsMouseDoubleClicked
  128. IsMouseDragging
  129. GetMousePosition
  130. GetMousePositionWindow
  131. GetMouseDelta
  132. Control:
  133. IsControlHovered
  134. IsControlClicked
  135. GetControlSize
  136. IsVoidHovered
  137. IsVoidClicked
  138. Keyboard:
  139. IsKeyDown
  140. IsKeyPressed
  141. IsKeyReleased
  142. Shape:
  143. Rectangle
  144. Circle
  145. Triangle
  146. Line
  147. Curve
  148. GetCurveControlPointCount
  149. GetCurveControlPoint
  150. EvaluateCurve
  151. EvaluateCurveMouse
  152. Polygon
  153. --]]
  154. local Slab = {}
  155. -- Slab version numbers.
  156. local Version_Major = 0
  157. local Version_Minor = 5
  158. local Version_Revision = 0
  159. local FrameNumber = 0
  160. local function TextInput(Ch)
  161. Input.Text(Ch)
  162. if love.textinput ~= nil then
  163. love.textinput(Ch)
  164. end
  165. end
  166. local function WheelMoved(X, Y)
  167. Window.WheelMoved(X, Y)
  168. if love.wheelmoved ~= nil then
  169. love.wheelmoved(X, Y)
  170. end
  171. end
  172. --[[
  173. Initialize
  174. Initializes Slab and hooks into the required events. This function should be called in love.load.
  175. args: [Table] The list of parameters passed in by the user on the command-line. This should be passed in from
  176. love.load function.
  177. Return: None.
  178. --]]
  179. function Slab.Initialize(args)
  180. Style.API.Initialize()
  181. love.handlers['textinput'] = TextInput
  182. love.handlers['wheelmoved'] = WheelMoved
  183. end
  184. --[[
  185. GetVersion
  186. Retrieves the current version of Slab being used as a string.
  187. Return: [String] String of the current Slab version.
  188. --]]
  189. function Slab.GetVersion()
  190. return string.format("%d.%d.%d", Version_Major, Version_Minor, Version_Revision)
  191. end
  192. --[[
  193. GetLoveVersion
  194. Retrieves the current version of Love being used as a string.
  195. Return: [String] String of the current Love version.
  196. --]]
  197. function Slab.GetLoveVersion()
  198. local Major, Minor, Revision, Codename = love.getVersion()
  199. return string.format("%d.%d.%d - %s", Major, Minor, Revision, Codename)
  200. end
  201. --[[
  202. Update
  203. Updates the input state and states of various widgets. This function must be called every frame.
  204. This should be called before any Slab calls are made to ensure proper responses to Input are made.
  205. dt: [Number] The delta time for the frame. This should be passed in from love.update.
  206. Return: None.
  207. --]]
  208. function Slab.Update(dt)
  209. FrameNumber = FrameNumber + 1
  210. Stats.Reset()
  211. Stats.Begin('Frame')
  212. Stats.Begin('Update')
  213. Mouse.Update()
  214. Keyboard.Update()
  215. Input.Update(dt)
  216. DrawCommands.Reset()
  217. Window.Reset()
  218. Window.SetFrameNumber(FrameNumber)
  219. if MenuState.IsOpened then
  220. MenuState.WasOpened = MenuState.IsOpened
  221. if Mouse.IsClicked(1) then
  222. MenuState.RequestClose = true
  223. end
  224. end
  225. Stats.End('Update')
  226. end
  227. --[[
  228. Draw
  229. This function will execute all buffered draw calls from the various Slab calls made prior. This
  230. function should be called from love.draw and should be called at the very to ensure Slab is rendered
  231. above the user's workspace.
  232. Return: None.
  233. --]]
  234. function Slab.Draw()
  235. Stats.Begin('Draw')
  236. Window.Validate()
  237. if MenuState.RequestClose then
  238. Menu.Close()
  239. MenuBar.Clear()
  240. end
  241. Mouse.UpdateCursor()
  242. if Mouse.IsReleased(1) then
  243. Button.ClearClicked()
  244. end
  245. DrawCommands.Execute()
  246. Stats.End('Draw')
  247. Stats.End('Frame')
  248. end
  249. --[[
  250. GetStyle
  251. Retrieve the style table associated with the current instance of Slab. This will allow the user to add custom styling
  252. to their controls.
  253. Return: [Table] The style table.
  254. --]]
  255. function Slab.GetStyle()
  256. return Style
  257. end
  258. --[[
  259. BeginWindow
  260. This function begins the process of drawing widgets to a window. This function must be followed up with
  261. an EndWindow call to ensure proper behavior of drawing windows.
  262. Id: [String] A unique string identifying this window in the project.
  263. Options: [Table] List of options that control how this window will behave.
  264. X: [Number] The X position to start rendering the window at.
  265. Y: [Number] The Y position to start rendering the window at.
  266. W: [Number] The starting width of the window.
  267. H: [Number] The starting height of the window.
  268. ContentW: [Number] The starting width of the content contained within this window.
  269. ContentH: [Number] The starting height of the content contained within this window.
  270. BgColor: [Table] The background color value for this window. Will use the default style WindowBackgroundColor if this is empty.
  271. Title: [String] The title to display for this window. If emtpy, no title bar will be rendered and the window will not be movable.
  272. AllowMove: [Boolean] Controls whether the window is movable within the title bar area. The default value is true.
  273. AllowResize: [Boolean] Controls whether the window is resizable. The default value is true. AutoSizeWindow must be false for this to work.
  274. AllowFocus: [Boolean] Controls whether the window can be focused. The default value is true.
  275. Border: [Number] The value which controls how much empty space should be left between all sides of the window from the content.
  276. The default value is 4.0
  277. NoOutline: [Boolean] Controls whether an outline should not be rendered. The default value is false.
  278. IsMenuBar: [Boolean] Controls whether if this window is a menu bar or not. This flag should be ignored and is used by the menu bar
  279. system. The default value is false.
  280. AutoSizeWindow: [Boolean] Automatically updates the window size to match the content size. The default value is true.
  281. AutoSizeWindowW: [Boolean] Automatically update the window width to match the content size. This value is taken from AutoSizeWindow by default.
  282. AutoSizeWindowH: [Boolean] Automatically update the window height to match the content size. This value is taken from AutoSizeWindow by default.
  283. AutoSizeContent: [Boolean] The content size of the window is automatically updated with each new widget. The default value is true.
  284. Layer: [String] The layer to which to draw this window. This is used internally and should be ignored by the user.
  285. ResetPosition: [Boolean] Determines if the window should reset any delta changes to its position.
  286. ResetSize: [Boolean] Determines if the window should reset any delta changes to its size.
  287. ResetContent: [Boolean] Determines if the window should reset any delta changes to its content size.
  288. ResetLayout: [Boolean] Will reset the position, size, and content. Short hand for the above 3 flags.
  289. SizerFilter: [Table] Specifies what sizers are enabled for the window. If nothing is specified, all sizers are available. The values can
  290. be: NW, NE, SW, SE, N, S, E, W
  291. CanObstruct: [Boolean] Sets whether this window is considered for obstruction of other windows and their controls. The default value is true.
  292. Rounding: [Number] Amount of rounding to apply to the corners of the window.
  293. Columns: [Number] The number of columns for this window. Must be larger than 1 for columns to work properly.
  294. Return: None
  295. --]]
  296. function Slab.BeginWindow(Id, Options)
  297. Window.Begin(Id, Options)
  298. end
  299. --[[
  300. EndWindow
  301. This function must be called after a BeginWindow and associated widget calls. If the user fails to call this, an assertion will be thrown
  302. to alert the user.
  303. Return: None.
  304. --]]
  305. function Slab.EndWindow()
  306. Window.End()
  307. end
  308. --[[
  309. GetWindowPosition
  310. Retrieves the active window's position.
  311. Return: [Number], [Number] The X and Y position of the active window.
  312. --]]
  313. function Slab.GetWindowPosition()
  314. return Window.GetPosition()
  315. end
  316. --[[
  317. GetWindowSize
  318. Retrieves the active window's size.
  319. Return: [Number], [Number] The width and height of the active window.
  320. --]]
  321. function Slab.GetWindowSize()
  322. return Window.GetSize()
  323. end
  324. --[[
  325. GetWindowContentSize
  326. Retrieves the active window's content size.
  327. Return: [Number], [Number] The width and height of the active window content.
  328. --]]
  329. function Slab.GetWindowContentSize()
  330. return Window.GetContentSize()
  331. end
  332. --[[
  333. GetWindowActiveSize
  334. Retrieves the active window's active size minus the borders. This could be the size of the window or
  335. the size of the current column.
  336. Return: [Number], [Number] The width and height of the window's active bounds.
  337. --]]
  338. function Slab.GetWindowActiveSize()
  339. return Window.GetBorderlessSize()
  340. end
  341. --[[
  342. BeginColumn
  343. The start of a column. EndColumn must be called after a call to BeginColumn and all controls have been rendered.
  344. Index: [Number] The index to the column to add controls to. This must be a valid column between 1 and the max column index
  345. defined by the Columns option in BeginWindow.
  346. Return: None.
  347. --]]
  348. function Slab.BeginColumn(Index)
  349. Window.BeginColumn(Index)
  350. end
  351. --[[
  352. EndColumn
  353. The end of a column. Must be called after a BeginColumn call.
  354. Return: None.
  355. --]]
  356. function Slab.EndColumn()
  357. Window.EndColumn()
  358. end
  359. --[[
  360. BeginMainMenuBar
  361. This function begins the process for setting up the main menu bar. This should be called outside of any BeginWindow/EndWindow calls.
  362. The user should only call EndMainMenuBar if this function returns true. Use BeginMenu/EndMenu calls to add menu items on the main menu bar.
  363. Example:
  364. if Slab.BeginMainMenuBar() then
  365. if Slab.BeginMenu("File") then
  366. if Slab.MenuItem("Quit") then
  367. love.event.quit()
  368. end
  369. Slab.EndMenu()
  370. end
  371. Slab.EndMainMenuBar()
  372. end
  373. Return: [Boolean] Returns true if the main menu bar process has started.
  374. --]]
  375. function Slab.BeginMainMenuBar()
  376. Cursor.SetPosition(0.0, 0.0)
  377. return Slab.BeginMenuBar(true)
  378. end
  379. --[[
  380. EndMainMenuBar
  381. This function should be called if BeginMainMenuBar returns true.
  382. Return: None.
  383. --]]
  384. function Slab.EndMainMenuBar()
  385. Slab.EndMenuBar()
  386. end
  387. --[[
  388. BeginMenuBar
  389. This function begins the process of rendering a menu bar for a window. This should only be called within a BeginWindow/EndWindow context.
  390. IsMainMenuBar: [Boolean] Is this menu bar for the main viewport. Used internally. Should be ignored for all other calls.
  391. Return: [Boolean] Returns true if the menu bar process has started.
  392. --]]
  393. function Slab.BeginMenuBar(IsMainMenuBar)
  394. return MenuBar.Begin(IsMainMenuBar)
  395. end
  396. --[[
  397. EndMenuBar
  398. This function should be called if BeginMenuBar returns true.
  399. Return: None.
  400. --]]
  401. function Slab.EndMenuBar()
  402. MenuBar.End()
  403. end
  404. --[[
  405. BeginMenu
  406. Adds a menu item that when the user hovers over, opens up an additional context menu. When used within a menu bar, BeginMenu calls
  407. will be added to the bar. Within a context menu, the menu item will be added within the context menu with an additional arrow to notify
  408. the user more options are available. If this function returns true, the user must call EndMenu.
  409. Label: [String] The label to display for this menu.
  410. Return: [Boolean] Returns true if the menu item is being hovered.
  411. --]]
  412. function Slab.BeginMenu(Label)
  413. return Menu.BeginMenu(Label)
  414. end
  415. --[[
  416. EndMenu
  417. Finishes up a BeginMenu. This function must be called if BeginMenu returns true.
  418. Return: None.
  419. --]]
  420. function Slab.EndMenu()
  421. Menu.EndMenu()
  422. end
  423. --[[
  424. BeginContextMenuItem
  425. Opens up a context menu based on if the user right clicks on the last item. This function should be placed immediately after an item
  426. call to open up a context menu for that specific item. If this function returns true, EndContextMenu must be called.
  427. Example:
  428. if Slab.Button("Button!") then
  429. -- Perform logic here when button is clicked
  430. end
  431. -- This will only return true if the previous button is hot and the user right-clicks.
  432. if Slab.BeginContextMenuItem() then
  433. Slab.MenuItem("Button Item 1")
  434. Slab.MenuItem("Button Item 2")
  435. Slab.EndContextMenu()
  436. end
  437. Return: [Boolean] Returns true if the user right clicks on the previous item call. EndContextMenu must be called in order for
  438. this to function properly.
  439. --]]
  440. function Slab.BeginContextMenuItem()
  441. return Menu.BeginContextMenu({IsItem = true})
  442. end
  443. --[[
  444. BeginContextMenuWindow
  445. Opens up a context menu based on if the user right clicks anywhere within the window. It is recommended to place this function at the end
  446. of a window's widget calls so that Slab can catch any BeginContextMenuItem calls before this call. If this function returns true,
  447. EndContextMenu must be called.
  448. Return: [Boolean] Returns true if the user right clicks anywhere within the window. EndContextMenu must be called in order for this
  449. to function properly.
  450. --]]
  451. function Slab.BeginContextMenuWindow()
  452. return Menu.BeginContextMenu({IsWindow = true})
  453. end
  454. --[[
  455. EndContextMenu
  456. Finishes up any BeginContextMenuItem/BeginContextMenuWindow if they return true.
  457. Return: None.
  458. --]]
  459. function Slab.EndContextMenu()
  460. Menu.EndContextMenu()
  461. end
  462. --[[
  463. MenuItem
  464. Adds a menu item to a given context menu.
  465. Label: [String] The label to display to the user.
  466. Return: [Boolean] Returns true if the user clicks on this menu item.
  467. --]]
  468. function Slab.MenuItem(Label)
  469. return Menu.MenuItem(Label)
  470. end
  471. --[[
  472. MenuItemChecked
  473. Adds a menu item to a given context menu. If IsChecked is true, then a check mark will be rendered next to the
  474. label.
  475. Example:
  476. local Checked = false
  477. if Slab.MenuItemChecked("Menu Item", Checked)
  478. Checked = not Checked
  479. end
  480. Label: [String] The label to display to the user.
  481. IsChecked: [Boolean] Determines if a check mark should be rendered next to the label.
  482. Return: [Boolean] Returns true if the user clicks on this menu item.
  483. --]]
  484. function Slab.MenuItemChecked(Label, IsChecked)
  485. return Menu.MenuItemChecked(Label, IsChecked)
  486. end
  487. --[[
  488. Separator
  489. This functions renders a separator line in the window.
  490. Option: [Table] List of options for how this separator will be drawn.
  491. IncludeBorders: [Boolean] Whether to extend the separator to include the window borders. This is false by default.
  492. H: [Number] The height of the separator. This doesn't change the line thickness, rather, specifies the cursor advancement
  493. in the Y direction.
  494. Return: None.
  495. --]]
  496. function Slab.Separator(Options)
  497. Separator.Begin(Options)
  498. end
  499. --[[
  500. Button
  501. Adds a button to the active window.
  502. Label: [String] The label to display on the button.
  503. Options: [Table] List of options for how this button will behave.
  504. Tooltip: [String] The tooltip to display when the user hovers over this button.
  505. AlignRight: [Boolean] Flag to push this button to the right side of the window.
  506. ExpandW: [Boolean] Expands the button to fit the contents of the window.
  507. Rounding: [Number] Amount of rounding to apply to the corners of the button.
  508. Invisible: [Boolean] Don't render the button, but keep the behavior.
  509. W: [Number] Override the width of the button.
  510. H: [Number] Override the height of the button.
  511. Disabled: [Boolean] If true, the button is not interactable by the user.
  512. Return: [Boolean] Returns true if the user clicks on this button.
  513. --]]
  514. function Slab.Button(Label, Options)
  515. return Button.Begin(Label, Options)
  516. end
  517. --[[
  518. RadioButton
  519. Adds a radio button entry to the active window. The grouping of radio buttons is determined by the user. An Index can
  520. be applied to the given radio button and a SelectedIndex can be passed in to determine if this specific radio button
  521. is the selected one.
  522. Label: [String] The label to display next to the button.
  523. Options: [Table] List of options for how this radio button will behave.
  524. Index: [Number] The index of this radio button. Will be 0 by default and not selectable. Assign an index to group the button.
  525. SelectedIndex: [Number] The index of the radio button that is selected. If this equals the Index field, then this radio button
  526. will be rendered as selected.
  527. Tooltip: [String] The tooltip to display when the user hovers over the button or label.
  528. Return: [Boolean] Returns true if the user clicks on this button.
  529. --]]
  530. function Slab.RadioButton(Label, Options)
  531. return Button.BeginRadio(Label, Options)
  532. end
  533. --[[
  534. Text
  535. Adds text to the active window.
  536. Label: [String] The string to be displayed in the window.
  537. Options: [Table] List of options for how this text is displayed.
  538. Color: [Table] The color to render the text.
  539. Pad: [Number] How far to pad the text from the left side of the current cursor position.
  540. IsSelectable: [Boolean] Whether this text is selectable using the text's Y position and the window X and width as the
  541. hot zone.
  542. IsSelectableTextOnly: [Boolean] Only available if IsSelectable is true. Will use the text width instead of the
  543. window width to determine the hot zone.
  544. IsSelected: [Boolean] Forces the hover background to be rendered.
  545. SelectOnHover: [Boolean] Returns true if the user is hovering over the hot zone of this text.
  546. HoverColor: [Table] The color to render the background if the IsSelected option is true.
  547. CenterX: [Boolean] Should the text be centered relative to the current active bounds.
  548. Return: [Boolean] Returns true if SelectOnHover option is set to true. False otherwise.
  549. --]]
  550. function Slab.Text(Label, Options)
  551. return Text.Begin(Label, Options)
  552. end
  553. --[[
  554. TextSelectable
  555. This function is a shortcut for SlabText with the IsSelectable option set to true.
  556. Label: [String] The string to be displayed in the window.
  557. Options: [Table] List of options for how this text is displayed.
  558. See Slab.Text for all options.
  559. Return: [Boolean] Returns true if user clicks on this text. False otherwise.
  560. --]]
  561. function Slab.TextSelectable(Label, Options)
  562. Options = Options == nil and {} or Options
  563. Options.IsSelectable = true
  564. return Slab.Text(Label, Options)
  565. end
  566. --[[
  567. Textf
  568. Adds formatted text to the active window. This text will wrap to fit within the contents of
  569. either the window or a user specified width.
  570. Label: [String] The text to be rendered.
  571. Options: [Table] List of options for how this text is displayed.
  572. Color: [Table] The color to render the text.
  573. W: [Number] The width to restrict the text to. If this option is not specified, then the window
  574. width is used.
  575. Align: [String] The alignment to use for this text. For more information, refer to the love documentation
  576. at https://love2d.org/wiki/AlignMode. Below are the available options:
  577. center: Align text center.
  578. left: Align text left.
  579. right: Align text right.
  580. justify: Align text both left and right.
  581. Return: None.
  582. --]]
  583. function Slab.Textf(Label, Options)
  584. Text.BeginFormatted(Label, Options)
  585. end
  586. --[[
  587. CheckBox
  588. Renders a check box with a label. The check box when enabled will render an 'X'.
  589. Enabled: [Boolean] Will render an 'X' within the box if true. Will be an empty box otherwise.
  590. Label: [String] The label to display after the check box.
  591. Options: [Table] List of options for how this check box will behave.
  592. Tooltip: [String] Text to be displayed if the user hovers over the check box.
  593. Id: [String] An optional Id that can be supplied by the user. By default, the Id will be the label.
  594. Rounding: [Number] Amount of rounding to apply to the corners of the check box.
  595. Return: [Boolean] Returns true if the user clicks within the check box.
  596. --]]
  597. function Slab.CheckBox(Enabled, Label, Options)
  598. return CheckBox.Begin(Enabled, Label, Options)
  599. end
  600. --[[
  601. Input
  602. This function will render an input box for a user to input text in. This widget behaves like input boxes
  603. found in other applications. This function will only return true if it has focus and user has either input
  604. text or pressed the return key.
  605. Example:
  606. local Text = "Hello World"
  607. if Slab.Input('Example', {Text = Text}) then
  608. Text = Slab.GetInputText()
  609. end
  610. Id: [String] A string that uniquely identifies this Input within the context of the window.
  611. Options: [Table] List of options for how this Input will behave.
  612. Tooltip: [String] Text to be displayed if the user hovers over the Input box.
  613. ReturnOnText: [Boolean] Will cause this function to return true whenever the user has input
  614. a new character into the Input box. This is true by default.
  615. Text: [String] The text to be supplied to the input box. It is recommended to use this option
  616. when ReturnOnText is true.
  617. BgColor: [Table] The background color for the input box.
  618. SelectColor: [Table] The color used when the user is selecting text within the input box.
  619. SelectOnFocus: [Boolean] When this input box is focused by the user, the text contents within the input
  620. will be selected. This is true by default.
  621. NumbersOnly: [Boolean] When true, only numeric characters and the '.' character are allowed to be input into
  622. the input box. If no text is input, the input box will display '0'.
  623. W: [Number] The width of the input box. By default, will be 150.0
  624. H: [Number] The height of the input box. By default, will be the height of the current font.
  625. ReadOnly: [Boolean] Whether this input field can be editable or not.
  626. Align: [String] Aligns the text within the input box. Options are:
  627. left: Aligns the text to the left. This will be set when this Input is focused.
  628. center: Aligns the text in the center. This is the default for when the text is not focused.
  629. Rounding: [Number] Amount of rounding to apply to the corners of the input box.
  630. MinNumber: [Number] The minimum value that can be entered into this input box. Only valid when NumbersOnly is true.
  631. MaxNumber: [Number] The maximum value that can be entered into this input box. Only valid when NumbersOnly is true.
  632. MultiLine: [Boolean] Determines whether this input control should support multiple lines. If this is true, then the
  633. SelectOnFocus flag will be false. The given text will also be sanitized to remove controls characters such as
  634. '\r'. Also, the text will be left aligned.
  635. MultiLineW: [Number] The width for which the lines of text should be wrapped at.
  636. Return: [Boolean] Returns true if the user has pressed the return key while focused on this input box. If ReturnOnText
  637. is set to true, then this function will return true whenever the user has input any character into the input box.
  638. --]]
  639. function Slab.Input(Id, Options)
  640. return Input.Begin(Id, Options)
  641. end
  642. --[[
  643. GetInputText
  644. Retrieves the text entered into the focused input box. Refer to the documentation for Slab.Input for an example on how to
  645. use this function.
  646. Return: [String] Returns the text entered into the focused input box.
  647. --]]
  648. function Slab.GetInputText()
  649. return Input.GetText()
  650. end
  651. --[[
  652. GetInputNumber
  653. Retrieves the text entered into the focused input box and attempts to conver the text into a number. Will always return a valid
  654. number.
  655. Return: [Number] Returns the text entered into the focused input box as a number.
  656. --]]
  657. function Slab.GetInputNumber()
  658. local Result = tonumber(Input.GetText())
  659. if Result == nil then
  660. Result = 0
  661. end
  662. return Result
  663. end
  664. --[[
  665. GetInputCursorPos
  666. Retrieves the position of the input cursor for the focused input control. There are three values that are returned. The first one
  667. is the absolute position of the cursor with regards to the text for the control. The second is the column position of the cursor
  668. on the current line. The final value is the line number. The column will match the absolute position if the input control is not
  669. multi line.
  670. Return: [Number], [Number], [Number] The absolute position of the cursor, the column position of the cursor on the current line,
  671. and the line number of the cursor. These values will all be zero if no input control is focused.
  672. --]]
  673. function Slab.GetInputCursorPos()
  674. return Input.GetCursorPos()
  675. end
  676. --[[
  677. IsInputFocused
  678. Returns whether the input control with the given Id is focused or not.
  679. Id: [String] The Id of the input control to check.
  680. Return: [Boolean] True if the input control with the given Id is focused. False otherwise.
  681. --]]
  682. function Slab.IsInputFocused(Id)
  683. return Input.IsFocused(Id)
  684. end
  685. --[[
  686. SetInputFocus
  687. Sets the focus of the input control to the control with the given Id. The focus is set at the beginning
  688. of the next frame to avoid any input events from the current frame.
  689. Id: [String] The Id of the input control to focus.
  690. --]]
  691. function Slab.SetInputFocus(Id)
  692. Input.SetFocused(Id)
  693. end
  694. --[[
  695. SetInputCursorPos
  696. Sets the absolute text position in bytes of the focused input control. This value is applied on the next frame.
  697. This function can be combined with the SetInputFocus function to modify the cursor positioning of the desired
  698. input control. Note that the input control supports UTF8 characters so if the desired position is not a valid
  699. character, the position will be altered to find the next closest valid character.
  700. Pos: [Number] The absolute position in bytes of the text of the focused input control.
  701. --]]
  702. function Slab.SetInputCursorPos(Pos)
  703. Input.SetCursorPos(Pos)
  704. end
  705. --[[
  706. SetInputCursorPosLine
  707. Sets the column and line number of the focused input control. These values are applied on the next frame. This
  708. function behaves the same as SetInputCursorPos, but allows for setting the cursor by column and line.
  709. Column: [Number] The text position in bytes of the current line.
  710. Line: [Number] The line number to set.
  711. --]]
  712. function Slab.SetInputCursorPosLine(Column, Line)
  713. Input.SetCursorPosLine(Column, Line)
  714. end
  715. --[[
  716. BeginTree
  717. This function will render a tree item with an optional label. The tree can be expanded or collapsed based on whether
  718. the user clicked on the tree item. This function can also be nested to create a hierarchy of tree items. This function
  719. will return false when collapsed and true when expanded. If this function returns true, Slab.EndTree must be called in
  720. order for this tree item to behave properly. The hot zone of this tree item will be the height of the label and the width
  721. of the window by default.
  722. Id: [String] A string uniquely identifying this tree item within the context of the window.
  723. Options: [Table] List of options for how this tree item will behave.
  724. Label: [String] The text to be rendered for this tree item.
  725. Tooltip: [String] The text to be rendered when the user hovers over this tree item.
  726. IsLeaf: [Boolean] If this is true, this tree item will not be expandable/collapsable.
  727. OpenWithHighlight: [Boolean] If this is true, the tree will be expanded/collapsed when the user hovers over the hot
  728. zone of this tree item. If this is false, the user must click the expand/collapse icon to interact with this tree
  729. item.
  730. Icon: [Object] A user supplied image. This must be a valid Love image or the call will assert.
  731. IconPath: [String] If the Icon option is nil, then a path can be specified. Slab will load and
  732. manage the image resource.
  733. IsSelected: [Boolean] If true, will render a highlight rectangle around the tree item.
  734. IsOpen: [Boolean] Will force the tree item to be expanded.
  735. Return: [Boolean] Returns true if this tree item is expanded. Slab.EndTree must be called if this returns true.
  736. --]]
  737. function Slab.BeginTree(Id, Options)
  738. return Tree.Begin(Id, Options)
  739. end
  740. --[[
  741. EndTree
  742. Finishes up any BeginTree calls if those functions return true.
  743. Return: None.
  744. --]]
  745. function Slab.EndTree()
  746. Tree.End()
  747. end
  748. --[[
  749. BeginComboBox
  750. This function renders a non-editable input field with a drop down arrow. When the user clicks this option, a window is
  751. created and the user can supply their own Slab.TextSelectable calls to add possible items to select from. This function
  752. will return true if the combo box is opened. Slab.EndComboBox must be called if this function returns true.
  753. Example:
  754. local Options = {"Apple", "Banana", "Orange", "Pear", "Lemon"}
  755. local Options_Selected = ""
  756. if Slab.BeginComboBox('Fruits', {Selected = Options_Selected}) then
  757. for K, V in pairs(Options) do
  758. if Slab.TextSelectable(V) then
  759. Options_Selected = V
  760. end
  761. end
  762. Slab.EndComboBox()
  763. end
  764. Id: [String] A string that uniquely identifies this combo box within the context of the active window.
  765. Options: [Table] List of options that control how this combo box behaves.
  766. Tooltip: [String] Text that is rendered when the user hovers over this combo box.
  767. Selected: [String] Text that is displayed in the non-editable input box for this combo box.
  768. W: [Number] The width of the combo box. The default value is 150.0.
  769. Rounding: [Number] Amount of rounding to apply to the corners of the combo box.
  770. Return: [Boolean] This function will return true if the combo box is open.
  771. --]]
  772. function Slab.BeginComboBox(Id, Options)
  773. return ComboBox.Begin(Id, Options)
  774. end
  775. --[[
  776. EndComboBox
  777. Finishes up any BeginComboBox calls if those functions return true.
  778. Return: None.
  779. --]]
  780. function Slab.EndComboBox()
  781. ComboBox.End()
  782. end
  783. --[[
  784. Image
  785. Draws an image at the current cursor position. The Id uniquely identifies this
  786. image to manage behaviors with this image. An image can be supplied through the
  787. options or a path can be specified which Slab will manage the loading and storing of
  788. the image reference.
  789. Id: [String] A string uniquely identifying this image within the context of the current window.
  790. Options: [Table] List of options controlling how the image should be drawn.
  791. Image: [Object] A user supplied image. This must be a valid Love image or the call will assert.
  792. Path: [String] If the Image option is nil, then a path must be specified. Slab will load and
  793. manage the image resource.
  794. Rotation: [Number] The rotation value to apply when this image is drawn.
  795. Scale: [Number] The scale value to apply to both the X and Y axis.
  796. ScaleX: [Number] The scale value to apply to the X axis.
  797. ScaleY: [Number] The scale value to apply to the Y axis.
  798. Color: [Table] The color to use when rendering this image.
  799. SubX: [Number] The X-coordinate used inside the given image.
  800. SubY: [Number] The Y-coordinate used inside the given image.
  801. SubW: [Number] The width used inside the given image.
  802. SubH: [Number] The height used insided the given image.
  803. WrapX: [String] The horizontal wrapping mode for this image. The available options are 'clamp', 'repeat',
  804. 'mirroredrepeat', and 'clampzero'. For more information refer to the Love2D documentation on wrap modes at
  805. https://love2d.org/wiki/WrapMode.
  806. WrapY: [String] The vertical wrapping mode for this image. The available options are 'clamp', 'repeat',
  807. 'mirroredrepeat', and 'clampzero'. For more information refer to the Love2D documentation on wrap modes at
  808. https://love2d.org/wiki/WrapMode.
  809. Return: None.
  810. --]]
  811. function Slab.Image(Id, Options)
  812. Image.Begin(Id, Options)
  813. end
  814. --[[
  815. SameLine
  816. This forces the cursor to move back up to the same line as the previous widget. By default, all Slab widgets will
  817. advance the cursor to the next line based on the height of the current line. By using this call with other widget
  818. calls, the user will be able to set up multiple widgets on the same line to control how a window may look.
  819. Options: [Table] List of options that controls how the cursor should handle the same line.
  820. Pad: [Number] Extra padding to apply in the X direction.
  821. CenterY: [Boolean] Controls whether the cursor should be centered in the Y direction on the line. By default
  822. the line will use the NewLineSize, which is the height of the current font to center the cursor.
  823. Return: None.
  824. --]]
  825. function Slab.SameLine(Options)
  826. Cursor.SameLine(Options)
  827. end
  828. --[[
  829. NewLine
  830. This forces the cursor to advance to the next line based on the height of the current font.
  831. Return: None.
  832. --]]
  833. function Slab.NewLine()
  834. Cursor.NewLine()
  835. end
  836. --[[
  837. SetCursorPos
  838. Sets the cursor position. The default behavior is to set the cursor position relative to
  839. the current window. The absolute position can be set if the 'Absolute' option is set.
  840. Controls will only be drawn within a window. If the cursor is set outside of the current
  841. window context, the control will not be displayed.
  842. X: [Number] The X coordinate to place the cursor. If nil, then the X coordinate is not modified.
  843. Y: [Number] The Y coordinate to place the cursor. If nil, then the Y coordinate is not modified.
  844. Options: [Table] List of options that control how the cursor position should be set.
  845. Absolute: [Boolean] If true, will place the cursor using absolute coordinates.
  846. Return: None.
  847. --]]
  848. function Slab.SetCursorPos(X, Y, Options)
  849. Options = Options == nil and {} or Options
  850. Options.Absolute = Options.Absolute == nil and false or Options.Absolute
  851. if Options.Absolute then
  852. X = X == nil and Cursor.GetX() or X
  853. Y = Y == nil and Cursor.GetY() or Y
  854. Cursor.SetPosition(X, Y)
  855. else
  856. X = X == nil and Cursor.GetX() - Cursor.GetAnchorX() or X
  857. Y = Y == nil and Cursor.GetY() - Cursor.GetAnchorY() or Y
  858. Cursor.SetRelativePosition(X, Y)
  859. end
  860. end
  861. --[[
  862. GetCursorPos
  863. Gets the cursor position. The default behavior is to get the cursor position relative to
  864. the current window. The absolute position can be retrieved if the 'Absolute' option is set.
  865. Options: [Table] List of options that control how the cursor position should be retrieved.
  866. Absolute: [Boolean] If true, will return the cursor position in absolute coordinates.
  867. Return: [Number], [Number] The X and Y coordinates of the cursor.
  868. --]]
  869. function Slab.GetCursorPos(Options)
  870. Options = Options == nil and {} or Options
  871. Options.Absolute = Options.Absolute == nil and false or Options.Absolute
  872. local X, Y = Cursor.GetPosition()
  873. if not Options.Absolute then
  874. X = X - Cursor.GetAnchorX()
  875. Y = Y - Cursor.GetAnchorY()
  876. end
  877. return X, Y
  878. end
  879. --[[
  880. Properties
  881. Iterates through the table's key-value pairs and adds them to the active window. This currently only does
  882. a shallow loop and will not iterate through nested tables.
  883. TODO: Iterate through nested tables.
  884. Table: [Table] The list of properties to build widgets for.
  885. Return: None.
  886. --]]
  887. function Slab.Properties(Table)
  888. if Table ~= nil then
  889. for K, V in pairs(Table) do
  890. local Type = type(V)
  891. if Type == "boolean" then
  892. if Slab.CheckBox(V, K) then
  893. Table[K] = not Table[K]
  894. end
  895. elseif Type == "number" then
  896. Slab.Text(K .. ": ")
  897. Slab.SameLine()
  898. if Slab.Input(K, {Text = tostring(V), NumbersOnly = true, ReturnOnText = false}) then
  899. Table[K] = Slab.GetInputNumber()
  900. end
  901. elseif Type == "string" then
  902. Slab.Text(K .. ": ")
  903. Slab.SameLine()
  904. if Slab.Input(K, {Text = V, ReturnOnText = false}) then
  905. Table[K] = Slab.GetInputText()
  906. end
  907. end
  908. end
  909. end
  910. end
  911. --[[
  912. BeginListBox
  913. Begins the process of creating a list box. If this function is called, EndListBox must be called after all
  914. items have been added.
  915. Id: [String] A string uniquely identifying this list box within the context of the current window.
  916. Options: [Table] List of options controlling the behavior of the list box.
  917. W: [Number] The width of the list box. If nil, then the width of the window is used.
  918. H: [Number] The height of the list box. If nil, a default value of 150 is used. If H is 0, then
  919. the list box will stretch to the height of the window.
  920. Clear: [Boolean] Clears out the items in the list. It is recommended to only call this if the list items
  921. has changed and should not be set to true on every frame.
  922. Rounding: [Number] Amount of rounding to apply to the corners of the list box.
  923. Return: None.
  924. --]]
  925. function Slab.BeginListBox(Id, Options)
  926. ListBox.Begin(Id, Options)
  927. end
  928. --[[
  929. EndListBox
  930. Ends the list box container. Will close off the region and properly adjust the cursor.
  931. Return: None.
  932. --]]
  933. function Slab.EndListBox()
  934. ListBox.End()
  935. end
  936. --[[
  937. BeginListBoxItem
  938. Adds an item to the current list box with the given Id. The user can then draw controls however they see
  939. fit to display a single item. This allows the user to draw list items such as a texture with a name or just
  940. a text to represent the item. If this is called, EndListBoxItem must be called to complete the item.
  941. Id: [String] A string uniquely identifying this item within the context of the current list box.
  942. Options: [Table] List of options that control the behavior of the active list item.
  943. Selected: [Boolean] If true, will draw the item with a selection background.
  944. Return: None.
  945. --]]
  946. function Slab.BeginListBoxItem(Id, Options)
  947. ListBox.BeginItem(Id, Options)
  948. end
  949. --[[
  950. IsListBoxItemClicked
  951. Checks to see if a hot list item is clicked. This should only be called within a BeginListBoxLitem/EndListBoxItem
  952. block.
  953. Button: [Number] The button to check for the click of the item.
  954. IsDoubleClick: [Boolean] Check for double-click instead of single click.
  955. Return: [Boolean] Returns true if the active item is hovered with mouse and the requested mouse button is clicked.
  956. --]]
  957. function Slab.IsListBoxItemClicked(Button, IsDoubleClick)
  958. return ListBox.IsItemClicked(Button, IsDoubleClick)
  959. end
  960. --[[
  961. EndListBoxItem
  962. Ends the current item and commits the bounds of the item to the list.
  963. Return: None.
  964. --]]
  965. function Slab.EndListBoxItem()
  966. ListBox.EndItem()
  967. end
  968. --[[
  969. OpenDialog
  970. Opens the dialog box with the given Id. If the dialog box was opened, then it is pushed onto the stack.
  971. Calls to the BeginDialog with this same Id will return true if opened.
  972. Id: [String] A string uniquely identifying this dialog box.
  973. Return: None.
  974. --]]
  975. function Slab.OpenDialog(Id)
  976. Dialog.Open(Id)
  977. end
  978. --[[
  979. BeginDialog
  980. Begins the dialog window with the given Id if it is open. If this function returns true, then EndDialog must be called.
  981. Dialog boxes are windows which are centered in the center of the viewport. The dialog box cannot be moved and will
  982. capture all input from all other windows.
  983. Id: [String] A string uniquely identifying this dialog box.
  984. Options: [Table] List of options that control how this dialog box behaves. These are the same parameters found
  985. for BeginWindow, with some caveats. Certain options are overridden by the Dialog system. They are:
  986. X, Y, Layer, AllowFocus, AllowMove, and AutoSizeWindow.
  987. Return: [Boolean] Returns true if the dialog with the given Id is open.
  988. --]]
  989. function Slab.BeginDialog(Id, Options)
  990. return Dialog.Begin(Id, Options)
  991. end
  992. --[[
  993. EndDialog
  994. Ends the dialog window if a call to BeginDialog returns true.
  995. Return: None.
  996. --]]
  997. function Slab.EndDialog()
  998. Dialog.End()
  999. end
  1000. --[[
  1001. CloseDialog
  1002. Closes the currently active dialog box.
  1003. Return: None.
  1004. --]]
  1005. function Slab.CloseDialog()
  1006. Dialog.Close()
  1007. end
  1008. --[[
  1009. MessageBox
  1010. Opens a message box to be displayed to the user with a title and a message. Buttons can be specified through the options
  1011. table which when clicked, the string of the button is returned. This function should be called every frame when a message
  1012. box wants to be displayed.
  1013. Title: [String] The title to display for the message box.
  1014. Message: [String] The message to be displayed. The text is aligned in the center. Multi-line strings are supported.
  1015. Options: [Table] List of options to control the behavior of the message box.
  1016. Buttons: [Table] List of buttons to display with the message box. The order of the buttons are displayed from right to left.
  1017. Return: [String] The name of the button that was clicked. If none was clicked, an emtpy string is returned.
  1018. --]]
  1019. function Slab.MessageBox(Title, Message, Options)
  1020. return Dialog.MessageBox(Title, Message, Options)
  1021. end
  1022. --[[
  1023. FileDialog
  1024. Opens up a dialog box that displays a file explorer for opening or saving files or directories. This function does not create any file
  1025. handles, it just returns the list of files selected by the user.
  1026. Options: [Table] List of options that control the behavior of the file dialog.
  1027. AllowMultiSelect: [Boolean] Allows the user to select multiple items in the file dialog.
  1028. Directory: [String] The starting directory when the file dialog is open. If none is specified, the dialog
  1029. will start at love.filesystem.getSourceBaseDirectory and the dialog will remember the last
  1030. directory navigated to by the user between calls to this function.
  1031. Type: [String] The type of file dialog to use. The options are:
  1032. openfile: This is the default method. The user will have access to both directories and files. However,
  1033. only file selections are returned.
  1034. opendirectory: This type is used to filter the file dialog for directories only. No files will appear
  1035. in the list.
  1036. savefile: This type is used to select a name of a file to save. The user will be prompted if they wish to overwrite
  1037. an existing file.
  1038. Filters: [Table] A list of filters the user can select from when browsing files. The table can contain tables or strings.
  1039. Table: If a table is used for a filter, it should contain two elements. The first element is the filter while the second
  1040. element is the description of the filter e.g. {"*.lua", "Lua Files"}
  1041. String: If a raw string is used, then it should just be the filter. It is recommended to use the table option since a
  1042. description can be given for each filter.
  1043. Return: [Table] Returns items for how the user interacted with this file dialog.
  1044. Button: [String] The button the user clicked. Will either be OK or Cancel.
  1045. Files: [Table] An array of selected file items the user selected when OK is pressed. Will be empty otherwise.
  1046. --]]
  1047. function Slab.FileDialog(Options)
  1048. return Dialog.FileDialog(Options)
  1049. end
  1050. --[[
  1051. ColorPicker
  1052. Displays a window to allow the user to pick a hue and saturation value of a color. This should be called every frame and the result
  1053. should be handled to stop displaying the color picker and store the resulting color.
  1054. Options: [Table] List of options that control the behavior of the color picker.
  1055. Color: [Table] The color to modify. This should be in the format of 0-1 for each color component (RGBA).
  1056. Return: [Table] Returns the button and color the user has selected.
  1057. Button: [String] The button the user clicked. Will either be OK or Cancel.
  1058. Color: [Table] The new color the user has chosen. This will always be returned.
  1059. --]]
  1060. function Slab.ColorPicker(Options)
  1061. return ColorPicker.Begin(Options)
  1062. end
  1063. --[[
  1064. IsMouseDown
  1065. Determines if a given mouse button is down.
  1066. Button: [Number] The button to check for. The valid numbers are: 1 - Left, 2 - Right, 3 - Middle.
  1067. Return: [Boolean] True if the given button is down. False otherwise.
  1068. --]]
  1069. function Slab.IsMouseDown(Button)
  1070. return Mouse.IsPressed(Button and Button or 1)
  1071. end
  1072. --[[
  1073. IsMouseClicked
  1074. Determines if a given mouse button changes state from up to down this frame.
  1075. Button: [Number] The button to check for. The valid numbers are: 1 - Left, 2 - Right, 3 - Middle.
  1076. Return: [Boolean] True if the given button changes state from up to down. False otherwise.
  1077. --]]
  1078. function Slab.IsMouseClicked(Button)
  1079. return Mouse.IsClicked(Button and Button or 1)
  1080. end
  1081. --[[
  1082. IsMouseReleased
  1083. Determines if a given mouse button changes state from down to up this frame.
  1084. Button: [Number] The button to check for. The valid numbers are: 1 - Left, 2 - Right, 3 - Middle.
  1085. Return: [Boolean] True if the given button changes state from down to up. False otherwise.
  1086. --]]
  1087. function Slab.IsMouseReleased(Button)
  1088. return Mouse.IsReleased(Button and Button or 1)
  1089. end
  1090. --[[
  1091. IsMouseDoubleClicked
  1092. Determines if a given mouse button has been clicked twice within a given time frame.
  1093. Button: [Number] The button to check for. The valid numbers are: 1 - Left, 2 - Right, 3 - Middle.
  1094. Return: [Boolean] True if the given button was double clicked. False otherwise.
  1095. --]]
  1096. function Slab.IsMouseDoubleClicked(Button)
  1097. return Mouse.IsDoubleClicked(Button and Button or 1)
  1098. end
  1099. --[[
  1100. IsMouseDragging
  1101. Determines if a given mouse button is down and there has been movement.
  1102. Button: [Number] The button to check for. The valid numbers are: 1 - Left, 2 - Right, 3 - Middle.
  1103. Return: [Boolean] True if the button is held down and is moving. False otherwise.
  1104. --]]
  1105. function Slab.IsMouseDragging(Button)
  1106. return Mouse.IsDragging(Button and Button or 1)
  1107. end
  1108. --[[
  1109. GetMousePosition
  1110. Retrieves the current mouse position in the viewport.
  1111. Return: [Number], [Number] The X and Y coordinates of the mouse position.
  1112. --]]
  1113. function Slab.GetMousePosition()
  1114. return Mouse.Position()
  1115. end
  1116. --[[
  1117. GetMousePositionWindow
  1118. Retrieves the current mouse position within the current window. This position will include any transformations
  1119. added to the window such as scrolling.
  1120. Return: [Number], [Number] The X and Y coordinates of the mouse position within the window.
  1121. --]]
  1122. function Slab.GetMousePositionWindow()
  1123. return Window.GetMousePosition()
  1124. end
  1125. --[[
  1126. GetMouseDelta
  1127. Retrieves the change in mouse coordinates from the last frame.
  1128. Return: [Number], [Number] The X and Y coordinates of the delta from the last frame.
  1129. --]]
  1130. function Slab.GetMouseDelta()
  1131. return Mouse.GetDelta()
  1132. end
  1133. --[[
  1134. IsControlHovered
  1135. Checks to see if the last control added to the window is hovered by the mouse.
  1136. Return: [Boolean] True if the last control is hovered, false otherwise.
  1137. --]]
  1138. function Slab.IsControlHovered()
  1139. local Result = Window.IsItemHot()
  1140. if not Result then
  1141. local X, Y = Slab.GetMousePositionWindow()
  1142. Result = Cursor.IsInItemBounds(X, Y)
  1143. end
  1144. return Result
  1145. end
  1146. --[[
  1147. IsControlClicked
  1148. Checks to see if the previous control is hovered and clicked.
  1149. Button: [Number] The button to check for. The valid numbers are: 1 - Left, 2 - Right, 3 - Middle.
  1150. Return: [Boolean] True if the previous control is hovered and clicked. False otherwise.
  1151. --]]
  1152. function Slab.IsControlClicked(Button)
  1153. return Slab.IsControlHovered() and Slab.IsMouseClicked(Button)
  1154. end
  1155. --[[
  1156. GetControlSize
  1157. Retrieves the last declared control's size.
  1158. Return: [Number], [Number] The width and height of the last control declared.
  1159. --]]
  1160. function Slab.GetControlSize()
  1161. local X, Y, W, H = Cursor.GetItemBounds()
  1162. return W, H
  1163. end
  1164. --[[
  1165. IsVoidHovered
  1166. Checks to see if any non-Slab area of the viewport is hovered.
  1167. Return: [Boolean] True if any non-Slab area of the viewport is hovered. False otherwise.
  1168. --]]
  1169. function Slab.IsVoidHovered()
  1170. return Region.GetHotInstanceId() == '' and not Region.IsScrolling()
  1171. end
  1172. --[[
  1173. IsVoidClicked
  1174. Checks to see if any non-Slab area of the viewport is clicked.
  1175. Button: [Number] The button to check for. The valid numbers are: 1 - Left, 2 - Right, 3 - Middle.
  1176. Return: [Boolean] True if any non-Slab area of the viewport is clicked. False otherwise.
  1177. --]]
  1178. function Slab.IsVoidClicked(Button)
  1179. return Slab.IsMouseClicked(Button) and Slab.IsVoidHovered()
  1180. end
  1181. --[[
  1182. IsKeyDown
  1183. Checks to see if a specific key is held down. The key should be one of the love defined KeyConstant which the list can
  1184. be found at https://love2d.org/wiki/KeyConstant.
  1185. Key: [String] A love defined key constant.
  1186. Return: [Boolean] True if the key is held down. False otherwise.
  1187. --]]
  1188. function Slab.IsKeyDown(Key)
  1189. return Keyboard.IsDown(Key)
  1190. end
  1191. --[[
  1192. IsKeyPressed
  1193. Checks to see if a specific key state went from up to down this frame. The key should be one of the love defined KeyConstant which the list can
  1194. be found at https://love2d.org/wiki/KeyConstant.
  1195. Key: [String] A love defined key constant.
  1196. Return: [Boolean] True if the key state went from up to down this frame. False otherwise.
  1197. --]]
  1198. function Slab.IsKeyPressed(Key)
  1199. return Keyboard.IsPressed(Key, true)
  1200. end
  1201. --[[
  1202. IsKeyPressed
  1203. Checks to see if a specific key state went from down to up this frame. The key should be one of the love defined KeyConstant which the list can
  1204. be found at https://love2d.org/wiki/KeyConstant.
  1205. Key: [String] A love defined key constant.
  1206. Return: [Boolean] True if the key state went from down to up this frame. False otherwise.
  1207. --]]
  1208. function Slab.IsKeyReleased(Key)
  1209. return Keyboard.IsReleased(Key)
  1210. end
  1211. --[[
  1212. Rectangle
  1213. Draws a rectangle at the current cursor position for the active window.
  1214. Options: [Table] List of options that control how this rectangle is displayed.
  1215. Mode: [String] Whether this rectangle should be filled or outlined. The default value is 'fill'.
  1216. W: [Number] The width of the rectangle.
  1217. H: [Number] The height of the rectangle.
  1218. Color: [Table] The color to use for this rectangle.
  1219. Rounding: [Number] or [Table]
  1220. [Number] Amount of rounding to apply to all corners.
  1221. [Table] Define the rounding for each corner. The order goes top left, top right, bottom right, and bottom left.
  1222. Outline: [Boolean] If the Mode option is 'fill', this option will allow an outline to be drawn.
  1223. OutlineColor: [Table] The color to use for the outline if requested.
  1224. Segments: [Number] Number of points to add for each corner if rounding is requested.
  1225. Return: None.
  1226. --]]
  1227. function Slab.Rectangle(Options)
  1228. Shape.Rectangle(Options)
  1229. end
  1230. --[[
  1231. Circle
  1232. Draws a circle at the current cursor position plus the radius for the active window.
  1233. Options: [Table] List of options that control how this circle is displayed.
  1234. Mode: [String] Whether this circle should be filled or outlined. The default value is 'fill'.
  1235. Radius: [Number] The size of the circle.
  1236. Color: [Table] The color to use for the circle.
  1237. Segments: [Number] The number of segments used for drawing the circle.
  1238. Return: None.
  1239. --]]
  1240. function Slab.Circle(Options)
  1241. Shape.Circle(Options)
  1242. end
  1243. --[[
  1244. Triangle
  1245. Draws a triangle at the current cursor position plus the radius for the active window.
  1246. Option: [Table] List of options that control how this triangle is displayed.
  1247. Mode: [String] Whether this triangle should be filled or outlined. The default value is 'fill'.
  1248. Radius: [Number] The distance from the center of the triangle.
  1249. Rotation: [Number] The rotation of the triangle in degrees.
  1250. Color: [Table] The color to use for the triangle.
  1251. Return: None.
  1252. --]]
  1253. function Slab.Triangle(Options)
  1254. Shape.Triangle(Options)
  1255. end
  1256. --[[
  1257. Line
  1258. Draws a line starting at the current cursor position and going to the defined points in this function.
  1259. X2: [Number] The X coordinate for the destination.
  1260. Y2: [Number] The Y coordinate for the destination.
  1261. Option: [Table] List of options that control how this line is displayed.
  1262. Width: [Number] How thick the line should be.
  1263. Color: [Table] The color to use for the line.
  1264. Return: None.
  1265. --]]
  1266. function Slab.Line(X2, Y2, Options)
  1267. Shape.Line(X2, Y2, Options)
  1268. end
  1269. --[[
  1270. Curve
  1271. Draws a bezier curve with the given points as control points. The points should be defined in local space. Slab will translate the curve to the
  1272. current cursor position. There should two or more points defined for a proper curve.
  1273. Points: [Table] List of points to define the control points of the curve.
  1274. Options: [Table] List of options that control how this curve is displayed.
  1275. Color: [Table] The color to use for this curve.
  1276. Depth: [Number] The number of recursive subdivision steps to use when rendering the curve. If nil, the default LÖVE 2D value is used which is 5.
  1277. Return: None.
  1278. --]]
  1279. function Slab.Curve(Points, Options)
  1280. Shape.Curve(Points, Options)
  1281. end
  1282. --[[
  1283. GetCurveControlPointCount
  1284. Returns the number of control points defined with the last call to Curve.
  1285. Return: [Number] The number of control points defined for the previous curve.
  1286. --]]
  1287. function Slab.GetCurveControlPointCount()
  1288. return Shape.GetCurveControlPointCount()
  1289. end
  1290. --[[
  1291. GetCurveControlPoint
  1292. Returns the point for the given control point index. This point by default will be in local space defined by the points given in the Curve function.
  1293. The translated position can be requested by setting the LocalSpace option to false.
  1294. Index: [Number] The index of the control point to retrieve.
  1295. Options: [Table] A list of options that control what is returned by this function.
  1296. LocalSpace: [Boolean] Returns either the translated or untranslated control point. This is true by default.
  1297. Return: [Number], [Number] The translated X, Y coordinates of the given control point.
  1298. --]]
  1299. function Slab.GetCurveControlPoint(Index, Options)
  1300. return Shape.GetCurveControlPoint(Index, Options)
  1301. end
  1302. --[[
  1303. EvaluateCurve
  1304. Returns the point at the given time. The time value should be between 0 and 1 inclusive. The point returned will be in local space. For the translated
  1305. position, set the LocalSpace option to false.
  1306. Time: [Number] The time on the curve between 0 and 1.
  1307. Options: [Table] A list of options that control what is returned by this function.
  1308. LocalSpace: [Boolean] Returnes either the translated or untranslated control point. This is true by default.
  1309. Return: [Number], [Number] The X and Y coordinates at the given time on the curve.
  1310. --]]
  1311. function Slab.EvaluateCurve(Time, Options)
  1312. return Shape.EvaluateCurve(Time, Options)
  1313. end
  1314. --[[
  1315. EvaluateCurveMouse
  1316. Returns the point on the curve at the given X-coordinate of the mouse relative to the end points of the curve.
  1317. Options: [Table] A list of options that control what is returned by this function.
  1318. Refer to the documentation for EvaluateCurve for the list of options.
  1319. Return: [Number], [Number] The X and Y coordinates at the given X mouse position on the curve.
  1320. --]]
  1321. function Slab.EvaluateCurveMouse(Options)
  1322. local X1, Y1 = Slab.GetCurveControlPoint(1, {LocalSpace = false})
  1323. local X2, Y2 = Slab.GetCurveControlPoint(Slab.GetCurveControlPointCount(), {LocalSpace = false})
  1324. local Left = math.min(X1, X2)
  1325. local W = math.abs(X2 - X1)
  1326. local X, Y = Slab.GetMousePositionWindow()
  1327. local Offset = math.max(X - Left, 0.0)
  1328. Offset = math.min(Offset, W)
  1329. return Slab.EvaluateCurve(Offset / W, Options)
  1330. end
  1331. --[[
  1332. Polygon
  1333. Renders a polygon with the given points. The points should be defined in local space. Slab will translate the position to the current cursor position.
  1334. Points: [Table] List of points that define this polygon.
  1335. Options: [Table] List of options that control how this polygon is drawn.
  1336. Color: [Table] The color to render this polygon.
  1337. Mode: [String] Whether to use 'fill' or 'line' to draw this polygon. The default is 'fill'.
  1338. Return: None.
  1339. --]]
  1340. function Slab.Polygon(Points, Options)
  1341. Shape.Polygon(Points, Options)
  1342. end
  1343. return Slab