settings.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. # THIS IS A SOURCE CODE FILE FROM I'M NOT EVEN HUMAN THE GAME.
  2. # IT COULD BE USED IN A DIFFERENT PIECE OF SOFTWARE ( LIKE A
  3. # DIFFERENT GAME ), BUT IT WAS ORIGINALLY WRITTEN FOR I'M NOT
  4. # EVEN HUMAN THE GAME.
  5. # THE DEVELOPERS OF THE GAME ARE : (C) J.Y.AMIHUD, AYYZEE AND
  6. # OTHER CONTRIBUTORS. THIS AND OTHER FILES IN THIS GAME,
  7. # UNLESS SPECIFICALLY NOTED, COULD BE USED UNDER THE TERMS OF
  8. # GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER VERSION.
  9. import os
  10. import json
  11. # GTK module ( Graphical interface
  12. import gi
  13. gi.require_version('Gtk', '3.0')
  14. from gi.repository import Gtk
  15. import cairo
  16. from modules import ui
  17. def layer(game):
  18. # Setting up a cairo layer
  19. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
  20. game.current['w'],
  21. game.current['h'])
  22. layer = cairo.Context(surface)
  23. layer.set_antialias(cairo.ANTIALIAS_NONE)
  24. ui.color(game, layer, "#FFFFFF")
  25. ui.text(game, layer, "Settings",
  26. int(game.current["w"]/2), 5,
  27. align="center")
  28. def do():
  29. game.scene = "main_menu"
  30. ui.button(game, layer,
  31. 5,
  32. 5 ,
  33. int(game.current["w"] / 3 ) - 30 ,
  34. 13,
  35. menu="settings",
  36. string="< Back",
  37. func=do)
  38. # Setting up the scroller
  39. if "settings" not in game.scroll:
  40. game.scroll["settings"] = 0
  41. max_width = min(200, int(game.current["w"]/2))
  42. putx = int(game.current["w"]/2-max_width/2)
  43. # Fancy frame
  44. ui.color(game, layer, "green")
  45. layer.set_line_width(2)
  46. layer.move_to(putx-5,25)
  47. layer.line_to(putx, 20)
  48. layer.line_to(putx+max_width+3, 20)
  49. layer.line_to(putx+max_width+3, game.current["h"]-25)
  50. layer.line_to(putx+max_width-2, game.current["h"]-15)
  51. layer.line_to(putx-5, game.current["h"]-15)
  52. layer.close_path()
  53. layer.stroke_preserve()
  54. ui.color(game, layer, "dark_blue")
  55. layer.fill_preserve()
  56. current_Y = 0
  57. #layer.rectangle(4,20,
  58. # game.current["w"]-10,
  59. # game.current["h"]-25)
  60. layer.clip()
  61. if "settings_graphics_active" not in game.current:
  62. game.current["settings_graphics_active"] = False
  63. wicon = "right"
  64. if game.current["settings_graphics_active"]:
  65. wicon = "down"
  66. def do():
  67. game.current["settings_graphics_active"] = not game.current["settings_graphics_active"]
  68. game.menus = {}
  69. ui.button(game, layer,
  70. putx,
  71. current_Y+25,
  72. max_width ,
  73. 13,
  74. menu="settings",
  75. scroll="settings",
  76. icon=wicon,
  77. string="Graphics",
  78. func=do)
  79. current_Y += 15
  80. if game.current["settings_graphics_active"]:
  81. # VIRTUAL PIXEL TO REAL PIXEL RATIO
  82. ui.color(game, layer, "#00FF00")
  83. ui.text(game, layer, "Size: ",
  84. putx,
  85. current_Y+28+game.scroll["settings"])
  86. def do():
  87. game.settings["pixels"] += 1
  88. save_settings(game)
  89. ui.button(game, layer,
  90. putx+max_width-55,
  91. current_Y+25,
  92. 13,
  93. 13,
  94. menu="settings",
  95. scroll="settings",
  96. string="+",
  97. func=do)
  98. ui.color(game, layer, "#00FF00")
  99. ui.text(game, layer, str(game.settings["pixels"]),
  100. putx+max_width-30,
  101. current_Y+28+game.scroll["settings"])
  102. def do():
  103. game.settings["pixels"] -= 1
  104. if game.settings["pixels"] < 1:
  105. game.settings["pixels"] = 1
  106. save_settings(game)
  107. ui.button(game, layer,
  108. putx+max_width-13,
  109. current_Y+25,
  110. 13,
  111. 13,
  112. menu="settings",
  113. scroll="settings",
  114. string="-",
  115. func=do)
  116. current_Y += 15
  117. # FULLSCREEN
  118. ficon = "fullscreen"
  119. if game.settings["fullscreen"]:
  120. ficon = "unfullscreen"
  121. def do():
  122. game.settings["fullscreen"] = not game.settings["fullscreen"]
  123. save_settings(game)
  124. ui.button(game, layer,
  125. putx+10,
  126. current_Y+25,
  127. max_width -10,
  128. 13,
  129. menu="settings",
  130. scroll="settings",
  131. icon=ficon,
  132. string="Fullscreen",
  133. func=do)
  134. current_Y += 15
  135. # TESTING MODE
  136. dicon = "false"
  137. if game.current["testing"]:
  138. dicon = "true"
  139. def do():
  140. game.current["testing"] = not game.current["testing"]
  141. ui.button(game, layer,
  142. putx+10,
  143. current_Y+25,
  144. max_width -10,
  145. 13,
  146. menu="settings",
  147. scroll="settings",
  148. icon=dicon,
  149. string="Debug Mode",
  150. func=do)
  151. current_Y += 15
  152. # Depth of Field option
  153. if game.settings["Blur"]:
  154. wicon = "true"
  155. else:
  156. wicon = "false"
  157. def do():
  158. game.settings["Blur"] = not game.settings["Blur"]
  159. save_settings(game)
  160. ui.button(game, layer,
  161. putx+10,
  162. current_Y+25,
  163. max_width -10,
  164. 13,
  165. menu="settings",
  166. scroll="settings",
  167. icon=wicon,
  168. string="Depth of Field",
  169. func=do)
  170. current_Y += 15
  171. if game.settings["Blur"]:
  172. if game.settings["NN-Upscaler-Blur"]:
  173. wicon = "true"
  174. else:
  175. wicon = "false"
  176. def do():
  177. game.settings["NN-Upscaler-Blur"] = not game.settings["NN-Upscaler-Blur"]
  178. save_settings(game)
  179. ui.button(game, layer,
  180. putx+20,
  181. current_Y+25,
  182. max_width -20,
  183. 13,
  184. menu="settings",
  185. scroll="settings",
  186. icon=wicon,
  187. string="Pixelate",
  188. func=do)
  189. current_Y += 15
  190. # Depth of Field downscale NN option
  191. if game.settings["NN-Downscaler-Blur"]:
  192. wicon = "true"
  193. else:
  194. wicon = "false"
  195. def do():
  196. game.settings["NN-Downscaler-Blur"] = not game.settings["NN-Downscaler-Blur"]
  197. save_settings(game)
  198. ui.button(game, layer,
  199. putx+20,
  200. current_Y+25,
  201. max_width -20,
  202. 13,
  203. menu="settings",
  204. scroll="settings",
  205. icon=wicon,
  206. string="Pixelate+",
  207. func=do)
  208. current_Y += 15
  209. current_Y += 15
  210. # WOLDS
  211. if "settings_world_active" not in game.current:
  212. game.current["settings_world_active"] = False
  213. wicon = "right"
  214. if game.current["settings_world_active"]:
  215. wicon = "down"
  216. def do():
  217. game.current["settings_world_active"] = not game.current["settings_world_active"]
  218. game.menus = {}
  219. ui.button(game, layer,
  220. putx,
  221. current_Y+25,
  222. max_width ,
  223. 13,
  224. menu="settings",
  225. scroll="settings",
  226. icon=wicon,
  227. string="World: "+game.worlds[game.settings["world"]]["title"],
  228. func=do)
  229. current_Y += 15
  230. # List of worlds
  231. if game.current["settings_world_active"]:
  232. current_Y += 15
  233. for world in game.worlds:
  234. def do():
  235. game.settings["world"] = world
  236. game.current["settings_world_active"] = False
  237. game.menus = {}
  238. save_settings(game)
  239. wicon = "radio_false"
  240. if game.settings["world"] == world:
  241. wicon = "radio_true"
  242. ui.button(game, layer,
  243. putx+10,
  244. current_Y+25,
  245. max_width - 25 ,
  246. 13,
  247. menu="settings",
  248. scroll="settings",
  249. icon=wicon,
  250. string=str(game.worlds[world]["title"]),
  251. func=do)
  252. # Editor
  253. def do():
  254. game.settings["world"] = world
  255. game.current["settings_world_active"] = False
  256. game.menus = {}
  257. game.chunks = [[],{}]
  258. save_settings(game)
  259. game.scene = "editor"
  260. ui.button(game, layer,
  261. putx+max_width-13,
  262. current_Y+25,
  263. 13,
  264. 13,
  265. menu="settings",
  266. scroll="settings",
  267. icon="edit",
  268. func=do)
  269. current_Y += 15
  270. # Autosave option
  271. current_Y += 15
  272. if game.settings["autosave"]:
  273. wicon = "true"
  274. else:
  275. wicon = "false"
  276. def do():
  277. game.settings["autosave"] = not game.settings["autosave"]
  278. save_settings(game)
  279. ui.button(game, layer,
  280. putx+10,
  281. current_Y+25,
  282. max_width -10,
  283. 13,
  284. menu="settings",
  285. scroll="settings",
  286. icon=wicon,
  287. string="Autosave",
  288. func=do)
  289. current_Y += 15
  290. ui.scroll_area(game, layer, "settings", 5,20,
  291. game.current["w"]-10,
  292. game.current["h"]-50,
  293. current_Y)
  294. # Navigating the menus
  295. ui.button_navigate(game, "settings")
  296. return surface
  297. def update_worlds(game):
  298. """This function updates the selection of playable worlds."""
  299. game.worlds = {}
  300. for world in os.listdir(os.getcwd()+"/assets/worlds"):
  301. try:
  302. with open("assets/worlds/"+world+"/world.json") as f:
  303. metadata = json.load(f)
  304. except:
  305. metadata = {"title":world}
  306. game.worlds[world] = metadata
  307. def get_settings_folder(folder="ineh/"):
  308. try:
  309. data_dir = os.environ["XDG_DATA_HOME"] + "/" + folder
  310. except:
  311. data_dir = os.path.expanduser("~/.local/share/"+folder)
  312. try:
  313. os.makedirs(data_dir)
  314. except:
  315. pass
  316. return data_dir
  317. def save_settings(game):
  318. """This function saves the settings"""
  319. with open(get_settings_folder()+"config.json", 'w') as f:
  320. json.dump(game.settings, f, indent=4, sort_keys=True)
  321. def load_settings(game):
  322. """This function loads settings, or creates the settings
  323. from defautls"""
  324. defaults = {
  325. "pixels":4, # How big are pixels on the screen
  326. "fullscreen":False, # Whether the games is fullscreen
  327. "world":"ImNotEvenHuman", # What wold you are playing
  328. "autosave":True,
  329. "undo_history_buffer":10,
  330. "Blur":False,
  331. "NN-Downscaler-Blur":False, # Downscaler for the Depth of field effect when downscaling sprites
  332. "NN-Upscaler-Blur":False # Upscaler for the Depth of field effect when upscaling back sprites
  333. }
  334. try:
  335. with open(get_settings_folder()+"config.json") as f:
  336. game.settings = json.load(f)
  337. except:
  338. game.settings = defaults
  339. # Adds new missing attributes
  340. for i in defaults:
  341. if i not in game.settings:
  342. game.settings[i] = defaults[i]
  343. # print(game.settings)