-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAstusPandaEngine.py
More file actions
484 lines (409 loc) · 20.3 KB
/
AstusPandaEngine.py
File metadata and controls
484 lines (409 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
Version = "developer preview"
version = Version
from AGeLib import *
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# These imports make the IDE happy
from ..AstusPandaEngine.AGeLib import *
"""
To get the linter for panda3d working:
Explanation: https://stackoverflow.com/questions/72769867/autocompletion-for-panda3d-in-vscode
Source: https://github.com/panda3d/panda3d/issues/1327
Required files: https://github.com/WMOkiishi/types-panda3d/tree/master/src/panda3d-stubs
Tool to download a directory from github: https://download-directory.github.io/
"""
import panda3d as p3d
import panda3d.core as p3dc
from direct.showbase.ShowBase import ShowBase
from direct.showbase.Loader import Loader as p3dLoader
from direct.showbase.MessengerGlobal import messenger
#import direct.directbase.DirectStart #This import does not import anything but instead starts a ShowBase instance
from direct.showbase.DirectObject import DirectObject
#from pandac.PandaModules import WindowProperties
import sys
import os
import typing
import random
import numpy as np
import _APE_WrapperClasses as Classes
P3D_WIN_WIDTH = 400
P3D_WIN_HEIGHT = 240
#region shortcut functions
def engine() -> 'APE':
return App().engine
def base() -> 'APEPandaBase':
return App().base
def globalClock() -> 'p3dc.ClockObject':
return __builtins__["globalClock"]
def render() -> p3dc.NodePath:
return App().base.render
def loader() -> p3dLoader:
return App().base.loader
def window() -> 'APEWindow':
return App().MainWindow
def lightManager() -> Classes._lightManager:
return App().lightManager
def pipelineActive() -> bool:
return App().RenderPipelineActive
def loadModel(*args,**argv) -> p3dc.NodePath:
return loader().loadModel(*args,**argv)
#endregion shortcut functions
#region helper functions
def colour(colour: typing.Union[QtGui.QColor,QtGui.QBrush]) -> typing.Tuple[float,float,float,float]:
if isinstance(colour, QtGui.QBrush): colour = colour.color()
try:
return (colour.redF(), colour.greenF(), colour.blueF(), colour.alphaF())
except:
return colour
#endregion helper functions
#region Engine Classes
class APE():
def __init__(self, base:'APEPandaBase', tobsprRenderPipeline:bool):
self.RenderPipelineActive = tobsprRenderPipeline
self.base = base
App().engine = self
def start(self):
"""
This method is called at the end of `start()`. \n
Reimplement this method with the stuff that starts your game.
"""
pass
def processAndRender(self):
App().processEvents()
base().graphicsEngine.renderFrame()
base().eventMgr.doEvents()
base().graphicsEngine.renderFrame()
base().eventMgr.doEvents()
def fastRender(self):
base().graphicsEngine.renderFrame()
base().eventMgr.doEvents()
class APEApp(AGeApp):
def __init__(self, args = [], useExcepthook = True):
super(APEApp, self).__init__(args,useExcepthook)
self.base:'APEPandaBase' = None #base
self.engine:'APE' = None
self.RenderPipelineActive:bool = False
self.lightManager:'Classes._lightManager' = None
self.installEventFilter(self)
def eventFilter(self, source, event):
try:
# switch control back to qt when the user clicks on the window
if event.type() == 2 and source.window() == self.MainWindow and self.base.mainWinForeground: self.MainWindow.activateWindow()
except: pass
return super(APEApp, self).eventFilter(source, event) # let the normal eventFilter handle the event
def init_1(self, tobsprRenderPipeline:bool):
self.RenderPipelineActive = tobsprRenderPipeline
self.lightManager = Classes._lightManager()
def init_2(self, base:'APEPandaBase', engine:'APE'):
self.base = base
self.engine = engine
class APEWindow(AWWF):
S_PandaKeystroke = pyqtSignal(str)
S_PandaButtonDown = pyqtSignal(str)
S_PandaButtonUp = pyqtSignal(str)
def __init__(self, widget, parent=None):
super(APEWindow, self).__init__(parent, initTopBar=False, FullscreenHidesBars=True)
self.TopBar.init(IncludeFontSpinBox=True,IncludeErrorButton=True)
self.setWindowTitle("Test")
self.cw = QtWidgets.QWidget(self)
self.PandaContainer:'PandaWidget' = widget(self.cw)
self.setupUI()
self.setCentralWidget(self.cw)
def setupUI(self):
"""
This Method sets up the UI. \n
If you want a different layout reimplement this Method. \n
All you need to do is to create a layout and apply it to `self.cw`.\n
`self.cw` is automatically set as the Central Widget. \n
`self.PandaContainer` is the Panda3D Widget and is created automatically.
"""
layout = QtWidgets.QVBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.addWidget(self.PandaContainer)
self.lineedit = AGeWidgets.LineEdit(self, PlaceholderText = "Write something and press return to generate a Notification")
def sendMessage():
NC(3,self.lineedit.text(),DplStr="LineEdit Input")
self.lineedit.clear()
self.lineedit.returnPressed.connect(sendMessage)
layout.addWidget(self.lineedit)
self.cw.setLayout(layout)
class PandaWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(PandaWidget, self).__init__(parent)
self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding))
def resizeEvent(self, evt):
wp = p3dc.WindowProperties()
# This works with Qt5 but it does not work with Qt6 if the DPI of the screen is not 100% (thus creating a too small PandaWidget on 4k screens with 150% scaling)
# I will leave this code here in case the new code does not work correctly with older Qt5 versions
#wp.setSize(self.width()-self.width()%4, self.height()-self.height()%4)
# This works with Qt5 and Qt6 for all DPI scalings
wp.setSize(round(self.width()*self.devicePixelRatioF())-round(self.width()*self.devicePixelRatioF())%4, round(self.height()*self.devicePixelRatioF())-round(self.height()*self.devicePixelRatioF())%4)
wp.setOrigin(0,0)
base().win.requestProperties(wp)
def moveEvent(self, evt):
wp = p3dc.WindowProperties()
# This works with Qt5 but it does not work with Qt6 if the DPI of the screen is not 100% (thus creating a too small PandaWidget on 4k screens with 150% scaling)
# I will leave this code here in case the new code does not work correctly with older Qt5 versions
#wp.setSize(self.width()-self.width()%4, self.height()-self.height()%4)
# This works with Qt5 and Qt6 for all DPI scalings
wp.setSize(round(self.width()*self.devicePixelRatioF())-round(self.width()*self.devicePixelRatioF())%4, round(self.height()*self.devicePixelRatioF())-round(self.height()*self.devicePixelRatioF())%4)
base().win.requestProperties(wp)
def minimumSizeHint(self):
return QtCore.QSize(400,300)
class APEPandaBase(ShowBase):
def __init__(self,rp):
ShowBase.__init__(self)
self.render_pipeline = rp
self._lastForcedForeground = None
def keystrokeSignal(self, keyname):
self.MainWindow.S_PandaKeystroke.emit(keyname)
def buttonDownSignal(self, keyname):
self.MainWindow.S_PandaButtonDown.emit(keyname)
def buttonUpSignal(self, keyname):
self.MainWindow.S_PandaButtonUp.emit(keyname)
def registerWindow(self,window:'APEWindow'):
self.MainWindow:'APEWindow' = window
self.accept("f11", lambda: self.MainWindow.toggleFullscreen())
self.accept("f12", lambda: App().makeScreenshot(self.MainWindow))
#CRITICAL: When holding a key while moving the mouse off the panda3d window the key is never released which is really bad
wp = p3dc.WindowProperties()
wp.setOrigin(0,0)
wp.setSize(P3D_WIN_WIDTH, P3D_WIN_HEIGHT)
wp.set_parent_window(int(self.MainWindow.PandaContainer.winId())) #CRITICAL: Use a "WindowHandle" instead since the int version is deprecated!
#wp.
self.openDefaultWindow(props=wp)
# Note the lack of parentheses here!
self.taskMgr.add(self.focusUpdaterTask)
def step(self):
self.taskMgr.step()
async def focusUpdaterTask(self, task):
if self.MainWindow.isActiveWindow():
#if self.win.get_pointer(0).getInWindow(): # panda3d is not reliable so I use Qt instead
if self.MainWindow.PandaContainer.rect().contains(self.MainWindow.PandaContainer.mapFromGlobal(QtGui.QCursor().pos())):
#if not self.win.getProperties().getForeground(): # This is once again not reliable and I therefore need to keep track of it myself...
if self._lastForcedForeground is not True:
# Ensure that the panda3d widget becomes active when its APE Window is active the mouse is inside the panda3d widget
wp = p3dc.WindowProperties()
wp.set_foreground(True)
self._lastForcedForeground = True
#print("Forcing Foreground: True")
self.win.requestProperties(wp)
else:
#if self.win.getProperties().getForeground(): # This is once again not reliable and I therefore need to keep track of it myself...
if self._lastForcedForeground is not False:
# Ensure that the panda3d widget becomes inactive when the mouse leaves it since it can not receive button inputs if the mouse is gone
wp = p3dc.WindowProperties()
wp.set_foreground(False)
self._lastForcedForeground = False
#print("Forcing Foreground: False")
self.win.requestProperties(wp)
else:
#if self.win.getProperties().getForeground(): # This is once again not reliable and I therefore need to keep track of it myself...
if self._lastForcedForeground is not False:
# Ensure the the panda3d widget knows that it is inactive when its APE window is not active
wp = p3dc.WindowProperties()
wp.set_foreground(False)
self._lastForcedForeground = False
#print("Forcing Foreground: False")
self.win.requestProperties(wp)
return task.cont
def windowEvent(self, win:'p3dc.GraphicsWindow'):
if win != self.win:
# This event isn't about our window.
return
properties = win.getProperties()
if properties != self._ShowBase__prevWindowProperties: # pylint: disable=access-member-before-definition
self._ShowBase__prevWindowProperties = properties # pylint: disable=access-member-before-definition
self.notify.debug("Got window event: %s" % (repr(properties)))
if not properties.getOpen():
# If the user closes the main window, we should exit.
self.notify.info("User closed main window.")
self.userExit()
if properties.getForeground() and not self.mainWinForeground:
self.mainWinForeground = 1
elif not properties.getForeground() and self.mainWinForeground:
self.mainWinForeground = 0
if properties.getMinimized() and not self.mainWinMinimized:
# If the main window is minimized, throw an event to
# stop the music.
self.mainWinMinimized = 1
messenger.send('PandaPaused')
elif not properties.getMinimized() and self.mainWinMinimized:
# If the main window is restored, throw an event to
# restart the music.
self.mainWinMinimized = 0
messenger.send('PandaRestarted')
# If we have not forced the aspect ratio, let's see if it has
# changed and update the camera lenses and aspect2d parameters
self.adjustWindowAspectRatio(self.getAspectRatio())
if win.hasSize() and win.getSbsLeftYSize() != 0:
self.pixel2d.setScale(2.0 / win.getSbsLeftXSize(), 1.0, 2.0 / win.getSbsLeftYSize())
if self.wantRender2dp:
self.pixel2dp.setScale(2.0 / win.getSbsLeftXSize(), 1.0, 2.0 / win.getSbsLeftYSize())
else:
xsize, ysize = self.getSize()
if xsize > 0 and ysize > 0:
self.pixel2d.setScale(2.0 / xsize, 1.0, 2.0 / ysize)
if self.wantRender2dp:
self.pixel2dp.setScale(2.0 / xsize, 1.0, 2.0 / ysize)
#class World(DirectObject):
# def __init__(self):
# self.accept("a", self.pressedA)
# self.accept("escape", sys.exit)
# #
#
# def pressedA(self):
# print("a pressed, keyboard focus ok")
# #self.cam = base.makeCamera(self)#.buff
# #self.camNode = self.cam.node()
# #self.camLens = self.camNode.get_lens()
# #self.cam.setPos(0, -28, 6)
# ##self.win.setClearColorActive(True)
# ##self.win.setClearColor(p3dc.VBase4(0, 0.5, 0, 1))
# #self.testModel = loader.loadModel('panda')
# #self.testModel.reparentTo(render)
# #
# ## This rotates the actor 180 degrees on heading and 90 degrees on pitch.
# #myInterval4 = self.testModel.hprInterval(1.0, p3dc.Vec3(360, 0, 0))
# #myInterval4.loop()
#endregion Engine Classes
#region Main Functions
def start(name = "APE Test", engine = APE, base = APEPandaBase, app = APEApp, window = APEWindow, widget = PandaWidget, start = True, tobsprRenderPipeline = True):
"""
Starts the application using the supplied base classes. If a class is not supplied a standard class is used. \n
`name`: str - The name of the application. \n
`engine`: APE - The Engine class derived from APE. \n
`base`: APEPandaBase - The Base class derived from APEPandaBase. \n
`app`: APEApp - The App class derived from APEApp. \n
`window`: APEWindow - The Window class derived from APEWindow. \n
`widget`: PandaWidget - The Widget class for the panda3D display derived from PandaWidget. \n
`start`: bool - If `True` engine.start() is called. Set this to `False` if you want to start the engine later. \n
`tobsprRenderPipeline`: bool - Set this to `False` if your application is not compatible with tobspr's Render Pipeline. If `True` the user will be asked if they want to use it. \n
\n
You do not need to use this function to start your application but it is highly recommended.
"""
print(AGeAux.cTimeSStr(),": ",name,"Application Startup")
_app = app(sys.argv)
_app.ModuleVersions += f"\nAstus Panda Engine {Version}\nPanda3D {p3d.__version__}"
UseRenderPipeline = QtWidgets.QMessageBox.question(None,"Render Pipeline","Do You want to use tobspr's Render Pipeline?\nThis will make everything look pretty at the cost of performance.") == QtWidgets.QMessageBox.Yes if tobsprRenderPipeline else False
if UseRenderPipeline:
# Insert the pipeline path to the system path, this is required to be
# able to import the pipeline classes. In case you placed the render
# pipeline in a subfolder of your project, you have to adjust this.
sys.path.insert(0, "../tobsprRenderPipeline")
sys.path.insert(0, "tobsprRenderPipeline")
# Import render pipeline classes
from tobsprRenderPipeline import rpcore
# Construct and create the pipeline
render_pipeline = rpcore.RenderPipeline()
render_pipeline.pre_showbase_init()
Classes._PipelineImport()
else:
render_pipeline = False
_app.init_1(UseRenderPipeline)
p3dc.loadPrcFileData("", "window-type none")
_base = base(render_pipeline)
_engine = engine(_base, render_pipeline)
_app.init_2(_base,_engine)
print(AGeAux.cTimeSStr(),": ",name,"Window Startup")
_window = window(widget)
_app.setMainWindow(_window)
_window.setWindowTitle(name)
_app.setApplicationName(name+"-App")
_base.registerWindow(_window)
if UseRenderPipeline:
render_pipeline.create(_base)
# this basically creates an idle task
timer = QtCore.QTimer(_window)
timer.timeout.connect( _base.step )
timer.start(0)
#
print(AGeAux.cTimeSStr(),": ",name,"Window Started\n")
if hasattr(_window,"LastOpenState"):
_window.LastOpenState()
else:
_window.show()
if start:
_engine.start()
_base.buttonThrowers[0].node().setKeystrokeEvent('keystroke')
_base.buttonThrowers[0].node().setButtonDownEvent('buttonDown')
_base.buttonThrowers[0].node().setButtonUpEvent('buttonUp')
_base.accept('keystroke', _base.keystrokeSignal)
_base.accept('buttonDown', _base.buttonDownSignal)
_base.accept('buttonUp', _base.buttonUpSignal)
try:
_app.exec_()
except:
_app.exec()
#endregion Main Functions
#region Main Classes
class APEScene():
def __init__(self):
self._models = {}
def addModel(self, model, name):
self._models[name] = model
return model
class APEObject():
def __init__(self):
pass
#endregion Main Classes
#region Laboratory window
class APELabWindow(AWWF):
S_PandaKeystroke = pyqtSignal(str)
S_PandaButtonDown = pyqtSignal(str)
S_PandaButtonUp = pyqtSignal(str)
def __init__(self,widget = PandaWidget):
super(APELabWindow, self).__init__(IncludeErrorButton=True, FullscreenHidesBars=True)
App().setMainWindow(self)
self.LastOpenState = self.showMaximized
self.CentralSplitter = QtWidgets.QSplitter(self)
self.setCentralWidget(self.CentralSplitter)
self.TabWidget = QtWidgets.QTabWidget(self.CentralSplitter)
#
self.cw = QtWidgets.QWidget(self.CentralSplitter)
self.PandaContainer = widget(self.cw)
self.PandaContainer.installEventFilter(self)
self.Console1 = AGeIDE.ConsoleWidget(self)
self.Console1.setGlobals(self.globals())
#self.Console1.setText("self.genPlayer()\nself.HexGrid = AGE.HexGrid(self.AGE)\n")
self.TabWidget.addTab(self.Console1, "Con1")
self.Console2 = AGeIDE.ConsoleWidget(self)
self.Console2.setGlobals(self.globals())
#self.Console2.setText("self.genFloorAndPlayer()\n")
self.TabWidget.addTab(self.Console2, "Con2")
#self.GeneratorEditor = AGeIDE.OverloadWidget(self, self.gen, "gen")
#self.TabWidget.addTab(self.GeneratorEditor, "Gen")
self.Overload1 = AGeIDE.OverloadWidget(self)
self.Overload1.setGlobals(self.globals())
self.TabWidget.addTab(self.Overload1, "Overload 1")
self.Overload2 = AGeIDE.OverloadWidget(self)
self.Overload2.setGlobals(self.globals())
self.TabWidget.addTab(self.Overload2, "Overload 2")
self.Inspect = AGeIDE.InspectWidget(self)
self.Inspect.setGlobals(self.globals())
self.TabWidget.addTab(self.Inspect, "Inspect")
self.BalanceSizes = True
self.setupUI()
if self.BalanceSizes: self.CentralSplitter.setSizes([int(App().screenAt(QtGui.QCursor().pos()).size().width()/2), int(App().screenAt(QtGui.QCursor().pos()).size().width()/2)])
def setupUI(self):
"""
This Method sets up the UI. \n
If you want a different layout reimplement this Method. \n
All you need to do is to create a layout and apply it to `self.cw`.\n
`self.cw` is automatically set as the Central Widget. \n
`self.PandaContainer` is the Panda3D Widget and is created automatically.
"""
layout = QtWidgets.QVBoxLayout()
layout.setContentsMargins(0,0,0,0)
layout.addWidget(self.PandaContainer)
self.cw.setLayout(layout)
def eventFilter(self, source, event):
#if event.type() == 6: # QtCore.QEvent.KeyPress
#if hasattr(self,"AGE"):
# self.AGE.eventFilter(source, event)
return super().eventFilter(source, event) # let the normal eventFilter handle the event
def globals(self):
return vars(sys.modules['__main__'])
def gen(self):
pass
#endregion Laboratory window