[Ocemp-CVS] ocempgui/ocempgui/widgets Renderer.py, 1.61.2.13, 1.61.2.14 __init__.py, 1.39.2.3, 1.39
Status: Beta
Brought to you by:
marcusva
From: Marcus v. A. <mar...@us...> - 2006-11-15 12:27:28
|
Update of /cvsroot/ocemp/ocempgui/ocempgui/widgets In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv19568/ocempgui/widgets Modified Files: Tag: rel_0_2 Renderer.py __init__.py Log Message: Merged from HEAD: Fixed assignment of surfaces to the Renderer.screen attribute and the event handling for them. Added embedding example with partial screen assignments. Index: Renderer.py =================================================================== RCS file: /cvsroot/ocemp/ocempgui/ocempgui/widgets/Renderer.py,v retrieving revision 1.61.2.13 retrieving revision 1.61.2.14 diff -u -d -r1.61.2.13 -r1.61.2.14 --- Renderer.py 14 Nov 2006 12:17:23 -0000 1.61.2.13 +++ Renderer.py 15 Nov 2006 12:27:26 -0000 1.61.2.14 @@ -28,7 +28,7 @@ from pygame import display, Surface, event, QUIT, VIDEORESIZE, KMOD_SHIFT from pygame import K_TAB, KMOD_LALT, KMOD_RALT, KMOD_ALT, KMOD_CTRL, KMOD_RCTRL from pygame import KMOD_LCTRL, KMOD_RSHIFT, KMOD_LSHIFT, KEYUP, KEYDOWN, mouse -from pygame import error as PygameError, time as PygameTime +from pygame import error as PygameError, time as PygameTime, Rect from ocempgui.events import EventManager, Event from ocempgui.access import IIndexable from ocempgui.draw import Complex, String @@ -328,6 +328,17 @@ renderer = Renderer () renderer.screen = your_main_screen_or_surface + Note, that if the assigned scren is a surface, which will be blit at + a certain position x, y (e.g. mainscreen.blit (renderer.screen, (10, 10)), + the Renderer's screen offset values have to be adjusted, too: + + Renderer.topleft = 10, 10 # Set both, x and y offset. + Renderer.x = 10 # Only x offset. + Renderer.y = 10 # Only y offset. + + This is necessary to let the Renderer correctly translate mouse + positions to its + Then you can send the events processed in your event loop to the Renderer and its objects via the distribute_events() method: @@ -348,6 +359,10 @@ support_resize - Indicates, whether resize events should be supported. Default is False and automatically set to True, if create_screen() is used. + rect - The area occupied by the Renderer. + x, y, ... - The Renderer allows to reposition itself through the + width, ... various attributes offered by its rect attribute. + size """ def __init__ (self): IIndexable.__init__ (self) @@ -402,6 +417,57 @@ # _supportresize is enabled. self.__flags = 0 + # Screen offsets for partial screen assignments (Renderer is bound + # to a surface instead to the whole pygame window. + self._rect = Rect (0, 0, 0, 0) + + def _get_rect (self): + """W._get_rect () -> pygame.Rect + + Gets a copy of the widget's rect. + """ + return Rect (self._rect) + + def _get_rect_attr (self, attr): + """R._get_rect_attr (...) -> var + + Gets the wanted attribute value from the underlying rect. + """ + return getattr (self._rect, attr) + + def _set_rect_attr (self, attr, value): + """R._set_rect_attr (...) -> None + + Sets a specific attribute value on the underlying rect. + + Raises an AttributeError if the attr argument is the width, + height or size. + """ + if attr in ("width", "height", "size"): + # The width and height are protected! + raise AttributeError ("%s attribute is read-only" % attr) + setattr (self._rect, attr, value) + + def initclass (cls): + """R.initclass () -> None + + Class method to expose the attributes of the own self.rect attribute. + + The method usually is called in the __init__.py script of the + module. + """ + attributes = dir (Rect) + for attr in attributes: + if not attr.startswith ("__") and \ + not callable (getattr (Rect, attr)): + def get_attr (self, attr=attr): + return cls._get_rect_attr (self, attr) + def set_attr (self, value, attr=attr): + return cls._set_rect_attr (self, attr, value) + prop = property (get_attr, set_attr) + setattr (cls, attr, prop) + initclass = classmethod (initclass) + def set_title (self, title): """R.set_title (...) -> None @@ -415,19 +481,23 @@ self._title = title display.set_caption (self._title) - def set_screen (self, screen): + def set_screen (self, screen, x=0, y=0): """R.set_screen (...) -> None Sets the screen to use for the Renderer and its widgets. Sets the screen surface, the renderer will draw the widgets on - (usually, you want this to be the entire screen). + (usually, you want this to be the entire screen). The optional + x and y arguments specify, where the screen will be blit on the + pygame display (this is important for event handling). Raises a TypeError, if the passed argument does not inherit from pygame.Surface. """ if screen and not isinstance (screen, Surface): raise TypeError ("screen must inherit from Surface") + self._rect = screen.get_rect () + self.topleft = x, y self._screen = screen self._create_bg () @@ -620,7 +690,7 @@ Removes one or more widgets from the Renderer. - Removes one or more widgets from the event and indexing system of the + Removes one or more widgets from the event and indexing systen of the Renderer class. Raises a TypeError, if one of the passed arguments does not @@ -645,10 +715,9 @@ rects.append (widget.rect) widget.manager = None - self.remove_index (*widgets) - # Clean up + # Clean up. if cleanup: self._redraw_widgets (True, *rects) @@ -1061,9 +1130,11 @@ """ resize = self.support_resize ismod = self._is_mod + pos = None for event in events: ev = None + pos = None etype = event.type if etype == QUIT: return False @@ -1072,12 +1143,36 @@ self.create_screen (event.w, event.h, self.__flags) elif etype == MOUSEMOTION: + pos = event.pos + # Check, whether the mouse is located within the assigned + # area. + if (pos[0] < self.left) or (pos[0] > self.right) or \ + (pos[1] < self.top) or (pos[1] > self.bottom): + continue + event.dict["pos"] = event.pos[0] - self.x, \ + event.pos[1] - self.y ev = Event (SIG_MOUSEMOVE, event) elif etype == MOUSEBUTTONDOWN: + pos = event.pos + # Check, whether the mouse is located within the assigned + # area. + if (pos[0] < self.left) or (pos[0] > self.right) or \ + (pos[1] < self.top) or (pos[1] > self.bottom): + continue + event.dict["pos"] = event.pos[0] - self.x, \ + event.pos[1] - self.y ev = Event (SIG_MOUSEDOWN, event) elif etype == MOUSEBUTTONUP: + pos = event.pos + # Check, whether the mouse is located within the assigned + # area. + if (pos[0] < self.left) or (pos[0] > self.right) or \ + (pos[1] < self.top) or (pos[1] > self.bottom): + continue + event.dict["pos"] = event.pos[0] - self.x, \ + event.pos[1] - self.y self._check_doubleclick (event) ev = Event (SIG_MOUSEUP, event) @@ -1118,6 +1213,10 @@ layer[2].emit_event (ev) else: self._grabber.emit_event (ev) + + # Restore the original position. + if pos != None: + event.dict["pos"] = pos return True def _loop (self): @@ -1173,3 +1272,5 @@ managers = property (lambda self: self.get_managers (), doc = "Tuple containing the available " \ "EventManagers for the different layers.") + rect = property (lambda self: self._get_rect (), + doc = "The area occupied by the Renderer.") Index: __init__.py =================================================================== RCS file: /cvsroot/ocemp/ocempgui/ocempgui/widgets/__init__.py,v retrieving revision 1.39.2.3 retrieving revision 1.39.2.4 diff -u -d -r1.39.2.3 -r1.39.2.4 --- __init__.py 19 Oct 2006 18:52:40 -0000 1.39.2.3 +++ __init__.py 15 Nov 2006 12:27:26 -0000 1.39.2.4 @@ -36,7 +36,7 @@ import Constants import StyleInformation -from Style import Style, WidgetStyle +from Style import WidgetStyle, Style # Basic widgets. from BaseWidget import BaseWidget @@ -77,6 +77,7 @@ from Diagram import Diagram from Graph2D import Graph2D -# Event and render system. +# Event and rendering system. from Renderer import Renderer +Renderer.initclass () from TwistedRenderer import TwistedRenderer |