Beispiel #1
0
    def __init__(self,
                 window,
                 x,
                 y,
                 width=Constants.DEFAULT_WIDTH,
                 height=Constants.DEFAULT_HEIGHT,
                 fillColor=None,
                 borderColor=(0, 0, 0),
                 borderWidth=1,
                 text=None,
                 font=None,
                 textColor=(0, 0, 0),
                 padding=0):
        Component.__init__(self,
                           window,
                           x,
                           y,
                           width=width,
                           height=height,
                           fillColor=fillColor,
                           borderColor=borderColor,
                           borderWidth=borderWidth)
        Clickable.__init__(self, x, y, width=width, height=height)
        self.type = "Button"
        self.font = font
        self.textColor = textColor
        self.label = None
        self.padding = padding
        self.text = text

        if text is not None:
            self.initText()
Beispiel #2
0
    def _draw(self, screen: Surface, changes: List[Rect]) -> None:
        """Draw the slider's visuals to the screen. <change> is a list of
        rectangles that represent the changed areas of the screen.
        """
        Component._draw(self, screen, changes)

        # Fill Background
        changes.append(screen.fill(self._style.primary_color, self._fill_rect))

        filled_rect = self._fill_rect.copy()
        filled_rect.width = filled_rect.width * \
            (self._value - self._min_value) / \
            (self._max_value - self._min_value)

        # Fill Foreground
        changes.append(screen.fill(self._style.secondary_color, filled_rect))

        if (self._show_handle):
            # Handle Fill
            changes.append(
                draw.circle(screen, self._style.tertiary_color,
                            filled_rect.midright, self._handle_radius - 1))

            # Handle Outline
            if (self._style.border_width > 0
                    and self._style.border_color is not None):

                changes.append(
                    draw.circle(screen, self._style.border_color,
                                filled_rect.midright, self._handle_radius,
                                self._style.border_width))
Beispiel #3
0
    def __init__(self,
                 rect: Rect,
                 on_value_changed: Callable[[float], None] = None,
                 value: float = 0,
                 min_value: float = 0,
                 max_value: float = 1,
                 show_handle: bool = True,
                 style: Style = None,
                 parent: Component = None) -> None:
        """Create a new Button. <rect> is the rectangle representing this
        component's position and size on the screen. <on_value_changed> is the
        function called when the slider's value is changed. <value> is the
        starting value of the slider. <min_value> is the smallest number the
        slider can have. <max_value> is the largest number the slider can have.
        <show_handle> is whether or not the handle should be displayed. <style>
        dictates the appearence of the component. If None, the default style
        will be used. <parent> is the component that is
        immediately above this component in the tree.
        """
        self._show_handle = show_handle
        Component.__init__(self, rect, style=style, parent=parent)

        self._value = value
        self._min_value = min_value
        self._max_value = max_value
        self._on_value_changed = on_value_changed
Beispiel #4
0
 def __init__(self, colour):
     Component.__init__(self, 2, 4, 7, colour)
     self.__pixels = [
         [2, 7],
         [3, 7],
         [4, 7]
     ]
Beispiel #5
0
    def set_rect(self, rect: Rect) -> None:
        """Set the slider's rectangle and update the useful area."""
        Component.set_rect(self, rect)
        self._fill_rect = rect.inflate(-2 * self._style.border_width,
                                       -2 * self._style.border_width)

        if (self._show_handle):
            self._handle_radius = self._fill_rect.height // 2
            self._fill_rect.inflate_ip(-2 * self._handle_radius,
                                       -self._handle_radius)
Beispiel #6
0
    def _draw(self, screen: Surface, changes: List[Rect]) -> None:
        """Draw the current animation frame to the screen. <changes> is a list
        of rectangles that represent the changed areas of the screen.
        """
        Component._draw(self, screen, changes)

        if (len(self._animation_frames) == 0):
            return

        changes.append(
            screen.blit(self._animation_frames[self._frame_index], self._rect))
