Open happyleibniz opened 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
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 — 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.
Benjamin — Yesterday at 6:10 PM Can you share your code?
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
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 — Today at 1:32 PM I want to allow arbitrary stretching like this.What can i do?
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
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
encountering a problem i can't drag the sprite when the window is resized some code snippet: