Style.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 Config = require(SLAB_PATH .. '.Internal.Core.Config')
  21. local Cursor = require(SLAB_PATH .. '.Internal.Core.Cursor')
  22. local FileSystem = require(SLAB_PATH .. '.Internal.Core.FileSystem')
  23. local Utility = require(SLAB_PATH .. '.Internal.Core.Utility')
  24. local API = {}
  25. local Styles = {}
  26. local StylePaths = {}
  27. local DefaultStyles = {}
  28. local CurrentStyle = ""
  29. local FontStack = {}
  30. local Style =
  31. {
  32. Font = nil,
  33. FontSize = 14,
  34. MenuColor = {0.2, 0.2, 0.2, 1.0},
  35. ScrollBarColor = {0.4, 0.4, 0.4, 1.0},
  36. ScrollBarHoveredColor = {0.8, 0.8, 0.8, 1.0},
  37. SeparatorColor = {0.5, 0.5, 0.5, 0.7},
  38. WindowBackgroundColor = {0.2, 0.2, 0.2, 1.0},
  39. WindowTitleFocusedColor = {0.26, 0.53, 0.96, 1.0},
  40. ButtonColor = {0.55, 0.55, 0.55, 1.0},
  41. RadioButtonSelectedColor = {0.2, 0.2, 0.2, 1.0},
  42. ButtonHoveredColor = {0.7, 0.7, 0.7, 1.0},
  43. ButtonPressedColor = {0.8, 0.8, 0.8, 1.0},
  44. ButtonDisabledTextColor = {0.35, 0.35, 0.35, 1.0},
  45. CheckBoxSelectedColor = {0.0, 0.0, 0.0, 1.0},
  46. TextColor = {0.875, 0.875, 0.875, 1.0},
  47. TextHoverBgColor = {0.5, 0.5, 0.5, 1.0},
  48. ComboBoxColor = {0.4, 0.4, 0.4, 1.0},
  49. ComboBoxHoveredColor = {0.55, 0.55, 0.55, 1.0},
  50. ComboBoxDropDownColor = {0.4, 0.4, 0.4, 1.0},
  51. ComboBoxDropDownHoveredColor = {0.55, 0.55, 0.55, 1.0},
  52. ComboBoxArrowColor = {1.0, 1.0, 1.0, 1.0},
  53. InputBgColor = {0.4, 0.4, 0.4, 1.0},
  54. InputEditBgColor = {0.6, 0.6, 0.6, 1.0},
  55. InputSelectColor = {0.14, 0.29, 0.53, 0.4},
  56. MultilineTextColor = {0.0, 0.0, 0.0, 1.0},
  57. WindowRounding = 2.0,
  58. ButtonRounding = 2.0,
  59. CheckBoxRounding = 2.0,
  60. ComboBoxRounding = 2.0,
  61. InputBgRounding = 2.0,
  62. ScrollBarRounding = 2.0,
  63. API = API
  64. }
  65. function API.Initialize()
  66. local StylePath = "/Internal/Resources/Styles/"
  67. local Path = SLAB_PATH .. StylePath
  68. Path = string.gsub(Path, "%.", "/")
  69. -- Use love's filesystem functions to support both packaged and unpackaged builds
  70. local Items = love.filesystem.getDirectoryItems(Path)
  71. local StyleName = nil
  72. for I, V in ipairs(Items) do
  73. if string.find(V, Path, 1, true) == nil then
  74. V = Path .. V
  75. end
  76. local LoadedStyle = API.LoadStyle(V, false, true)
  77. if LoadedStyle ~= nil then
  78. local Name = FileSystem.GetBaseName(V, true)
  79. if StyleName == nil then
  80. StyleName = Name
  81. end
  82. end
  83. end
  84. if not API.SetStyle("Dark") then
  85. API.SetStyle(StyleName)
  86. end
  87. Style.Font = love.graphics.newFont(Style.FontSize)
  88. API.PushFont(Style.Font)
  89. Cursor.SetNewLineSize(Style.Font:getHeight())
  90. end
  91. function API.LoadStyle(Path, Set, IsDefault)
  92. local Contents, Error = Config.LoadFile(Path, IsDefault)
  93. if Contents ~= nil then
  94. local Name = FileSystem.GetBaseName(Path, true)
  95. Styles[Name] = Contents
  96. StylePaths[Name] = Path
  97. if IsDefault then
  98. table.insert(DefaultStyles, Name)
  99. end
  100. if Set then
  101. API.SetStyle(Name)
  102. end
  103. else
  104. print("Failed to load style '" .. Path .. "'.\n" .. Error)
  105. end
  106. return Contents
  107. end
  108. function API.SetStyle(Name)
  109. if Name == nil then
  110. return false
  111. end
  112. local Other = Styles[Name]
  113. if Other ~= nil then
  114. CurrentStyle = Name
  115. for K, V in pairs(Style) do
  116. local New = Other[K]
  117. if New ~= nil then
  118. if type(V) == "table" then
  119. Utility.CopyValues(Style[K], New)
  120. else
  121. Style[K] = New
  122. end
  123. end
  124. end
  125. return true
  126. else
  127. print("Style '" .. Name .. "' is not loaded.")
  128. end
  129. return false
  130. end
  131. function API.GetStyleNames()
  132. local Result = {}
  133. for K, V in pairs(Styles) do
  134. table.insert(Result, K)
  135. end
  136. return Result
  137. end
  138. function API.GetCurrentStyleName()
  139. return CurrentStyle
  140. end
  141. function API.CopyCurrentStyle(Path)
  142. local NewStyle = Utility.Copy(Styles[CurrentStyle])
  143. local Result, Error = Config.Save(Path, NewStyle)
  144. if Result then
  145. local NewStyleName = FileSystem.GetBaseName(Path, true)
  146. Styles[NewStyleName] = NewStyle
  147. StylePaths[NewStyleName] = Path
  148. API.SetStyle(NewStyleName)
  149. else
  150. print("Failed to create new style at path '" .. Path "'. " .. Error)
  151. end
  152. end
  153. function API.SaveCurrentStyle()
  154. API.StoreCurrentStyle()
  155. local Path = StylePaths[CurrentStyle]
  156. local Settings = Styles[CurrentStyle]
  157. local Result, Error = Config.Save(Path, Settings)
  158. if not Result then
  159. print("Failed to save style '" .. CurrentStyle .. "'. " .. Error)
  160. end
  161. end
  162. function API.StoreCurrentStyle()
  163. Utility.CopyValues(Styles[CurrentStyle], Style)
  164. end
  165. function API.IsDefaultStyle(Name)
  166. return Utility.Contains(DefaultStyles, Name)
  167. end
  168. function API.PushFont(Font)
  169. if Font ~= nil then
  170. Style.Font = Font
  171. table.insert(FontStack, 1, Font)
  172. end
  173. end
  174. function API.PopFont()
  175. if #FontStack > 1 then
  176. table.remove(FontStack, 1)
  177. Style.Font = FontStack[1]
  178. end
  179. end
  180. return Style