def __init__(self, x=0, y=0, w=10, h=10): super(UserInterfaceItem, self).__init__() self.max_children = 0 # Used in ordered group self.rectangle = AABox(x, y, w, h) self.uigroup = TranslationGroup(self) self.batch = None self.vertex_list = None self.layout = Layout(self)
class UserInterfaceItem(GroupItem, MouseItem, metaclass=DerivedClassRegistery): ''' All aspects of a user interface must inherit from the UserInterfaceBase class. It handles grouping of UserInterface items, as well as their events and displays. ''' def __init__(self, x=0, y=0, w=10, h=10): super(UserInterfaceItem, self).__init__() self.max_children = 0 # Used in ordered group self.rectangle = AABox(x, y, w, h) self.uigroup = TranslationGroup(self) self.batch = None self.vertex_list = None self.layout = Layout(self) # PROPERTIES @property def position(self): return self.rectangle.position @position.setter def position(self, other): self.rectangle.position = other self.on_resize() @property def size(self): return self.rectangle.size @size.setter def size(self, other): self.rectangle.size = other self.on_resize() @property def absolute_position(self): ''' Sometimes, calculations must be done based on the absolute location of a rectangle, instead of the relative location used internally for rendering. This calculates it recursively. return - Vector giving the absolute position of the rectangle ''' if self.parent: return self.parent.absolute_position + self.position else: return self.position # Mouse Propagation def register_mouse_pressed(self, *params): self.recursive_child_operation('on_mouse_pressed', *params) def register_mouse_release(self, *params): self.recursive_child_operation('on_mouse_released', *params) def register_mouse_move(self, *params): self.recursive_child_operation('on_mouse_move', *params) def get_batch(self): ''' Recursively looks up the parent tree until a parent has a batch. It is assumed that the UserInterfaceRoot is the root of the tree. ''' return self.root_parent.batch def set_vertex_list(self): ''' Called at the end of creation of the UserInterface item. Use the get_batch method to grab the patch to attach the vertex_list to. ''' if self.vertex_list: self.vertex_list.delete() self.fill_vertex_list(self.get_batch()) def on_added_as_child(self): ''' This method is required in order to establish the appropriate pyglet groups in the appropriate order. This does not safegaurd against children loops Creates an OrderedGroup for the child, then sets that as the parent for the child's uigroup. ''' order = pyglet.graphics.OrderedGroup( self.parent.max_children, parent=self.parent.uigroup ) self.parent.max_children += 1 self.uigroup.parent = order self.set_vertex_list() def on_inflate_complete(self, creator, window): ''' When inflating from an XML window, it is sometimes necessary, such as when inflating a Root item, that more information is necessary than can be relayed through the default constructor. This method is called after the inflation process is complete. ''' self.layout.apply() def get_item_by_name(self, name): if 'name' in self.__dict__ and self.name == name: return self for child in self.children: item = child.get_item_by_name(name) if item is not None: return item else: # If no children have an item by that name return None def register_window_change(self, window): self.recursive_child_operation('on_window_resize', window) def on_window_resize(self, window): self.layout.apply() # DUMMY METHODS FOR OVERRIDING def fill_vertex_list(self, batch): pass def on_resize(self): pass