happyleibniz / Python-Operating-System

Python Operating System updating each 3 month
MIT License
4 stars 1 forks source link

Can't drag the sprite when the window is resized #18

Open happyleibniz opened 8 months ago

happyleibniz commented 8 months ago

encountering a problem i can't drag the sprite when the window is resized some code snippet:

    def on_mouse_motion(self, x, y, dx, dy):
        self.computer_is_hovered = (
                int(self.computer_sprite.x) < x < int(self.computer_sprite.x) + self.computer_sprite.width
                and self.computer_sprite.y < y < self.computer_sprite.y + self.computer_sprite.height
        )
        self.installer_is_hovered = (
                int(self.installer_sprite.x) < x < int(self.installer_sprite.x) + self.installer_sprite.width
                and self.installer_sprite.y < y < self.installer_sprite.y + self.installer_sprite.height
        )

    def on_mouse_press(self, x, y, button, modifiers):
        self.login_button.on_mouse_press(x, y, button, modifiers)
        if self.computer_is_hovered and button == pyglet.window.mouse.LEFT:
            if hasattr(self, 'last_mouse_release'):
                if (x, y, button) == self.last_mouse_release[:-1]:
                    if time.time() - self.last_mouse_release[-1] < 0.2:
                        print("computer.double_click")
    def on_resize(self, width, height):
        gl.glViewport(0, 0, width, height)
happyleibniz commented 8 months ago

from Benjamin (the owner of the pyglet server):: There is an on_mouse_drag event as well. You will need to use that for dragging. me:

oh yeah
    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        if self.computer_is_hovered:
            self.computer_sprite.x += dx
            self.computer_sprite.y += dy
happyleibniz commented 8 months ago

Benjamin — Yesterday at 5:06 PM I can't see any issues from looking at it, but is computer_is_hovered == True at the top of your drag event?

happyleibniz commented 8 months ago

happyleibniz — Yesterday at 5:31 PM yes it is normal when i'm not resizing the window,but whenever i resize it it didn't work.

happyleibniz commented 8 months ago

Benjamin — Yesterday at 6:10 PM Can you share your code?

happyleibniz commented 8 months ago

happyleibniz — Yesterday at 6:14 PM ok (uhh... the code is too long and the files are too big so i might as well create another sharable code) packed_data.zip

happyleibniz commented 8 months ago

Benjamin — Today at 8:03 AM I see your issue. You are stretching the image to fill the screen on resize. You actually can still drag the icon after resizing, but the x, y coordinates don't line up to the visuals anymore. What kind of end-result are you going for? If you want to allow arbitrary stretching like this, then it will take a little math to figure out the relative mouse pointer position, after stretching.

happyleibniz commented 8 months ago

happyleibniz — Today at 1:32 PM I want to allow arbitrary stretching like this.What can i do?

happyleibniz commented 8 months ago

Benjamin — Today at 2:06 PM You will need to know the original size of the Window. After resizing it, you can take the original and new sizes to scale the x position by. Like if the original size is 100w, and the new size is 150w, you divide those to get the scaling. Something like this:

class Init(pyglet.window.Window):
    def __init__(self, *args, **kwargs):
        ...
        self._initial_w = self.width
        self._initial_h = self.height
        self.x_scale = self.width / self._initial_w
        self.y_scale = self.height / self._initial_h

    def on_mouse_release(self, x, y, button, modifiers):
        x /= self.x_scale
        y /= self.y_scale
        ...

    def on_mouse_motion(self, x, y, dx, dy):
        x /= self.x_scale
        y /= self.y_scale
        dx /= self.x_scale
        dy /= self.y_scale
        ...

    def on_resize(self, width, height):
        gl.glViewport(0, 0, width, height)
        self.x_scale = self.width / self._initial_w
        self.y_scale = self.height / self._initial_h
happyleibniz commented 8 months ago

Benjamin — Today at 2:13 PM However, I would strongly discourage using glViewport in this way. You're kinda fighting against everything.

Another technique would be to simply scale the background sprite (sprite.scale_x & sprite.scale_y). This way you can avoid the usage of glViewport and still fill the screen.

The icon sprite would also need to be scaled, and probably re-positioned using a similar technique to find the ratio, but the mouse input events would then line up correctly to the actual pixels.

This is not really a common thing to do, because distorted aspect ratios tend to be received negatively by end users. There are different alternatives that are commonly used in games and applications. For example, letterboxing (adding black bars), or zooming in and cropping. happyleibniz — Today at 2:58 PM ok