This commit is contained in:
leo 2022-02-19 19:03:24 +01:00
parent acdb10cb15
commit 06b0f0caf1
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
2 changed files with 133 additions and 2 deletions

View File

@ -1,3 +1,42 @@
# Traffic
conf Peip
* [DOCUMENTATION](DOC.md#documentation)
* [Reseau routier .net.xml de SUMO](DOC.md#reseau-routier-netxml-de-sumo)
* [Edge](DOC.md#edge)
* [Lane](DOC.md#lane)
* [Junction/Node](DOC.md#junctionnode)
* [Compilation](DOC.md#compilation)
* [Pour generer les classes de l'ui à partir des .ui de Qt Designer](DOC.md#pour-generer-les-classes-de-lui-à-partir-des-ui-de-qt-designer)
* [Pour faire un executable](DOC.md#pour-faire-un-executable)
* [mainLoop.py](mainLoop.md#mainlooppy)
* [imports](mainLoop.md#imports)
* [classe mainLoop](mainLoop.md#classe-mainloop)
* [__init__](mainLoop.md#__init__)
* [start](mainLoop.md#start)
* [update](mainLoop.md#update)
* [openNetwork](mainLoop.md#opennetwork)
* [openVehicles](mainLoop.md#openvehicles)
* [quickLoad](mainLoop.md#quickload)
* [updateFps](mainLoop.md#updatefps)
* [main.py](main.md#mainpy)
* [Imports](main.md#imports)
* [Classe MainWindow](main.md#classe-mainwindow)
* [__init__](main.md#__init__)
* [keyPressEvent](main.md#keypressevent)
* [updateFPS](main.md#updatefps)
* [updatePhysicsFps](main.md#updatephysicsfps)
* [openNetwork](main.md#opennetwork)
* [openVehicle](main.md#openvehicle)
* [main](main.md#main)
* [mainPainter.py](mainPainter.md#mainpainterpy)
* [Imports](mainPainter.md#imports)
* [mainPainter](mainPainter.md#mainpainter)
* [__init__](mainPainter.md#__init__)
* [addMap](mainPainter.md#addmap)
* [addCarController](mainPainter.md#addcarcontroller)
* [generateTransform](mainPainter.md#generatetransform)
* [resizeGL](mainPainter.md#resizegl)
* [paintEvent](mainPainter.md#paintevent)

92
mainPainter.md Normal file
View File

@ -0,0 +1,92 @@
# mainPainter.py
La classe qui gére l'affichage (de la partie principale, pas les ui d'infos)
## Imports
Comme d'hab les imports de Qt
```python
from PySide6.QtOpenGLWidgets import QOpenGLWidget
from PySide6.QtCore import Qt, QThread
from PySide6.QtGui import QPainter, QFont, QColor, QTransform, QOpenGLContext, QOpenGLFunctions
from PySide6 import QtOpenGL
```
## mainPainter
La classe principale, c'est un sous-classe de QOpenGLWidget
```python
class mainPainter(QOpenGLWidget):
```
## \_\_init\_\_
Notre initialisation, on récupère les deux classe de la carte et des voitures parce que c'est eux qui sont responsables de leurs affichage.
Le QTransform sert à calculer/stocker la transformation pour passer de coordonnées dans le monde réél-\>coords sur l'écran
```python
def __init__(self, parent=None):
super(mainPainter, self).__init__(parent)
self.map = None
self.carController = None
self.transform = QTransform()
```
## addMap
Juste un fonction pour passer la carte, on pourrait probablement le faire passer dans le constructeur plutôt
```python
def addMap(self, netMap):
self.map = netMap
```
## addCarController
Comme [addMap](#addMap)
```python
def addCarController(self, cc):
self.carController = cc
```
## generateTransform
La fonction qui genere la transformation monde-\>écran.
On récupere les limites du réseau, puis on translate/scale en fonction
Pour avoir un peu de marge on rajoute 10 de tout les côtés
Comme l'origine est en haut à gauche et les y decroissant on scale de -1 sur y pour remettre l'image à l'endroit
```python
def generateTransform(self):
bounds = self.map.getBounds()
if(bounds is None):
return
bounds[0] = list(map(lambda b: b-10,bounds[0]))
bounds[1] = list(map(lambda b: b+10, bounds[1]))
self.transform.reset()
tr = QTransform()
tr.translate(-bounds[0][0],-bounds[0][1])
scale=min(self.width()/(bounds[1][0]-bounds[0][0]),self.height()/(bounds[1][1]-bounds[0][1]))
sc = QTransform()
sc.scale(scale,-scale)
self.transform = tr * sc * QTransform(1, 0, 0, 1, 0, self.height())
```
## resizeGL
Fonction appelé quand le widget est redimensionné (ça inclut la création), le cas échéant on recalcule la transformation avec la nouvelle taille
```python
def resizeGL(self,w,h):
self.generateTransform()
```
## paintEvent
Fonction appelé à chaque appel sur update.
On crée notre QPainter, on active l'anti-aliasing (marche pas en openGL apparement), on clear l'écran, on applique la transformation et on demande à notre carte et nos voitures de se dessiner.
```python
def paintEvent(self, e):
painter = QPainter(self)
painter.RenderHint(QPainter.Antialiasing)
painter.eraseRect(e.rect());
painter.setTransform(self.transform)
self.map.draw(painter)
self.carController.draw(painter)
```