How to add relative zoom to ZoomGestureHandler in python

28 Views Asked by At

I am currently working on implementing a custom zoom gesture in Python for my mobile graphics library. Despite trying multiple solutions, including exploring ChatGPT, I encountered an issue with the code not functioning correctly.

The problem arises when I perform a pinch and zoom in, release, and attempt to zoom in again. Instead of continuing to zoom in, the zoom resets, which is not the intended behavior.

I would greatly appreciate any assistance from the community in resolving this issue. Thank you in advance for your help.

import sys
import sdl2
import sdl2.ext
import numpy as np
import math as ml
from spectrax import operations as op

class ZoomGestureHandler:
    def __init__(self, window):
        self.zoom_level = 1.0
        self.zoom_factor = 0.01
        self.max_zoom = 2.0
        self.min_zoom = 0.5
        self.window = window
        self.finger_positions = [[0, 0], [0, 0]]
        self.finger_positions_pre = [[0, 0], [0, 0]]
        self.finger_count = 0
        self.zooming = False
        self.image_zoom = 0
        
    def get_zoom(self):
        # Use this function to retrieve the zoom_level
        zoom = abs(ml.dist(*self.finger_positions)) * 10
        return zoom if zoom > 1 else 1
    
    def event_handler(self, event):
        zoom_handler = self
        
        fingers = event.touch_gestures.fingers
        if event.mouse.press and "0" in fingers and "1" in fingers and event.touch_gestures.finger_count > 1:
            f1 = event.touch_gestures.fingers["0"].xy
            f2 = event.touch_gestures.fingers["1"].xy
            self.finger_positions = [f1, f2]
            self.zooming = True
        else:
            self.zooming = False


0

There are 0 best solutions below