Commit | Line | Data |
---|---|---|
2aaa10d3 | 1 | from panda3d.core import AmbientLight, DirectionalLight, Point3, Texture, \ |
5fdf77d0 | 2 | TextPropertiesManager, TextNode, Spotlight, PerspectiveLens |
36099535 FC |
3 | from panda3d.bullet import BulletPlaneShape, BulletGhostNode |
4 | from direct.gui.OnscreenImage import OnscreenImage | |
2aaa10d3 FC |
5 | from direct.gui.OnscreenText import OnscreenText |
6 | from direct.gui.DirectGui import DirectButton, DirectFrame | |
36099535 | 7 | from direct.gui.DirectGuiGlobals import FLAT, DISABLED, NORMAL |
1be87278 FC |
8 | from direct.showbase.DirectObject import DirectObject |
9 | from pmachines.items.background import Background | |
10 | from pmachines.items.box import Box | |
a5dc83f4 | 11 | from pmachines.sidepanel import SidePanel |
79f81d48 | 12 | from lib.engine.gui.cursor import MouseCursor |
13263131 | 13 | from lib.lib.p3d.gfx import P3dGfxMgr |
1be87278 FC |
14 | |
15 | ||
16 | class Scene(DirectObject): | |
17 | ||
5964572b | 18 | def __init__(self, world, exit_cb): |
1be87278 FC |
19 | super().__init__() |
20 | self._world = world | |
5964572b | 21 | self._exit_cb = exit_cb |
1be87278 | 22 | self._set_camera() |
79f81d48 FC |
23 | self._cursor = MouseCursor( |
24 | 'assets/buttons/arrowUpLeft.png', (.04, 1, .04), (.5, .5, .5, 1), | |
25 | (.01, .01)) | |
36099535 | 26 | self._set_gui() |
1be87278 FC |
27 | self._set_lights() |
28 | self._set_input() | |
c8d8653f | 29 | self._set_mouse_plane() |
a0acba9a FC |
30 | self.items = [Box(world, self._mouse_plane_node, 3, self.cb_inst)] |
31 | self._paused = True | |
32 | self.__store_state() | |
2aaa10d3 | 33 | self._set_instructions() |
5964572b FC |
34 | self._bg = Background() |
35 | self._side_panel = SidePanel(world, self._mouse_plane_node, (-5, 4), (-3, 1), 1, self.items) | |
36 | self._scene_tsk = taskMgr.add(self.on_frame, 'on_frame') | |
37 | ||
38 | def destroy(self): | |
39 | self._unset_gui() | |
40 | self._unset_lights() | |
41 | self._unset_input() | |
42 | self._unset_mouse_plane() | |
43 | [itm.destroy() for itm in self.items] | |
44 | self._bg.destroy() | |
45 | self._side_panel.destroy() | |
46 | taskMgr.remove(self._scene_tsk) | |
1be87278 FC |
47 | |
48 | def _set_camera(self): | |
49 | base.camera.set_pos(0, -20, 0) | |
50 | base.camera.look_at(0, 0, 0) | |
51 | ||
a0acba9a FC |
52 | def __load_img_btn(self, path, col): |
53 | img = OnscreenImage('assets/buttons/%s.png' % path) | |
54 | img.set_transparency(True) | |
55 | img.set_color(col) | |
56 | img.detach_node() | |
57 | return img | |
58 | ||
36099535 | 59 | def _set_gui(self): |
01b221a6 FC |
60 | def load_images_btn(path, col): |
61 | colors = { | |
62 | 'gray': [ | |
63 | (.6, .6, .6, 1), # ready | |
64 | (1, 1, 1, 1), # press | |
65 | (.8, .8, .8, 1), # rollover | |
66 | (.4, .4, .4, .4)], | |
67 | 'green': [ | |
68 | (.1, .68, .1, 1), | |
69 | (.1, 1, .1, 1), | |
70 | (.1, .84, .1, 1), | |
71 | (.4, .1, .1, .4)]}[col] | |
a0acba9a | 72 | return [self.__load_img_btn(path, col) for col in colors] |
36099535 FC |
73 | abl, abr = base.a2dBottomLeft, base.a2dBottomRight |
74 | btn_info = [ | |
5964572b | 75 | ('home', self.on_home, NORMAL, abl, 'gray'), |
01b221a6 FC |
76 | ('information', self.on_information, DISABLED, abl, 'gray'), |
77 | ('right', self.on_play, NORMAL, abr, 'green'), | |
78 | ('next', self.on_next, DISABLED, abr, 'gray'), | |
79 | ('previous', self.on_prev, DISABLED, abr, 'gray'), | |
80 | ('rewind', self.on_rewind, DISABLED, abr, 'gray')] | |
36099535 | 81 | num_l = num_r = 0 |
a0acba9a | 82 | btns = [] |
36099535 | 83 | for binfo in btn_info: |
01b221a6 | 84 | imgs = load_images_btn(binfo[0], binfo[4]) |
36099535 FC |
85 | if binfo[3] == base.a2dBottomLeft: |
86 | sign, num = 1, num_l | |
87 | num_l += 1 | |
88 | else: | |
89 | sign, num = -1, num_r | |
90 | num_r += 1 | |
91 | fcols = (.4, .4, .4, .14), (.3, .3, .3, .05) | |
92 | btn = DirectButton( | |
93 | image=imgs, scale=.05, pos=(sign * (.06 + .11 * num), 1, .06), | |
94 | parent=binfo[3], command=binfo[1], state=binfo[2], relief=FLAT, | |
54a1397e FC |
95 | frameColor=fcols[0] if binfo[2] == NORMAL else fcols[1], |
96 | rolloverSound=loader.load_sfx('assets/audio/sfx/rollover.ogg'), | |
97 | clickSound=loader.load_sfx('assets/audio/sfx/click.ogg')) | |
36099535 | 98 | btn.set_transparency(True) |
a0acba9a FC |
99 | btns += [btn] |
100 | self.__home_btn, self.__info_btn, self.__right_btn, self.__next_btn, \ | |
101 | self.__prev_btn, self.__rewind_btn = btns | |
36099535 | 102 | |
5964572b FC |
103 | def _unset_gui(self): |
104 | btns = [ | |
105 | self.__home_btn, self.__info_btn, self.__right_btn, | |
106 | self.__next_btn, self.__prev_btn, self.__rewind_btn] | |
107 | [btn.destroy() for btn in btns] | |
108 | ||
1be87278 FC |
109 | def _set_directional_light(self, name, hpr, color): |
110 | light = DirectionalLight(name) | |
111 | light_np = render.attach_new_node(light) | |
112 | light_np.set_hpr(*hpr) | |
113 | light.set_color(color) | |
114 | render.set_light(light_np) | |
5964572b | 115 | return light_np |
1be87278 FC |
116 | |
117 | def _set_lights(self): | |
118 | alight = AmbientLight('alight') # for ao | |
119 | alight.set_color((.4, .4, .4, 1)) | |
120 | alnp = render.attach_new_node(alight) | |
121 | render.set_light(alnp) | |
5964572b FC |
122 | self._key_light = self._set_directional_light( |
123 | 'key light', (315, -60, 0), (3.6, 3.6, 3.6, 1)) | |
124 | self._fill_light = self._set_directional_light( | |
125 | 'fill light', (195, -30, 0), (.4, .4, .4, 1)) | |
126 | self._rim_light = self._set_directional_light( | |
127 | 'rim light', (75, -30, 0), (.3, .3, .3, 1)) | |
5fdf77d0 FC |
128 | slight = Spotlight('slight') |
129 | slight.set_color((.1, .1, .1, 1)) | |
130 | slight.setLens(PerspectiveLens()) | |
131 | self.__shadow_light = render.attachNewNode(slight) | |
132 | self.__shadow_light.set_pos(-5, -80, 5) | |
133 | self.__shadow_light.look_at((0, 0, 0)) | |
134 | render.set_light(self.__shadow_light) | |
135 | self.__shadow_light.node().set_shadow_caster(True, 2048, 2048) | |
136 | self.__shadow_light.node().get_lens().set_film_size(2048, 2048) | |
137 | self.__shadow_light.node().get_lens().set_near_far(1, 256) | |
5964572b FC |
138 | |
139 | def _unset_lights(self): | |
5fdf77d0 FC |
140 | for light in [self._key_light, self._fill_light, self._rim_light, |
141 | self.__shadow_light]: | |
5964572b FC |
142 | render.clear_light(light) |
143 | light.remove_node() | |
1be87278 FC |
144 | |
145 | def _set_input(self): | |
49c79300 | 146 | self.accept('mouse1', self.on_click_l) |
c8d8653f | 147 | self.accept('mouse1-up', self.on_release) |
49c79300 FC |
148 | self.accept('mouse3', self.on_click_r) |
149 | self.accept('mouse3-up', self.on_release) | |
c8d8653f | 150 | |
5964572b FC |
151 | def _unset_input(self): |
152 | for evt in ['mouse1', 'mouse1-up', 'mouse3', 'mouse3-up']: | |
153 | self.ignore(evt) | |
154 | ||
c8d8653f | 155 | def _set_mouse_plane(self): |
651713a9 | 156 | shape = BulletPlaneShape((0, -1, 0), 0) |
c8d8653f FC |
157 | #self._mouse_plane_node = BulletRigidBodyNode('mouse plane') |
158 | self._mouse_plane_node = BulletGhostNode('mouse plane') | |
159 | self._mouse_plane_node.addShape(shape) | |
160 | #np = render.attachNewNode(self._mouse_plane_node) | |
161 | #self._world.attachRigidBody(self._mouse_plane_node) | |
06af3aa9 | 162 | self._world.attach_ghost(self._mouse_plane_node) |
1be87278 | 163 | |
5964572b FC |
164 | def _unset_mouse_plane(self): |
165 | self._world.remove_ghost(self._mouse_plane_node) | |
166 | ||
37ba71d8 FC |
167 | def _get_hits(self): |
168 | if not base.mouseWatcherNode.has_mouse(): return [] | |
13263131 | 169 | p_from, p_to = P3dGfxMgr.world_from_to(base.mouseWatcherNode.get_mouse()) |
37ba71d8 FC |
170 | return self._world.ray_test_all(p_from, p_to).get_hits() |
171 | ||
49c79300 | 172 | def _on_click(self, method): |
a0acba9a FC |
173 | if self._paused: |
174 | return | |
c8d8653f FC |
175 | for hit in self._get_hits(): |
176 | if hit.get_node() == self._mouse_plane_node: | |
177 | pos = hit.get_hit_pos() | |
37ba71d8 | 178 | for hit in self._get_hits(): |
1be87278 | 179 | for item in [i for i in self.items if hit.get_node() == i.node]: |
49c79300 | 180 | getattr(item, method)(pos) |
79f81d48 FC |
181 | img = 'move' if method == 'on_click_l' else 'rotate' |
182 | self._cursor.set_image('assets/buttons/%s.png' % img) | |
49c79300 FC |
183 | |
184 | def on_click_l(self): | |
185 | self._on_click('on_click_l') | |
186 | ||
187 | def on_click_r(self): | |
188 | self._on_click('on_click_r') | |
c8d8653f FC |
189 | |
190 | def on_release(self): | |
49c79300 | 191 | [item.on_release() for item in self.items] |
79f81d48 | 192 | self._cursor.set_image('assets/buttons/arrowUpLeft.png') |
37ba71d8 | 193 | |
651713a9 FC |
194 | def on_aspect_ratio_changed(self): |
195 | [item.on_aspect_ratio_changed() for item in self.items] | |
a5dc83f4 | 196 | self._side_panel.update(self.items) |
651713a9 | 197 | |
37ba71d8 | 198 | def on_frame(self, task): |
c8d8653f FC |
199 | hits = self._get_hits() |
200 | pos = None | |
201 | for hit in self._get_hits(): | |
202 | if hit.get_node() == self._mouse_plane_node: | |
203 | pos = hit.get_hit_pos() | |
204 | hit_nodes = [hit.get_node() for hit in hits] | |
37ba71d8 FC |
205 | items_hit = [itm for itm in self.items if itm.node in hit_nodes] |
206 | items_no_hit = [itm for itm in self.items if itm not in items_hit] | |
207 | [itm.on_mouse_on() for itm in items_hit] | |
208 | [itm.on_mouse_off() for itm in items_no_hit] | |
c8d8653f FC |
209 | if pos: |
210 | [itm.on_mouse_move(pos) for itm in self.items] | |
37ba71d8 | 211 | return task.cont |
c8d8653f | 212 | |
7850ccc4 FC |
213 | def cb_inst(self, item): |
214 | self.items += [item] | |
215 | ||
c8d8653f FC |
216 | def on_play(self): |
217 | [itm.play() for itm in self.items] | |
36099535 FC |
218 | |
219 | def on_next(self): | |
220 | print('on_next') | |
221 | ||
222 | def on_prev(self): | |
223 | print('on_prev') | |
224 | ||
225 | def on_rewind(self): | |
226 | print('on_rewind') | |
227 | ||
228 | def on_home(self): | |
5964572b | 229 | self._exit_cb() |
36099535 FC |
230 | |
231 | def on_information(self): | |
232 | print('on_information') | |
2aaa10d3 FC |
233 | |
234 | def _set_instructions(self): | |
235 | mgr = TextPropertiesManager.get_global_ptr() | |
236 | for name in ['mouse_l', 'mouse_r']: | |
237 | graphic = OnscreenImage('assets/buttons/%s.png' % name) | |
238 | graphic.set_scale(.5) | |
239 | graphic.get_texture().set_minfilter(Texture.FTLinearMipmapLinear) | |
240 | graphic.get_texture().set_anisotropic_degree(2) | |
241 | mgr.set_graphic(name, graphic) | |
242 | graphic.set_z(-.2) | |
243 | graphic.set_transparency(True) | |
244 | graphic.detach_node() | |
245 | frm = DirectFrame(frameColor=(.4, .4, .4, .06), | |
246 | frameSize=(-.6, .6, -.3, .3)) | |
247 | font = base.loader.load_font('assets/fonts/Hanken-Book.ttf') | |
248 | font.clear() | |
249 | font.set_pixels_per_unit(60) | |
250 | font.set_minfilter(Texture.FTLinearMipmapLinear) | |
251 | font.set_outline((0, 0, 0, 1), .8, .2) | |
252 | txt = _('keep \5mouse_l\5 pressed to drag an item\n\n' | |
253 | 'keep \5mouse_r\5 pressed to rotate an item') | |
254 | self._txt = OnscreenText( | |
255 | txt, parent=frm, font=font, scale=0.06, fg=(.9, .9, .9, 1), | |
256 | align=TextNode.A_left) | |
257 | u_l = self._txt.textNode.get_upper_left_3d() | |
258 | l_r = self._txt.textNode.get_lower_right_3d() | |
259 | w, h = l_r[0] - u_l[0], u_l[2] - l_r[2] | |
a0acba9a FC |
260 | btn_scale = .05 |
261 | mar = .06 # margin | |
2aaa10d3 | 262 | z = h / 2 - font.get_line_height() * self._txt['scale'][1] |
a0acba9a | 263 | z += (btn_scale + 2 * mar) / 2 |
2aaa10d3 FC |
264 | self._txt['pos'] = -w / 2, z |
265 | u_l = self._txt.textNode.get_upper_left_3d() | |
266 | l_r = self._txt.textNode.get_lower_right_3d() | |
a0acba9a FC |
267 | c_l_r = l_r[0], l_r[1], l_r[2] - 2 * mar - btn_scale |
268 | fsz = u_l[0] - mar, l_r[0] + mar, c_l_r[2] - mar, u_l[2] + mar | |
2aaa10d3 | 269 | frm['frameSize'] = fsz |
a0acba9a FC |
270 | colors = [ |
271 | (.6, .6, .6, 1), # ready | |
272 | (1, 1, 1, 1), # press | |
273 | (.8, .8, .8, 1), # rollover | |
274 | (.4, .4, .4, .4)] | |
275 | imgs = [self.__load_img_btn('exitRight', col) for col in colors] | |
276 | btn = DirectButton( | |
277 | image=imgs, scale=btn_scale, | |
278 | pos=(l_r[0] - btn_scale, 1, l_r[2] - mar - btn_scale), | |
279 | parent=frm, command=self.__on_close_instructions, extraArgs=[frm], | |
280 | relief=FLAT, frameColor=(.6, .6, .6, .08), | |
281 | rolloverSound=loader.load_sfx('assets/audio/sfx/rollover.ogg'), | |
282 | clickSound=loader.load_sfx('assets/audio/sfx/click.ogg')) | |
283 | btn.set_transparency(True) | |
284 | ||
285 | def __store_state(self): | |
286 | btns = [ | |
287 | self.__home_btn, self.__info_btn, self.__right_btn, | |
288 | self.__next_btn, self.__prev_btn, self.__rewind_btn] | |
289 | self.__btn_state = [btn['state'] for btn in btns] | |
290 | for btn in btns: | |
291 | btn['state'] = DISABLED | |
292 | [itm.store_state() for itm in self.items] | |
293 | ||
294 | def __restore_state(self): | |
295 | btns = [ | |
296 | self.__home_btn, self.__info_btn, self.__right_btn, | |
297 | self.__next_btn, self.__prev_btn, self.__rewind_btn] | |
298 | for btn, state in zip(btns, self.__btn_state): | |
299 | btn['state'] = state | |
300 | [itm.restore_state() for itm in self.items] | |
301 | self._paused = False | |
302 | ||
303 | def __on_close_instructions(self, frm): | |
304 | frm.remove_node() | |
305 | self.__restore_state() |