API.lua 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 ComboBox = require(SLAB_PATH .. '.Internal.UI.ComboBox')
  26. local Cursor = require(SLAB_PATH .. '.Internal.Core.Cursor')
  27. local Dialog = require(SLAB_PATH .. '.Internal.UI.Dialog')
  28. local DrawCommands = require(SLAB_PATH .. '.Internal.Core.DrawCommands')
  29. local Image = require(SLAB_PATH .. '.Internal.UI.Image')
  30. local Input = require(SLAB_PATH .. '.Internal.UI.Input')
  31. local Keyboard = require(SLAB_PATH .. '.Internal.Input.Keyboard')
  32. local ListBox = require(SLAB_PATH .. '.Internal.UI.ListBox')
  33. local Mouse = require(SLAB_PATH .. '.Internal.Input.Mouse')
  34. local Menu = require(SLAB_PATH .. '.Internal.UI.Menu')
  35. local MenuState = require(SLAB_PATH .. '.Internal.UI.MenuState')
  36. local MenuBar = require(SLAB_PATH .. '.Internal.UI.MenuBar')
  37. local Separator = require(SLAB_PATH .. '.Internal.UI.Separator')
  38. local Style = require(SLAB_PATH .. '.Style')
  39. local Text = require(SLAB_PATH .. '.Internal.UI.Text')
  40. local Tree = require(SLAB_PATH .. '.Internal.UI.Tree')
  41. local Window = require(SLAB_PATH .. '.Internal.UI.Window')
  42. --[[
  43. Slab
  44. Slab is an immediate mode GUI toolkit for the Love 2D framework. This library is designed to
  45. allow users to easily add this library to their existing Love 2D projects and quickly create
  46. tools to enable them to iterate on their ideas quickly. The user should be able to utilize this
  47. library with minimal integration steps and is completely written in Lua and utilizes
  48. the Love 2D API. No compiled binaries are required and the user will have access to the source
  49. so that they may make adjustments that meet the needs of their own projects and tools. Refer
  50. to main.lua and SlabTest.lua for example usage of this library.
  51. Supported Version: 11.2.0
  52. API:
  53. Initialize
  54. GetVersion
  55. GetLoveVersion
  56. Update
  57. Draw
  58. GetStyle
  59. BeginWindow
  60. EndWindow
  61. GetWindowPosition
  62. GetWindowSize
  63. GetWindowContentSize
  64. BeginMainMenuBar
  65. EndMainMenuBar
  66. BeginMenuBar
  67. EndMenuBar
  68. BeginMenu
  69. EndMenu
  70. BeginContextMenuItem
  71. BeginContextMenuWindow
  72. EndContextMenu
  73. MenuItem
  74. MenuItemChecked
  75. Separator
  76. Button
  77. Text
  78. TextSelectable
  79. Textf
  80. CheckBox
  81. Input
  82. GetInputText
  83. BeginTree
  84. EndTree
  85. BeginComboBox
  86. EndComboBox
  87. Image
  88. Cursor:
  89. SameLine
  90. NewLine
  91. SetCursorPos
  92. Properties
  93. ListBox:
  94. BeginListBox
  95. EndListBox
  96. BeginListBoxItem
  97. IsListBoxItemClicked
  98. EndListBoxItem
  99. Dialog:
  100. OpenDialog
  101. BeginDialog
  102. EndDialog
  103. CloseDialog
  104. MessageBox
  105. FileDialog
  106. --]]
  107. local Slab = {}
  108. -- Slab version numbers.
  109. local Version_Major = 0
  110. local Version_Minor = 2
  111. local Version_Revision = 0
  112. local function TextInput(Ch)
  113. Input.Text(Ch)
  114. if love.textinput ~= nil then
  115. love.textinput(Ch)
  116. end
  117. end
  118. local function WheelMoved(X, Y)
  119. Window.WheelMoved(X, Y)
  120. if love.wheelmoved ~= nil then
  121. love.wheelmoved(X, Y)
  122. end
  123. end
  124. --[[
  125. Initialize
  126. Initializes Slab and hooks into the required events. This function should be called in love.load.
  127. args: [Table] The list of parameters passed in by the user on the command-line. This should be passed in from
  128. love.load function.
  129. Return: None.
  130. --]]
  131. function Slab.Initialize(args)
  132. Style.Initialize()
  133. love.handlers['textinput'] = TextInput
  134. love.handlers['wheelmoved'] = WheelMoved
  135. end
  136. --[[
  137. GetVersion
  138. Retrieves the current version of Slab being used as a string.
  139. Return: [String] String of the current Slab version.
  140. --]]
  141. function Slab.GetVersion()
  142. return string.format("%d.%d.%d", Version_Major, Version_Minor, Version_Revision)
  143. end
  144. --[[
  145. GetLoveVersion
  146. Retrieves the current version of Love being used as a string.
  147. Return: [String] String of the current Love version.
  148. --]]
  149. function Slab.GetLoveVersion()
  150. local Major, Minor, Revision, Codename = love.getVersion()
  151. return string.format("%d.%d.%d - %s", Major, Minor, Revision, Codename)
  152. end
  153. --[[
  154. Update
  155. Updates the input state and states of various widgets. This function must be called every frame.
  156. This should be called before any Slab calls are made to ensure proper responses to Input are made.
  157. dt: [Number] The delta time for the frame. This should be passed in from love.update.
  158. Return: None.
  159. --]]
  160. function Slab.Update(dt)
  161. Mouse.Update()
  162. Keyboard.Update()
  163. Input.Update(dt)
  164. DrawCommands.Reset()
  165. Window.Reset()
  166. if MenuState.IsOpened then
  167. MenuState.WasOpened = MenuState.IsOpened
  168. if Mouse.IsClicked(1) then
  169. MenuState.RequestClose = true
  170. end
  171. end
  172. end
  173. --[[
  174. Draw
  175. This function will execute all buffered draw calls from the various Slab calls made prior. This
  176. function should be called from love.draw and should be called at the very to ensure Slab is rendered
  177. above the user's workspace.
  178. Return: None.
  179. --]]
  180. function Slab.Draw()
  181. Window.Validate()
  182. if MenuState.RequestClose then
  183. Menu.Close()
  184. MenuBar.Clear()
  185. end
  186. if Mouse.IsReleased(1) then
  187. Button.ClearClicked()
  188. end
  189. DrawCommands.Execute()
  190. end
  191. --[[
  192. GetStyle
  193. Retrieve the style table associated with the current instance of Slab. This will allow the user to add custom styling
  194. to their controls.
  195. Return: [Table] The style table.
  196. --]]
  197. function Slab.GetStyle()
  198. return Style
  199. end
  200. --[[
  201. BeginWindow
  202. This function begins the process of drawing widgets to a window. This function must be followed up with
  203. an EndWindow call to ensure proper behavior of drawing windows.
  204. Id: [String] A unique string identifying this window in the project.
  205. Options: [Table] List of options that control how this window will behave.
  206. X: [Number] The X position to start rendering the window at.
  207. Y: [Number] The Y position to start rendering the window at.
  208. W: [Number] The starting width of the window.
  209. H: [Number] The starting height of the window.
  210. ContentW: [Number] The starting width of the content contained within this window.
  211. ContentH: [Number] The starting height of the content contained within this window.
  212. BgColor: [Table] The background color value for this window. Will use the default style WindowBackgroundColor if this is empty.
  213. Title: [String] The title to display for this window. If emtpy, no title bar will be rendered and the window will not be movable.
  214. AllowMove: [Boolean] Controls whether the window is movable within the title bar area. The default value is true.
  215. AllowResize: [Boolean] Controls whether the window is resizable. The default value is true. AutoSizeWindow must be false for this to work.
  216. AllowFocus: [Boolean] Controls whether the window can be focused. The default value is true.
  217. Border: [Number] The value which controls how much empty space should be left between all sides of the window from the content.
  218. The default value is 4.0
  219. NoOutline: [Boolean] Controls whether an outline should not be rendered. The default value is false.
  220. 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
  221. system. The default value is false.
  222. AutoSizeWindow: [Boolean] Automatically updates the window size to match the content size. The default value is true.
  223. AutoSizeWindowW: [Boolean] Automatically update the window width to match the content size. This value is taken from AutoSizeWindow by default.
  224. AutoSizeWindowH: [Boolean] Automatically update the window height to match the content size. This value is taken from AutoSizeWindow by default.
  225. AutoSizeContent: [Boolean] The content size of the window is automatically updated with each new widget. The default value is true.
  226. Layer: [String] The layer to which to draw this window. This is used internally and should be ignored by the user.
  227. ResetPosition: [Boolean] Determines if the window should reset any delta changes to its position.
  228. ResetSize: [Boolean] Determines if the window should reset any delta changes to its size.
  229. ResetContent: [Boolean] Determines if the window should reset any delta changes to its content size.
  230. ResetLayout: [Boolean] Will reset the position, size, and content. Short hand for the above 3 flags.
  231. SizerFilter: [Table] Specifies what sizers are enabled for the window. If nothing is specified, all sizers are available. The values can
  232. be: NW, NE, SW, SE, N, S, E, W
  233. CanObstruct: [Boolean] Sets whether this window is considered for obstruction of other windows and their controls. The default value is true.
  234. Return: None
  235. --]]
  236. function Slab.BeginWindow(Id, Options)
  237. Window.Begin(Id, Options)
  238. end
  239. --[[
  240. EndWindow
  241. This function must be called after a BeginWindow and associated widget calls. If the user fails to call this, an assertion will be thrown
  242. to alert the user.
  243. Return: None.
  244. --]]
  245. function Slab.EndWindow()
  246. Window.End()
  247. end
  248. --[[
  249. GetWindowPosition
  250. Retrieves the active window's position.
  251. Return: [Number], [Number] The X and Y position of the active window.
  252. --]]
  253. function Slab.GetWindowPosition()
  254. return Window.GetPosition()
  255. end
  256. --[[
  257. GetWindowSize
  258. Retrieves the active window's size.
  259. Return: [Number], [Number] The width and height of the active window.
  260. --]]
  261. function Slab.GetWindowSize()
  262. return Window.GetSize()
  263. end
  264. --[[
  265. GetWindowContentSize
  266. Retrieves the active window's content size.
  267. Return: [Number], [Number] The width and height of the active window content.
  268. --]]
  269. function Slab.GetWindowContentSize()
  270. return Window.GetContentSize()
  271. end
  272. --[[
  273. BeginMainMenuBar
  274. This function begins the process for setting up the main menu bar. This should be called outside of any BeginWindow/EndWindow calls.
  275. The user should only call EndMainMenuBar if this function returns true. Use BeginMenu/EndMenu calls to add menu items on the main menu bar.
  276. Example:
  277. if Slab.BeginMainMenuBar() then
  278. if Slab.BeginMenu("File") then
  279. if Slab.MenuItem("Quit") then
  280. love.event.quit()
  281. end
  282. Slab.EndMenu()
  283. end
  284. Slab.EndMainMenuBar()
  285. end
  286. Return: [Boolean] Returns true if the main menu bar process has started.
  287. --]]
  288. function Slab.BeginMainMenuBar()
  289. Cursor.SetPosition(0.0, 0.0)
  290. return Slab.BeginMenuBar(true)
  291. end
  292. --[[
  293. EndMainMenuBar
  294. This function should be called if BeginMainMenuBar returns true.
  295. Return: None.
  296. --]]
  297. function Slab.EndMainMenuBar()
  298. Slab.EndMenuBar()
  299. end
  300. --[[
  301. BeginMenuBar
  302. This function begins the process of rendering a menu bar for a window. This should only be called within a BeginWindow/EndWindow context.
  303. IsMainMenuBar: [Boolean] Is this menu bar for the main viewport. Used internally. Should be ignored for all other calls.
  304. Return: [Boolean] Returns true if the menu bar process has started.
  305. --]]
  306. function Slab.BeginMenuBar(IsMainMenuBar)
  307. return MenuBar.Begin(IsMainMenuBar)
  308. end
  309. --[[
  310. EndMenuBar
  311. This function should be called if BeginMenuBar returns true.
  312. Return: None.
  313. --]]
  314. function Slab.EndMenuBar()
  315. MenuBar.End()
  316. end
  317. --[[
  318. BeginMenu
  319. Adds a menu item that when the user hovers over, opens up an additional context menu. When used within a menu bar, BeginMenu calls
  320. 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
  321. the user more options are available. If this function returns true, the user must call EndMenu.
  322. Label: [String] The label to display for this menu.
  323. Return: [Boolean] Returns true if the menu item is being hovered.
  324. --]]
  325. function Slab.BeginMenu(Label)
  326. return Menu.BeginMenu(Label)
  327. end
  328. --[[
  329. EndMenu
  330. Finishes up a BeginMenu. This function must be called if BeginMenu returns true.
  331. Return: None.
  332. --]]
  333. function Slab.EndMenu()
  334. Menu.EndMenu()
  335. end
  336. --[[
  337. BeginContextMenuItem
  338. 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
  339. call to open up a context menu for that specific item. If this function returns true, EndContextMenu must be called.
  340. Example:
  341. if Slab.Button("Button!") then
  342. -- Perform logic here when button is clicked
  343. end
  344. -- This will only return true if the previous button is hot and the user right-clicks.
  345. if Slab.BeginContextMenuItem() then
  346. Slab.MenuItem("Button Item 1")
  347. Slab.MenuItem("Button Item 2")
  348. Slab.EndContextMenu()
  349. end
  350. Return: [Boolean] Returns true if the user right clicks on the previous item call. EndContextMenu must be called in order for
  351. this to function properly.
  352. --]]
  353. function Slab.BeginContextMenuItem()
  354. return Menu.BeginContextMenu({IsItem = true})
  355. end
  356. --[[
  357. BeginContextMenuWindow
  358. 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
  359. of a window's widget calls so that Slab can catch any BeginContextMenuItem calls before this call. If this function returns true,
  360. EndContextMenu must be called.
  361. Return: [Boolean] Returns true if the user right clicks anywhere within the window. EndContextMenu must be called in order for this
  362. to function properly.
  363. --]]
  364. function Slab.BeginContextMenuWindow()
  365. return Menu.BeginContextMenu({IsWindow = true})
  366. end
  367. --[[
  368. EndContextMenu
  369. Finishes up any BeginContextMenuItem/BeginContextMenuWindow if they return true.
  370. Return: None.
  371. --]]
  372. function Slab.EndContextMenu()
  373. Menu.EndContextMenu()
  374. end
  375. --[[
  376. MenuItem
  377. Adds a menu item to a given context menu.
  378. Label: [String] The label to display to the user.
  379. Return: [Boolean] Returns true if the user clicks on this menu item.
  380. --]]
  381. function Slab.MenuItem(Label)
  382. return Menu.MenuItem(Label)
  383. end
  384. --[[
  385. MenuItemChecked
  386. Adds a menu item to a given context menu. If IsChecked is true, then a check mark will be rendered next to the
  387. label.
  388. Example:
  389. local Checked = false
  390. if Slab.MenuItemChecked("Menu Item", Checked)
  391. Checked = not Checked
  392. end
  393. Label: [String] The label to display to the user.
  394. IsChecked: [Boolean] Determines if a check mark should be rendered next to the label.
  395. Return: [Boolean] Returns true if the user clicks on this menu item.
  396. --]]
  397. function Slab.MenuItemChecked(Label, IsChecked)
  398. return Menu.MenuItemChecked(Label, IsChecked)
  399. end
  400. --[[
  401. Separator
  402. This functions renders a separator line in the window.
  403. Return: None.
  404. --]]
  405. function Slab.Separator()
  406. Separator.Begin()
  407. end
  408. --[[
  409. Button
  410. Adds a button to the active window.
  411. Label: [String] The label to display on the button.
  412. Options: [Table] List of options for how this button will behave.
  413. Tooltip: [String] The tooltip to display when the user hovers over this button.
  414. AlignRight: [Boolean] Flag to push this button to the right side of the window.
  415. Return: [Boolean] Returns true if the user clicks on this button.
  416. --]]
  417. function Slab.Button(Label, Options)
  418. return Button.Begin(Label, Options)
  419. end
  420. --[[
  421. Text
  422. Adds text to the active window.
  423. Label: [String] The string to be displayed in the window.
  424. Options: [Table] List of options for how this text is displayed.
  425. Color: [Table] The color to render the text.
  426. Pad: [Number] How far to pad the text from the left side of the current cursor position.
  427. IsSelectable: [Boolean] Whether this text is selectable using the text's Y position and the window X and width as the
  428. hot zone.
  429. IsSelectableTextOnly: [Boolean] Only available if IsSelectable is true. Will use the text width instead of the
  430. window width to determine the hot zone.
  431. IsSelected: [Boolean] Forces the hover background to be rendered.
  432. SelectOnHover: [Boolean] Returns true if the user is hovering over the hot zone of this text.
  433. HoverColor: [Table] The color to render the background if the IsSelected option is true.
  434. Return: [Boolean] Returns true if SelectOnHover option is set to true. False otherwise.
  435. --]]
  436. function Slab.Text(Label, Options)
  437. return Text.Begin(Label, Options)
  438. end
  439. --[[
  440. TextSelectable
  441. This function is a shortcut for SlabText with the IsSelectable option set to true.
  442. Label: [String] The string to be displayed in the window.
  443. Options: [Table] List of options for how this text is displayed.
  444. See Slab.Text for all options.
  445. Return: [Boolean] Returns true if user clicks on this text. False otherwise.
  446. --]]
  447. function Slab.TextSelectable(Label, Options)
  448. Options = Options == nil and {} or Options
  449. Options.IsSelectable = true
  450. return Slab.Text(Label, Options)
  451. end
  452. --[[
  453. Textf
  454. Adds formatted text to the active window. This text will wrap to fit within the contents of
  455. either the window or a user specified width.
  456. Label: [String] The text to be rendered.
  457. Options: [Table] List of options for how this text is displayed.
  458. Color: [Table] The color to render the text.
  459. W: [Number] The width to restrict the text to. If this option is not specified, then the window
  460. width is used.
  461. Align: [String] The alignment to use for this text. For more information, refer to the love documentation
  462. at https://love2d.org/wiki/AlignMode. Below are the available options:
  463. center: Align text center.
  464. left: Align text left.
  465. right: Align text right.
  466. justify: Align text both left and right.
  467. Return: None.
  468. --]]
  469. function Slab.Textf(Label, Options)
  470. Text.BeginFormatted(Label, Options)
  471. end
  472. --[[
  473. CheckBox
  474. Renders a check box with a label. The check box when enabled will render an 'X'.
  475. Enabled: [Boolean] Will render an 'X' within the box if true. Will be an empty box otherwise.
  476. Label: [String] The label to display after the check box.
  477. Options: [Table] List of options for how this check box will behave.
  478. Tooltip: [String] Text to be displayed if the user hovers over the check box.
  479. Id: [String] An optional Id that can be supplied by the user. By default, the Id will be the label.
  480. Return: [Boolean] Returns true if the user clicks within the check box.
  481. --]]
  482. function Slab.CheckBox(Enabled, Label, Options)
  483. return CheckBox.Begin(Enabled, Label, Options)
  484. end
  485. --[[
  486. Input
  487. This function will render an input box for a user to input text in. This widget behaves like input boxes
  488. found in other applications. This function will only return true if it has focus and user has either input
  489. text or pressed the return key.
  490. Example:
  491. local Text = "Hello World"
  492. if Slab.Input('Example', {Text = Text}) then
  493. Text = Slab.GetInputText()
  494. end
  495. Id: [String] A string that uniquely identifies this Input within the context of the window.
  496. Options: [Table] List of options for how this Input will behave.
  497. Tooltip: [String] Text to be displayed if the user hovers over the Input box.
  498. ReturnOnText: [Boolean] Will cause this function to return true whenever the user has input
  499. a new character into the Input box. This is true by default.
  500. Text: [String] The text to be supplied to the input box. It is recommended to use this option
  501. when ReturnOnText is true.
  502. BgColor: [Table] The background color for the input box.
  503. SelectColor: [Table] The color used when the user is selecting text within the input box.
  504. SelectOnFocus: [Boolean] When this input box is focused by the user, the text contents within the input
  505. will be selected. This is true by default.
  506. NumbersOnly: [Boolean] When true, only numeric characters and the '.' character are allowed to be input into
  507. the input box. If no text is input, the input box will display '0'.
  508. W: [Number] The width of the input box. By default, will be 150.0
  509. H: [Number] The height of the input box. By default, will be the height of the current font.
  510. ReadOnly: [Boolean] Whether this input field can be editable or not.
  511. Align: [String] Aligns the text within the input box. Options are:
  512. left: Aligns the text to the left. This will be set when this Input is focused.
  513. center: Aligns the text in the center. This is the default for when the text is not focused.
  514. Return: [Boolean] Returns true if the user has pressed the return key while focused on this input box. If ReturnOnText
  515. is set to true, then this function will return true whenever the user has input any character into the input box.
  516. --]]
  517. function Slab.Input(Id, Options)
  518. return Input.Begin(Id, Options)
  519. end
  520. --[[
  521. GetInputText
  522. Retrieves the text entered into the focused input box. Refer to the documentation for Slab.Input for an example on how to
  523. use this function.
  524. Return: [String] Returns the text entered into the focused input box.
  525. --]]
  526. function Slab.GetInputText()
  527. return Input.GetText()
  528. end
  529. --[[
  530. BeginTree
  531. This function will render a tree item with an optional label. The tree can be expanded or collapsed based on whether
  532. the user clicked on the tree item. This function can also be nested to create a hierarchy of tree items. This function
  533. will return false when collapsed and true when expanded. If this function returns true, Slab.EndTree must be called in
  534. 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
  535. of the window by default.
  536. Id: [String] A string uniquely identifying this tree item within the context of the window.
  537. Options: [Table] List of options for how this tree item will behave.
  538. Label: [String] The text to be rendered for this tree item.
  539. Tooltip: [String] The text to be rendered when the user hovers over this tree item.
  540. IsLeaf: [Boolean] If this is true, this tree item will not be expandable/collapsable.
  541. OpenWithHighlight: [Boolean] If this is true, the tree will be expanded/collapsed when the user hovers over the hot
  542. zone of this tree item. If this is false, the user must click the expand/collapse icon to interact with this tree
  543. item.
  544. Icon: [Object] A user supplied image. This must be a valid Love image or the call will assert.
  545. IconPath: [String] If the Icon option is nil, then a path can be specified. Slab will load and
  546. manage the image resource.
  547. IsSelected: [Boolean] If true, will render a highlight rectangle around the tree item.
  548. IsOpen: [Boolean] Will force the tree item to be expanded.
  549. Return: [Boolean] Returns true if this tree item is expanded. Slab.EndTree must be called if this returns true.
  550. --]]
  551. function Slab.BeginTree(Id, Options)
  552. return Tree.Begin(Id, Options)
  553. end
  554. --[[
  555. EndTree
  556. Finishes up any BeginTree calls if those functions return true.
  557. Return: None.
  558. --]]
  559. function Slab.EndTree()
  560. Tree.End()
  561. end
  562. --[[
  563. BeginComboBox
  564. This function renders a non-editable input field with a drop down arrow. When the user clicks this option, a window is
  565. created and the user can supply their own Slab.TextSelectable calls to add possible items to select from. This function
  566. will return true if the combo box is opened. Slab.EndComboBox must be called if this function returns true.
  567. Example:
  568. local Options = {"Apple", "Banana", "Orange", "Pear", "Lemon"}
  569. local Options_Selected = ""
  570. if Slab.BeginComboBox('Fruits', {Selected = Options_Selected}) then
  571. for K, V in pairs(Options) do
  572. if Slab.TextSelectable(V) then
  573. Options_Selected = V
  574. end
  575. end
  576. Slab.EndComboBox()
  577. end
  578. Id: [String] A string that uniquely identifies this combo box within the context of the active window.
  579. Options: [Table] List of options that control how this combo box behaves.
  580. Tooltip: [String] Text that is rendered when the user hovers over this combo box.
  581. Selected: [String] Text that is displayed in the non-editable input box for this combo box.
  582. W: [Number] The width of the combo box. The default value is 150.0.
  583. Return: [Boolean] This function will return true if the combo box is open.
  584. --]]
  585. function Slab.BeginComboBox(Id, Options)
  586. return ComboBox.Begin(Id, Options)
  587. end
  588. --[[
  589. EndComboBox
  590. Finishes up any BeginComboBox calls if those functions return true.
  591. Return: None.
  592. --]]
  593. function Slab.EndComboBox()
  594. ComboBox.End()
  595. end
  596. --[[
  597. Image
  598. Draws an image at the current cursor position. The Id uniquely identifies this
  599. image to manage behaviors with this image. An image can be supplied through the
  600. options or a path can be specified which Slab will manage the loading and storing of
  601. the image reference.
  602. Id: [String] A string uniquely identifying this image within the context of the current window.
  603. Options: [Table] List of options controlling how the image should be drawn.
  604. Image: [Object] A user supplied image. This must be a valid Love image or the call will assert.
  605. Path: [String] If the Image option is nil, then a path must be specified. Slab will load and
  606. manage the image resource.
  607. Rotation: [Number] The rotation value to apply when this image is drawn.
  608. Scale: [Number] The scale value to apply to both the X and Y axis.
  609. ScaleX: [Number] The scale value to apply to the X axis.
  610. ScaleY: [Number] The scale value to apply to the Y axis.
  611. Color: [Table] The color to use when rendering this image.
  612. ReturnOnHover: [Boolean] Returns true when the mouse is hovered over the image.
  613. ReturnOnClick: [Boolean] Returns true when the mouse is released over the image.
  614. SubX: [Number] The X-coordinate used inside the given image.
  615. SubY: [Number] The Y-coordinate used inside the given image.
  616. SubW: [Number] The width used inside the given image.
  617. SubH: [Number] The height used insided the given image.
  618. Return: [Boolean] Returns true if the mouse is hovering over the image or clicking on the image based on
  619. ReturnOnHover or ReturnOnClick options.
  620. --]]
  621. function Slab.Image(Id, Options)
  622. return Image.Begin(Id, Options)
  623. end
  624. --[[
  625. SameLine
  626. This forces the cursor to move back up to the same line as the previous widget. By default, all Slab widgets will
  627. advance the cursor to the next line based on the height of the current line. By using this call with other widget
  628. calls, the user will be able to set up multiple widgets on the same line to control how a window may look.
  629. Options: [Table] List of options that controls how the cursor should handle the same line.
  630. Pad: [Number] Extra padding to apply in the X direction.
  631. CenterY: [Boolean] Controls whether the cursor should be centered in the Y direction on the line. By default
  632. the line will use the NewLineSize, which is the height of the current font to center the cursor.
  633. Return: None.
  634. --]]
  635. function Slab.SameLine(Options)
  636. Cursor.SameLine(Options)
  637. end
  638. --[[
  639. NewLine
  640. This forces the cursor to advance to the next line based on the height of the current font.
  641. Return: None.
  642. --]]
  643. function Slab.NewLine()
  644. Cursor.NewLine()
  645. end
  646. --[[
  647. SetCursorPos
  648. Sets the cursor position. The default behavior is to set the cursor position relative to
  649. the current window. The absolute position can be set if the 'Absolute' option is set.
  650. Controls will only be drawn within a window. If the cursor is set outside of the current
  651. window context, the control will not be displayed.
  652. X: [Number] The X coordinate to place the cursor. If nil, then the X coordinate is not modified.
  653. Y: [Number] The Y coordinate to place the cursor. If nil, then the Y coordinate is not modified.
  654. Options: [Table] List of options that control how the cursor position should be set.
  655. Absolute: [Boolean] If true, will place the cursor using absolute coordinates.
  656. Return: None.
  657. --]]
  658. function Slab.SetCursorPos(X, Y, Options)
  659. Options = Options == nil and {} or Options
  660. Options.Absolute = Options.Absolute == nil and false or Options.Absolute
  661. if Options.Absolute then
  662. X = X == nil and Cursor.GetX() or X
  663. Y = Y == nil and Cursor.GetY() or Y
  664. Cursor.SetPosition(X, Y)
  665. else
  666. X = X == nil and Cursor.GetX() - Cursor.GetAnchorX() or X
  667. Y = Y == nil and Cursor.GetY() - Cursor.GetAnchorY() or Y
  668. Cursor.SetRelativePosition(X, Y)
  669. end
  670. end
  671. --[[
  672. Properties
  673. Iterates through the table's key-value pairs and adds them to the active window. This currently only does
  674. a shallow loop and will not iterate through nested tables.
  675. TODO: Iterate through nested tables.
  676. Table: [Table] The list of properties to build widgets for.
  677. Return: None.
  678. --]]
  679. function Slab.Properties(Table)
  680. if Table ~= nil then
  681. for K, V in pairs(Table) do
  682. local Type = type(V)
  683. if Type == "boolean" then
  684. if Slab.CheckBox(V, K) then
  685. Table[K] = not Table[K]
  686. end
  687. elseif Type == "number" then
  688. Slab.Text(K .. ": ")
  689. Slab.SameLine()
  690. if Slab.Input(K, {Text = tostring(V), NumbersOnly = true, ReturnOnText = false}) then
  691. Table[K] = tonumber(Slab.GetInputText())
  692. end
  693. elseif Type == "string" then
  694. Slab.Text(K .. ": ")
  695. Slab.SameLine()
  696. if Slab.Input(K, {Text = V, ReturnOnText = false}) then
  697. Table[K] = Slab.GetInputText()
  698. end
  699. end
  700. end
  701. end
  702. end
  703. --[[
  704. BeginListBox
  705. Begins the process of creating a list box. If this function is called, EndListBox must be called after all
  706. items have been added.
  707. Id: [String] A string uniquely identifying this list box within the context of the current window.
  708. Options: [Table] List of options controlling the behavior of the list box.
  709. W: [Number] The width of the list box. If nil, then the width of the window is used.
  710. H: [Number] The height of the list box. If nil, a default value of 150 is used. If H is 0, then
  711. the list box will stretch to the height of the window.
  712. Clear: [Boolean] Clears out the items in the list. It is recommended to only call this if the list items
  713. has changed and should not be set to true on every frame.
  714. Return: None.
  715. --]]
  716. function Slab.BeginListBox(Id, Options)
  717. ListBox.Begin(Id, Options)
  718. end
  719. --[[
  720. EndListBox
  721. Ends the list box container. Will close off the region and properly adjust the cursor.
  722. Return: None.
  723. --]]
  724. function Slab.EndListBox()
  725. ListBox.End()
  726. end
  727. --[[
  728. BeginListBoxItem
  729. Adds an item to the current list box with the given Id. The user can then draw controls however they see
  730. fit to display a single item. This allows the user to draw list items such as a texture with a name or just
  731. a text to represent the item. If this is called, EndListBoxItem must be called to complete the item.
  732. Id: [String] A string uniquely identifying this item within the context of the current list box.
  733. Options: [Table] List of options that control the behavior of the active list item.
  734. Selected: [Boolean] If true, will draw the item with a selection background.
  735. Return: None.
  736. --]]
  737. function Slab.BeginListBoxItem(Id, Options)
  738. ListBox.BeginItem(Id, Options)
  739. end
  740. --[[
  741. IsListBoxItemClicked
  742. Checks to see if a hot list item is clicked. This should only be called within a BeginListBoxLitem/EndListBoxItem
  743. block.
  744. Button: [Number] The button to check for the click of the item.
  745. IsDoubleClick: [Boolean] Check for double-click instead of single click.
  746. Return: [Boolean] Returns true if the active item is hovered with mouse and the requested mouse button is clicked.
  747. --]]
  748. function Slab.IsListBoxItemClicked(Button, IsDoubleClick)
  749. return ListBox.IsItemClicked(Button, IsDoubleClick)
  750. end
  751. --[[
  752. EndListBoxItem
  753. Ends the current item and commits the bounds of the item to the list.
  754. Return: None.
  755. --]]
  756. function Slab.EndListBoxItem()
  757. ListBox.EndItem()
  758. end
  759. --[[
  760. OpenDialog
  761. Opens the dialog box with the given Id. If the dialog box was opened, then it is pushed onto the stack.
  762. Calls to the BeginDialog with this same Id will return true if opened.
  763. Id: [String] A string uniquely identifying this dialog box.
  764. Return: None.
  765. --]]
  766. function Slab.OpenDialog(Id)
  767. Dialog.Open(Id)
  768. end
  769. --[[
  770. BeginDialog
  771. Begins the dialog window with the given Id if it is open. If this function returns true, then EndDialog must be called.
  772. Dialog boxes are windows which are centered in the center of the viewport. The dialog box cannot be moved and will
  773. capture all input from all other windows.
  774. Id: [String] A string uniquely identifying this dialog box.
  775. Options: [Table] List of options that control how this dialog box behaves. These are the same parameters found
  776. for BeginWindow, with some caveats. Certain options are overridden by the Dialog system. They are:
  777. X, Y, Layer, AllowFocus, AllowMove, and AutoSizeWindow.
  778. Return: [Boolean] Returns true if the dialog with the given Id is open.
  779. --]]
  780. function Slab.BeginDialog(Id, Options)
  781. return Dialog.Begin(Id, Options)
  782. end
  783. --[[
  784. EndDialog
  785. Ends the dialog window if a call to BeginDialog returns true.
  786. Return: None.
  787. --]]
  788. function Slab.EndDialog()
  789. Dialog.End()
  790. end
  791. --[[
  792. CloseDialog
  793. Closes the currently active dialog box.
  794. Return: None.
  795. --]]
  796. function Slab.CloseDialog()
  797. Dialog.Close()
  798. end
  799. --[[
  800. MessageBox
  801. Opens a message box to be displayed to the user with a title and a message. Buttons can be specified through the options
  802. table which when clicked, the string of the button is returned. This function should be called every frame when a message
  803. box wants to be displayed.
  804. Title: [String] The title to display for the message box.
  805. Message: [String] The message to be displayed. The text is aligned in the center. Multi-line strings are supported.
  806. Options: [Table] List of options to control the behavior of the message box.
  807. Buttons: [Table] List of buttons to display with the message box. The order of the buttons are displayed from right to left.
  808. Return: [String] The name of the button that was clicked. If none was clicked, an emtpy string is returned.
  809. --]]
  810. function Slab.MessageBox(Title, Message, Options)
  811. return Dialog.MessageBox(Title, Message, Options)
  812. end
  813. --[[
  814. FileDialog
  815. Opens up a dialog box that displays a file explorer for opening or saving files or directories. This function does not create any file
  816. handles, it just returns the list of files selected by the user.
  817. Options: [Table] List of options that control the behavior of the file dialog.
  818. AllowMultiSelect: [Boolean] Allows the user to select multiple items in the file dialog.
  819. Directory: [String] The starting directory when the file dialog is open. If none is specified, the dialog
  820. will start at love.filesystem.getSourceBaseDirectory and the dialog will remember the last
  821. directory navigated to by the user between calls to this function.
  822. Type: [String] The type of file dialog to use. The options are:
  823. openfile: This is the default method. The user will have access to both directories and files. However,
  824. only file selections are returned.
  825. opendirectory: This type is used to filter the file dialog for directories only. No files will appear
  826. in the list.
  827. savefile: This type is used to select a name of a file to save. The user will be prompted if they wish to overwrite
  828. an existing file.
  829. Filters: [Table] A list of filters the user can select from when browsing files. The table can contain tables or strings.
  830. Table: If a table is used for a filter, it should contain two elements. The first element is the filter while the second
  831. element is the description of the filter e.g. {"*.lua", "Lua Files"}
  832. String: If a raw string is used, then it should just be the filter. It is recommended to use the table option since a
  833. description can be given for each filter.
  834. Return: [Table] Returns items for how the user interacted with this file dialog.
  835. Button: [String] The button the user clicked. Will either be OK or Cancel.
  836. Files: [Table] An array of selected file items the user selected when OK is pressed. Will be empty otherwise.
  837. --]]
  838. function Slab.FileDialog(Options)
  839. return Dialog.FileDialog(Options)
  840. end
  841. return Slab