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