Style.lua 5.2 KB

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