f5f04f07e3504498b4f8a65e49e7510dc9792f95
1 from collections
import namedtuple
3 from logging
import info
4 from importlib
import import_module
5 from os
.path
import basename
6 from inspect
import isclass
7 from panda3d
.core
import Texture
, TextNode
, LPoint3f
8 from direct
.gui
.OnscreenImage
import OnscreenImage
9 from direct
.gui
.DirectGui
import DirectButton
, DirectFrame
, DirectEntry
, DirectOptionMenu
, OkDialog
10 from direct
.gui
.DirectGuiGlobals
import FLAT
, NORMAL
11 from direct
.gui
.OnscreenText
import OnscreenText
12 from direct
.showbase
.DirectObject
import DirectObject
13 from pmachines
.items
.item
import ItemStrategy
, FixedStrategy
, StillStrategy
14 from pmachines
.items
.box
import HitStrategy
15 from pmachines
.items
.domino
import DownStrategy
, UpStrategy
16 from pmachines
.editor
.augmented_frame
import AugmentedDirectFrame
17 from ya2
.utils
.gui
.base_page
import DirectOptionMenuTestable
18 from ya2
.utils
.gfx
import DirectGuiMixin
21 class Inspector(DirectObject
):
23 def __init__(self
, item
, all_items
, pos_mgr
):
26 self
.__all
_items
= all_items
27 self
.__pos
_mgr
= pos_mgr
28 self
._font
= base
.loader
.load_font(
29 'assets/fonts/Hanken-Book.ttf')
31 self
._font
.set_pixels_per_unit(60)
32 self
._font
.set_minfilter(Texture
.FTLinearMipmapLinear
)
33 self
._font
.set_outline((0, 0, 0, 1), .8, .2)
36 'text_font': self
._font
,
37 'text_fg': (.9, .9, .9, 1),
39 'frameColor': (.4, .4, .4, .14),
40 'rolloverSound': loader
.load_sfx(
41 'assets/audio/sfx/rollover.ogg'),
42 'clickSound': loader
.load_sfx(
43 'assets/audio/sfx/click.ogg')}
44 tooltip_args
= self
._common
['text_font'], self
._common
['scale'], self
._common
['text_fg']
46 self
._frm
= AugmentedDirectFrame(frameColor
=(.4, .4, .4, .06),
47 frameSize
=(0, w
, -h
, 0),
48 parent
=base
.a2dTopRight
,
50 delta_drag
=LPoint3f(-w
, 0, -h
),
51 collapse_pos
=(w
- .06, 1, -h
+ .06),
53 frame_name
='inspector')
55 p
= self
.__item
._np
.get_pos()
56 r
= self
.__item
._np
.get_r()
57 s
= self
.__item
._np
.get_scale()[0]
59 restitution
= self
.__item
._restitution
60 f
= self
.__item
._friction
62 if 'id' in self
.__item
.json
:
63 _id
= self
.__item
.json
['id']
65 if 'strategy' in self
.__item
.json
:
66 _strategy
= self
.__item
.json
['strategy']
68 if 'strategy_args' in self
.__item
.json
:
69 _strategy_args
= ' '.join(map(str, self
.__item
.json
['strategy_args']))
70 t
, pos_entry
= self
.__add
_row
('position', _('position'), f
'{round(p.x, 3)} {round(p.z, 3)}', self
.on_edit_position
, _('position (e.g. 0.1 2.3 4.5)'))
71 t
, rot_entry
= self
.__add
_row
('roll', _('roll'), f
'{round(r, 3)}', self
.on_edit_roll
, _('roll (e.g. 90)'))
72 t
, scale_entry
= self
.__add
_row
('scale', _('scale'), f
'{round(s, 3)}', self
.on_edit_scale
, _('scale (e.g. 1.2)'))
73 t
, mass_entry
= self
.__add
_row
('mass', _('mass'), f
'{round(m, 3)}', self
.on_edit_mass
, _('mass (default 1; 0 if fixed)'))
74 t
, restitution_entry
= self
.__add
_row
('restitution', _('restitution'), f
'{round(restitution, 3)}', self
.on_edit_restitution
, _('restitution (default 0.5)'))
75 t
, friction_entry
= self
.__add
_row
('friction', _('friction'), f
'{round(f, 3)}', self
.on_edit_friction
, _('friction (default 0.5)'))
76 t
, id_entry
= self
.__add
_row
('id', _('id'), _id
, self
.on_edit_id
, _('id of the item (for the strategies)'))
77 item_modules
= glob('pmachines/items/*.py')
78 item_modules
= [basename(i
)[:-3] for i
in item_modules
]
80 for item_module
in item_modules
:
81 mod_name
= 'pmachines.items.' + item_module
82 for member
in import_module(mod_name
).__dict
__.values():
83 if isclass(member
) and issubclass(member
, ItemStrategy
) and \
84 member
!= ItemStrategy
:
85 strategy_items
= list(set(strategy_items
+ [member
.__name
__]))
86 t
, strategy_entry
= self
.__add
_row
_option
(_('strategy'), _strategy
, strategy_items
, self
.on_edit_strategy
, _('the strategy of the item'))
88 def strategy_set(comps
):
89 strategy_labels
= [f
'inspector_strategy_{i.lower()}' for i
in strategy_items
]
90 for i
in strategy_labels
:
91 if i
in self
.__pos
_mgr
:
93 for l
, b
in zip(strategy_labels
, comps
):
94 b
.__class
__ = type('DirectFrameMixed', (DirectFrame
, DirectGuiMixin
), {})
96 self
.__pos
_mgr
[l
] = (p
[0] + 5, p
[1])
97 strategy_entry
._show
_cb
= strategy_set
98 p
= strategy_entry
.pos_pixel()
99 self
.__pos
_mgr
['editor_inspector_strategy'] = (p
[0] + 5, p
[1])
101 t
, strategy_args_entry
= self
.__add
_row
('strategy_args', _('strategy_args'), _strategy_args
, self
.on_edit_strategy_args
, _('the arguments of the strategy'))
102 fields
= ['position', 'roll', 'scale', 'mass', 'restitution', 'friction', 'id', 'strategy', 'strategy_args']
103 Entries
= namedtuple('Entries', fields
)
104 self
.__entries
= Entries(pos_entry
, rot_entry
, scale_entry
, mass_entry
, restitution_entry
, friction_entry
, id_entry
, strategy_entry
, strategy_args_entry
)
105 def load_images_btn(path
, col
):
108 (.6, .6, .6, 1), # ready
109 (1, 1, 1, 1), # press
110 (.8, .8, .8, 1), # rollover
116 (.4, .1, .1, .4)]}[col
]
117 return [self
.__load
_img
_btn
(path
, col
) for col
in colors
]
118 fcols
= (.4, .4, .4, .14), (.3, .3, .3, .05)
120 image
=load_images_btn('exitRight', 'gray'), scale
=.05,
121 pos
=(.06, 1, -h
+ .06),
122 parent
=self
._frm
, command
=self
.destroy
, state
=NORMAL
, relief
=FLAT
,
124 rolloverSound
=loader
.load_sfx('assets/audio/sfx/rollover.ogg'),
125 clickSound
=loader
.load_sfx('assets/audio/sfx/click.ogg'))
126 b
.__class
__ = type('DirectButtonMixed', (DirectButton
, DirectGuiMixin
), {})
127 pos_mgr
['editor_inspector_close'] = b
.pos_pixel()
128 b
.set_tooltip(_('Close'), *tooltip_args
)
129 self
.accept('item-rototranslated', self
.__on
_item
_rototranslated
)
131 image
=load_images_btn('trashcan', 'gray'), scale
=.05,
132 pos
=(.18, 1, -h
+ .06),
133 parent
=self
._frm
, command
=self
.__delete
_item
, state
=NORMAL
, relief
=FLAT
,
135 rolloverSound
=loader
.load_sfx('assets/audio/sfx/rollover.ogg'),
136 clickSound
=loader
.load_sfx('assets/audio/sfx/click.ogg'))
137 b
.__class
__ = type('DirectButtonMixed', (DirectButton
, DirectGuiMixin
), {})
138 pos_mgr
['editor_inspector_delete'] = b
.pos_pixel()
139 b
.set_tooltip(_('Delete the item'), *tooltip_args
)
141 def __add_row(self
, id_
, label
, text
, callback
, tooltip
):
143 tooltip_args
= self
._common
['text_font'], self
._common
['scale'], self
._common
['text_fg']
146 pos
=(.03, self
.__z
), parent
=self
._frm
,
147 font
=self
._common
['text_font'],
148 scale
=self
._common
['scale'],
149 fg
=self
._common
['text_fg'],
150 wordwrap
=20, align
=TextNode
.ALeft
)
152 scale
=self
._common
['scale'],
153 pos
=(.30, 1, self
.__z
),
154 entryFont
=self
._font
,
157 frameColor
=self
._common
['frameColor'],
160 text_fg
=self
._common
['text_fg'],
162 e
.__class
__ = type('DirectEntryMixed', (DirectEntry
, DirectGuiMixin
), {})
163 e
.set_tooltip(tooltip
, *tooltip_args
)
164 self
.__pos
_mgr
[f
'editor_inspector_{id_}'] = e
.pos_pixel()
168 def __add_row_option(self
, label
, text
, items
, callback
, tooltip
):
169 tooltip_args
= self
._common
['text_font'], self
._common
['scale'], self
._common
['text_fg']
172 pos
=(.03, self
.__z
), parent
=self
._frm
,
173 font
=self
._common
['text_font'],
174 scale
=self
._common
['scale'],
175 fg
=self
._common
['text_fg'],
176 wordwrap
=20, align
=TextNode
.ALeft
)
177 e
= DirectOptionMenuTestable(
178 scale
=self
._common
['scale'],
180 pos
=(.30, 1, self
.__z
),
187 frameColor
=self
._common
['frameColor'],
188 item_frameColor
=self
._common
['frameColor'],
189 popupMenu_frameColor
=self
._common
['frameColor'],
190 popupMarker_frameColor
=self
._common
['frameColor'],
191 text_font
=self
._font
,
192 text_fg
=self
._common
['text_fg'],
193 highlightColor
=(.9, .9, .9, .9),
194 item_text_font
=self
._font
,
195 item_text_fg
=self
._common
['text_fg'],
196 rolloverSound
=loader
.load_sfx('assets/audio/sfx/rollover.ogg'),
197 clickSound
=loader
.load_sfx('assets/audio/sfx/click.ogg'))
198 e
.__class
__ = type('DirectOptionMenuMixed', (DirectOptionMenu
, DirectGuiMixin
), {})
199 e
.set_tooltip(tooltip
, *tooltip_args
)
203 def __load_img_btn(self
, path
, col
):
204 img
= OnscreenImage('assets/images/buttons/%s.dds' % path
)
205 img
.set_transparency(True)
210 def __on_item_rototranslated(self
, np
):
213 self
.__entries
.position
.set('%s %s' % (str(round(pos
.x
, 3)), str(round(pos
.z
, 3))))
214 self
.__entries
.roll
.set('%s' % str(round(r
, 3)))
215 self
.__item
.json
['position'] = list(pos
)
216 self
.__item
.json
['roll'] = round(r
, 3)
218 def __delete_item(self
):
219 messenger
.send('editor-inspector-delete', [self
.__item
])
226 def on_edit_position(self
, txt
):
227 x
, z
= map(float, txt
.split())
228 self
.__item
.position
= [x
, 0, z
]
230 def on_edit_roll(self
, txt
):
231 self
.__item
.roll
= float(txt
)
233 def on_edit_scale(self
, txt
):
234 self
.__item
.scale
= float(txt
)
236 def on_edit_mass(self
, txt
):
237 self
.__item
.mass
= float(txt
)
239 def on_edit_restitution(self
, txt
):
240 self
.__item
.restitution
= float(txt
)
242 def on_edit_friction(self
, txt
):
243 self
.__item
.friction
= float(txt
)
245 def on_edit_id(self
, txt
):
248 def on_edit_strategy(self
, txt
):
250 self
.__entries
.strategy_args
.set('')
253 'StillStrategy': StillStrategy
,
254 'UpStrategy': UpStrategy
,
255 'HitStrategy': HitStrategy
,
256 'DownStrategy': DownStrategy
,
257 'FixedStrategy': FixedStrategy
}
258 class_
= name2class
[txt
]
261 if txt
== 'StillStrategy':
262 args
+= [self
.__item
._np
]
263 if txt
in ['UpStrategy', 'DownStrategy']:
264 args
+= [self
.__item
._np
]
266 args
+= [float(self
.__entries
.strategy_args
.get())]
269 if txt
== 'HitStrategy':
270 for item
in self
.__all
_items
:
271 if item
.id == self
.__entries
.strategy_args
.get():
273 args
+= [self
.__item
.node
]
274 args
+= [self
.__item
._world
]
276 self
.__item
.strategy
= class_(*args
)
277 self
.__item
.strategy_json
= txt
279 self
.__show
_error
_popup
()
281 def on_edit_strategy_args(self
, txt
):
282 self
.__item
.strategy_args_json
= txt
284 def __show_error_popup(self
):
285 self
.__dialog
= OkDialog(dialogName
='Strategy args errors',
286 text
=_('There are errors in the strategy args.'),
287 command
=self
.__actually
_close
)
288 self
.__dialog
['frameColor'] = (.4, .4, .4, .14)
289 self
.__dialog
['relief'] = FLAT
290 self
.__dialog
.component('text0')['fg'] = (.9, .9, .9, 1)
291 self
.__dialog
.component('text0')['font'] = self
._font
292 for b
in self
.__dialog
.buttonList
:
293 b
['frameColor'] = (.4, .4, .4, .14)
294 b
.component('text0')['fg'] = (.9, .9, .9, 1)
295 b
.component('text0')['font'] = self
._font
296 b
.component('text1')['fg'] = (.9, .1, .1, 1)
297 b
.component('text1')['font'] = self
._font
298 b
.component('text2')['fg'] = (.9, .9, .1, 1)
299 b
.component('text2')['font'] = self
._font
301 def __actually_close(self
, arg
):
302 self
.__entries
.strategy
.set('')
303 self
.__entries
.strategy_args
.set('')
304 self
.__dialog
.cleanup()
308 self
.ignore('item-rototranslated')
309 messenger
.send('editor-inspector-destroy')
312 class PixelSpaceInspector(DirectObject
):
314 def __init__(self
, item
, all_items
):
315 info('PixelSpaceInspector')
318 self
.__all
_items
= all_items
319 self
._font
= base
.loader
.load_font(
320 'assets/fonts/Hanken-Book.ttf')
322 self
._font
.set_pixels_per_unit(60)
323 self
._font
.set_minfilter(Texture
.FTLinearMipmapLinear
)
324 self
._font
.set_outline((0, 0, 0, 1), .8, .2)
327 'text_font': self
._font
,
328 'text_fg': (.9, .9, .9, 1),
330 'frameColor': (.4, .4, .4, .14),
331 'rolloverSound': loader
.load_sfx(
332 'assets/audio/sfx/rollover.ogg'),
333 'clickSound': loader
.load_sfx(
334 'assets/audio/sfx/click.ogg')}
336 self
._frm
= DirectFrame(frameColor
=(.4, .4, .4, .06),
337 frameSize
=(0, w
, -h
, 0),
338 parent
=base
.a2dTopRight
,
341 p
= self
.__item
._np
.get_pos()
343 if 'id' in self
.__item
.json
:
344 _id
= self
.__item
.json
['id']
345 t
, pos_entry
= self
.__add
_row
(_('position'), f
'{round(p.x, 3)}, {round(p.z, 3)}', self
.on_edit_position
)
346 t
, id_entry
= self
.__add
_row
(_('id'), _id
, self
.on_edit_id
)
347 fields
= ['position', 'id']
348 Entries
= namedtuple('Entries', fields
)
349 self
.__entries
= Entries(pos_entry
, id_entry
)
350 def load_images_btn(path
, col
):
353 (.6, .6, .6, 1), # ready
354 (1, 1, 1, 1), # press
355 (.8, .8, .8, 1), # rollover
361 (.4, .1, .1, .4)]}[col
]
362 return [self
.__load
_img
_btn
(path
, col
) for col
in colors
]
363 fcols
= (.4, .4, .4, .14), (.3, .3, .3, .05)
365 image
=load_images_btn('exitRight', 'gray'), scale
=.05,
366 pos
=(.06, 1, -h
+ .06),
367 parent
=self
._frm
, command
=self
.destroy
, state
=NORMAL
, relief
=FLAT
,
369 rolloverSound
=loader
.load_sfx('assets/audio/sfx/rollover.ogg'),
370 clickSound
=loader
.load_sfx('assets/audio/sfx/click.ogg'))
371 self
.accept('item-rototranslated', self
.__on
_item
_rototranslated
)
373 image
=load_images_btn('trashcan', 'gray'), scale
=.05,
374 pos
=(.18, 1, -h
+ .06),
375 parent
=self
._frm
, command
=self
.__delete
_item
, state
=NORMAL
, relief
=FLAT
,
377 rolloverSound
=loader
.load_sfx('assets/audio/sfx/rollover.ogg'),
378 clickSound
=loader
.load_sfx('assets/audio/sfx/click.ogg'))
380 def __add_row(self
, label
, text
, callback
):
384 pos
=(.03, self
.__z
), parent
=self
._frm
,
385 font
=self
._common
['text_font'],
386 scale
=self
._common
['scale'],
387 fg
=self
._common
['text_fg'],
388 wordwrap
=20, align
=TextNode
.ALeft
)
390 scale
=self
._common
['scale'],
391 pos
=(.30, 1, self
.__z
),
392 entryFont
=self
._font
,
395 frameColor
=self
._common
['frameColor'],
398 text_fg
=self
._common
['text_fg'],
403 def __load_img_btn(self
, path
, col
):
404 img
= OnscreenImage('assets/images/buttons/%s.dds' % path
)
405 img
.set_transparency(True)
410 def __on_item_rototranslated(self
, np
):
411 pos
= np
.pos2d_pixel()
412 self
.__entries
.position
.set('%s %s' % (str(round(pos
[0], 3)), str(round(pos
[1], 3))))
413 self
.__item
.json
['position'] = list(pos
)
415 def __delete_item(self
):
416 messenger
.send('editor-inspector-delete', [self
.__item
])
423 def on_edit_position(self
, txt
):
424 x
, z
= map(float, txt
.split())
425 self
.__item
.position
= [x
, 0, z
]
427 def on_edit_id(self
, txt
):
430 def __actually_close(self
, arg
):
431 self
.__entries
.strategy
.set('')
432 self
.__entries
.strategy_args
.set('')
433 self
.__dialog
.cleanup()
437 self
.ignore('item-rototranslated')
438 messenger
.send('editor-inspector-destroy')
441 class WorldSpaceInspector(DirectObject
):
443 def __init__(self
, item
, all_items
, pos_mgr
):
444 info('WorldSpaceInspector')
447 self
.__all
_items
= all_items
448 self
.__pos
_mgr
= pos_mgr
449 self
._font
= base
.loader
.load_font(
450 'assets/fonts/Hanken-Book.ttf')
452 self
._font
.set_pixels_per_unit(60)
453 self
._font
.set_minfilter(Texture
.FTLinearMipmapLinear
)
454 self
._font
.set_outline((0, 0, 0, 1), .8, .2)
457 'text_font': self
._font
,
458 'text_fg': (.9, .9, .9, 1),
460 'frameColor': (.4, .4, .4, .14),
461 'rolloverSound': loader
.load_sfx(
462 'assets/audio/sfx/rollover.ogg'),
463 'clickSound': loader
.load_sfx(
464 'assets/audio/sfx/click.ogg')}
465 tooltip_args
= self
._common
['text_font'], self
._common
['scale'], self
._common
['text_fg']
467 self
._frm
= DirectFrame(frameColor
=(.4, .4, .4, .06),
468 frameSize
=(0, w
, -h
, 0),
469 parent
=base
.a2dTopRight
,
472 p
= self
.__item
._np
.get_pos()
474 if 'id' in self
.__item
.json
:
475 _id
= self
.__item
.json
['id']
476 t
, pos_entry
= self
.__add
_row
('position', _('position'), f
'{round(p.x, 3)} {round(p.z, 3)}', self
.on_edit_position
, _('position (e.g. 0.1 2.3 4.5)'))
477 t
, id_entry
= self
.__add
_row
('id', _('id'), _id
, self
.on_edit_id
, _('id of the item (for the strategies)'))
478 fields
= ['position', 'id']
479 Entries
= namedtuple('Entries', fields
)
480 self
.__entries
= Entries(pos_entry
, id_entry
)
481 def load_images_btn(path
, col
):
484 (.6, .6, .6, 1), # ready
485 (1, 1, 1, 1), # press
486 (.8, .8, .8, 1), # rollover
492 (.4, .1, .1, .4)]}[col
]
493 return [self
.__load
_img
_btn
(path
, col
) for col
in colors
]
494 fcols
= (.4, .4, .4, .14), (.3, .3, .3, .05)
496 image
=load_images_btn('exitRight', 'gray'), scale
=.05,
497 pos
=(.06, 1, -h
+ .06),
498 parent
=self
._frm
, command
=self
.destroy
, state
=NORMAL
, relief
=FLAT
,
500 rolloverSound
=loader
.load_sfx('assets/audio/sfx/rollover.ogg'),
501 clickSound
=loader
.load_sfx('assets/audio/sfx/click.ogg'))
502 b
.__class
__ = type('DirectButtonMixed', (DirectButton
, DirectGuiMixin
), {})
503 pos_mgr
['editor_inspector_test_close'] = b
.pos_pixel()
504 b
.set_tooltip(_('Close'), *tooltip_args
)
505 self
.accept('item-rototranslated', self
.__on
_item
_rototranslated
)
507 image
=load_images_btn('trashcan', 'gray'), scale
=.05,
508 pos
=(.18, 1, -h
+ .06),
509 parent
=self
._frm
, command
=self
.__delete
_item
, state
=NORMAL
, relief
=FLAT
,
511 rolloverSound
=loader
.load_sfx('assets/audio/sfx/rollover.ogg'),
512 clickSound
=loader
.load_sfx('assets/audio/sfx/click.ogg'))
513 b
.__class
__ = type('DirectButtonMixed', (DirectButton
, DirectGuiMixin
), {})
514 pos_mgr
['editor_inspector_test_delete'] = b
.pos_pixel()
515 b
.set_tooltip(_('Delete the item'), *tooltip_args
)
517 def __add_row(self
, id_
, label
, text
, callback
, tooltip
):
519 tooltip_args
= self
._common
['text_font'], self
._common
['scale'], self
._common
['text_fg']
522 pos
=(.03, self
.__z
), parent
=self
._frm
,
523 font
=self
._common
['text_font'],
524 scale
=self
._common
['scale'],
525 fg
=self
._common
['text_fg'],
526 wordwrap
=20, align
=TextNode
.ALeft
)
528 scale
=self
._common
['scale'],
529 pos
=(.30, 1, self
.__z
),
530 entryFont
=self
._font
,
533 frameColor
=self
._common
['frameColor'],
536 text_fg
=self
._common
['text_fg'],
538 e
.__class
__ = type('DirectEntryMixed', (DirectEntry
, DirectGuiMixin
), {})
539 e
.set_tooltip(tooltip
, *tooltip_args
)
540 self
.__pos
_mgr
[f
'editor_inspector_test_{id_}'] = e
.pos_pixel()
544 def __load_img_btn(self
, path
, col
):
545 img
= OnscreenImage('assets/images/buttons/%s.dds' % path
)
546 img
.set_transparency(True)
551 def __on_item_rototranslated(self
, np
):
553 self
.__entries
.position
.set('%s %s' % (str(round(pos
.x
, 3)), str(round(pos
.z
, 3))))
554 self
.__item
.json
['position'] = list(pos
)
556 def __delete_item(self
):
557 messenger
.send('editor-inspector-delete', [self
.__item
])
564 def on_edit_position(self
, txt
):
565 x
, z
= map(float, txt
.split())
566 self
.__item
.position
= [x
, 0, z
]
568 def on_edit_id(self
, txt
):
571 def __actually_close(self
, arg
):
572 self
.__entries
.strategy
.set('')
573 self
.__entries
.strategy_args
.set('')
574 self
.__dialog
.cleanup()
578 self
.ignore('item-rototranslated')
579 messenger
.send('editor-inspector-destroy')