Commit | Line | Data |
---|---|---|
31237524 | 1 | from os.path import exists |
2aaa10d3 | 2 | from panda3d.core import AmbientLight, DirectionalLight, Point3, Texture, \ |
bdfa6dda | 3 | TextPropertiesManager, TextNode, Spotlight, PerspectiveLens, BitMask32 |
36099535 FC |
4 | from panda3d.bullet import BulletPlaneShape, BulletGhostNode |
5 | from direct.gui.OnscreenImage import OnscreenImage | |
2aaa10d3 FC |
6 | from direct.gui.OnscreenText import OnscreenText |
7 | from direct.gui.DirectGui import DirectButton, DirectFrame | |
36099535 | 8 | from direct.gui.DirectGuiGlobals import FLAT, DISABLED, NORMAL |
1be87278 FC |
9 | from direct.showbase.DirectObject import DirectObject |
10 | from pmachines.items.background import Background | |
11 | from pmachines.items.box import Box | |
d5932612 | 12 | from pmachines.items.shelf import Shelf |
e1438449 | 13 | from pmachines.items.domino import Domino |
8254721d | 14 | from pmachines.items.basketball import Basketball |
ea38777c | 15 | from pmachines.items.teetertooter import TeeterTooter |
a5dc83f4 | 16 | from pmachines.sidepanel import SidePanel |
79f81d48 | 17 | from lib.engine.gui.cursor import MouseCursor |
13263131 | 18 | from lib.lib.p3d.gfx import P3dGfxMgr |
1be87278 FC |
19 | |
20 | ||
21 | class Scene(DirectObject): | |
22 | ||
31237524 | 23 | def __init__(self, world, exit_cb, auto_close_instr, dbg_items): |
1be87278 FC |
24 | super().__init__() |
25 | self._world = world | |
5964572b | 26 | self._exit_cb = exit_cb |
31237524 | 27 | self._dbg_items = dbg_items |
1be87278 | 28 | self._set_camera() |
79f81d48 FC |
29 | self._cursor = MouseCursor( |
30 | 'assets/buttons/arrowUpLeft.png', (.04, 1, .04), (.5, .5, .5, 1), | |
31 | (.01, .01)) | |
36099535 | 32 | self._set_gui() |
1be87278 FC |
33 | self._set_lights() |
34 | self._set_input() | |
c8d8653f | 35 | self._set_mouse_plane() |
d5932612 FC |
36 | self.items = [] |
37 | self.reset() | |
9830561d | 38 | self._paused = False |
7c0a81ae | 39 | self._item_active = None |
e669403e | 40 | if auto_close_instr: |
32aa4dae | 41 | self.__store_state() |
e669403e FC |
42 | self.__restore_state() |
43 | else: | |
44 | self._set_instructions() | |
5964572b FC |
45 | self._bg = Background() |
46 | self._side_panel = SidePanel(world, self._mouse_plane_node, (-5, 4), (-3, 1), 1, self.items) | |
47 | self._scene_tsk = taskMgr.add(self.on_frame, 'on_frame') | |
48 | ||
d5932612 FC |
49 | def current_bottom(self): |
50 | curr_bottom = 1 | |
51 | for item in self.items: | |
52 | if item.repos_done: | |
53 | curr_bottom = min(curr_bottom, item.get_bottom()) | |
54 | return curr_bottom | |
55 | ||
05fd23ec | 56 | def reset(self): |
d5932612 | 57 | [itm.destroy() for itm in self.items] |
e1438449 FC |
58 | self.items = [Box(self._world, self._mouse_plane_node, 3, self.cb_inst, self.current_bottom, self.repos)] |
59 | self.items += [Shelf(self._world, self._mouse_plane_node, 3, self.cb_inst, self.current_bottom, self.repos)] | |
60 | self.items += [Domino(self._world, self._mouse_plane_node, 3, self.cb_inst, self.current_bottom, self.repos)] | |
8254721d | 61 | self.items += [Basketball(self._world, self._mouse_plane_node, 3, self.cb_inst, self.current_bottom, self.repos)] |
ea38777c | 62 | self.items += [TeeterTooter(self._world, self._mouse_plane_node, 3, self.cb_inst, self.current_bottom, self.repos)] |
32aa4dae FC |
63 | self._commands = [] |
64 | self._command_idx = 0 | |
05fd23ec | 65 | |
5964572b FC |
66 | def destroy(self): |
67 | self._unset_gui() | |
68 | self._unset_lights() | |
69 | self._unset_input() | |
70 | self._unset_mouse_plane() | |
71 | [itm.destroy() for itm in self.items] | |
72 | self._bg.destroy() | |
73 | self._side_panel.destroy() | |
407412a5 | 74 | self._cursor.destroy() |
5964572b | 75 | taskMgr.remove(self._scene_tsk) |
1be87278 FC |
76 | |
77 | def _set_camera(self): | |
78 | base.camera.set_pos(0, -20, 0) | |
79 | base.camera.look_at(0, 0, 0) | |
80 | ||
a0acba9a FC |
81 | def __load_img_btn(self, path, col): |
82 | img = OnscreenImage('assets/buttons/%s.png' % path) | |
83 | img.set_transparency(True) | |
84 | img.set_color(col) | |
85 | img.detach_node() | |
86 | return img | |
87 | ||
36099535 | 88 | def _set_gui(self): |
01b221a6 FC |
89 | def load_images_btn(path, col): |
90 | colors = { | |
91 | 'gray': [ | |
92 | (.6, .6, .6, 1), # ready | |
93 | (1, 1, 1, 1), # press | |
94 | (.8, .8, .8, 1), # rollover | |
95 | (.4, .4, .4, .4)], | |
96 | 'green': [ | |
97 | (.1, .68, .1, 1), | |
98 | (.1, 1, .1, 1), | |
99 | (.1, .84, .1, 1), | |
100 | (.4, .1, .1, .4)]}[col] | |
a0acba9a | 101 | return [self.__load_img_btn(path, col) for col in colors] |
36099535 FC |
102 | abl, abr = base.a2dBottomLeft, base.a2dBottomRight |
103 | btn_info = [ | |
5964572b | 104 | ('home', self.on_home, NORMAL, abl, 'gray'), |
9830561d | 105 | ('information', self._set_instructions, NORMAL, abl, 'gray'), |
01b221a6 FC |
106 | ('right', self.on_play, NORMAL, abr, 'green'), |
107 | ('next', self.on_next, DISABLED, abr, 'gray'), | |
108 | ('previous', self.on_prev, DISABLED, abr, 'gray'), | |
05fd23ec | 109 | ('rewind', self.reset, NORMAL, abr, 'gray')] |
36099535 | 110 | num_l = num_r = 0 |
a0acba9a | 111 | btns = [] |
36099535 | 112 | for binfo in btn_info: |
01b221a6 | 113 | imgs = load_images_btn(binfo[0], binfo[4]) |
36099535 FC |
114 | if binfo[3] == base.a2dBottomLeft: |
115 | sign, num = 1, num_l | |
116 | num_l += 1 | |
117 | else: | |
118 | sign, num = -1, num_r | |
119 | num_r += 1 | |
120 | fcols = (.4, .4, .4, .14), (.3, .3, .3, .05) | |
121 | btn = DirectButton( | |
122 | image=imgs, scale=.05, pos=(sign * (.06 + .11 * num), 1, .06), | |
123 | parent=binfo[3], command=binfo[1], state=binfo[2], relief=FLAT, | |
54a1397e FC |
124 | frameColor=fcols[0] if binfo[2] == NORMAL else fcols[1], |
125 | rolloverSound=loader.load_sfx('assets/audio/sfx/rollover.ogg'), | |
126 | clickSound=loader.load_sfx('assets/audio/sfx/click.ogg')) | |
36099535 | 127 | btn.set_transparency(True) |
a0acba9a FC |
128 | btns += [btn] |
129 | self.__home_btn, self.__info_btn, self.__right_btn, self.__next_btn, \ | |
130 | self.__prev_btn, self.__rewind_btn = btns | |
31237524 FC |
131 | if self._dbg_items: |
132 | self._info_txt = OnscreenText( | |
133 | '', parent=base.a2dTopRight, scale=0.04, | |
134 | pos=(-.03, -.06), fg=(.9, .9, .9, 1), align=TextNode.A_right) | |
36099535 | 135 | |
5964572b FC |
136 | def _unset_gui(self): |
137 | btns = [ | |
138 | self.__home_btn, self.__info_btn, self.__right_btn, | |
139 | self.__next_btn, self.__prev_btn, self.__rewind_btn] | |
140 | [btn.destroy() for btn in btns] | |
31237524 FC |
141 | if self._dbg_items: |
142 | self._info_txt.destroy() | |
5964572b | 143 | |
64eae9c7 FC |
144 | def _set_spotlight(self, name, pos, look_at, color, shadows=False): |
145 | light = Spotlight(name) | |
146 | if shadows: | |
147 | light.setLens(PerspectiveLens()) | |
1be87278 | 148 | light_np = render.attach_new_node(light) |
64eae9c7 FC |
149 | light_np.set_pos(pos) |
150 | light_np.look_at(look_at) | |
1be87278 FC |
151 | light.set_color(color) |
152 | render.set_light(light_np) | |
5964572b | 153 | return light_np |
1be87278 FC |
154 | |
155 | def _set_lights(self): | |
156 | alight = AmbientLight('alight') # for ao | |
64eae9c7 FC |
157 | alight.set_color((.15, .15, .15, 1)) |
158 | self._alnp = render.attach_new_node(alight) | |
159 | render.set_light(self._alnp) | |
160 | self._key_light = self._set_spotlight( | |
161 | 'key light', (-5, -80, 5), (0, 0, 0), (2.8, 2.8, 2.8, 1)) | |
162 | self._shadow_light = self._set_spotlight( | |
163 | 'key light', (-5, -80, 5), (0, 0, 0), (.58, .58, .58, 1), True) | |
164 | self._shadow_light.node().set_shadow_caster(True, 2048, 2048) | |
165 | self._shadow_light.node().get_lens().set_film_size(2048, 2048) | |
166 | self._shadow_light.node().get_lens().set_near_far(1, 256) | |
167 | self._shadow_light.node().set_camera_mask(BitMask32(0x01)) | |
5964572b FC |
168 | |
169 | def _unset_lights(self): | |
64eae9c7 | 170 | for light in [self._alnp, self._key_light, self._shadow_light]: |
5964572b FC |
171 | render.clear_light(light) |
172 | light.remove_node() | |
1be87278 FC |
173 | |
174 | def _set_input(self): | |
49c79300 | 175 | self.accept('mouse1', self.on_click_l) |
c8d8653f | 176 | self.accept('mouse1-up', self.on_release) |
49c79300 FC |
177 | self.accept('mouse3', self.on_click_r) |
178 | self.accept('mouse3-up', self.on_release) | |
c8d8653f | 179 | |
5964572b FC |
180 | def _unset_input(self): |
181 | for evt in ['mouse1', 'mouse1-up', 'mouse3', 'mouse3-up']: | |
182 | self.ignore(evt) | |
183 | ||
c8d8653f | 184 | def _set_mouse_plane(self): |
651713a9 | 185 | shape = BulletPlaneShape((0, -1, 0), 0) |
c8d8653f FC |
186 | #self._mouse_plane_node = BulletRigidBodyNode('mouse plane') |
187 | self._mouse_plane_node = BulletGhostNode('mouse plane') | |
188 | self._mouse_plane_node.addShape(shape) | |
189 | #np = render.attachNewNode(self._mouse_plane_node) | |
190 | #self._world.attachRigidBody(self._mouse_plane_node) | |
06af3aa9 | 191 | self._world.attach_ghost(self._mouse_plane_node) |
1be87278 | 192 | |
5964572b FC |
193 | def _unset_mouse_plane(self): |
194 | self._world.remove_ghost(self._mouse_plane_node) | |
195 | ||
37ba71d8 FC |
196 | def _get_hits(self): |
197 | if not base.mouseWatcherNode.has_mouse(): return [] | |
13263131 | 198 | p_from, p_to = P3dGfxMgr.world_from_to(base.mouseWatcherNode.get_mouse()) |
37ba71d8 FC |
199 | return self._world.ray_test_all(p_from, p_to).get_hits() |
200 | ||
31237524 FC |
201 | def _update_info(self, item): |
202 | txt = '' | |
203 | if item: | |
204 | txt = '%.3f %.3f\n%.3f°' % ( | |
205 | item._np.get_x(), item._np.get_z(), item._np.get_r()) | |
206 | self._info_txt['text'] = txt | |
207 | ||
49c79300 | 208 | def _on_click(self, method): |
a0acba9a FC |
209 | if self._paused: |
210 | return | |
c8d8653f FC |
211 | for hit in self._get_hits(): |
212 | if hit.get_node() == self._mouse_plane_node: | |
213 | pos = hit.get_hit_pos() | |
37ba71d8 | 214 | for hit in self._get_hits(): |
1be87278 | 215 | for item in [i for i in self.items if hit.get_node() == i.node]: |
7c0a81ae FC |
216 | if not self._item_active: |
217 | self._item_active = item | |
49c79300 | 218 | getattr(item, method)(pos) |
79f81d48 | 219 | img = 'move' if method == 'on_click_l' else 'rotate' |
a6843832 FC |
220 | if not (img == 'rotate' and not item._instantiated): |
221 | self._cursor.set_image('assets/buttons/%s.png' % img) | |
49c79300 FC |
222 | |
223 | def on_click_l(self): | |
224 | self._on_click('on_click_l') | |
225 | ||
226 | def on_click_r(self): | |
227 | self._on_click('on_click_r') | |
c8d8653f FC |
228 | |
229 | def on_release(self): | |
32aa4dae FC |
230 | if self._item_active and not self._item_active._first_command: |
231 | self._commands = self._commands[:self._command_idx] | |
232 | self._commands += [self._item_active] | |
233 | self._command_idx += 1 | |
234 | self.__prev_btn['state'] = NORMAL | |
235 | fcols = (.4, .4, .4, .14), (.3, .3, .3, .05) | |
236 | self.__prev_btn['frameColor'] = fcols[0] | |
237 | if self._item_active._command_idx == len(self._item_active._commands) - 1: | |
238 | self.__next_btn['state'] = DISABLED | |
239 | self.__next_btn['frameColor'] = fcols[1] | |
7c0a81ae | 240 | self._item_active = None |
49c79300 | 241 | [item.on_release() for item in self.items] |
79f81d48 | 242 | self._cursor.set_image('assets/buttons/arrowUpLeft.png') |
37ba71d8 | 243 | |
e1438449 | 244 | def repos(self): |
d5932612 FC |
245 | for item in self.items: |
246 | item.repos_done = False | |
e1438449 | 247 | self.items = sorted(self.items, key=lambda itm: itm.__class__.__name__) |
651713a9 | 248 | [item.on_aspect_ratio_changed() for item in self.items] |
a5dc83f4 | 249 | self._side_panel.update(self.items) |
e1438449 FC |
250 | max_x = -9 |
251 | for item in self.items: | |
252 | if not item._instantiated: | |
253 | max_x = max(item._np.get_x(), max_x) | |
254 | for item in self.items: | |
255 | if not item._instantiated: | |
256 | item.repos_x(max_x) | |
257 | ||
258 | def on_aspect_ratio_changed(self): | |
259 | self.repos() | |
651713a9 | 260 | |
37ba71d8 | 261 | def on_frame(self, task): |
c8d8653f FC |
262 | hits = self._get_hits() |
263 | pos = None | |
264 | for hit in self._get_hits(): | |
265 | if hit.get_node() == self._mouse_plane_node: | |
266 | pos = hit.get_hit_pos() | |
267 | hit_nodes = [hit.get_node() for hit in hits] | |
7c0a81ae FC |
268 | if self._item_active: |
269 | items_hit = [self._item_active] | |
270 | else: | |
271 | items_hit = [itm for itm in self.items if itm.node in hit_nodes] | |
37ba71d8 FC |
272 | items_no_hit = [itm for itm in self.items if itm not in items_hit] |
273 | [itm.on_mouse_on() for itm in items_hit] | |
274 | [itm.on_mouse_off() for itm in items_no_hit] | |
7c0a81ae FC |
275 | if pos and self._item_active: |
276 | self._item_active.on_mouse_move(pos) | |
31237524 FC |
277 | if self._dbg_items: |
278 | self._update_info(items_hit[0] if items_hit else None) | |
37ba71d8 | 279 | return task.cont |
c8d8653f | 280 | |
7850ccc4 FC |
281 | def cb_inst(self, item): |
282 | self.items += [item] | |
283 | ||
c8d8653f FC |
284 | def on_play(self): |
285 | [itm.play() for itm in self.items] | |
36099535 FC |
286 | |
287 | def on_next(self): | |
32aa4dae FC |
288 | self._commands[self._command_idx].redo() |
289 | self._command_idx += 1 | |
290 | fcols = (.4, .4, .4, .14), (.3, .3, .3, .05) | |
291 | self.__prev_btn['state'] = NORMAL | |
292 | self.__prev_btn['frameColor'] = fcols[0] | |
293 | more_commands = self._command_idx < len(self._commands) | |
294 | self.__next_btn['state'] = NORMAL if more_commands else DISABLED | |
295 | self.__next_btn['frameColor'] = fcols[0] if more_commands else fcols[1] | |
36099535 FC |
296 | |
297 | def on_prev(self): | |
32aa4dae FC |
298 | self._command_idx -= 1 |
299 | self._commands[self._command_idx].undo() | |
300 | fcols = (.4, .4, .4, .14), (.3, .3, .3, .05) | |
301 | self.__next_btn['state'] = NORMAL | |
302 | self.__next_btn['frameColor'] = fcols[0] | |
303 | self.__prev_btn['state'] = NORMAL if self._command_idx else DISABLED | |
304 | self.__prev_btn['frameColor'] = fcols[0] if self._command_idx else fcols[1] | |
36099535 | 305 | |
36099535 | 306 | def on_home(self): |
5964572b | 307 | self._exit_cb() |
36099535 | 308 | |
2aaa10d3 | 309 | def _set_instructions(self): |
9830561d FC |
310 | self._paused = True |
311 | self.__store_state() | |
2aaa10d3 FC |
312 | mgr = TextPropertiesManager.get_global_ptr() |
313 | for name in ['mouse_l', 'mouse_r']: | |
314 | graphic = OnscreenImage('assets/buttons/%s.png' % name) | |
315 | graphic.set_scale(.5) | |
316 | graphic.get_texture().set_minfilter(Texture.FTLinearMipmapLinear) | |
317 | graphic.get_texture().set_anisotropic_degree(2) | |
318 | mgr.set_graphic(name, graphic) | |
319 | graphic.set_z(-.2) | |
320 | graphic.set_transparency(True) | |
321 | graphic.detach_node() | |
322 | frm = DirectFrame(frameColor=(.4, .4, .4, .06), | |
323 | frameSize=(-.6, .6, -.3, .3)) | |
324 | font = base.loader.load_font('assets/fonts/Hanken-Book.ttf') | |
325 | font.clear() | |
326 | font.set_pixels_per_unit(60) | |
327 | font.set_minfilter(Texture.FTLinearMipmapLinear) | |
328 | font.set_outline((0, 0, 0, 1), .8, .2) | |
329 | txt = _('keep \5mouse_l\5 pressed to drag an item\n\n' | |
330 | 'keep \5mouse_r\5 pressed to rotate an item') | |
331 | self._txt = OnscreenText( | |
332 | txt, parent=frm, font=font, scale=0.06, fg=(.9, .9, .9, 1), | |
333 | align=TextNode.A_left) | |
334 | u_l = self._txt.textNode.get_upper_left_3d() | |
335 | l_r = self._txt.textNode.get_lower_right_3d() | |
336 | w, h = l_r[0] - u_l[0], u_l[2] - l_r[2] | |
a0acba9a FC |
337 | btn_scale = .05 |
338 | mar = .06 # margin | |
2aaa10d3 | 339 | z = h / 2 - font.get_line_height() * self._txt['scale'][1] |
a0acba9a | 340 | z += (btn_scale + 2 * mar) / 2 |
2aaa10d3 FC |
341 | self._txt['pos'] = -w / 2, z |
342 | u_l = self._txt.textNode.get_upper_left_3d() | |
343 | l_r = self._txt.textNode.get_lower_right_3d() | |
a0acba9a FC |
344 | c_l_r = l_r[0], l_r[1], l_r[2] - 2 * mar - btn_scale |
345 | fsz = u_l[0] - mar, l_r[0] + mar, c_l_r[2] - mar, u_l[2] + mar | |
2aaa10d3 | 346 | frm['frameSize'] = fsz |
a0acba9a FC |
347 | colors = [ |
348 | (.6, .6, .6, 1), # ready | |
349 | (1, 1, 1, 1), # press | |
350 | (.8, .8, .8, 1), # rollover | |
351 | (.4, .4, .4, .4)] | |
352 | imgs = [self.__load_img_btn('exitRight', col) for col in colors] | |
353 | btn = DirectButton( | |
354 | image=imgs, scale=btn_scale, | |
355 | pos=(l_r[0] - btn_scale, 1, l_r[2] - mar - btn_scale), | |
356 | parent=frm, command=self.__on_close_instructions, extraArgs=[frm], | |
357 | relief=FLAT, frameColor=(.6, .6, .6, .08), | |
358 | rolloverSound=loader.load_sfx('assets/audio/sfx/rollover.ogg'), | |
359 | clickSound=loader.load_sfx('assets/audio/sfx/click.ogg')) | |
360 | btn.set_transparency(True) | |
361 | ||
362 | def __store_state(self): | |
363 | btns = [ | |
364 | self.__home_btn, self.__info_btn, self.__right_btn, | |
365 | self.__next_btn, self.__prev_btn, self.__rewind_btn] | |
366 | self.__btn_state = [btn['state'] for btn in btns] | |
367 | for btn in btns: | |
368 | btn['state'] = DISABLED | |
369 | [itm.store_state() for itm in self.items] | |
370 | ||
371 | def __restore_state(self): | |
372 | btns = [ | |
373 | self.__home_btn, self.__info_btn, self.__right_btn, | |
374 | self.__next_btn, self.__prev_btn, self.__rewind_btn] | |
375 | for btn, state in zip(btns, self.__btn_state): | |
376 | btn['state'] = state | |
377 | [itm.restore_state() for itm in self.items] | |
378 | self._paused = False | |
379 | ||
380 | def __on_close_instructions(self, frm): | |
381 | frm.remove_node() | |
382 | self.__restore_state() |