editor.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. from modules import world
  18. def layer(game):
  19. # Setting up a cairo layer
  20. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
  21. game.current['w'],
  22. game.current['h'])
  23. layer = cairo.Context(surface)
  24. layer.set_antialias(cairo.ANTIALIAS_NONE)
  25. def do():
  26. game.scene = "settings"
  27. ui.button(game, layer,
  28. 5,
  29. 5 ,
  30. int(game.current["w"] / 3 ) - 30 ,
  31. 13,
  32. menu="editor",
  33. string="< Back",
  34. func=do)
  35. ui.color(game, layer, "#FFFFFF")
  36. ui.text(game, layer, game.worlds[game.settings["world"]]["title"],
  37. int(game.current["w"]/2), 5,
  38. align="center")
  39. # THE WORLD VIEW
  40. world.draw(game, layer, int(game.current["w"] / 3), 20,
  41. int(game.current["w"]/3*2),
  42. game.current["h"]-50, grid=True)
  43. # BOTTOM TOOL BAR
  44. # Heigh setting ( Layer )
  45. def do():
  46. game.current["camera"][2] += 1
  47. ui.button(game, layer,
  48. 2,
  49. game.current["h"]-30,
  50. 13 ,
  51. 13 ,
  52. menu="editor",
  53. icon="up",
  54. func=do)
  55. def do():
  56. game.current["camera"][2] -= 1
  57. ui.button(game, layer,
  58. 2,
  59. game.current["h"]-15,
  60. 13 ,
  61. 13 ,
  62. menu="editor",
  63. icon="down",
  64. func=do)
  65. ui.color(game, layer, "#00FF00")
  66. ui.text(game, layer, "Height",
  67. 17,game.current["h"]-30)
  68. ui.text(game, layer, str(game.current["camera"][2]),
  69. 17,game.current["h"]-15)
  70. # Brush size
  71. def do():
  72. game.current["editor_brush"] += 1
  73. ui.button(game, layer,
  74. 65,
  75. game.current["h"]-30,
  76. 13 ,
  77. 13 ,
  78. menu="editor",
  79. icon="up",
  80. func=do)
  81. def do():
  82. game.current["editor_brush"] -= 1
  83. if game.current["editor_brush"] < 1:
  84. game.current["editor_brush"] = 1
  85. ui.button(game, layer,
  86. 65,
  87. game.current["h"]-15,
  88. 13 ,
  89. 13 ,
  90. menu="editor",
  91. icon="down",
  92. func=do)
  93. ui.color(game, layer, "#00FF00")
  94. ui.text(game, layer, "Brush Size",
  95. 82,game.current["h"]-30)
  96. ui.text(game, layer, str(game.current["editor_brush"]),
  97. 82,game.current["h"]-15)
  98. # Save button
  99. def do():
  100. world.save_chunk(game)
  101. ui.button(game, layer,
  102. game.current["w"]-15,
  103. game.current["h"]-15,
  104. 13 ,
  105. 13 ,
  106. menu="editor",
  107. icon="floppy",
  108. func=do)
  109. # Save with hotkey
  110. if game.current["ctrlDown"] and 115 in game.current["keys"]:
  111. do()
  112. game.current["keys"].remove(115)
  113. #####################################
  114. # TREE VIEW SELECTOR #
  115. #####################################
  116. # The next section will implement a tree view
  117. # to select various editable assets.
  118. # Setting up the scroller
  119. if "editor" not in game.scroll:
  120. game.scroll["editor"] = 0
  121. current_Y = 0
  122. layer.rectangle(1, 19,
  123. int(game.current["w"]/3)-3,
  124. game.current["h"]-70)
  125. layer.clip() # From here, Draw only inside the mask layer.rectangle(1, 19...
  126. if "editor_chooser" not in game.current:
  127. game.current["editor_chooser"] = {}
  128. for types in game.elements:
  129. if "type_"+types not in game.current["editor_chooser"]:
  130. game.current["editor_chooser"]["type_"+types] = False
  131. def do():
  132. game.menus = {}
  133. game.current["editor_chooser"]["type_"+types] = not game.current["editor_chooser"]["type_"+types]
  134. if game.current["editor_chooser"]["type_"+types]:
  135. wicon = "down"
  136. else:
  137. wicon = "right"
  138. ui.button(game, layer,
  139. 2,
  140. current_Y+20,
  141. int(game.current["w"]/3)-4 ,
  142. 13 ,
  143. menu="editor",
  144. scroll="editor",
  145. icon=wicon,
  146. string=types,
  147. func=do,
  148. borders=False)
  149. current_Y += 13
  150. if game.current["editor_chooser"]["type_"+types]:
  151. current_Y += 5
  152. for asset in game.elements[types]:
  153. if "asset_"+asset not in game.current["editor_chooser"]:
  154. game.current["editor_chooser"]["asset_"+asset] = False
  155. try:
  156. assetname = game.images["assets/elements/"+types+"/"+asset]["title"]
  157. except:
  158. assetname = asset
  159. try:
  160. assetColor = game.images["assets/elements/"+types+"/"+asset]["menu_color"]
  161. except Exception as e:
  162. assetColor = "#000000"
  163. print("EEEEEEEEE: ",e)
  164. def do():
  165. game.menus = {}
  166. game.current["editor_chooser"]["asset_"+asset] = not game.current["editor_chooser"]["asset_"+asset]
  167. if game.current["editor_chooser"]["asset_"+asset]:
  168. wicon = "down"
  169. else:
  170. wicon = "right"
  171. ui.color(game, layer, assetColor)
  172. layer.rectangle(7, current_Y+20+game.scroll["editor"], int(game.current["w"]/3)-9 , 13)
  173. layer.fill()
  174. if isBright(assetColor):
  175. colorToSet = "#222222"
  176. else:
  177. colorToSet = "#FFFFFF"
  178. ui.button(game, layer,
  179. 7,
  180. current_Y+20,
  181. int(game.current["w"]/3)-9 ,
  182. 13 ,
  183. menu="editor",
  184. scroll="editor",
  185. icon=wicon,
  186. string=assetname,
  187. func=do,
  188. borders=False,
  189. btnTxtColor=colorToSet)
  190. current_Y += 13
  191. if game.current["editor_chooser"]["asset_"+asset]:
  192. current_Y += 5
  193. for n, version in enumerate(game.elements[types][asset]):
  194. def do():
  195. game.current["editor_selection"][0] = types
  196. game.current["editor_selection"][1] = asset
  197. game.current["editor_selection"][2] = n
  198. if game.current["editor_selection"][2] == n\
  199. and game.current["editor_selection"][1] == asset\
  200. and game.current["editor_selection"][0] == types:
  201. wicon = "radio_true"
  202. else:
  203. wicon = "radio_false"
  204. try:
  205. versionColor = version["menu_color"]
  206. except:
  207. versionColor = assetColor
  208. ui.color(game, layer, versionColor)
  209. layer.rectangle(10, current_Y+20+game.scroll["editor"], int(game.current["w"]/3)-12 , 13)
  210. layer.fill()
  211. if isBright(versionColor):
  212. colorToSet = "#222222"
  213. else:
  214. colorToSet = "#DDDDDD"
  215. ui.button(game, layer,
  216. 10,
  217. current_Y+20,
  218. int(game.current["w"]/3)-12 ,
  219. 13 ,
  220. menu="editor",
  221. scroll="editor",
  222. icon=wicon,
  223. string=str(version["title"]),
  224. func=do,
  225. borders=False,
  226. btnTxtColor=colorToSet)
  227. # Mouse over the block
  228. if int(game.current["mx"]) in range(10, int(game.current["w"]/3)+6 )\
  229. and int(game.current["my"]) in range(int(current_Y+20+game.scroll["editor"]), int(current_Y+20+game.scroll["editor"]+13)):
  230. ui.image(game, layer,
  231. game.current["mx"]-20,
  232. game.current["my"]-20,
  233. "assets/elements/"+types+"/"+asset, version["title"], offset=True)
  234. current_Y += 13
  235. current_Y += 5
  236. current_Y += 5
  237. ui.scroll_area(game, layer, "editor", 1, 19,
  238. int(game.current["w"]/3)-3,
  239. game.current["h"]-70,
  240. current_Y)
  241. # Navigating the menus
  242. ui.button_navigate(game, "editor")
  243. # Check chunks in memory
  244. if 32 in game.current["keys"]:
  245. print(game.chunks)
  246. print()
  247. game.current["keys"].remove(32)
  248. return surface
  249. def isBright(code):
  250. new_c = []
  251. for c in range(3):
  252. ind = int((len(code)-1)/3)
  253. new_c.append( int(code[ind*c+1:ind*c+ind+1], 16)/255 )
  254. avg = sum(new_c)/len(new_c)
  255. if avg > 0.5:
  256. return True
  257. else:
  258. return False