changeset 602:1aac5a3b17e1 pyntnclick

Remove anything that has to do with focus (hopefully not breaking anything)
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 11 Feb 2012 20:05:22 +0200
parents 0ae62d824d2f
children 3ce19d33b51f
files pyntnclick/widgets/base.py pyntnclick/widgets/game.py pyntnclick/widgets/listbox.py pyntnclick/widgets/messagebox.py pyntnclick/widgets/overlay.py pyntnclick/widgets/text.py pyntnclick/widgets/toollist.py
diffstat 7 files changed, 0 insertions(+), 171 deletions(-) [+]
line wrap: on
line diff
--- a/pyntnclick/widgets/base.py	Sat Feb 11 20:00:59 2012 +0200
+++ b/pyntnclick/widgets/base.py	Sat Feb 11 20:05:22 2012 +0200
@@ -13,8 +13,6 @@
         if not isinstance(rect, pygame.Rect):
             rect = pygame.Rect(rect, (0, 0))
         self.rect = rect
-        self.focussable = False
-        self.focussed = False
         self.modal = False
         self.parent = None
         self.disabled = False
@@ -45,54 +43,15 @@
         "Override me"
         pass
 
-    def grab_focus(self):
-        if self.focussable:
-            # Find the root and current focus
-            root = self
-            while root.parent is not None:
-                root = root.parent
-            focus = root
-            focus_modal_base = None
-            while (isinstance(focus, Container)
-                    and focus.focussed_child is not None):
-                if focus.modal:
-                    focus_modal_base = focus
-                focus = focus.children[focus.focussed_child]
-
-            # Don't leave a modal heirarchy
-            if focus_modal_base:
-                widget = self
-                while widget.parent is not None:
-                    if widget == focus_modal_base:
-                        break
-                    widget = widget.parent
-                else:
-                    return False
-
-            root.defocus()
-            widget = self
-            while widget.parent is not None:
-                parent = widget.parent
-                if isinstance(parent, Container):
-                    idx = parent.children.index(widget)
-                    parent.focussed_child = idx
-                widget = parent
-            self.focussed = True
-            return True
-        return False
-
     def disable(self):
         if not self.disabled:
             self.disabled = True
-            self._focussable_when_enabled = self.focussable
-            self.focussable = False
             if hasattr(self, 'prepare'):
                 self.prepare()
 
     def enable(self):
         if self.disabled:
             self.disabled = False
-            self.focussable = self._focussable_when_enabled
             if hasattr(self, 'prepare'):
                 self.prepare()
 
@@ -110,7 +69,6 @@
 
     def forced_click(self):
         """Force calling the clicked handler"""
-        self.grab_focus()
         for callback, args in self.callbacks['clicked']:
             if callback(None, self, *args):
                 return True
@@ -124,7 +82,6 @@
             rect = pygame.Rect(0, 0, 0, 0)
         super(Container, self).__init__(rect)
         self.children = []
-        self.focussed_child = None
 
     def event(self, ev):
         """Push an event down through the tree, and fire our own event as a
@@ -133,9 +90,6 @@
         if ev.type in (MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN):
             for child in self.children[:]:
                 if child.rect.collidepoint(ev.pos):
-                    if ev.type == MOUSEBUTTONDOWN and child.focussable:
-                        if not child.grab_focus():
-                            continue
                     if child.event(ev):
                         return True
 
@@ -154,77 +108,9 @@
 
     def remove(self, widget):
         widget.parent = None
-        if self.focussed_child is not None:
-            child = self.children[self.focussed_child]
         self.children.remove(widget)
-        # We don't update the rect, for reasons of simplificty and laziness
-        if self.focussed_child is not None and child in self.children:
-            # Fix focus index
-            self.focussed_child = self.children.index(child)
-        else:
-            self.focussed_child = None
-
-    def defocus(self):
-        if self.focussed_child is not None:
-            child = self.children[self.focussed_child]
-            if isinstance(child, Container):
-                if not child.defocus():
-                    return False
-            child.focussed = False
-            self.focussed_child = None
-            return True
-
-    def adjust_focus(self, direction):
-        """Try and adjust focus in direction (integer)
-        """
-        if self.focussed_child is not None:
-            child = self.children[self.focussed_child]
-            if isinstance(child, Container):
-                if child.adjust_focus(direction):
-                    return True
-                elif child.modal:
-                    # We're modal, go back
-                    if child.adjust_focus(-direction):
-                        return True
-            else:
-                child.focussed = False
-
-        current = self.focussed_child
-        if current is None:
-            current = -1 if direction > 0 else len(self.children)
-        if direction > 0:
-            possibles = list(enumerate(self.children))[current + 1:]
-        else:
-            possibles = list(enumerate(self.children))[:current]
-            possibles.reverse()
-        for i, child in possibles:
-            if child.focussable:
-                child.focussed = True
-                self.focussed_child = i
-                return True
-            if isinstance(child, Container):
-                if child.adjust_focus(direction):
-                    self.focussed_child = i
-                    return True
-        else:
-            if self.parent is None:
-                if self.focussed_child is not None:
-                    # At the end, mark the last one as focussed, again
-                    child = self.children[self.focussed_child]
-                    if isinstance(child, Container):
-                        if child.adjust_focus(-direction):
-                            return True
-                    else:
-                        child.focussed = True
-                        return True
-            else:
-                self.focussed_child = None
-            return False
 
     def draw(self, surface):
-        if self.parent is None and not self.focussed:
-            self.focussed = True
-            self.adjust_focus(1)
         for child in self.children:
             child.draw(surface)
 
@@ -243,30 +129,6 @@
         assert not widget.modal
         super(GridContainer, self).add(widget)
 
-    def adjust_focus(self, direction):
-        if isinstance(direction, int):
-            direction = (direction, 0)
-
-        if len(self.children) == 0:
-            return False
-
-        if self.focussed_child is None:
-            if sum(direction) > 0:
-                self.focussed_child = 0
-            else:
-                self.focussed_child = len(self.children) - 1
-        else:
-            self.children[self.focussed_child].focussed = False
-            if direction[0] != 0:
-                self.focussed_child += direction[0]
-            if direction[1] != 0:
-                self.focussed_child += self.width * direction[1]
-        if not 0 <= self.focussed_child < len(self.children):
-            self.focussed_child = None
-            return False
-        self.children[self.focussed_child].focussed = True
-        return True
-
 
 class Box(Container):
     """A container that draws a filled background with a border"""
--- a/pyntnclick/widgets/game.py	Sat Feb 11 20:00:59 2012 +0200
+++ b/pyntnclick/widgets/game.py	Sat Feb 11 20:05:22 2012 +0200
@@ -11,7 +11,6 @@
         self.world = world
         rect = Rect(offset, world.get_size())
         super(GameWidget, self).__init__(rect)
-        self.focussable = True
         self.add_callback(FlipArrowsEvent, self.flip_arrows)
 
     def flip_arrows(self, ev, widget):
@@ -23,4 +22,3 @@
 
     def restart(self):
         self.world.restart()
-        self.grab_focus()
--- a/pyntnclick/widgets/listbox.py	Sat Feb 11 20:00:59 2012 +0200
+++ b/pyntnclick/widgets/listbox.py	Sat Feb 11 20:05:22 2012 +0200
@@ -39,6 +39,3 @@
             self.parent.paused = False
         self.parent.remove(self)
         return True
-
-    def grab_focus(self):
-        return self.ok_button.grab_focus()
--- a/pyntnclick/widgets/messagebox.py	Sat Feb 11 20:00:59 2012 +0200
+++ b/pyntnclick/widgets/messagebox.py	Sat Feb 11 20:05:22 2012 +0200
@@ -49,6 +49,3 @@
             self.post_callback()
         if getattr(self, 'parent_modal', False):
             self.parent.modal = True
-
-    def grab_focus(self):
-        return self.ok_button.grab_focus()
--- a/pyntnclick/widgets/overlay.py	Sat Feb 11 20:00:59 2012 +0200
+++ b/pyntnclick/widgets/overlay.py	Sat Feb 11 20:05:22 2012 +0200
@@ -1,20 +1,6 @@
 from pyntnclick.widgets.base import Button
 
 
-class OverlayOnFocusButton(Button):
-    """A non-visiable clickable area, that causes an overlay to be
-       displayed when focussed"""
-
-    def __init__(self, rect, image):
-        self.image = image
-        super(OverlayOnFocusButton, self).__init__(rect)
-        self.focussable = True
-
-    def draw(self, surface):
-        if self.focussed:
-            surface.blit(self.image, surface.get_rect())
-
-
 class OverlayButton(Button):
     """A non-visiable clickable area, that causes an overlay to be
     displayed. Doesn't really understand this focus thing."""
@@ -22,7 +8,6 @@
     def __init__(self, rect, image):
         self.image = image
         super(OverlayButton, self).__init__(rect)
-        self.focussable = True
 
     def draw(self, surface):
         if not self.disabled:
--- a/pyntnclick/widgets/text.py	Sat Feb 11 20:00:59 2012 +0200
+++ b/pyntnclick/widgets/text.py	Sat Feb 11 20:05:22 2012 +0200
@@ -39,23 +39,16 @@
 
 class TextButton(Button, TextWidget):
     def __init__(self, *args, **kwargs):
-        self.focus_color = kwargs.pop('focus_color', FOCUS_COLOR)
         self.padding = kwargs.pop('padding', 10)
         self.border = kwargs.pop('border', 3)
         super(TextButton, self).__init__(*args, **kwargs)
-        if not isinstance(self.focus_color, pygame.Color):
-            self.focus_color = pygame.Color(self.focus_color)
-        self.focussable = True
 
     def prepare(self):
         super(TextButton, self).prepare()
         text = self.surface
         text_rect = self.text_rect
-        self._focussed = self.focussed
         if self.disabled:
             color = pygame.Color('#666666')
-        elif self.focussed:
-            color = self.focus_color
         else:
             color = self.color
 
@@ -71,6 +64,4 @@
                          self.border)
 
     def draw(self, surface):
-        if self._focussed != self.focussed:
-            self.prepare()
         super(TextButton, self).draw(surface)
--- a/pyntnclick/widgets/toollist.py	Sat Feb 11 20:00:59 2012 +0200
+++ b/pyntnclick/widgets/toollist.py	Sat Feb 11 20:05:22 2012 +0200
@@ -16,7 +16,6 @@
         self.prev_but = None
         self.next_but = None
         self.fill_page()
-        self.focussable = True
 
     def fill_page(self):
         for widget in self.children[:]: