Source

dark-hammer / src / pymodules / dhwidgets / eng_view.py

The branch '0.4.7' does not exist.
from PyQt4 import QtCore, QtGui
import dheng

class qEngineView(QtGui.QWidget):
    def __init__(self, parent):
        super(qEngineView, self).__init__(parent)

        self.setAttribute(QtCore.Qt.WA_PaintOnScreen, True)
        self.setAttribute(QtCore.Qt.WA_NativeWindow, True)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.setMouseTracking(True)
        self.setMinimumSize(64, 64)
        self.setSizePolicy(\
            QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding))        

        self.timer = QtCore.QTimer(self)
        self.sz_timer = QtCore.QTimer(self)
        self.sz_timer.setSingleShot(True)
        self.timer.start(20)

        self.prev_x = 0
        self.prev_y = 0
        self.mouse_dwn = False
        self.cam = None

        # events
        self.timer.timeout.connect(self.update)
        self.sz_timer.timeout.connect(self.resize)

    def set_cam(self, cam):
        dheng.app_set_rendertarget(str(self.winId().__hex__()))
        if cam != None:
            dheng.scn_setcam(cam)
            dheng.cam_set_viewsize(cam, self.width(), self.height())
        self.cam = cam

    def paintEvent(self, e):
        if not self.cam:
            return
        dheng.eng_update()

    def paintEngine(self):
        return None

    def resize(self):
        if not self.cam:
            return
        dheng.app_resize_window(str(self.winId().__hex__()), self.width(), self.height())
        dheng.cam_set_viewsize(self.cam, self.width(), self.height())

    def keyPressEvent(self, e):
        if not self.cam:
            return
        if self.mouse_dwn:
            cam = dheng.scn_getcam()
            key = e.key()
            if key == QtCore.Qt.Key_W or key == QtCore.Qt.Key_Up:
                dheng.cam_fwd(self.cam, 0.5)
            elif e.key() == QtCore.Qt.Key_S or key == QtCore.Qt.Key_Down:
                dheng.cam_fwd(self.cam, -0.5)
            elif e.key() == QtCore.Qt.Key_D or key == QtCore.Qt.Key_Right:
                dheng.cam_strafe(self.cam, 0.5)
            elif e.key() == QtCore.Qt.Key_A or key == QtCore.Qt.Key_Left:
                dheng.cam_strafe(self.cam, -0.5)
    
    def mouseMoveEvent(self, e):
        if not self.cam:
            return
        pt = e.pos()

        if self.mouse_dwn:
            cam = dheng.scn_getcam()
            dx = pt.x() - self.prev_x
            dy = pt.y() - self.prev_y
            dheng.cam_yaw(cam, dx*0.01)
            dheng.cam_pitch(cam, dy*0.01)
            
        self.prev_x = pt.x()
        self.prev_y = pt.y()

    def resizeEvent(self, e):
        if not self.cam:
            return
        if not self.sz_timer.isActive():
            self.sz_timer.start(500)

    def update(self):
        if not self.cam:
            return
        dheng.cam_update(self.cam)
        dheng.eng_update()

    def mousePressEvent(self, e):
        self.mouse_dwn = True

    def mouseReleaseEvent(self, e):
        self.mouse_dwn = False

    def wheelEvent(self, e):
        if not self.cam:
            return
        steps = e.delta()/15
        dheng.cam_fwd(self.cam, steps)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.