f67c45b75587804f3ca316eb6455df4e94c548eb
1 from panda3d
.core
import AmbientLight
, DirectionalLight
, Point3
2 from panda3d
.bullet
import BulletPlaneShape
, BulletGhostNode
3 from direct
.gui
.OnscreenImage
import OnscreenImage
4 from direct
.gui
.DirectGui
import DirectButton
5 from direct
.gui
.DirectGuiGlobals
import FLAT
, DISABLED
, NORMAL
6 from direct
.showbase
.DirectObject
import DirectObject
7 from pmachines
.items
.background
import Background
8 from pmachines
.items
.box
import Box
9 from lib
.engine
.gui
.cursor
import MouseCursor
12 class Scene(DirectObject
):
14 def __init__(self
, world
):
18 self
._cursor
= MouseCursor(
19 'assets/buttons/arrowUpLeft.png', (.04, 1, .04), (.5, .5, .5, 1),
24 self
._set
_mouse
_plane
()
26 self
.items
= [Box(world
)]
27 taskMgr
.add(self
.on_frame
, 'on_frame')
29 def _set_camera(self
):
30 base
.camera
.set_pos(0, -20, 0)
31 base
.camera
.look_at(0, 0, 0)
34 def load_img_btn(path
, col
):
35 img
= OnscreenImage('assets/buttons/%s.png' % path
)
36 img
.set_transparency(True)
40 def load_images_btn(path
, col
):
43 (.6, .6, .6, 1), # ready
45 (.8, .8, .8, 1), # rollover
51 (.4, .1, .1, .4)]}[col
]
52 return [load_img_btn(path
, col
) for col
in colors
]
53 abl
, abr
= base
.a2dBottomLeft
, base
.a2dBottomRight
55 ('home', self
.on_home
, DISABLED
, abl
, 'gray'),
56 ('information', self
.on_information
, DISABLED
, abl
, 'gray'),
57 ('right', self
.on_play
, NORMAL
, abr
, 'green'),
58 ('next', self
.on_next
, DISABLED
, abr
, 'gray'),
59 ('previous', self
.on_prev
, DISABLED
, abr
, 'gray'),
60 ('rewind', self
.on_rewind
, DISABLED
, abr
, 'gray')]
62 for binfo
in btn_info
:
63 imgs
= load_images_btn(binfo
[0], binfo
[4])
64 if binfo
[3] == base
.a2dBottomLeft
:
70 fcols
= (.4, .4, .4, .14), (.3, .3, .3, .05)
72 image
=imgs
, scale
=.05, pos
=(sign
* (.06 + .11 * num
), 1, .06),
73 parent
=binfo
[3], command
=binfo
[1], state
=binfo
[2], relief
=FLAT
,
74 frameColor
=fcols
[0] if binfo
[2] == NORMAL
else fcols
[1],
75 rolloverSound
=loader
.load_sfx('assets/audio/sfx/rollover.ogg'),
76 clickSound
=loader
.load_sfx('assets/audio/sfx/click.ogg'))
77 btn
.set_transparency(True)
79 def _set_directional_light(self
, name
, hpr
, color
):
80 light
= DirectionalLight(name
)
81 light_np
= render
.attach_new_node(light
)
82 light_np
.set_hpr(*hpr
)
83 light
.set_color(color
)
84 render
.set_light(light_np
)
86 def _set_lights(self
):
87 alight
= AmbientLight('alight') # for ao
88 alight
.set_color((.4, .4, .4, 1))
89 alnp
= render
.attach_new_node(alight
)
90 render
.set_light(alnp
)
91 self
._set
_directional
_light
('key light', (315, -60, 0),
93 self
._set
_directional
_light
('fill light', (195, -30, 0),
95 self
._set
_directional
_light
('rim light', (75, -30, 0), (.3, .3, .3, 1))
98 self
.accept('mouse1', self
.on_click_l
)
99 self
.accept('mouse1-up', self
.on_release
)
100 self
.accept('mouse3', self
.on_click_r
)
101 self
.accept('mouse3-up', self
.on_release
)
103 def _set_mouse_plane(self
):
104 shape
= BulletPlaneShape((0, -1, 0), 1)
105 #self._mouse_plane_node = BulletRigidBodyNode('mouse plane')
106 self
._mouse
_plane
_node
= BulletGhostNode('mouse plane')
107 self
._mouse
_plane
_node
.addShape(shape
)
108 #np = render.attachNewNode(self._mouse_plane_node)
109 #self._world.attachRigidBody(self._mouse_plane_node)
110 self
._world
.attachGhost(self
._mouse
_plane
_node
)
113 if not base
.mouseWatcherNode
.has_mouse(): return []
114 p_from
= Point3() # in camera coordinates
115 p_to
= Point3() # in camera coordinates
116 base
.camLens
.extrude(base
.mouseWatcherNode
.get_mouse(), p_from
, p_to
)
117 p_from
= render
.get_relative_point(base
.cam
, p_from
) # global coords
118 p_to
= render
.get_relative_point(base
.cam
, p_to
) # global coords
119 return self
._world
.ray_test_all(p_from
, p_to
).get_hits()
121 def _on_click(self
, method
):
122 for hit
in self
._get
_hits
():
123 if hit
.get_node() == self
._mouse
_plane
_node
:
124 pos
= hit
.get_hit_pos()
125 for hit
in self
._get
_hits
():
126 for item
in [i
for i
in self
.items
if hit
.get_node() == i
.node
]:
127 getattr(item
, method
)(pos
)
128 img
= 'move' if method
== 'on_click_l' else 'rotate'
129 self
._cursor
.set_image('assets/buttons/%s.png' % img
)
131 def on_click_l(self
):
132 self
._on
_click
('on_click_l')
134 def on_click_r(self
):
135 self
._on
_click
('on_click_r')
137 def on_release(self
):
138 [item
.on_release() for item
in self
.items
]
139 self
._cursor
.set_image('assets/buttons/arrowUpLeft.png')
141 def on_frame(self
, task
):
142 hits
= self
._get
_hits
()
144 for hit
in self
._get
_hits
():
145 if hit
.get_node() == self
._mouse
_plane
_node
:
146 pos
= hit
.get_hit_pos()
147 hit_nodes
= [hit
.get_node() for hit
in hits
]
148 items_hit
= [itm
for itm
in self
.items
if itm
.node
in hit_nodes
]
149 items_no_hit
= [itm
for itm
in self
.items
if itm
not in items_hit
]
150 [itm
.on_mouse_on() for itm
in items_hit
]
151 [itm
.on_mouse_off() for itm
in items_no_hit
]
153 [itm
.on_mouse_move(pos
) for itm
in self
.items
]
157 [itm
.play() for itm
in self
.items
]
171 def on_information(self
):
172 print('on_information')