settings.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. current_Y = 0
  42. layer.rectangle(4,20,
  43. game.current["w"]-10,
  44. game.current["h"]-25)
  45. layer.clip()
  46. # WORLD
  47. if "settings_world_active" not in game.current:
  48. game.current["settings_world_active"] = False
  49. wicon = "right"
  50. if game.current["settings_world_active"]:
  51. wicon = "down"
  52. def do():
  53. game.current["settings_world_active"] = not game.current["settings_world_active"]
  54. game.menus = {}
  55. ui.button(game, layer,
  56. 5,
  57. current_Y+25,
  58. int(game.current["w"]) - 11 ,
  59. 13,
  60. menu="settings",
  61. scroll="settings",
  62. icon=wicon,
  63. string="World: "+game.worlds[game.settings["world"]]["title"],
  64. func=do)
  65. current_Y += 15
  66. # List of worlds
  67. if game.current["settings_world_active"]:
  68. for world in game.worlds:
  69. def do():
  70. game.settings["world"] = world
  71. game.current["settings_world_active"] = False
  72. game.menus = {}
  73. save_settings(game)
  74. wicon = "radio_false"
  75. if game.settings["world"] == world:
  76. wicon = "radio_true"
  77. ui.button(game, layer,
  78. 15,
  79. current_Y+25,
  80. int(game.current["w"]) - 36 ,
  81. 13,
  82. menu="settings",
  83. scroll="settings",
  84. icon=wicon,
  85. string=str(game.worlds[world]["title"]),
  86. func=do)
  87. # Editor
  88. def do():
  89. game.settings["world"] = world
  90. game.current["settings_world_active"] = False
  91. game.menus = {}
  92. game.chunks = [[],{}]
  93. save_settings(game)
  94. game.scene = "editor"
  95. ui.button(game, layer,
  96. game.current["w"]-19,
  97. current_Y+25,
  98. 13,
  99. 13,
  100. menu="settings",
  101. scroll="settings",
  102. icon="edit",
  103. func=do)
  104. current_Y += 15
  105. # VIRTUAL PIXEL TO REAL PIXEL RATIO
  106. ui.color(game, layer, "#00FF00")
  107. ui.text(game, layer, "Pixels size: ",
  108. 5,
  109. current_Y+28+game.scroll["settings"])
  110. def do():
  111. game.settings["pixels"] += 1
  112. save_settings(game)
  113. ui.button(game, layer,
  114. game.current["w"]-75,
  115. current_Y+25,
  116. 13,
  117. 13,
  118. menu="settings",
  119. scroll="settings",
  120. string="+",
  121. func=do)
  122. ui.color(game, layer, "#00FF00")
  123. ui.text(game, layer, str(game.settings["pixels"]),
  124. game.current["w"]-50,
  125. current_Y+28+game.scroll["settings"])
  126. def do():
  127. game.settings["pixels"] -= 1
  128. if game.settings["pixels"] < 1:
  129. game.settings["pixels"] = 1
  130. save_settings(game)
  131. ui.button(game, layer,
  132. game.current["w"]-19,
  133. current_Y+25,
  134. 13,
  135. 13,
  136. menu="settings",
  137. scroll="settings",
  138. string="-",
  139. func=do)
  140. current_Y += 15
  141. # FULLSCREEN
  142. ficon = "fullscreen"
  143. if game.settings["fullscreen"]:
  144. ficon = "unfullscreen"
  145. def do():
  146. game.settings["fullscreen"] = not game.settings["fullscreen"]
  147. save_settings(game)
  148. ui.button(game, layer,
  149. 5,
  150. current_Y+25,
  151. int(game.current["w"]/2) - 5,
  152. 13,
  153. menu="settings",
  154. scroll="settings",
  155. icon=ficon,
  156. string="Fullscreen",
  157. func=do)
  158. # TESTING MODE
  159. dicon = "false"
  160. if game.current["testing"]:
  161. dicon = "true"
  162. def do():
  163. game.current["testing"] = not game.current["testing"]
  164. ui.button(game, layer,
  165. int(game.current["w"]/2) + 2,
  166. current_Y+25,
  167. int(game.current["w"]/2) - 8,
  168. 13,
  169. menu="settings",
  170. scroll="settings",
  171. icon=dicon,
  172. string="Debug Mode",
  173. func=do)
  174. current_Y += 15
  175. ui.scroll_area(game, layer, "settings", 5,20,
  176. game.current["w"]-10,
  177. game.current["h"]-25,
  178. current_Y)
  179. # Navigating the menus
  180. ui.button_navigate(game, "settings")
  181. return surface
  182. def update_worlds(game):
  183. """This function updates the selection of playable worlds."""
  184. game.worlds = {}
  185. for world in os.listdir(os.getcwd()+"/assets/worlds"):
  186. try:
  187. with open("assets/worlds/"+world+"/world.json") as f:
  188. metadata = json.load(f)
  189. except:
  190. metadata = {"title":world}
  191. game.worlds[world] = metadata
  192. def get_settings_folder(folder="ineh/"):
  193. try:
  194. data_dir = os.environ["XDG_DATA_HOME"] + "/" + folder
  195. except:
  196. data_dir = os.path.expanduser("~/.local/share/"+folder)
  197. try:
  198. os.makedirs(data_dir)
  199. except:
  200. pass
  201. return data_dir
  202. def save_settings(game):
  203. """This function saves the settings"""
  204. with open(get_settings_folder()+"config.json", 'w') as f:
  205. json.dump(game.settings, f, indent=4, sort_keys=True)
  206. def load_settings(game):
  207. """This function loads settings, or creates the settings
  208. from defautls"""
  209. defaults = {
  210. "pixels":4, # How big are pixels on the screen
  211. "fullscreen":False, # Whether the games is fullscreen
  212. "world":"ImNotEvenHuman"} # What wold you are playing
  213. try:
  214. with open(get_settings_folder()+"config.json") as f:
  215. game.settings = json.load(f)
  216. except:
  217. game.settings = defaults