py-vpet/window_qt.py
2024-12-11 18:31:41 +01:00

136 lines
4.2 KiB
Python
Executable File

#!/bin/python3
import datetime
import time
import asyncio
from PyQt5.QtWidgets import QOpenGLWidget
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt
from OpenGL.GL import *
from OpenGL.GLU import gluOrtho2D
from PIL import Image
from vpet import Rotate
class TransparentGLWidget(QOpenGLWidget):
def __init__(self, image_path):
super().__init__()
self.image_path = image_path
self.texture = None
self.image_width = 1
self.image_height = 1
def initializeGL(self):
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_DEPTH_TEST)
glClearColor(0.0, 0.0, 0.0, 0.0)
# Textur init
self.texture, self.image_width, self.image_height = self.load_texture(self.image_path)
def resizeGL(self, w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, w, 0, h) # Orthografische Projektion für 2D
glMatrixMode(GL_MODELVIEW)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glPushMatrix() # Matrix speichern
self.draw_textured_quad()
glPopMatrix() # Matrix wiederherstellen
def load_texture(self, image_path):
img = Image.open(image_path).convert("RGBA")
img = img.rotate(angle=Rotate.up)
img_data = img.tobytes()
width, height = img.size
# Create OpenGL texture
texture_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture_id)
# Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
# Load texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data)
return texture_id, width, height
def draw_textured_quad(self):
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texture)
width, height = self.width(), self.height()
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0)
glVertex2f(0, 0) # Down left corner
glTexCoord2f(1.0, 0.0)
glVertex2f(width, 0) # Down right corner
glTexCoord2f(1.0, 1.0)
glVertex2f(width, height) # Up right corner
glTexCoord2f(0.0, 1.0)
glVertex2f(0, height) # Up left corner
glEnd()
glDisable(GL_TEXTURE_2D)
class MainWindow(QMainWindow):
def __init__(self, image_path):
super().__init__()
# self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setCentralWidget(TransparentGLWidget(image_path))
self.click_event_time = 0
img = Image.open(image_path)
self.resize(img.width, img.height)
async def loop(self):
self.move(0, 0)
self.show()
print('Loop')
def mousePressEvent(self, event):
self.click_event_time = time.time()
if event.button() == Qt.LeftButton:
self.drag_position = event.globalPos()
def mouseReleaseEvent(self, event):
time_delta = time.time() - self.click_event_time
if time_delta <= 0.2:
if event.button() == Qt.LeftButton:
print('Click left')
if event.button() == Qt.RightButton:
print('Click right')
self.click_event_time = 0
def mouseMoveEvent(self, event):
self.click_event_time = 0
if event.buttons() == Qt.LeftButton:
delta = event.globalPos() - self.drag_position
self.move(self.pos() + delta)
self.drag_position = event.globalPos()
async def start():
app = QApplication([])
image_path = 'pets_sprites/demonite/demonite_idle1.png' # Ersetze dies mit deinem Bildpfad
window = MainWindow(image_path)
await window.loop()
app.exec_()
asyncio.run(start())