Beispiel #7
0
 def __init__(self,
              rect: Rect,
              text: str = "",
              style: Style = None,
              parent: Component = None) -> None:
     """Create a new Label. <rect> is the rectangle representing this
     component's position and size on the screen. <text> is the text to be
     displayed. <style> dictates the appearence of the component. If None,
     the default style will be used. <parent> is the component that is
     immediately above this component in the tree.
     """
     Component.__init__(self, rect, style=style, parent=parent)
     self.set_text(text)
Beispiel #8
0
    def _draw(self, screen: Surface, changes: List[Rect]) -> None:
        """Draw the button's visuals to the screen. <change> is a list of
        rectangles that represent the changed areas of the screen.
        """
        Component._draw(self, screen, changes)

        # Draw hover color
        if (self.is_hovered()):
            changes.append(
                screen.fill(self._style.hover_color, self._rect,
                            BLEND_RGB_MULT))

        self._draw_text(screen, changes)
Beispiel #9
0
    def update(self, dt: float):
        """Update the frame index. <dt> is the time since last update in
        milliseconds.
        """
        Component.update(self, dt)

        if (len(self._animation_frames) == 0):
            return

        self._time_since_last_frame += dt
        if (self._time_since_last_frame > self._time_per_frame):
            self._time_since_last_frame -= self._time_per_frame
            self._frame_index = (self._frame_index + 1) % len(
                self._animation_frames)
            self._redraw()
Beispiel #10
0
    def _hit_test(self, pos: Tuple[int, int],
                  component: Component) -> Component:
        """Get which component is at postion <pos> starting from <component>"""

        children = component.get_children()
        for i in range(len(children) - 1, -1, -1):

            hit = self._hit_test(pos, children[i])
            if (hit is not None):
                return hit

        if (component.get_rect().collidepoint(pos) and component.is_enabled()):
            return component

        return None
Beispiel #11
0
    def reload(self):
        """
        Reloads components status snapshot from disk.
        """
        self._components = self._persistance.load()
        for configuration in self._configuration.itervalues():
            if not configuration.uid in self._components:
                self._components[
                    configuration.uid] = Component.create_instance(
                        typeid=configuration.typeid,
                        uid=configuration.uid,
                        configuration=configuration)
            else:
                self._components[
                    configuration.uid].configuration = configuration

        self._detached = list()
        for uid in self._components:
            if not uid in self._configuration:
                if not self._components[uid].is_alive:
                    self._persistance.delete_status(uid)
                else:
                    self._components[uid] = DetachedComponent(
                        **self._components[uid].__dict__)
                    self._detached.append(uid)
Beispiel #12
0
 def reload(self):
     """
     Reloads components status snapshot from disk.
     """
     self._components = self._persistance.load()
     for configuration in self._configuration.itervalues():
         if not configuration.uid in self._components:
             self._components[configuration.uid] = Component.create_instance(typeid = configuration.typeid,
                                                                             uid = configuration.uid,
                                                                             configuration = configuration)
         else:
             self._components[configuration.uid].configuration = configuration
Beispiel #13
0
    def load(self):
        """Loads components status data from the status file"""
        components = dict()

        c = self.__conn.cursor()
        c.execute(self.__SELECT_STATUS__)
        for row in c.fetchall():
            args = dict(**row)
            args["status_persistance"] = self
            component = Component.create_instance(**args)
            components[component.uid] = component

        return components
Beispiel #14
0
 def __init__(self,
              rect: Rect,
              expand_width: bool = True,
              expand_height: bool = True,
              space_around: bool = False,
              style: Style = None,
              parent: Component = None) -> None:
     """Create a new VerticalPanel. <rect> is the rectangle representing
     this component's position and size on the screen. <expand_width>
     decides whether or not to change the children's width to fill this
     component's rect. <expand_height> decides whether or not to change
     the children's height to fill this component's rect. <space_around>
     decides whether or not to add spacing on the top and bottom side of
     this component's children. The children of this component will be
     centered. <style> dictates the appearence of the component. If None,
     the default style will be used. <parent> is the component that is
     immediately above this component in the tree.
     """
     self._expand_width = expand_width
     self._expand_height = expand_height
     self._space_around = space_around
     Component.__init__(self, rect, style=style, parent=parent)
