ya2 · news · projects · code · about

refactoring of side panel
[pmachines.git] / pmachines / pmachines.py
CommitLineData
8ee66edd 1import argparse
4894bb48
FC
2import simplepbr
3import gltf
8ee66edd
FC
4from sys import platform, argv
5from logging import info
6from os.path import exists
7from os import makedirs
1be87278
FC
8from panda3d.core import Filename, load_prc_file_data, AntialiasAttrib
9from panda3d.bullet import BulletWorld, BulletDebugNode
8ee66edd 10from direct.showbase.ShowBase import ShowBase
1be87278 11from pmachines.scene import Scene
54a1397e 12from pmachines.music import MusicMgr
9ba5488b
FC
13from lib.dictfile import DctFile
14from lib.lib.p3d.p3d import LibP3d
2aaa10d3 15from lib.engine.lang import LangMgr
8ee66edd
FC
16
17
18class Pmachines:
19
20 def __init__(self):
21 self._configure()
22 self.base = ShowBase()
23 info('platform: %s' % platform)
24 info('exists main.py: %s' % exists('main.py'))
8ee66edd 25 args = self._parse_args()
9ba5488b 26 self._prepare_window(args)
8ee66edd
FC
27 self.updating = args.update
28 self.version = args.version
29 if args.update:
30 return
9ba5488b 31 MusicMgr(self._options['settings']['volume'])
a0b33e12 32 self.lang_mgr = LangMgr(self._options['settings']['language'],
2aaa10d3
FC
33 'pmachines',
34 'assets/locale/')
1be87278 35 self._set_physics()
651713a9 36 self._scene = Scene(self.world)
8ee66edd
FC
37
38 def _configure(self):
39 load_prc_file_data('', 'window-title pmachines')
4894bb48
FC
40 load_prc_file_data('', 'framebuffer-multisample 1')
41 load_prc_file_data('', 'multisamples 4')
42 load_prc_file_data('', 'framebuffer-srgb true')
8ee66edd
FC
43
44 def _parse_args(self):
45 parser = argparse.ArgumentParser()
46 parser.add_argument('--update', action='store_true')
47 parser.add_argument('--version', action='store_true')
9ba5488b 48 parser.add_argument('--optfile')
8ee66edd
FC
49 cmd_line = [arg for arg in iter(argv[1:]) if not arg.startswith('-psn_')]
50 args = parser.parse_args(cmd_line)
51 return args
52
9ba5488b 53 def _prepare_window(self, args):
8ee66edd
FC
54 data_path = ''
55 if (platform.startswith('win') or platform.startswith('linux')) and (
56 not exists('main.py') or __file__.startswith('/app/bin/')):
57 # it is the deployed version for windows
58 data_path = str(Filename.get_user_appdata_directory()) + '/pmachines'
59 home = '/home/flavio' # we must force this for wine
60 if data_path.startswith('/c/users/') and exists(home + '/.wine/'):
61 data_path = home + '/.wine/drive_' + data_path[1:]
62 info('creating dirs: %s' % data_path)
63 makedirs(data_path, exist_ok=True)
9ba5488b
FC
64 optfile = args.optfile if args.optfile else 'options.ini'
65 info('data path: %s' % data_path)
66 info('option file: %s' % optfile)
67 info('fixed path: %s' % LibP3d.fixpath(data_path + '/' + optfile))
68 default_opt = {
69 'settings': {
a0b33e12
FC
70 'volume': 1,
71 'language': 'en'},
9ba5488b 72 'development': {
a5dc83f4 73 'simplepbr': 1,
9ba5488b
FC
74 'verbose_log': 0,
75 'physics_debug': 0}}
76 opt_path = LibP3d.fixpath(data_path + '/' + optfile) if data_path else optfile
77 opt_exists = exists(opt_path)
78 self._options = DctFile(
79 LibP3d.fixpath(data_path + '/' + optfile) if data_path else optfile,
80 default_opt)
81 if not opt_exists:
82 self._options.store()
4894bb48 83 gltf.patch_loader(base.loader)
a5dc83f4
FC
84 if self._options['development']['simplepbr']:
85 pipeline = simplepbr.init(
86 use_normal_maps=True,
87 use_emission_maps=False,
88 use_occlusion_maps=True
89 )
4894bb48 90 render.setAntialias(AntialiasAttrib.MAuto)
1be87278
FC
91 self.base.set_background_color(0, 0, 0, 1)
92 self.base.disable_mouse()
651713a9
FC
93 #self.base.accept('window-event', self._on_win_evt)
94 self.base.accept('aspectRatioChanged', self._on_aspect_ratio_changed)
1be87278
FC
95
96 def _set_physics(self):
9ba5488b
FC
97 if self._options['development']['physics_debug']:
98 debug_node = BulletDebugNode('Debug')
99 debug_node.show_wireframe(True)
100 debug_node.show_constraints(True)
101 debug_node.show_bounding_boxes(True)
102 debug_node.show_normals(True)
103 debug_np = render.attach_new_node(debug_node)
104 debug_np.show()
1be87278
FC
105 self.world = BulletWorld()
106 self.world.set_gravity((0, 0, -9.81))
9ba5488b
FC
107 if self._options['development']['physics_debug']:
108 self.world.set_debug_node(debug_np.node())
1be87278
FC
109 def update(task):
110 dt = globalClock.get_dt()
111 self.world.do_physics(dt)
112 return task.cont
113 taskMgr.add(update, 'update')
651713a9
FC
114
115 def _on_aspect_ratio_changed(self):
116 self._scene.on_aspect_ratio_changed()