Advertisement
M0nkePr0

Theme

Oct 15th, 2022 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. local httpService = game:GetService('HttpService')
  2. local ThemeManager = {} do
  3. ThemeManager.Folder = 'LinoriaLibSettings'
  4. -- if not isfolder(ThemeManager.Folder) then makefolder(ThemeManager.Folder) end
  5.  
  6. ThemeManager.Library = nil
  7. ThemeManager.BuiltInThemes = {
  8. ['Jester'] = { 1, httpService:JSONDecode('{"FontColor":"ffffff","MainColor":"242424","AccentColor":"db4467","BackgroundColor":"1c1c1c","OutlineColor":"373737"}') },
  9. ['Green'] = { 2, httpService:JSONDecode('{"FontColor":"ffffff","MainColor":"141414","AccentColor":"00ff8b","BackgroundColor":"1c1c1c","OutlineColor":"3c3c3c"}') },
  10. ['Blue'] = { 3, httpService:JSONDecode('{"FontColor":"ffffff","MainColor":"1c1c1c","AccentColor":"0055ff","BackgroundColor":"141414","OutlineColor":"323232"}') },
  11. ['Mint'] = { 4, httpService:JSONDecode('{"FontColor":"ffffff","MainColor":"242424","AccentColor":"3db488","BackgroundColor":"1c1c1c","OutlineColor":"373737"}') },
  12. ['Tokyo Night'] = { 5, httpService:JSONDecode('{"FontColor":"ffffff","MainColor":"191925","AccentColor":"6759b3","BackgroundColor":"16161f","OutlineColor":"323232"}') },
  13. ['Ubuntu'] = { 6, httpService:JSONDecode('{"FontColor":"ffffff","MainColor":"3e3e3e","AccentColor":"e2581e","BackgroundColor":"323232","OutlineColor":"191919"}') },
  14. }
  15.  
  16. function ThemeManager:ApplyTheme(theme)
  17. local customThemeData = self:GetCustomTheme(theme)
  18. local data = customThemeData or self.BuiltInThemes[theme]
  19.  
  20. if not data then return end
  21.  
  22. -- custom themes are just regular dictionaries instead of an array with { index, dictionary }
  23.  
  24. local scheme = data[2]
  25. for idx, col in next, customThemeData or scheme do
  26. self.Library[idx] = Color3.fromHex(col)
  27.  
  28. if Options[idx] then
  29. Options[idx]:SetValueRGB(Color3.fromHex(col))
  30. end
  31. end
  32.  
  33. self:ThemeUpdate()
  34. end
  35.  
  36. function ThemeManager:ThemeUpdate()
  37. -- This allows us to force apply themes without loading the themes tab :)
  38. local options = { "FontColor", "MainColor", "AccentColor", "BackgroundColor", "OutlineColor" }
  39. for i, field in next, options do
  40. if Options and Options[field] then
  41. self.Library[field] = Options[field].Value
  42. end
  43. end
  44.  
  45. self.Library.AccentColorDark = self.Library:GetDarkerColor(self.Library.AccentColor);
  46. self.Library:UpdateColorsUsingRegistry()
  47. end
  48.  
  49. function ThemeManager:LoadDefault()
  50. local theme = 'Default'
  51. local content = isfile(self.Folder .. '/themes/default.txt') and readfile(self.Folder .. '/themes/default.txt')
  52.  
  53. local isDefault = true
  54. if content then
  55. if self.BuiltInThemes[content] then
  56. theme = content
  57. elseif self:GetCustomTheme(content) then
  58. theme = content
  59. isDefault = false;
  60. end
  61. elseif self.BuiltInThemes[self.DefaultTheme] then
  62. theme = self.DefaultTheme
  63. end
  64.  
  65. if isDefault then
  66. Options.ThemeManager_ThemeList:SetValue(theme)
  67. else
  68. self:ApplyTheme(theme)
  69. end
  70. end
  71.  
  72. function ThemeManager:SaveDefault(theme)
  73. writefile(self.Folder .. '/themes/default.txt', theme)
  74. end
  75.  
  76. function ThemeManager:CreateThemeManager(groupbox)
  77. groupbox:AddLabel('Background color'):AddColorPicker('BackgroundColor', { Default = self.Library.BackgroundColor });
  78. groupbox:AddLabel('Main color') :AddColorPicker('MainColor', { Default = self.Library.MainColor });
  79. groupbox:AddLabel('Accent color'):AddColorPicker('AccentColor', { Default = self.Library.AccentColor });
  80. groupbox:AddLabel('Outline color'):AddColorPicker('OutlineColor', { Default = self.Library.OutlineColor });
  81. groupbox:AddLabel('Font color') :AddColorPicker('FontColor', { Default = self.Library.FontColor });
  82.  
  83. local ThemesArray = {}
  84. for Name, Theme in next, self.BuiltInThemes do
  85. table.insert(ThemesArray, Name)
  86. end
  87.  
  88. table.sort(ThemesArray, function(a, b) return self.BuiltInThemes[a][1] < self.BuiltInThemes[b][1] end)
  89.  
  90. groupbox:AddDivider()
  91. groupbox:AddDropdown('ThemeManager_ThemeList', { Text = 'Theme list', Values = ThemesArray, Default = 1 })
  92.  
  93. groupbox:AddButton('Set as default', function()
  94. self:SaveDefault(Options.ThemeManager_ThemeList.Value)
  95. self.Library:Notify(string.format('Set default theme to %q', Options.ThemeManager_ThemeList.Value))
  96. end)
  97.  
  98. Options.ThemeManager_ThemeList:OnChanged(function()
  99. self:ApplyTheme(Options.ThemeManager_ThemeList.Value)
  100. end)
  101.  
  102. groupbox:AddDivider()
  103. groupbox:AddDropdown('ThemeManager_CustomThemeList', { Text = 'Custom themes', Values = self:ReloadCustomThemes(), AllowNull = true, Default = 1 })
  104. groupbox:AddInput('ThemeManager_CustomThemeName', { Text = 'Custom theme name' })
  105.  
  106. groupbox:AddButton('Load custom theme', function()
  107. self:ApplyTheme(Options.ThemeManager_CustomThemeList.Value)
  108. end)
  109.  
  110. groupbox:AddButton('Save custom theme', function()
  111. self:SaveCustomTheme(Options.ThemeManager_CustomThemeName.Value)
  112.  
  113. Options.ThemeManager_CustomThemeList.Values = self:ReloadCustomThemes()
  114. Options.ThemeManager_CustomThemeList:SetValues()
  115. Options.ThemeManager_CustomThemeList:SetValue(nil)
  116. end)
  117.  
  118. groupbox:AddButton('Refresh list', function()
  119. Options.ThemeManager_CustomThemeList.Values = self:ReloadCustomThemes()
  120. Options.ThemeManager_CustomThemeList:SetValues()
  121. Options.ThemeManager_CustomThemeList:SetValue(nil)
  122. end)
  123.  
  124. groupbox:AddButton('Set as default', function()
  125. if Options.ThemeManager_CustomThemeList.Value ~= nil and Options.ThemeManager_CustomThemeList.Value ~= '' then
  126. self:SaveDefault(Options.ThemeManager_CustomThemeList.Value)
  127. self.Library:Notify(string.format('Set default theme to %q', Options.ThemeManager_CustomThemeList.Value))
  128. end
  129. end)
  130.  
  131. ThemeManager:LoadDefault()
  132.  
  133. local function UpdateTheme()
  134. self:ThemeUpdate()
  135. end
  136.  
  137. Options.BackgroundColor:OnChanged(UpdateTheme)
  138. Options.MainColor:OnChanged(UpdateTheme)
  139. Options.AccentColor:OnChanged(UpdateTheme)
  140. Options.OutlineColor:OnChanged(UpdateTheme)
  141. Options.FontColor:OnChanged(UpdateTheme)
  142. end
  143.  
  144. function ThemeManager:GetCustomTheme(file)
  145. local path = self.Folder .. '/themes/' .. file
  146. if not isfile(path) then
  147. return nil
  148. end
  149.  
  150. local data = readfile(path)
  151. local success, decoded = pcall(httpService.JSONDecode, httpService, data)
  152.  
  153. if not success then
  154. return nil
  155. end
  156.  
  157. return decoded
  158. end
  159.  
  160. function ThemeManager:SaveCustomTheme(file)
  161. if file:gsub(' ', '') == '' then
  162. return self.Library:Notify('Invalid file name for theme (empty)', 3)
  163. end
  164.  
  165. local theme = {}
  166. local fields = { "FontColor", "MainColor", "AccentColor", "BackgroundColor", "OutlineColor" }
  167.  
  168. for _, field in next, fields do
  169. theme[field] = Options[field].Value:ToHex()
  170. end
  171.  
  172. writefile(self.Folder .. '/themes/' .. file .. '.json', httpService:JSONEncode(theme))
  173. end
  174.  
  175. function ThemeManager:ReloadCustomThemes()
  176. local list = listfiles(self.Folder .. '/themes')
  177.  
  178. local out = {}
  179. for i = 1, #list do
  180. local file = list[i]
  181. if file:sub(-5) == '.json' then
  182. -- i hate this but it has to be done ...
  183.  
  184. local pos = file:find('.json', 1, true)
  185. local char = file:sub(pos, pos)
  186.  
  187. while char ~= '/' and char ~= '\\' and char ~= '' do
  188. pos = pos - 1
  189. char = file:sub(pos, pos)
  190. end
  191.  
  192. if char == '/' or char == '\\' then
  193. table.insert(out, file:sub(pos + 1))
  194. end
  195. end
  196. end
  197.  
  198. return out
  199. end
  200.  
  201. function ThemeManager:SetLibrary(lib)
  202. self.Library = lib
  203. end
  204.  
  205. function ThemeManager:BuildFolderTree()
  206. local paths = {}
  207.  
  208. -- build the entire tree if a path is like some-hub/phantom-forces
  209. -- makefolder builds the entire tree on Synapse X but not other exploits
  210.  
  211. local parts = self.Folder:split('/')
  212. for idx = 1, #parts do
  213. paths[#paths + 1] = table.concat(parts, '/', 1, idx)
  214. end
  215.  
  216. table.insert(paths, self.Folder .. '/themes')
  217. table.insert(paths, self.Folder .. '/settings')
  218.  
  219. for i = 1, #paths do
  220. local str = paths[i]
  221. if not isfolder(str) then
  222. makefolder(str)
  223. end
  224. end
  225. end
  226.  
  227. function ThemeManager:SetFolder(folder)
  228. self.Folder = folder
  229. self:BuildFolderTree()
  230. end
  231.  
  232. function ThemeManager:CreateGroupBox(tab)
  233. assert(self.Library, 'Must set ThemeManager.Library first!')
  234. return tab:AddLeftGroupbox('Themes')
  235. end
  236.  
  237. function ThemeManager:ApplyToTab(tab)
  238. assert(self.Library, 'Must set ThemeManager.Library first!')
  239. local groupbox = self:CreateGroupBox(tab)
  240. self:CreateThemeManager(groupbox)
  241. end
  242.  
  243. function ThemeManager:ApplyToGroupbox(groupbox)
  244. assert(self.Library, 'Must set ThemeManager.Library first!')
  245. self:CreateThemeManager(groupbox)
  246. end
  247.  
  248. ThemeManager:BuildFolderTree()
  249. end
  250. return ThemeManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement