Example #1
0
 def resize(self):
     if not self.fixed_min_size:
         self.min_size = Vect.merge(*[c.min_size for c in self.children])
     if not self.fixed_optimal_size:
         self.optimal_size = Vect.merge(
             self.min_size,
             *[c.optimal_size for c in self.children])
Example #2
0
 def __set_text(self, value):
     # assert isinstance(value, (str, unicode))
     self.__text = unicode(value)
     # XXX: We need to recompute the optimal size !!!
     self.surface = None # TODO: remove that
     if not self.fixed_optimal_size:
         self.optimal_size = Vect(32 * len(value), 40)
         self.min_size = Vect(0, 40)
     self.need_redraw(self.rect)
Example #3
0
 def __init__(self, size, fullscreen=False):
     Painter.__init__(self, Vect(0, 0), Rect((0, 0), size))
     pygame.display.init()
     pygame.font.init()
     flags = pygame.RESIZABLE
     self.surface = pygame.display.set_mode(size, flags)
     pygame.display.set_caption("Tichy")
Example #4
0
 def to_surface(self, surface):
     """Return a engine similar to this one but
     drawing into a given surface"""
     ret = SdlPainter.__new__(SdlPainter)
     ret.pos = Vect(0, 0)
     ret.mask = Rect((0, 0), surface.get_size())
     ret.surface = surface
     return ret
Example #5
0
 def organize(self):
     if not self.children:
         return
     axis = self.axis
     cell_size = self.cell_min_size()
     for i, c in enumerate(self.children):
         c.pos = Vect(0, 0).set(
             axis, i / self.nb * (cell_size[axis] + self.spacing)).set(
             axis - 1, i % self.nb * (cell_size[axis - 1] + self.spacing))
         c.size = cell_size
Example #6
0
 def __init__(self, events_source, painter, **kargs):
     self.redraw_rect = None
     super(Screen, self).__init__(None,
                                  events_source=events_source,
                                  modal=False,
                                  **kargs)
     self.size = Vect(480, 640)  # TODO find a better way
     self.painter = painter
     self.redraw_rect = self.rect
     self.monitor(events_source, 'tick', self.on_tick)
Example #7
0
    def organize(self):
        axis = self.axis
        # First we put all the children in there minimum size
        for c in self.children:
            c.size = c.min_size.set(axis - 1, self.contents_size[axis - 1])
            assert c.size[axis] >= c.min_size[axis]

        # We grow the children as much as we can
        length = self.contents_size[axis]
        while True:
            children_need = [
                c for c in self.children if c.optimal_size[axis] > c.size[axis]
            ]
            if not children_need:
                break

            tot = sum((c.size[axis] + self.spacing \
                           for c in self.children)) - self.spacing
            free = max(0, length - tot)

            free_per_child = free / len(children_need)
            for c in children_need:
                if c.optimal_size[axis] - c.size[axis] < free_per_child:
                    c.size = c.size.set(axis, c.optimal_size[axis])
                    break
            else:
                break
        for c in children_need:
            c.size = c.size.set(axis, c.size[axis] + free_per_child)

        # If we still have some free space, we grow the expand children
        tot = sum((c.size[axis] + self.spacing for c in self.children)) - \
            self.spacing
        free = length - tot
        if free > 0:
            nb_expand = sum(1 for c in self.children if c.expand)
            if nb_expand:
                for c in self.children:
                    if not c.expand:
                        continue
                    given = free / nb_expand
                    assert given >= 0
                    c.size = c.size.set(axis, c.size[axis] + given)

        # Finally we also set the positions
        pos = self.contents_pos
        for c in self.children:
            assert c.size[axis] >= c.min_size[axis], \
                (c, c.size[axis], c.min_size[axis])
            c.pos = pos
            pos += Vect(c.size[0] + self.spacing if axis == 0 else 0,
                        c.size[1] + self.spacing if axis == 1 else 0)
Example #8
0
    def draw_frame(self, frame, size):
        """Draw a frame. This function is in fact currently very slow !"""
        size = asvect(size)

        def indexes():
            width, height = size
            # The corners
            yield (0, 0), (0, 0)
            yield (width - 8, 0), (24, 0)
            yield (0, height - 8), (0, 24)
            yield (width - 8, height - 8), (24, 24)

            mwidth = width / 2
            mheight = height / 2

            # The borders
            for i in range(1, mwidth / 8):
                yield (i * 8, 0), (8, 0)
                yield (i * 8, height - 8), (8, 24)
            for i in range(mwidth / 8, width / 8 - 1):
                yield (i * 8, 0), (16, 0)
                yield (i * 8, height - 8), (16, 24)

            for i in range(1, mheight / 8):
                yield (0, i * 8), (0, 8)
                yield (width - 8, i * 8), (24, 8)
            for i in range(mheight / 8, height / 8 - 1):
                yield (0, i * 8), (0, 16)
                yield (width - 8, i * 8), (24, 16)

        frame.image.load(self)
        surf = frame.image.surf
        for dest, src in indexes():
            self.move(Vect(*dest))
            self.draw_surface(surf, Rect(asvect(src), Vect(8, 8)))
            self.umove(Vect(*dest))

        c1 = surf.get_at((16, 15))
        half_size = Vect(size[0] - 16, size[1] / 2 - 8)
        self.move(Vect(8, 8))
        self.fill(c1, half_size)
        self.umove(Vect(8, 8))

        c2 = surf.get_at((16, 16))
        self.move(Vect(8, half_size[1] + 8))
        self.fill(c2, half_size)
        self.umove(Vect(8, half_size[1] + 8))
Example #9
0
    def resize(self):
        axis = self.axis
        optimal_size = Vect(0, 0)
        min_size = Vect(0, 0)
        if self.children:
            min_size = min_size.set(
                axis, sum(c.min_size[axis] for c in self.children))
            min_size = min_size.set(
                axis - 1, max(c.min_size[axis - 1] for c in self.children))

            optimal_size = optimal_size.set(
                axis, sum(c.optimal_size[axis] + self.spacing \
                              for c in self.children) - self.spacing)
            optimal_size = optimal_size.set(
                axis - 1, max(c.optimal_size[axis - 1] \
                                  for c in self.children))
            optimal_size += Vect(self.border, self.border) * 2

        self.min_size = min_size
        self.optimal_size = optimal_size
Example #10
0
    def resize(self):
        if not self.children:
            return
        axis = self.axis
        cell_optimal_size = self.cell_optimal_size()
        cell_min_size = self.cell_min_size()

        nb_per_line = self.nb

        # Quite ugly algorithm to compute the number of lines
        # I do like this cause I want to avoid importing math
        nb_lines = len(self.children) / nb_per_line
        if len(self.children) % nb_per_line != 0:
            nb_lines += 1

#         self.optimal_size = Vect(nb_per_line * cell_optimal_size[0],
#                                  nb_lines * cell_optimal_size[1])
        self.min_size = Vect(
            nb_per_line * (cell_min_size[0] + self.spacing) - self.spacing,
            nb_lines * (cell_min_size[1] + self.spacing) - self.spacing)

        self.optimal_size = self.min_size
Example #11
0
    def __init__(self, parent, style=None, optimal_size=None,
                 min_size=None, expand=False, item=None,
                 same_as=None, tags=[], pos=None, **kargs):
        """Create a new Widget

        parameters:

        - parent The parent widget where we put this widget

        - style The style associated with this widget The style is not
          compulsory, it is only something that can be used by the
          Design

        - optimal_size The size requested for the widget

        - min_size The minimum size requested for the widget

        - expand If true the widget will try to take all the place it
          can

        - item What is the item associated with this widget (None if
          not) This is only used for the style rules

        - same_as This can be used to pass an other widget instance
          that we know has the same style than this one It is only
          used for optimazation when we want to show a huge number of
          similar widgets

        - tags A list of string, Can be used by the style rules
        """
        super(Widget, self).__init__()
        self.children = []
        self.item = item    # Set to None if the object is not a view
                            # on an item
        parent = parent.get_contents_child() if parent else None
        assert isinstance(parent, Widget) or parent is None
        self.parent = parent

        self.style_dict = {}

        self.tags = set(tags)

        self.fixed_optimal_size = optimal_size is not None

        self.__optimal_size = optimal_size or Vect(0, 0)
        self.__min_size = min_size
        self.expand = expand

        self.__organized = False
        self.__resized = False

        self.__destroyed = False

        self.rect = Rect((0, 0), min_size or Vect(0, 0))
        self.__pos = pos or Vect(0, 0)
        if same_as is None:
            self.style = style
        else:
            self.__style = same_as.__style
            self.style_dict = same_as.style_dict

        self.fixed_min_size = min_size is not None or \
            'min-size' in self.style_dict

        self.focused = None
        self.clickable = False
        self.surface = None     # This is used for the widget that
                                # keep a copy of there surface for
                                # optimisation
        self.store_surface = False   # Set to true for the widget to
                                     # keep a memory of it own surface

        if parent:
            parent.add(self)
Example #12
0
 def __get_contents_rect(self):
     border = self.border
     return Rect(self.rect.pos + Vect(border, border),
                 self.rect.size - Vect(border, border) * 2)
Example #13
0
 def cell_min_size(self):
     return Vect(max(w.min_size[0] for w in self.children),
                 max(w.min_size[1] for w in self.children))
Example #14
0
 def cell_optimal_size(self):
     return Vect(max(w.optimal_size[0] for w in self.children),
                 max(w.optimal_size[1] for w in self.children))
Example #15
0
 def __init__(self, pos=None, mask=None):
     self.pos = pos or Vect(0, 0)
     self.mask = mask
Example #16
0
 def __get_min_size(self):
     return self.__min_size or self.style_dict.get('min-size', Vect(0, 0))
Example #17
0
 def draw_label(self, w):
     surf = self.surface_from_text(w.font, w.text, length=w.size.x)
     self.move(Vect(8, 8))
     self.draw_surface(surf)
     self.umove(Vect(8, 8))