1 from panda3d
.core
import AmbientLight
, DirectionalLight
, Point3
, Texture
, \
2 TextPropertiesManager
, TextNode
3 from panda3d
.bullet
import BulletPlaneShape
, BulletGhostNode
4 from direct
.gui
.OnscreenImage
import OnscreenImage
5 from direct
.gui
.OnscreenText
import OnscreenText
6 from direct
.gui
.DirectGui
import DirectButton
, DirectFrame
7 from direct
.gui
.DirectGuiGlobals
import FLAT
, DISABLED
, NORMAL
8 from direct
.showbase
.DirectObject
import DirectObject
9 from pmachines
.items
.background
import Background
10 from pmachines
.items
.box
import Box
11 from pmachines
.sidepanel
import SidePanel
12 from lib
.engine
.gui
.cursor
import MouseCursor
13 from lib
.lib
.p3d
.gfx
import P3dGfxMgr
16 class Scene(DirectObject
):
18 def __init__(self
, world
):
22 self
._cursor
= MouseCursor(
23 'assets/buttons/arrowUpLeft.png', (.04, 1, .04), (.5, .5, .5, 1),
28 self
._set
_mouse
_plane
()
29 self
._set
_instructions
()
31 self
.items
= [Box(world
, self
._mouse
_plane
_node
, 3, self
.cb_inst
)]
32 self
._side
_panel
= SidePanel(world
, self
._mouse
_plane
_node
, (-5, 4), (-3, 1), 1)
33 taskMgr
.add(self
.on_frame
, 'on_frame')
35 def _set_camera(self
):
36 base
.camera
.set_pos(0, -20, 0)
37 base
.camera
.look_at(0, 0, 0)
40 def load_img_btn(path
, col
):
41 img
= OnscreenImage('assets/buttons/%s.png' % path
)
42 img
.set_transparency(True)
46 def load_images_btn(path
, col
):
49 (.6, .6, .6, 1), # ready
51 (.8, .8, .8, 1), # rollover
57 (.4, .1, .1, .4)]}[col
]
58 return [load_img_btn(path
, col
) for col
in colors
]
59 abl
, abr
= base
.a2dBottomLeft
, base
.a2dBottomRight
61 ('home', self
.on_home
, DISABLED
, abl
, 'gray'),
62 ('information', self
.on_information
, DISABLED
, abl
, 'gray'),
63 ('right', self
.on_play
, NORMAL
, abr
, 'green'),
64 ('next', self
.on_next
, DISABLED
, abr
, 'gray'),
65 ('previous', self
.on_prev
, DISABLED
, abr
, 'gray'),
66 ('rewind', self
.on_rewind
, DISABLED
, abr
, 'gray')]
68 for binfo
in btn_info
:
69 imgs
= load_images_btn(binfo
[0], binfo
[4])
70 if binfo
[3] == base
.a2dBottomLeft
:
76 fcols
= (.4, .4, .4, .14), (.3, .3, .3, .05)
78 image
=imgs
, scale
=.05, pos
=(sign
* (.06 + .11 * num
), 1, .06),
79 parent
=binfo
[3], command
=binfo
[1], state
=binfo
[2], relief
=FLAT
,
80 frameColor
=fcols
[0] if binfo
[2] == NORMAL
else fcols
[1],
81 rolloverSound
=loader
.load_sfx('assets/audio/sfx/rollover.ogg'),
82 clickSound
=loader
.load_sfx('assets/audio/sfx/click.ogg'))
83 btn
.set_transparency(True)
85 def _set_directional_light(self
, name
, hpr
, color
):
86 light
= DirectionalLight(name
)
87 light_np
= render
.attach_new_node(light
)
88 light_np
.set_hpr(*hpr
)
89 light
.set_color(color
)
90 render
.set_light(light_np
)
92 def _set_lights(self
):
93 alight
= AmbientLight('alight') # for ao
94 alight
.set_color((.4, .4, .4, 1))
95 alnp
= render
.attach_new_node(alight
)
96 render
.set_light(alnp
)
97 self
._set
_directional
_light
('key light', (315, -60, 0),
99 self
._set
_directional
_light
('fill light', (195, -30, 0),
101 self
._set
_directional
_light
('rim light', (75, -30, 0), (.3, .3, .3, 1))
103 def _set_input(self
):
104 self
.accept('mouse1', self
.on_click_l
)
105 self
.accept('mouse1-up', self
.on_release
)
106 self
.accept('mouse3', self
.on_click_r
)
107 self
.accept('mouse3-up', self
.on_release
)
109 def _set_mouse_plane(self
):
110 shape
= BulletPlaneShape((0, -1, 0), 0)
111 #self._mouse_plane_node = BulletRigidBodyNode('mouse plane')
112 self
._mouse
_plane
_node
= BulletGhostNode('mouse plane')
113 self
._mouse
_plane
_node
.addShape(shape
)
114 #np = render.attachNewNode(self._mouse_plane_node)
115 #self._world.attachRigidBody(self._mouse_plane_node)
116 self
._world
.attach_ghost(self
._mouse
_plane
_node
)
119 if not base
.mouseWatcherNode
.has_mouse(): return []
120 p_from
, p_to
= P3dGfxMgr
.world_from_to(base
.mouseWatcherNode
.get_mouse())
121 return self
._world
.ray_test_all(p_from
, p_to
).get_hits()
123 def _on_click(self
, method
):
124 for hit
in self
._get
_hits
():
125 if hit
.get_node() == self
._mouse
_plane
_node
:
126 pos
= hit
.get_hit_pos()
127 for hit
in self
._get
_hits
():
128 for item
in [i
for i
in self
.items
if hit
.get_node() == i
.node
]:
129 getattr(item
, method
)(pos
)
130 img
= 'move' if method
== 'on_click_l' else 'rotate'
131 self
._cursor
.set_image('assets/buttons/%s.png' % img
)
133 def on_click_l(self
):
134 self
._on
_click
('on_click_l')
136 def on_click_r(self
):
137 self
._on
_click
('on_click_r')
139 def on_release(self
):
140 [item
.on_release() for item
in self
.items
]
141 self
._cursor
.set_image('assets/buttons/arrowUpLeft.png')
143 def on_aspect_ratio_changed(self
):
144 [item
.on_aspect_ratio_changed() for item
in self
.items
]
145 self
._side
_panel
.update(self
.items
)
147 def on_frame(self
, task
):
148 hits
= self
._get
_hits
()
150 for hit
in self
._get
_hits
():
151 if hit
.get_node() == self
._mouse
_plane
_node
:
152 pos
= hit
.get_hit_pos()
153 hit_nodes
= [hit
.get_node() for hit
in hits
]
154 items_hit
= [itm
for itm
in self
.items
if itm
.node
in hit_nodes
]
155 items_no_hit
= [itm
for itm
in self
.items
if itm
not in items_hit
]
156 [itm
.on_mouse_on() for itm
in items_hit
]
157 [itm
.on_mouse_off() for itm
in items_no_hit
]
159 [itm
.on_mouse_move(pos
) for itm
in self
.items
]
162 def cb_inst(self
, item
):
166 [itm
.play() for itm
in self
.items
]
180 def on_information(self
):
181 print('on_information')
183 def _set_instructions(self
):
184 mgr
= TextPropertiesManager
.get_global_ptr()
185 for name
in ['mouse_l', 'mouse_r']:
186 graphic
= OnscreenImage('assets/buttons/%s.png' % name
)
187 graphic
.set_scale(.5)
188 graphic
.get_texture().set_minfilter(Texture
.FTLinearMipmapLinear
)
189 graphic
.get_texture().set_anisotropic_degree(2)
190 mgr
.set_graphic(name
, graphic
)
192 graphic
.set_transparency(True)
193 graphic
.detach_node()
194 frm
= DirectFrame(frameColor
=(.4, .4, .4, .06),
195 frameSize
=(-.6, .6, -.3, .3))
196 font
= base
.loader
.load_font('assets/fonts/Hanken-Book.ttf')
198 font
.set_pixels_per_unit(60)
199 font
.set_minfilter(Texture
.FTLinearMipmapLinear
)
200 font
.set_outline((0, 0, 0, 1), .8, .2)
201 txt
= _('keep \5mouse_l\5 pressed to drag an item\n\n'
202 'keep \5mouse_r\5 pressed to rotate an item')
203 self
._txt
= OnscreenText(
204 txt
, parent
=frm
, font
=font
, scale
=0.06, fg
=(.9, .9, .9, 1),
205 align
=TextNode
.A_left
)
206 u_l
= self
._txt
.textNode
.get_upper_left_3d()
207 l_r
= self
._txt
.textNode
.get_lower_right_3d()
208 w
, h
= l_r
[0] - u_l
[0], u_l
[2] - l_r
[2]
209 z
= h
/ 2 - font
.get_line_height() * self
._txt
['scale'][1]
210 self
._txt
['pos'] = -w
/ 2, z
211 u_l
= self
._txt
.textNode
.get_upper_left_3d()
212 l_r
= self
._txt
.textNode
.get_lower_right_3d()
214 fsz
= u_l
[0] - mar
, l_r
[0] + mar
, l_r
[2] - mar
, u_l
[2] + mar
215 frm
['frameSize'] = fsz