Settings.gd 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. extends WindowPanel
  2. var platformSection : String = Util.GetPlatformName()
  3. const defaultSection : String = "Default"
  4. const userSection : String = "User"
  5. @onready var accessors : Array = [
  6. {
  7. "Render-MinWindowSize": [init_minwinsize, set_minwinsize, apply_minwinsize, null],
  8. "Render-Fullscreen": [init_fullscreen, set_fullscreen, apply_fullscreen, $Margin/TabBar/Render/RenderVBox/VisualVBox/Fullscreen],
  9. "Render-Scaling": [init_scaling, set_scaling, apply_scaling, $Margin/TabBar/Render/RenderVBox/VisualVBox/Scaling/Option],
  10. "Render-WindowSize": [init_resolution, set_resolution, apply_resolution, $Margin/TabBar/Render/RenderVBox/VisualVBox/WindowResolution/Option],
  11. "Render-WindowPos": [init_windowPos, set_windowPos, apply_windowPos, null],
  12. "Render-ActionOverlay": [init_actionoverlay, set_actionoverlay, apply_actionoverlay, $Margin/TabBar/Render/RenderVBox/VisualVBox/ActionOverlay],
  13. "Render-Lighting": [init_lighting, set_lighting, apply_lighting, $Margin/TabBar/Render/RenderVBox/EffectVBox/Lighting],
  14. "Render-HQ4x": [init_hq4x, set_hq4x, apply_hq4x, $Margin/TabBar/Render/RenderVBox/EffectVBox/HQx4],
  15. "Render-CRT": [init_crt, set_crt, apply_crt, $Margin/TabBar/Render/RenderVBox/EffectVBox/CRT],
  16. "Audio-General": [init_audiogeneral, set_audiogeneral, apply_audiogeneral, $"Margin/TabBar/Audio/VBoxContainer/Global Volume/HSlider"],
  17. "Session-AccountName": [init_sessionaccountname, set_sessionaccountname, apply_sessionaccountname, null],
  18. "Session-FirstLogin": [init_sessionfirstlogin, set_sessionfirstlogin, apply_sessionfirstlogin, null],
  19. "Session-Overlay": [init_sessionoverlay, set_sessionoverlay, apply_sessionoverlay, null],
  20. "Session-ShortcutCells": [init_shortcutcells, set_shortcutcells, apply_shortcutcells, null],
  21. }
  22. ]
  23. enum CATEGORY { RENDER, SOUND, COUNT }
  24. enum ACC_TYPE { INIT, SET, APPLY, LABEL }
  25. # MinWindowSize
  26. func init_minwinsize(apply : bool):
  27. if apply:
  28. var minSize : Vector2 = GetVal("Render-MinWindowSize")
  29. apply_minwinsize(minSize)
  30. func set_minwinsize(minSize : Vector2):
  31. SetVal("Render-MinWindowSize", minSize)
  32. apply_minwinsize(minSize)
  33. func apply_minwinsize(minSize : Vector2):
  34. DisplayServer.window_set_min_size(minSize, 0)
  35. # FullScreen
  36. func init_fullscreen(apply : bool):
  37. var pressed : bool = GetVal("Render-Fullscreen")
  38. accessors[CATEGORY.RENDER]["Render-Fullscreen"][ACC_TYPE.LABEL].set_pressed_no_signal(pressed)
  39. if apply:
  40. apply_fullscreen(pressed)
  41. func set_fullscreen(pressed : bool):
  42. SetVal("Render-Fullscreen", pressed)
  43. apply_fullscreen(pressed)
  44. func apply_fullscreen(pressed : bool):
  45. if pressed:
  46. apply_resolution(DisplayServer.screen_get_size())
  47. clear_resolution_labels()
  48. if DisplayServer.window_get_mode(0) != DisplayServer.WINDOW_MODE_FULLSCREEN:
  49. DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
  50. else:
  51. populate_resolution_labels(DisplayServer.screen_get_size())
  52. if DisplayServer.window_get_mode(0) == DisplayServer.WINDOW_MODE_FULLSCREEN:
  53. DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
  54. # Window Resolution
  55. const resolutionEntriesCount : int = 5
  56. func init_resolution(apply : bool):
  57. var resolution : Vector2i = GetVal("Render-WindowSize")
  58. populate_resolution_labels(resolution)
  59. if apply:
  60. apply_resolution(resolution)
  61. func clear_resolution_labels():
  62. accessors[CATEGORY.RENDER]["Render-WindowSize"][ACC_TYPE.LABEL].clear()
  63. func populate_resolution_labels(resolution : Vector2i):
  64. clear_resolution_labels()
  65. if DisplayServer.window_get_mode() != DisplayServer.WINDOW_MODE_FULLSCREEN:
  66. var minScreenSize : Vector2i = GetVal("Render-MinWindowSize")
  67. var maxScreenSize : Vector2i = DisplayServer.screen_get_size()
  68. var label : OptionButton = accessors[CATEGORY.RENDER]["Render-WindowSize"][ACC_TYPE.LABEL]
  69. for i in resolutionEntriesCount:
  70. var item : Vector2i = calculate_resolution(i, minScreenSize, maxScreenSize)
  71. label.add_item(str(item))
  72. label.add_separator()
  73. label.add_item(str(resolution))
  74. label.selected = label.item_count - 1
  75. func calculate_resolution(index : int, minScreenSize : Vector2, maxScreenSize : Vector2) -> Vector2i:
  76. return lerp(minScreenSize, maxScreenSize, index / maxf(resolutionEntriesCount - 1, 1.0))
  77. func set_resolutionIdx(resolutionIdx : int):
  78. var minScreenSize : Vector2i = GetVal("Render-MinWindowSize")
  79. var maxScreenSize : Vector2i = DisplayServer.screen_get_size()
  80. var resolution : Vector2i = calculate_resolution(resolutionIdx, minScreenSize, maxScreenSize)
  81. set_resolution(resolution)
  82. func set_resolution(resolution : Vector2i):
  83. SetVal("Render-WindowSize", resolution)
  84. apply_resolution(resolution)
  85. func apply_resolution(resolution : Vector2i):
  86. var windowSize : Vector2i = DisplayServer.screen_get_size()
  87. var minScreenSize : Vector2i = GetVal("Render-MinWindowSize")
  88. var newSize : Vector2i = clamp(resolution, minScreenSize, windowSize)
  89. var currentPos : Vector2i = GetVal("Render-WindowPos")
  90. if currentPos == Vector2i(-1, -1):
  91. currentPos = (windowSize - resolution) / 2
  92. var newPosition : Vector2i = Vector2i(clampi(currentPos.x, 0, (windowSize - resolution).x), clampi(currentPos.y, 0, (windowSize - resolution).y))
  93. DisplayServer.window_set_size(newSize)
  94. set_windowPos(newPosition)
  95. init_actionoverlay(true)
  96. populate_resolution_labels(resolution)
  97. # Window Position
  98. func init_windowPos(apply : bool):
  99. if apply:
  100. var pos : Vector2 = GetVal("Render-WindowPos")
  101. apply_windowPos(pos)
  102. func set_windowPos(pos : Vector2):
  103. SetVal("Render-WindowPos", pos)
  104. apply_windowPos(pos)
  105. func save_windowPos():
  106. set_windowPos(get_viewport().get_position())
  107. func apply_windowPos(pos : Vector2):
  108. if pos != Vector2(-1, -1):
  109. DisplayServer.window_set_position(pos)
  110. # DoubleResolution
  111. func init_scaling(apply : bool):
  112. var mode : int = GetVal("Render-Scaling")
  113. accessors[CATEGORY.RENDER]["Render-Scaling"][ACC_TYPE.LABEL].selected = mode
  114. if apply:
  115. apply_scaling(mode)
  116. func set_scaling(mode : int):
  117. SetVal("Render-Scaling", mode)
  118. apply_scaling(mode)
  119. func apply_scaling(mode : int):
  120. Launcher.Root.set_content_scale_factor(mode + 1)
  121. init_actionoverlay(true)
  122. # ActionOverlay
  123. func init_actionoverlay(apply : bool):
  124. var enable : bool = GetVal("Render-ActionOverlay")
  125. accessors[CATEGORY.RENDER]["Render-ActionOverlay"][ACC_TYPE.LABEL].set_pressed_no_signal(enable)
  126. if apply:
  127. apply_actionoverlay(enable)
  128. func set_actionoverlay(enable : bool):
  129. SetVal("Render-ActionOverlay", enable)
  130. apply_actionoverlay(enable)
  131. func apply_actionoverlay(enable : bool):
  132. if Launcher.GUI and Launcher.GUI.sticks:
  133. Launcher.GUI.sticks.Enable(enable)
  134. # Lighting
  135. func init_lighting(apply : bool):
  136. var enable : bool = GetVal("Render-Lighting")
  137. accessors[CATEGORY.RENDER]["Render-Lighting"][ACC_TYPE.LABEL].set_pressed_no_signal(enable)
  138. if apply:
  139. apply_lighting(enable)
  140. func set_lighting(enable : bool):
  141. SetVal("Render-Lighting", enable)
  142. apply_lighting(enable)
  143. func apply_lighting(enable : bool):
  144. Effects.EnableLighting(enable)
  145. # HQ4x
  146. func init_hq4x(apply : bool):
  147. var enable : bool = GetVal("Render-HQ4x")
  148. accessors[CATEGORY.RENDER]["Render-HQ4x"][ACC_TYPE.LABEL].set_pressed_no_signal(enable)
  149. if apply:
  150. apply_hq4x(enable)
  151. func set_hq4x(enable : bool):
  152. SetVal("Render-HQ4x", enable)
  153. apply_hq4x(enable)
  154. func apply_hq4x(enable : bool):
  155. if Launcher.GUI and Launcher.GUI.HQ4xShader:
  156. Launcher.GUI.HQ4xShader.set_visible(enable)
  157. # CRT
  158. func init_crt(apply : bool):
  159. var enable : bool = GetVal("Render-CRT")
  160. accessors[CATEGORY.RENDER]["Render-CRT"][ACC_TYPE.LABEL].set_pressed_no_signal(enable)
  161. if apply:
  162. apply_crt(enable)
  163. func set_crt(enable : bool):
  164. SetVal("Render-CRT", enable)
  165. apply_crt(enable)
  166. func apply_crt(enable : bool):
  167. if Launcher.GUI and Launcher.GUI.CRTShader:
  168. Launcher.GUI.CRTShader.set_visible(enable)
  169. # Audio General
  170. func init_audiogeneral(apply : bool):
  171. var volumeRatio : float = GetVal("Audio-General")
  172. accessors[CATEGORY.RENDER]["Audio-General"][ACC_TYPE.LABEL].value = volumeRatio
  173. if apply:
  174. apply_audiogeneral(volumeRatio)
  175. func set_audiogeneral(volumeRatio : float):
  176. SetVal("Audio-General", volumeRatio)
  177. apply_audiogeneral(volumeRatio)
  178. func apply_audiogeneral(volumeRatio : float):
  179. if Launcher.Audio:
  180. var interpolation : float = clamp((log(clampf(volumeRatio, 0.0, 1.0)) + 5.0) / 5.0, 0.0, 1.06)
  181. Launcher.Audio.SetVolume((1.0 - interpolation) * -80.0)
  182. # Session Account Name
  183. func init_sessionaccountname(apply : bool):
  184. if apply:
  185. var accountName : String = GetVal("Session-AccountName")
  186. apply_sessionaccountname(accountName)
  187. func set_sessionaccountname(accountName : String):
  188. SetVal("Session-AccountName", accountName)
  189. apply_sessionaccountname(accountName)
  190. func apply_sessionaccountname(accountName : String):
  191. if Launcher.GUI and Launcher.GUI.loginPanel:
  192. Launcher.GUI.loginPanel.nameTextControl.set_text(accountName)
  193. # Session First Login
  194. func init_sessionfirstlogin(apply : bool):
  195. if apply:
  196. var firstTime : bool = GetVal("Session-FirstLogin")
  197. apply_sessionfirstlogin(firstTime)
  198. func set_sessionfirstlogin(firstTime : bool):
  199. SetVal("Session-FirstLogin", firstTime)
  200. apply_sessionfirstlogin(firstTime)
  201. func apply_sessionfirstlogin(firstTime : bool):
  202. if Launcher.GUI and firstTime:
  203. Launcher.GUI.DisplayFirstLogin()
  204. # Session Windows Overlay placement
  205. enum ESessionOverlay { NAME = 0, POSITION, SIZE, COUNT}
  206. func init_sessionoverlay(apply : bool):
  207. if apply:
  208. var overlay : Array = GetVal("Session-Overlay")
  209. apply_sessionoverlay(overlay)
  210. func save_sessionoverlay():
  211. var overlay : Array = []
  212. if Launcher.GUI and Launcher.GUI.windows:
  213. for window in Launcher.GUI.windows.get_children():
  214. if window.is_visible() and window.saveOverlayState:
  215. overlay.append([window.get_name().get_file(), window.get_position(), window.get_size()])
  216. set_sessionoverlay(overlay)
  217. func set_sessionoverlay(overlay : Array):
  218. SetVal("Session-Overlay", overlay)
  219. apply_sessionoverlay(overlay)
  220. func apply_sessionoverlay(overlay : Array):
  221. if Launcher.GUI and Launcher.GUI.windows:
  222. for window in overlay:
  223. if window.size() >= ESessionOverlay.COUNT:
  224. var floatingWindow : WindowPanel = Launcher.GUI.windows.get_node(window[ESessionOverlay.NAME])
  225. if floatingWindow:
  226. floatingWindow.set_visible(true)
  227. floatingWindow.set_size(window[ESessionOverlay.SIZE])
  228. floatingWindow.set_position(window[ESessionOverlay.POSITION])
  229. floatingWindow.UpdateWindow()
  230. # Shortcut cells
  231. func init_shortcutcells(apply : bool):
  232. if apply:
  233. var cells : Array = GetVal("Session-ShortcutCells")
  234. apply_shortcutcells(cells)
  235. func save_shortcutcells():
  236. var cells : Array = []
  237. if Launcher.GUI:
  238. for tile in Launcher.GUI.get_tree().get_nodes_in_group("CellTile"):
  239. if tile and tile.is_visible() and tile.draggable and tile.cell:
  240. cells.append([tile.name, tile.cell.id, tile.cell.type])
  241. set_shortcutcells(cells)
  242. func set_shortcutcells(cells : Array):
  243. SetVal("Session-ShortcutCells", cells)
  244. func apply_shortcutcells(cells : Array):
  245. if Launcher.GUI and Launcher.GUI:
  246. for tile in Launcher.GUI.get_tree().get_nodes_in_group("CellTile"):
  247. if cells.size() == 0:
  248. break
  249. if tile and tile.draggable:
  250. for cellInfo in cells:
  251. if cellInfo and cellInfo is Array and cellInfo.size() >= 3 and cellInfo[0] == tile.name:
  252. var cell : BaseCell = null
  253. match cellInfo[2]:
  254. CellCommons.Type.ITEM:
  255. if DB.ItemsDB.has(cellInfo[1]):
  256. cell = DB.ItemsDB[cellInfo[1]]
  257. CellCommons.Type.EMOTE:
  258. if DB.EmotesDB.has(cellInfo[1]):
  259. cell = DB.EmotesDB[cellInfo[1]]
  260. CellCommons.Type.SKILL:
  261. if DB.SkillsDB.has(cellInfo[1]):
  262. cell = DB.SkillsDB[cellInfo[1]]
  263. if cell:
  264. tile.AssignData(cell)
  265. CellTile.RefreshShortcuts(cell)
  266. cells.erase(cellInfo)
  267. break
  268. #
  269. func _on_visibility_changed():
  270. RefreshSettings(false)
  271. func _ready():
  272. if not FSM:
  273. return
  274. RefreshSettings(true)
  275. FSM.enter_game.connect(RefreshSettings.bind(true))
  276. FSM.exit_game.connect(SaveSettings.bind())
  277. if LauncherCommons.isMobile or LauncherCommons.isWeb:
  278. accessors[CATEGORY.RENDER]["Render-WindowSize"][ACC_TYPE.LABEL].get_parent().set_visible(false)
  279. accessors[CATEGORY.RENDER]["Render-Fullscreen"][ACC_TYPE.LABEL].set_visible(false)
  280. # Conf accessors
  281. func RefreshSettings(apply : bool):
  282. for category in accessors:
  283. for option in category:
  284. category[option][ACC_TYPE.INIT].call_deferred(apply)
  285. func SaveSettings():
  286. save_sessionoverlay()
  287. save_windowPos()
  288. save_shortcutcells()
  289. Conf.SaveType("settings", Conf.Type.USERSETTINGS)
  290. func SetVal(key : String, value):
  291. Conf.SetValue(userSection, key, Conf.Type.USERSETTINGS, value)
  292. func GetVal(key : String):
  293. var value = Conf.GetVariant(userSection, key, Conf.Type.USERSETTINGS, null)
  294. if value == null:
  295. value = Conf.GetVariant(platformSection, key, Conf.Type.SETTINGS, null)
  296. if value == null:
  297. value = Conf.GetVariant(defaultSection, key, Conf.Type.SETTINGS, null)
  298. return value