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")
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
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))
def test1(): from image import Image from painter import Painter from geo import Vect, Rect from style import Style, Frame from sdl_painter import SdlPainter im = Image('frame.png') f = Frame(im) style = Style() style.background = f p = Painter(SdlPainter()) l = Label(None, 'hello', style=style) l.rect = Rect((0, 0), (1, 1)) l.draw(p) p.flip() raw_input()
def mouse_motion(self, pos): if (pos in Rect((0, 0), self.size)) != self.pressed: self.pressed = pos in self.rect return super(Button, self).mouse_motion(pos)
def set_mask(self, rect): self.mask = Rect(rect[0], rect[1])
class Painter(object): """This class is used to draw all the widgets The idea is to subclass it when we want special drawing style. """ def __init__(self, pos=None, mask=None): self.pos = pos or Vect(0, 0) self.mask = mask def draw_widget(self, w): style = w.get_style_dict() background = style.get('background') if background: background.draw(self, w.size) if isinstance(w, Label): self.draw_label(w) 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)) def draw(self, o): if isinstance(o, Widget): return self.draw_widget(o) def set_mask(self, rect): self.mask = Rect(rect[0], rect[1]) def move(self, v): self.pos = self.pos + v self.mask = self.mask.move(-v) def umove(self, v): self.move(-v) def clip(self, r): self.mask = self.mask.clip(r) def to_surface(self, surface): """Return a engine similar to this one but drawing into a given surface """ raise NotImplementedError def surface_from_size(self, size): """Create a new surface""" raise NotImplementedError def surface_from_image(self, path): """Create a new surface""" raise NotImplementedError def surface_from_text(self, font, text): """Create a new surface from a text""" raise NotImplementedError def font_from_file(self, file, size=24): raise NotImplementedError def draw_surface(self, surf, area=None): raise NotImplementedError def fill(self, color, size=None): raise NotImplementedError 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)) def flip(self, rect=None): raise NotImplementedError
def __get_contents_rect(self): border = self.border return Rect(self.rect.pos + Vect(border, border), self.rect.size - Vect(border, border) * 2)
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)
def __set_size(self, value): if value == self.size: return self.rect = Rect(self.rect.pos, value) self.organized = False