tuikit/driver.py
author Radek Brich <radek.brich@devl.cz>
Wed, 30 Jan 2013 19:44:01 +0100
changeset 72 6e0656600754
parent 41 37b7dfc3eae6
child 77 fc1989059e19
permissions -rw-r--r--
DriverSDL: Fix event dispatching. Add SDL events test.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
'''Tuikit driver base.'''
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
from tuikit.common import Size, ClipStack, UnicodeGraphics
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
class Driver:
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
     8
24
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
    '''Abstract driver interface. Use as base for drivers.'''
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    10
24
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
    def __init__(self):
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        '''Initialize instance attributes.'''
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    13
        #: Screen size.
24
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        self.size = Size()
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    15
        #: Clipping region stack.
24
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
        self.clipstack = ClipStack()
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    17
        #: Unicode graphics characters.
24
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        self.unigraph = UnicodeGraphics()
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    19
        #: Stack of color prefixes.
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    20
        self.colorprefix = []
24
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    23
    ## drawing ##
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    24
24
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
    def puts(self, x, y, s):
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
        '''Output string of characters.'''
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
        for c in s:
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
            self.putch(x, y, c)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
            x += 1
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
    def hline(self, x, y, w, c=' '):
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
        '''Draw horizontal line.'''
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
        if isinstance(c, str):
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
            s = c*w
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
        else:
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
            s = [c]*w
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
        self.puts(x, y, s)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
    def vline(self, x, y, h, c=' '):
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        '''Draw vertical line.'''
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
        for i in range(h):
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
            self.putch(x, y+i, c)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
    def fill(self, x, y, w, h, c=' '):
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
        '''Fill rectangular area.'''
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
        for i in range(h):
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
            self.hline(x, y + i, w, c)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
    def frame(self, x, y, w, h):
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
        '''Draw rectangular frame using line-drawing characters.'''
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
        self.putch(x, y, self.unigraph.ULCORNER)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
        self.putch(x+w-1, y, self.unigraph.URCORNER)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        self.putch(x, y+h-1, self.unigraph.LLCORNER)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
        self.putch(x+w-1, y+h-1, self.unigraph.LRCORNER)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
        self.hline(x+1, y, w-2, self.unigraph.HLINE)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
        self.hline(x+1, y+h-1, w-2, self.unigraph.HLINE)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
        self.vline(x, y+1, h-2, self.unigraph.VLINE)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
        self.vline(x+w-1, y+1, h-2, self.unigraph.VLINE)
b248ef500557 Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    60
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    61
    ## colors ##
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    62
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    63
    def pushcolorprefix(self, name):
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    64
        self.colorprefix.append(name)
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    65
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    66
    def popcolorprefix(self):
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    67
        self.colorprefix.pop()
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    68