n4archive / pigame

A pygame wrapper for the libary pitft_touchscreen.
GNU Lesser General Public License v3.0
6 stars 1 forks source link

Fix #1 #6

Closed nift4 closed 5 years ago

nift4 commented 5 years ago

Fixes #1 Review and test needed!

BroMarduk commented 5 years ago

OK, I tested this and it works regarding the events occurring correctly, but I did find another issue.

Currently the code creates an array to store a list of events (pitft.pigameevs=[]). This array just keeps growing is size as items are never removed. EVDEV will eventually wrap around (I think the id is an integer) and there will be conflicts with duplicates.

I think the goal is to detect the MOUSEMOTION event from MOUSEBUTTONDOWN. A better way would be to just keep the buttondown/up state and toggle the state accordingly on a touch or lift. No more ever-growing array and it correctly distinguishes between events.

If you have another reason for storing the event IDs, let me know.

import pygame,pitft_touchscreen
from pygame.locals import *
pitft=pitft_touchscreen.pitft_touchscreen()
pitft.button_state=0
pitft.pl={'x':0,'y':0}
def init(rotation:int=90):
    pitft.pigamerotr=rotation
    pitft.start()
def run():
    while not pitft.queue_empty():
        for r in pitft.get_event():
            e={"y":(r["x"] if r["x"] else pitft.pl["x"]),"x":(r["y"] if r["y"] else pitft.pl["y"])}
            if e["x"] is None or e["y"] is None:
                break
            rel=(e["x"]-pitft.pl["x"],e["y"]-pitft.pl["y"])
            pitft.pl={"x":e["x"],"y":e["y"]}
            if pitft.pigamerotr==90:
                e={"x":e["x"],"y":240-e["y"]}
            elif pitft.pigamerotr==270:
                e={"x":320-e["x"],"y":e["y"]}
            else:
                raise(Exception("PiTft rotation is unsupported"))
            d={}
            t=MOUSEBUTTONUP if r["touch"]==0 else (MOUSEBUTTONDOWN if pitft.button_state == 0 else MOUSEMOTION)
            if t==MOUSEBUTTONDOWN:
                pitft.button_state = 1
                d["button"]=1
                d["pos"]=(e["x"],e["y"])
                pygame.mouse.set_pos(e["x"],e["y"])
            elif t==MOUSEBUTTONUP:
                pitft.button_state = 0
                d["button"]=1
                d["pos"]=(e["x"],e["y"])
            else:
                d["buttons"]=(True,False,False)
                d["rel"]=rel
                d["pos"]=(e["x"],e["y"])
                pygame.mouse.set_pos(e["x"],e["y"])
            pe=pygame.event.Event(t,d)
            pygame.event.post(pe)
def quit():
    pitft.stop()

If you want to create a branch and give me permission to edit in it, I can change code directly there.

nift4 commented 5 years ago

I have a branch, but how allow editing?

nift4 commented 5 years ago

Branch deleted, please make a fork! It's not easy on GitHub to do permission management per branch

BroMarduk commented 5 years ago

Will do.