google-code-export / simple-pygame-gui

Automatically exported from code.google.com/p/simple-pygame-gui
1 stars 1 forks source link

[patch] big listbox scrolling speedup #11

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
ListBoxes were redrawing their entire contents for every redraw, even when
most items hadn't changed since the last redraw.  This fix only redraws the
items where hover/selected status has changed since the last redraw.  I
have a large list (800+ items) that I scroll rapidly with moveUp/moveDown
via the joystick, and it was REALLY slow before, and is very fast now.

Original issue reported on code.google.com by mackst...@gmail.com on 2 Oct 2008 at 10:20

Attachments:

GoogleCodeExporter commented 9 years ago
Also another speedup would be to only redraw items within the currently 
scrolled-to
region.

Original comment by mackst...@gmail.com on 2 Oct 2008 at 10:23

GoogleCodeExporter commented 9 years ago
Uh, ignore that last comment.  I'm a little sleep-deprived.

Here is another speedup which avoids checking the max width of all rendered 
items for
every refresh:

in __init__:

        self.maxItemWidth = None

in refresh:

-        maxItemWidth = self.size[0]
-        for item in self.items:
-            itemWidth = self.style['font'].size(str(item))[0]
-            if itemWidth  > maxItemWidth:
-                maxItemWidth = itemWidth 
+        if self.maxItemWidth is None:
+            maxItemWidth = self.size[0]
+            for item in self.items:
+                itemWidth = self.style['font'].size(str(item))[0]
+                if itemWidth  > maxItemWidth:
+                    maxItemWidth = itemWidth 
+            
+            if self.style['autosize']:
+                self.size = maxItemWidth, len(self.items) * itemheight 
+            self.maxItemWidth = maxItemWidth

-        if self.style['autosize']:
-            self.size = maxItemWidth, len(self.items) * itemheight 
-        

Original comment by mackst...@gmail.com on 2 Oct 2008 at 10:41

GoogleCodeExporter commented 9 years ago
Ok, one problem I've found is that when you change items dynamically, the new
plain_on_last_refresh list becomes out of sync with it.  What I did was to work
around this to use a property so they both always stay in sync:

    def set_items(self, items):
        self._items = items
        self.plain_on_last_refresh = [False] * len(items)
    items = property(lambda self: self._items, set_items)

and remove this from __init__:

self.plain_on_last_refresh = [False] * len(items)

I can make a clean patch with all of this stuff combined if it would help.

Original comment by mackst...@gmail.com on 3 Oct 2008 at 5:00