ya2 · news · projects · code · about

changed the colors of the buttons
[pmachines.git] / pmachines / scene.py
CommitLineData
1be87278 1from panda3d.core import AmbientLight, DirectionalLight, Point3
36099535
FC
2from panda3d.bullet import BulletPlaneShape, BulletGhostNode
3from direct.gui.OnscreenImage import OnscreenImage
4from direct.gui.DirectGui import DirectButton
5from direct.gui.DirectGuiGlobals import FLAT, DISABLED, NORMAL
1be87278
FC
6from direct.showbase.DirectObject import DirectObject
7from pmachines.items.background import Background
8from pmachines.items.box import Box
9
10
11class Scene(DirectObject):
12
13 def __init__(self, world):
14 super().__init__()
15 self._world = world
16 self._set_camera()
36099535 17 self._set_gui()
1be87278
FC
18 self._set_lights()
19 self._set_input()
c8d8653f 20 self._set_mouse_plane()
1be87278
FC
21 Background()
22 self.items = [Box(world)]
37ba71d8 23 taskMgr.add(self.on_frame, 'on_frame')
1be87278
FC
24
25 def _set_camera(self):
26 base.camera.set_pos(0, -20, 0)
27 base.camera.look_at(0, 0, 0)
28
36099535
FC
29 def _set_gui(self):
30 def load_img_btn(path, col):
31 img = OnscreenImage('assets/buttons/%s.png' % path)
32 img.set_transparency(True)
33 img.set_color(col)
34 img.detach_node()
35 return img
01b221a6
FC
36 def load_images_btn(path, col):
37 colors = {
38 'gray': [
39 (.6, .6, .6, 1), # ready
40 (1, 1, 1, 1), # press
41 (.8, .8, .8, 1), # rollover
42 (.4, .4, .4, .4)],
43 'green': [
44 (.1, .68, .1, 1),
45 (.1, 1, .1, 1),
46 (.1, .84, .1, 1),
47 (.4, .1, .1, .4)]}[col]
36099535
FC
48 return [load_img_btn(path, col) for col in colors]
49 abl, abr = base.a2dBottomLeft, base.a2dBottomRight
50 btn_info = [
01b221a6
FC
51 ('home', self.on_home, DISABLED, abl, 'gray'),
52 ('information', self.on_information, DISABLED, abl, 'gray'),
53 ('right', self.on_play, NORMAL, abr, 'green'),
54 ('next', self.on_next, DISABLED, abr, 'gray'),
55 ('previous', self.on_prev, DISABLED, abr, 'gray'),
56 ('rewind', self.on_rewind, DISABLED, abr, 'gray')]
36099535
FC
57 num_l = num_r = 0
58 for binfo in btn_info:
01b221a6 59 imgs = load_images_btn(binfo[0], binfo[4])
36099535
FC
60 if binfo[3] == base.a2dBottomLeft:
61 sign, num = 1, num_l
62 num_l += 1
63 else:
64 sign, num = -1, num_r
65 num_r += 1
66 fcols = (.4, .4, .4, .14), (.3, .3, .3, .05)
67 btn = DirectButton(
68 image=imgs, scale=.05, pos=(sign * (.06 + .11 * num), 1, .06),
69 parent=binfo[3], command=binfo[1], state=binfo[2], relief=FLAT,
70 frameColor=fcols[0] if binfo[2] == NORMAL else fcols[1])
71 btn.set_transparency(True)
72
1be87278
FC
73 def _set_directional_light(self, name, hpr, color):
74 light = DirectionalLight(name)
75 light_np = render.attach_new_node(light)
76 light_np.set_hpr(*hpr)
77 light.set_color(color)
78 render.set_light(light_np)
79
80 def _set_lights(self):
81 alight = AmbientLight('alight') # for ao
82 alight.set_color((.4, .4, .4, 1))
83 alnp = render.attach_new_node(alight)
84 render.set_light(alnp)
85 self._set_directional_light('key light', (315, -60, 0),
86 (3.6, 3.6, 3.6, 1))
87 self._set_directional_light('fill light', (195, -30, 0),
88 (.4, .4, .4, 1))
89 self._set_directional_light('rim light', (75, -30, 0), (.3, .3, .3, 1))
90
91 def _set_input(self):
49c79300 92 self.accept('mouse1', self.on_click_l)
c8d8653f 93 self.accept('mouse1-up', self.on_release)
49c79300
FC
94 self.accept('mouse3', self.on_click_r)
95 self.accept('mouse3-up', self.on_release)
c8d8653f
FC
96
97 def _set_mouse_plane(self):
98 shape = BulletPlaneShape((0, -1, 0), 1)
99 #self._mouse_plane_node = BulletRigidBodyNode('mouse plane')
100 self._mouse_plane_node = BulletGhostNode('mouse plane')
101 self._mouse_plane_node.addShape(shape)
102 #np = render.attachNewNode(self._mouse_plane_node)
103 #self._world.attachRigidBody(self._mouse_plane_node)
104 self._world.attachGhost(self._mouse_plane_node)
1be87278 105
37ba71d8
FC
106 def _get_hits(self):
107 if not base.mouseWatcherNode.has_mouse(): return []
1be87278
FC
108 p_from = Point3() # in camera coordinates
109 p_to = Point3() # in camera coordinates
110 base.camLens.extrude(base.mouseWatcherNode.get_mouse(), p_from, p_to)
111 p_from = render.get_relative_point(base.cam, p_from) # global coords
112 p_to = render.get_relative_point(base.cam, p_to) # global coords
37ba71d8
FC
113 return self._world.ray_test_all(p_from, p_to).get_hits()
114
49c79300 115 def _on_click(self, method):
c8d8653f
FC
116 for hit in self._get_hits():
117 if hit.get_node() == self._mouse_plane_node:
118 pos = hit.get_hit_pos()
37ba71d8 119 for hit in self._get_hits():
1be87278 120 for item in [i for i in self.items if hit.get_node() == i.node]:
49c79300
FC
121 getattr(item, method)(pos)
122
123 def on_click_l(self):
124 self._on_click('on_click_l')
125
126 def on_click_r(self):
127 self._on_click('on_click_r')
c8d8653f
FC
128
129 def on_release(self):
49c79300 130 [item.on_release() for item in self.items]
37ba71d8
FC
131
132 def on_frame(self, task):
c8d8653f
FC
133 hits = self._get_hits()
134 pos = None
135 for hit in self._get_hits():
136 if hit.get_node() == self._mouse_plane_node:
137 pos = hit.get_hit_pos()
138 hit_nodes = [hit.get_node() for hit in hits]
37ba71d8
FC
139 items_hit = [itm for itm in self.items if itm.node in hit_nodes]
140 items_no_hit = [itm for itm in self.items if itm not in items_hit]
141 [itm.on_mouse_on() for itm in items_hit]
142 [itm.on_mouse_off() for itm in items_no_hit]
c8d8653f
FC
143 if pos:
144 [itm.on_mouse_move(pos) for itm in self.items]
37ba71d8 145 return task.cont
c8d8653f
FC
146
147 def on_play(self):
148 [itm.play() for itm in self.items]
36099535
FC
149
150 def on_next(self):
151 print('on_next')
152
153 def on_prev(self):
154 print('on_prev')
155
156 def on_rewind(self):
157 print('on_rewind')
158
159 def on_home(self):
160 print('on_home')
161
162 def on_information(self):
163 print('on_information')