kxgames / glooey

An object-oriented GUI library for pyglet.
MIT License
91 stars 6 forks source link

Form/Editable label failing with glScissor #57

Closed Rahuum closed 2 years ago

Rahuum commented 2 years ago

Fixes #55

kalekundert commented 2 years ago

Thanks for figuring this out. I think you need to keep the aligned_rect.round() call, though, because it actually rounds to the nearest integer, while int() just truncates the fractional component of the number. In other words:

>>> int(1.9)
1
>>> round(1.9)
2

Alternatively, it might be better to fix the vecrec.Rect.round() method to just return ints in the first place. This relates to an obscure behavior of the round() function that I didn't know about until just now:

>>> help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

So the reason Rect.round() is returning floats is that we're explicitly passing 0 as the number of digits. If we rewrite Rect.round() as show below, it will behave exactly like round() and return ints when no digits are specified:

def round(self, *args, **kwargs):
        """ Round the dimensions of the given rectangle to the given number of digits. """
        self._left = round(self._left, *args, **kwargs)
        self._bottom = round(self._bottom, *args, **kwargs)
        self._width = round(self._width, *args, **kwargs)
        self._height = round(self._height, *args, **kwargs)

I think that would be a more robust solution.

kalekundert commented 2 years ago

In fact, I'll just make that change to Rect.round(). It'll just take me a few minutes...

kalekundert commented 2 years ago

Ok, I made the aforementioned changes to Rect.round(). I think that also fixes #55 without the need for these changes. Let me know if that isn't the case.

Rahuum commented 2 years ago

So that did fix it, though it does have the long-term issue of making it less self-documenting.

It may be worth it to investigate something like

def round(self, digits=None):
        """ Round the dimensions of the given rectangle to the given number of digits. """
        self._left = round(self._left, digits)
        self._bottom = round(self._bottom, digits)
        self._width = round(self._width, digits)
        self._height = round(self._height, digits)

or something. But I'll close this out since my own issue is actually closed, that's more a doco suggestion than anything.

kalekundert commented 2 years ago

Yeah, that occurred to me right after I finished making the change. I might go back and fix it when I get some time.