Beispiel #15
0
    def load(self):
        """Loads components status data from the status file"""
        components = dict()

        c = self.__conn.cursor()
        c.execute(self.__SELECT_STATUS__)
        for row in c.fetchall():
            args = dict(**row)
            args["status_persistance"] = self
            component = Component.create_instance(**args)
            components[component.uid] = component

        return components
Beispiel #16
0
    def _draw(self, screen: pygame.Surface,
              changes: List[pygame.Rect]) -> None:
        """Draw the textbox's visuals to the screen. <change> is a list of
        rectangles that represent the changed areas of the screen.
        """
        Component._draw(self, screen, changes)

        text_rect = self._text_image.get_rect()
        text_rect.center = self._rect.center

        # Draw Selection Background
        if (self._has_multi_char_selection()):
            start = self._style.font.size(self.get_text()[:self._cursor_pos])
            end = self._style.font.size(
                self.get_text()[:self._selection_end_pos])

            start_x = min(start[0], end[0])
            end_x = max(start[0], end[0])

            background_rect = pygame.Rect(text_rect.x + start_x, text_rect.y,
                                          end_x - start_x, text_rect.height)
            changes.append(screen.fill((0, 0, 255), background_rect))
            Label._draw_text(self, screen, changes)
            return

        # Draw text
        Label._draw_text(self, screen, changes)

        # Draw Blinker
        if (self._is_blinker_on):
            size = self._style.font.size(self.get_text()[:self._cursor_pos])
            cursor_rect = pygame.Rect(text_rect.x + size[0], text_rect.y, 2,
                                      text_rect.height)

            if (self._rect.x < cursor_rect.x < self._rect.right):
                changes.append(
                    screen.fill(self._style.primary_color, cursor_rect))
Beispiel #17
0
    def __init__(self,
                 rect: Rect,
                 folder_path: str = "",
                 animation_duration: float = 1000,
                 style: Style = None,
                 parent: Component = None) -> None:
        """Create a new AnimatedImage. <rect> is the rectangle representing
        this component's position and size on the screen. <folder_path> is
        the path to the folder containing the frames of the animation to be
        displayed. <animation_duration> is the length of the animation in
        miliseconds. <style> dictates the appearence of the component. If None,
        the default style will be used. <parent> is the component that is
        immediately above this component in the tree.
        """
        self._animation_frames = []
        self._raw_frames = []

        self._frame_index = 0
        self._time_since_last_frame = 0

        self._animation_duration = animation_duration
        self._load_animation(folder_path)

        Component.__init__(self, rect, style=style, parent=parent)
Beispiel #18
0
 def add_child(self, child: Component):
     """Add a component as a child. Does nothing if <child>
     is already a child component.
     """
     Component.add_child(self, child)
     self._layout_children()
Beispiel #19
0
def test_init():
    c = Component()
    assert c.parent is None
Beispiel #20
0
def test_engine__no_parent():
    c = Component()
    assert c.engine is None
Beispiel #21
0
def test_gamemap__no_parent():
    c = Component()
    assert c.gamemap is None
Beispiel #22
0
 def __init__(self, colour):
     Component.__init__(self, 3, 3, 6, colour)
     self.speed = 0
     self.__pixels = [
         [3, 6]
     ]
Beispiel #23
0
 def _draw(self, screen: Surface, changes: List[Rect]) -> None:
     """Draw the label's background and text to the <screen>. <change> is a
     list of rectangles that represent the changed areas of the screen.
     """
     Component._draw(self, screen, changes)
     self._draw_text(screen, changes)
Beispiel #24
0
 def set_rect(self, rect: Rect):
     """Set the animated image's rectangle and generate the animation's
     frames.
     """
     Component.set_rect(self, rect)
     self._generate_animation_frames()
Beispiel #25
0
 def set_rect(self, child_rect: Rect):
     """Set the component's rectangle."""
     Component.set_rect(self, child_rect)
     self._layout_children()