SlabTest.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. local Slab = require('Slab')
  21. local SlabDebug = require(SLAB_PATH .. '.SlabDebug')
  22. local SlabTest = {}
  23. local BasicWindow_Input = "This is some text!"
  24. local BasicWindow_Input_Numbers = ""
  25. local BasicWindow_CheckBox = false
  26. local BasicWindow_Options = {"Apple", "Banana", "Orange", "Pear", "Lemon"}
  27. local BasicWindow_Options_Selected = ""
  28. local BasicWindow_Properties =
  29. {
  30. X = 100.0,
  31. Y = 100.0,
  32. HasCollision = true,
  33. Name = "Player Name"
  34. }
  35. local ResetLayout = false
  36. local ListBoxIndex = 1
  37. local SlabTest_MessageBox = false
  38. local SlabTest_FileDialog = false
  39. local SlabTest_FileDialogType = 'openfile'
  40. function SlabTest.BasicWindow()
  41. Slab.BeginWindow('SlabTest_Basic', {Title = "Basic Window", X = 100.0, Y = 100.0, ResetLayout = ResetLayout})
  42. Slab.Text("Hello World!")
  43. if Slab.Button("Button!") then
  44. -- Perform logic here when button is clicked
  45. end
  46. if Slab.BeginContextMenuItem() then
  47. Slab.MenuItem("Button Item 1")
  48. Slab.MenuItem("Button Item 2")
  49. Slab.EndContextMenu()
  50. end
  51. if Slab.Input('BasicWindow_Name', {Text = BasicWindow_Input}) then
  52. BasicWindow_Input = Slab.GetInputText()
  53. end
  54. if Slab.Input('BasicWindow_Numbers', {Text = BasicWindow_Input_Numbers, NumbersOnly = true}) then
  55. BasicWindow_Input_Numbers = Slab.GetInputText()
  56. end
  57. Slab.Separator()
  58. if Slab.CheckBox(BasicWindow_CheckBox, "Check Box") then
  59. BasicWindow_CheckBox = not BasicWindow_CheckBox
  60. end
  61. if Slab.BeginComboBox('BasicWindow_Fruits', {Selected = BasicWindow_Options_Selected}) then
  62. for K, V in pairs(BasicWindow_Options) do
  63. if Slab.TextSelectable(V) then
  64. BasicWindow_Options_Selected = V
  65. end
  66. end
  67. Slab.EndComboBox()
  68. end
  69. if Slab.BeginContextMenuWindow() then
  70. Slab.MenuItem("BasicWindow Item 1")
  71. Slab.MenuItem("BasicWindow Item 2")
  72. Slab.MenuItem("BasicWindow Item 3")
  73. Slab.EndContextMenu()
  74. end
  75. Slab.Properties(BasicWindow_Properties)
  76. Slab.EndWindow()
  77. end
  78. function SlabTest.ResizableWindow()
  79. Slab.BeginWindow('SlabTest_Resizable', {Title = "Resizable Window", X = 350.0, Y = 100.0, AutoSizeWindow = false, ResetLayout = ResetLayout})
  80. Slab.Textf("This is a resizable window. This text will automatically wrap based on the window's dimensions.")
  81. Slab.NewLine()
  82. Slab.Textf("There is a new line separating these two texts.")
  83. Slab.EndWindow()
  84. end
  85. function SlabTest.TreeWindow()
  86. Slab.BeginWindow('SlabTest_Tree', {Title = "Tree Window", X = 600.0, Y = 100.0, AutoSizeWindow = false, ResetLayout = ResetLayout})
  87. if Slab.BeginTree('SlabTest_TreeRoot1', {Label = "Can be opened within rect", IconPath = SLAB_PATH .. "/Internal/Resources/Textures/folder.png"}) then
  88. Slab.BeginTree('SlabTest_Child1_Leaf', {Label = "Leaf 1", IsLeaf = true})
  89. if Slab.BeginTree('SlabTest_Child1', {Label = "Child 1"}) then
  90. Slab.BeginTree('SlabTest_Child1_Leaf1', {Label = "Leaf 2", IsLeaf = true})
  91. Slab.BeginTree('SlabTest_Child1_Leaf2', {Label = "Leaf 3", IsLeaf = true})
  92. Slab.EndTree()
  93. end
  94. Slab.EndTree()
  95. end
  96. if Slab.BeginTree('SlabTest_TreeRoot2', {Label = "Only opened with arrow.", OpenWithHighlight = false}) then
  97. Slab.BeginTree('SlabTest_Child1_leaf', {Label = "Leaf 1", IsLeaf = true})
  98. Slab.EndTree()
  99. end
  100. local Path = "/Internal/Resources/Textures/power.png"
  101. local ImageOptions =
  102. {
  103. Path = SLAB_PATH .. Path,
  104. Scale = 0.5,
  105. Color = {1.0, 0.0, 0.0, 1.0},
  106. ReturnOnClick = true,
  107. Tooltip = "This is a sample image."
  108. }
  109. if Slab.Image('SlabTest_Image', ImageOptions) then
  110. -- Perform logic when clicked
  111. end
  112. Slab.BeginListBox('SlabTest_ListBox')
  113. for I = 1, 10, 1 do
  114. Slab.BeginListBoxItem('Item ' .. I, {Selected = I == ListBoxIndex})
  115. Slab.Text("Item " .. I)
  116. if Slab.IsListBoxItemClicked() then
  117. ListBoxIndex = I
  118. end
  119. Slab.EndListBoxItem()
  120. end
  121. Slab.EndListBox()
  122. Slab.Text("After List Box")
  123. Slab.EndWindow()
  124. end
  125. function SlabTest.MainMenuBar()
  126. if Slab.BeginMainMenuBar() then
  127. if Slab.BeginMenu("File") then
  128. if Slab.BeginMenu("New") then
  129. if Slab.MenuItem("File") then
  130. -- Create a new file.
  131. end
  132. if Slab.MenuItem("Project") then
  133. -- Create a new project.
  134. end
  135. Slab.EndMenu()
  136. end
  137. if Slab.MenuItem("Open") then
  138. SlabTest_FileDialog = true
  139. SlabTest_FileDialogType = 'openfile'
  140. end
  141. if Slab.MenuItem("Save") then
  142. SlabTest_FileDialog = true
  143. SlabTest_FileDialogType = 'savefile'
  144. end
  145. Slab.MenuItem("Save As")
  146. Slab.Separator()
  147. if Slab.MenuItem("Quit") then
  148. love.event.quit()
  149. end
  150. Slab.EndMenu()
  151. end
  152. if Slab.BeginMenu("Windows") then
  153. if Slab.MenuItem("Reset Layout") then
  154. ResetLayout = true
  155. end
  156. if Slab.MenuItemChecked("Message Box", SlabTest_MessageBox) then
  157. SlabTest_MessageBox = not SlabTest_MessageBox
  158. end
  159. Slab.EndMenu()
  160. end
  161. SlabDebug.Menu()
  162. Slab.EndMainMenuBar()
  163. end
  164. end
  165. function SlabTest.GlobalContextMenu()
  166. if Slab.BeginContextMenuWindow() then
  167. Slab.MenuItem("Global Item 1")
  168. Slab.MenuItem("Global Item 2")
  169. Slab.MenuItem("Global Item 3")
  170. Slab.EndContextMenu()
  171. end
  172. end
  173. function SlabTest.Begin()
  174. SlabTest.MainMenuBar()
  175. SlabTest.BasicWindow()
  176. SlabTest.ResizableWindow()
  177. SlabTest.TreeWindow()
  178. SlabTest.GlobalContextMenu()
  179. SlabDebug.Begin()
  180. ResetLayout = false
  181. if SlabTest_MessageBox then
  182. local Result = Slab.MessageBox("Test", "This is a test message box.")
  183. if Result ~= "" then
  184. SlabTest_MessageBox = false
  185. end
  186. end
  187. if SlabTest_FileDialog then
  188. local Result = Slab.FileDialog({Type = SlabTest_FileDialogType, Filters = {{"*.*", "All Files"}, {"*.lua", "Lua Files"}, {"*.txt", "Text Files"}}})
  189. if Result.Button ~= "" then
  190. print("Button: " .. Result.Button)
  191. print("Files: " .. #Result.Files)
  192. for I, V in ipairs(Result.Files) do
  193. print(" " .. V)
  194. end
  195. SlabTest_FileDialog = false
  196. end
  197. end
  198. end
  199. return SlabTest