Commit | Line | Data |
---|---|---|
d3a2e50a FC |
1 | from copy import deepcopy |
2 | from json import dumps | |
3 | import hashlib | |
4 | from panda3d.core import Texture, TextNode | |
5 | from direct.gui.OnscreenImage import OnscreenImage | |
6 | from direct.gui.DirectGui import DirectButton, DirectFrame, DirectEntry, YesNoDialog | |
7 | from direct.gui.DirectGuiGlobals import FLAT, NORMAL | |
8 | from direct.gui.OnscreenText import OnscreenText | |
9 | ||
10 | ||
11 | class SceneEditor: | |
12 | ||
13 | def __init__(self, json, json_name): | |
14 | self.__json = json | |
15 | self.__json_name = json_name | |
16 | self._font = base.loader.load_font( | |
17 | 'assets/fonts/Hanken-Book.ttf') | |
18 | self._font.clear() | |
19 | self._font.set_pixels_per_unit(60) | |
20 | self._font.set_minfilter(Texture.FTLinearMipmapLinear) | |
21 | self._font.set_outline((0, 0, 0, 1), .8, .2) | |
22 | self._common = { | |
23 | 'scale': .046, | |
24 | 'text_font': self._font, | |
25 | 'text_fg': (.9, .9, .9, 1), | |
26 | 'relief': FLAT, | |
27 | 'frameColor': (.4, .4, .4, .14), | |
28 | 'rolloverSound': loader.load_sfx( | |
29 | 'assets/audio/sfx/rollover.ogg'), | |
30 | 'clickSound': loader.load_sfx( | |
31 | 'assets/audio/sfx/click.ogg')} | |
32 | w, h, tw, l = 1.8, .9, 30, .36 | |
33 | self._frm = DirectFrame(frameColor=(.4, .4, .4, .06), | |
34 | frameSize=(0, w, 0, h), | |
35 | parent=base.a2dBottomCenter, | |
36 | pos=(-w/2, 0, 0)) | |
37 | OnscreenText( | |
38 | _('Name'), pos=(l - .03, h - .1), parent=self._frm, | |
39 | font=self._common['text_font'], | |
40 | scale=self._common['scale'], | |
41 | fg=self._common['text_fg'], | |
42 | align=TextNode.A_right) | |
43 | self.__name_entry = DirectEntry( | |
44 | scale=self._common['scale'], | |
45 | pos=(l, 1, h - .1), | |
46 | entryFont=self._font, | |
47 | width=tw, | |
48 | frameColor=self._common['frameColor'], | |
49 | initialText=json['name'], | |
50 | parent=self._frm, | |
51 | text_fg=self._common['text_fg']) | |
52 | OnscreenText( | |
53 | _('Description'), pos=(l - .03, h - .2), parent=self._frm, | |
54 | font=self._common['text_font'], | |
55 | scale=self._common['scale'], | |
56 | fg=self._common['text_fg'], | |
57 | align=TextNode.A_right) | |
58 | def add_line_break(txt, entry): | |
59 | curpos = entry.node().getCursorPosition() | |
60 | entry.set(txt[:curpos]+ "\n" + txt[curpos:]) | |
61 | entry.node().setCursorPosition(curpos+1) | |
62 | entry['focus']=1 | |
63 | self.__instructions_entry = DirectEntry( | |
64 | scale=self._common['scale'], | |
65 | pos=(l, 1, h - .2), | |
66 | entryFont=self._font, | |
67 | width=tw, | |
68 | numLines=12, | |
69 | cursorKeys=True, | |
70 | frameColor=self._common['frameColor'], | |
71 | initialText=json['instructions'].replace('\\n', '\n'), | |
72 | parent=self._frm, | |
73 | text_fg=self._common['text_fg']) | |
74 | self.__instructions_entry['command'] = add_line_break | |
75 | self.__instructions_entry['extraArgs'] = [self.__instructions_entry] | |
76 | def load_images_btn(path, col): | |
77 | colors = { | |
78 | 'gray': [ | |
79 | (.6, .6, .6, 1), # ready | |
80 | (1, 1, 1, 1), # press | |
81 | (.8, .8, .8, 1), # rollover | |
82 | (.4, .4, .4, .4)], | |
83 | 'green': [ | |
84 | (.1, .68, .1, 1), | |
85 | (.1, 1, .1, 1), | |
86 | (.1, .84, .1, 1), | |
87 | (.4, .1, .1, .4)]}[col] | |
88 | return [self.__load_img_btn(path, col) for col in colors] | |
89 | fcols = (.4, .4, .4, .14), (.3, .3, .3, .05) | |
90 | btn = DirectButton( | |
91 | image=load_images_btn('exitRight', 'gray'), scale=.05, | |
92 | pos=(.06, 1, .06), | |
93 | parent=self._frm, command=self.__on_close, state=NORMAL, relief=FLAT, | |
94 | frameColor=fcols[0], | |
95 | rolloverSound=loader.load_sfx('assets/audio/sfx/rollover.ogg'), | |
96 | clickSound=loader.load_sfx('assets/audio/sfx/click.ogg')) | |
97 | btn = DirectButton( | |
98 | image=load_images_btn('save', 'gray'), scale=.05, | |
99 | pos=(.06, 1, .18), | |
100 | parent=self._frm, command=self.__on_save, state=NORMAL, relief=FLAT, | |
101 | frameColor=fcols[0], | |
102 | rolloverSound=loader.load_sfx('assets/audio/sfx/rollover.ogg'), | |
103 | clickSound=loader.load_sfx('assets/audio/sfx/click.ogg')) | |
104 | ||
105 | def __on_close(self): | |
106 | self.__json['name'] = self.__name_entry.get() | |
107 | self.__json['instructions'] = self.__instructions_entry.get() | |
108 | if self.__compute_hash() == self.__json['version']: | |
109 | self._frm.destroy() | |
110 | else: | |
111 | self.__dialog = YesNoDialog(dialogName='Unsaved changes', | |
112 | text=_('You have unsaved changes. Really quit?'), | |
113 | command=self.__actually_close) | |
114 | ||
115 | def __actually_close(self, arg): | |
116 | if arg: | |
117 | self._frm.destroy() | |
118 | self.__dialog.cleanup() | |
119 | ||
120 | def __on_save(self): | |
121 | self.__json['name'] = self.__name_entry.get() | |
122 | self.__json['instructions'] = self.__instructions_entry.get() | |
123 | self.__json['version'] = self.__compute_hash() | |
124 | with open('assets/scenes/%s.json' % self.__json_name, 'w') as f: | |
125 | f.write(dumps(self.__json, indent=2, sort_keys=True)) | |
126 | ||
127 | def __load_img_btn(self, path, col): | |
128 | img = OnscreenImage('assets/images/buttons/%s.dds' % path) | |
129 | img.set_transparency(True) | |
130 | img.set_color(col) | |
131 | img.detach_node() | |
132 | return img | |
133 | ||
134 | def __compute_hash(self): | |
135 | new_dict = deepcopy(self.__json) | |
136 | del new_dict['version'] | |
137 | new_dict_str = str(dumps(new_dict, indent=2, sort_keys=True)) | |
138 | h = hashlib.new('sha256') | |
139 | h.update(new_dict_str.encode()) | |
140 | return h.hexdigest()[:12] |