tuikit/driver_pygame.py
author Radek Brich <radek.brich@devl.cz>
Sat, 05 Jan 2013 00:37:11 +0100
changeset 46 2b43a7f38c34
parent 41 37b7dfc3eae6
permissions -rw-r--r--
Minor updates. Replace super() with direct class reference. Add demo_colors.
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
'''PyGame driver.'''
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
import pygame
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
import logging
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
from tuikit.driver import 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: 31
diff changeset
     8
from tuikit.common import Coords, Size
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
     9
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    10
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    11
class TerminalScreen:
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: 31
diff changeset
    12
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    13
    '''Provide character-level output to screen SDL surface.
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: 31
diff changeset
    14
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    15
    This is performance bottleneck and should be optimized as much as possible.
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: 31
diff changeset
    16
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    17
    '''
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: 31
diff changeset
    18
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    19
    def __init__(self):
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    20
        fontselect = 'dejavusansmono,liberationmono,freemono'
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    21
        self.font = pygame.font.SysFont(fontselect, 14)
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    22
        self.font_bold = pygame.font.SysFont(fontselect, 14, True)
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    23
        # get advance of some random char (all should be same in monospace font)
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    24
        advance = self.font.metrics('Q')[0][4]
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    25
        height = self.font.get_height()
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    26
        self.charsize = Size(advance, height)
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    27
        self.ascent = self.font.get_ascent()
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: 31
diff changeset
    28
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    29
        # choose self.render() implementation
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    30
        if hasattr(self.font, 'render_glyph'):
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    31
            self.render_char = self.render_glyph
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    32
        else:
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    33
            self.render_char = self.render_noglyph
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: 31
diff changeset
    34
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    35
        self.chars = None
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    36
        self.attrs = None
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    37
        self.default_attr = None
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    38
        self.current_attr = None
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    39
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    40
    def set_default_attr(self, fg, bg, flags):
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    41
        self.default_attr = (fg, bg, flags)
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: 31
diff changeset
    42
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    43
    def set_attr(self, fg, bg, flags):
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    44
        self.current_attr = (fg, bg, flags)
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: 31
diff changeset
    45
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    46
    def reset_attr(self):
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    47
        self.current_attr = self.default_attr
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    48
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    49
    def reset(self, w, h):
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    50
        self.w, self.h = w, h
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    51
        numchars = w * h
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    52
        self.chars = [' '] * numchars
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    53
        self.attrs = [self.default_attr] * numchars
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: 31
diff changeset
    54
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    55
    def clear(self):
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    56
        numchars = self.w * self.h
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    57
        for pos in range(numchars):
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    58
            self.chars[pos] = ' '
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    59
            self.attrs[pos] = self.default_attr
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: 31
diff changeset
    60
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    61
    def putch(self, x, y, c):
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    62
        pos = y * self.w + x
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    63
        self.chars[pos] = c
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    64
        self.attrs[pos] = self.current_attr
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: 31
diff changeset
    65
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    66
    def update(self, surface):
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    67
        pos = 0
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    68
        for y in range(self.h):
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    69
            for x in range(self.w):
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    70
                fgcolor, bgcolor, flags = self.attrs[pos]
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    71
                c = self.chars[pos]
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    72
                self.render_char(surface, x, y, c,
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    73
                    fgcolor, bgcolor, flags)
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    74
                pos += 1
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    75
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    76
    def render_glyph(self, screen, x, y, c, fgcolor, bgcolor, flags):
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    77
        '''Render using render_glyph and metrics.
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: 31
diff changeset
    78
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    79
        This is the correct way, but the output seems same as of render_noglyph
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    80
        and this implementation requires patching PyGame to work.
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: 31
diff changeset
    81
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    82
        This implements render() method. See render_noglyph for other implementation.
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: 31
diff changeset
    83
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: 31
diff changeset
    84
        '''
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    85
        # draw background
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    86
        dest = Coords(x * self.charsize.w, y * self.charsize.h)
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    87
        if bgcolor != self.default_attr[1]:
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    88
            screen.fill(bgcolor, pygame.Rect(dest.x, dest.y, self.charsize.w, self.charsize.h))
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: 31
diff changeset
    89
46
2b43a7f38c34 Minor updates. Replace super() with direct class reference. Add demo_colors.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    90
        if c == ' ':
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    91
            return
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    92
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    93
        # choose font
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
    94
        if flags == 1:
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    95
            font = self.font_bold
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    96
        else:
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    97
            font = self.font
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
    98
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    99
        # render character, get metrics
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   100
        surface = font.render_glyph(c, True, fgcolor, bgcolor)
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   101
        metrics = font.metrics(c)[0]
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   102
        minx, maxx, miny, maxy, advance = metrics
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   103
        height, ascent = self.charsize.h, self.ascent
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: 31
diff changeset
   104
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   105
        # clips origin and area of rendered character according to metrics
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   106
        startx, starty = 0, 0
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   107
        if minx < 0:
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   108
            startx = abs(minx)
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   109
            minx = 0
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   110
        if maxy > ascent:
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   111
            starty = maxy - ascent
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   112
            maxy -= starty
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   113
        if ascent - miny > height:
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   114
            miny = ascent - height
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   115
        if maxx > advance:
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   116
            maxx = advance
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: 31
diff changeset
   117
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   118
        # draw character
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   119
        dest.x += minx
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   120
        dest.y += ascent - maxy
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   121
        area = pygame.Rect(startx, starty, maxx - minx, maxy - miny)
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   122
        screen.blit(surface, tuple(dest), area)
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: 31
diff changeset
   123
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   124
    def render_noglyph(self, screen, x, y, c, fgcolor, bgcolor, attr):
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   125
        '''Render character using normal text rendering.
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: 31
diff changeset
   126
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   127
        This implements render() method. See render_glyph for other implementation.
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: 31
diff changeset
   128
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   129
        '''
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   130
        if attr == 'bold':
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   131
            font = self.font_bold
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   132
        else:
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   133
            font = self.font
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: 31
diff changeset
   134
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   135
        # render character, get metrics
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   136
        surface = font.render(c, True, fgcolor, bgcolor)
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   137
        metrics = font.metrics(c)[0]
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   138
        minx = metrics[0]
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   139
        startx = 0
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   140
        if minx < 0:
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   141
            startx = abs(minx)
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: 31
diff changeset
   142
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   143
        # draw background
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   144
        dest = Coords(x * self.charsize.w, y * self.charsize.h)
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   145
        screen.fill(bgcolor, pygame.Rect(dest.x, dest.y, self.charsize.w, self.charsize.h))
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: 31
diff changeset
   146
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   147
        # draw character
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   148
        area = pygame.Rect(startx, 0, self.charsize.w, self.charsize.h)
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   149
        screen.blit(surface, tuple(dest), area)
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
   150
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
   151
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
   152
class DriverPygame(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: 31
diff changeset
   153
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
   154
    '''PyGame driver class.'''
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: 31
diff changeset
   155
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   156
    keymap = {
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   157
        pygame.K_ESCAPE     : 'escape',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   158
        pygame.K_TAB        : 'tab',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   159
        pygame.K_RETURN     : 'enter',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   160
        pygame.K_BACKSPACE  : 'backspace',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   161
        pygame.K_F1         : 'f1',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   162
        pygame.K_F2         : 'f2',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   163
        pygame.K_F3         : 'f3',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   164
        pygame.K_F4         : 'f4',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   165
        pygame.K_F5         : 'f5',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   166
        pygame.K_F6         : 'f6',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   167
        pygame.K_F7         : 'f7',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   168
        pygame.K_F8         : 'f8',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   169
        pygame.K_F9         : 'f9',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   170
        pygame.K_F10        : 'f10',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   171
        pygame.K_F11        : 'f11',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   172
        pygame.K_F12        : 'f12',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   173
        pygame.K_INSERT     : 'insert',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   174
        pygame.K_DELETE     : 'delete',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   175
        pygame.K_HOME       : 'home',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   176
        pygame.K_END        : 'end',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   177
        pygame.K_PAGEUP     : 'pageup',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   178
        pygame.K_PAGEDOWN   : 'pagedown',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   179
        pygame.K_UP         : 'up',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   180
        pygame.K_DOWN       : 'down',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   181
        pygame.K_LEFT       : 'left',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   182
        pygame.K_RIGHT      : 'right',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   183
        pygame.K_PRINT      : 'print',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   184
        pygame.K_SCROLLOCK  : 'scrollock',
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   185
        pygame.K_PAUSE      : 'pause',
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
   186
        }
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: 31
diff changeset
   187
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   188
    colormap = {
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   189
        'black'         : (0,0,0),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   190
        'blue'          : (23,23,178),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   191
        'green'         : (23,178,23),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   192
        'cyan'          : (23,178,178),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   193
        'red'           : (178,23,23),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   194
        'magenta'       : (178,23,178),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   195
        'yellow'        : (178,103,23),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   196
        'white'         : (178,178,178),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   197
        'intenseblack'  : (104,104,104),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   198
        'intenseblue'   : (84,84,255),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   199
        'intensegreen'  : (84,255,84),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   200
        'intensecyan'   : (84,255,255),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   201
        'intensered'    : (255,84,84),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   202
        'intensemagenta': (255,84,255),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   203
        'intenseyellow' : (255,255,84),
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   204
        'intensewhite'  : (255,255,255),
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
   205
        }
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: 31
diff changeset
   206
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
   207
    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
   208
        '''Initialize instance attributes'''
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
   209
        Driver.__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
   210
        self.log = logging.getLogger('tuikit')
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
   211
        self.screen = None
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   212
        self.size.w, self.size.h = 120, 40  # screen size in characters
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
   213
        self.term = None
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   214
        self.charsize = Size(16, 8)  # character size in pixels
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   215
        self.last_keypress = None
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   216
        self.last_key = None
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   217
        self.colors = {}     # maps names to curses attributes
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   218
        self.colorstack = [] # pushcolor/popcolor puts or gets attributes from this
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
   219
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
   220
    def start(self, mainfunc):
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
   221
        pygame.init()
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
   222
        self.term = TerminalScreen()
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
   223
        self.charsize = self.term.charsize
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
   224
        self.resize_screen()
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
   225
        self.term.set_default_attr(self.colormap['white'], self.colormap['black'], 0)
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
   226
        self.term.reset_attr()
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
   227
        mainfunc()
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
   228
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
   229
    def resize_screen(self):
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
   230
        mode = self.size.w * self.charsize.w, self.size.h * self.charsize.h
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   231
        self.screen = pygame.display.set_mode(mode, pygame.RESIZABLE)
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
   232
        self.term.reset(self.size.w, self.size.h)
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
   233
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
   234
    ## input ##
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: 31
diff changeset
   235
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
   236
    def getevents(self, timeout=None):
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
   237
        '''Process input, return list of events.'''
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
   238
        events = []
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
   239
        for ev in pygame.event.get():
26
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   240
            # mouse
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
   241
            if ev.type == pygame.MOUSEMOTION:
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: 31
diff changeset
   242
                mx = ev.pos[0] // self.charsize.w
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: 31
diff changeset
   243
                my = ev.pos[1] // self.charsize.h
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: 31
diff changeset
   244
                events.append(('mousemove', mx, my))
26
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   245
            elif ev.type in (pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP):
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: 31
diff changeset
   246
                mx = ev.pos[0] // self.charsize.w
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: 31
diff changeset
   247
                my = ev.pos[1] // self.charsize.h
26
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   248
                evname = {pygame.MOUSEBUTTONDOWN: 'mousedown', pygame.MOUSEBUTTONUP: 'mouseup'}
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: 31
diff changeset
   249
                events.append((evname[ev.type], mx, my, ev.button))
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: 31
diff changeset
   250
26
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   251
            # keyboard
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
   252
            elif ev.type == pygame.KEYDOWN:
26
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   253
                keypress = self.keypress_from_pygame_event(ev)
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   254
                if keypress:
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   255
                    events.append(keypress)
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   256
                    self.last_keypress = keypress
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   257
                    self.last_key = ev.key
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   258
                    pygame.time.set_timer(pygame.USEREVENT, 200)
26
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   259
            elif ev.type == pygame.USEREVENT: # repeat last key press
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   260
                events.append(self.last_keypress)
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   261
                pygame.time.set_timer(pygame.USEREVENT, 50)
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   262
            elif ev.type == pygame.KEYUP:
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   263
                if ev.key == self.last_key:
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   264
                    pygame.time.set_timer(pygame.USEREVENT, 0)
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: 31
diff changeset
   265
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   266
            # window
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
   267
            elif ev.type == pygame.VIDEORESIZE:
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   268
                neww, newh = ev.w // self.charsize.w, ev.h // self.charsize.h
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   269
                if neww != self.size.w or newh != self.size.h:
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   270
                    self.size.w, self.size.h = neww, newh
28
feee783d4fc5 DriverPygame: output to character buffer, draw whole screen at once.
Radek Brich <radek.brich@devl.cz>
parents: 27
diff changeset
   271
                    self.resize_screen()
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   272
                    events.append(('resize',))
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
   273
            elif ev.type == pygame.QUIT:
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
   274
                events.append(('quit',))
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: 31
diff changeset
   275
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
   276
            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
   277
                self.log.warning('Unknown PyGame event: %r', ev.type)
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
   278
        return events
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
   279
26
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   280
    def keypress_from_pygame_event(self, ev):
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   281
        if ev.key in self.keymap:
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   282
            keypress = ('keypress', self.keymap[ev.key], None)
26
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   283
        elif ev.unicode:
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   284
            keypress = ('keypress', None, ev.unicode)
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   285
        else:
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   286
            self.log.debug('Unknown key: key=%r unicode=%r' % (ev.key, ev.unicode))
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   287
            keypress = None
26
37745c5abc49 DriverPygame: add mouse events and key press autorepeat.
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   288
        return keypress
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
   289
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
   290
    ## 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: 31
diff changeset
   291
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
   292
    def erase(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
   293
        '''Clear screen.'''
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
   294
        self.term.clear()
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
   295
        self.screen.fill(self.term.default_attr[1])
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: 31
diff changeset
   296
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
   297
    def putch(self, 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
   298
        if not self.clipstack.test(x, y):
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
   299
            return
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
   300
        self.term.putch(x, y, c)
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
   301
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
   302
    def commit(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
   303
        '''Commit changes to the screen.'''
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
   304
        self.term.update(self.screen)
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
   305
        pygame.display.flip()
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
   306
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
   307
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
   308
    ## 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: 31
diff changeset
   309
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   310
    def _parsecolor(self, name, attr=None):
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   311
        name = name.lower().strip()
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   312
        if attr == 'bold':
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   313
            name = 'intense' + name
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   314
        return self.colormap[name]
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: 31
diff changeset
   315
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   316
    def _parseattrs(self, attrs):
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   317
        res = ''
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   318
        for a in attrs:
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   319
            a = a.lower().strip()
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   320
            if a == 'bold':
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
   321
                res = 1
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   322
        return res
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   323
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
   324
    def setcolor(self, name, desc):
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
   325
        '''Define color name.
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: 31
diff changeset
   326
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
   327
        name - name of color (e.g. 'normal', 'active')
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
   328
        desc - color description - foreground, background, attributes (e.g. 'black on white, bold')
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: 31
diff changeset
   329
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
   330
        '''
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   331
        parts = desc.split(',')
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   332
        fg, bg = parts[0].split(' on ')
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   333
        attrs = parts[1:]
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   334
        attr = self._parseattrs(attrs)
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   335
        fg = self._parsecolor(fg, attr)
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   336
        bg = self._parsecolor(bg)
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   337
        self.colors[name] = (fg, bg, attr)
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
   338
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
   339
    def pushcolor(self, name):
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
   340
        '''Add color on top of stack and use this color for following output.'''
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   341
        # add prefix if such color is available
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   342
        if len(self.colorprefix):
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   343
            prefixname = self.colorprefix[-1] + name
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   344
            if prefixname in self.colors:
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   345
                name = prefixname
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   346
        col = self.colors[name]
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   347
        self.current_color = col
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   348
        self.colorstack.append(col)
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: 31
diff changeset
   349
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
   350
    def popcolor(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
   351
        '''Remove color from top of stack and use new top color for following output.'''
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   352
        self.colorstack.pop()
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   353
        if len(self.colorstack):
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   354
            col = self.colorstack[-1]
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
   355
            self.term.set_attr(col[0], col[1], col[2])
27
139d1241b4c5 DriverPygame: add colors, make window resizable.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   356
        else:
31
629d5edb1602 Update PyGame driver.
rbrich
parents: 29
diff changeset
   357
            self.term.reset_attr()
25
f69a1f0382ce Partial DriverPygame.putch() implementation. Add patch for PyGame implementing font.render_glyph(). Add test for PyGame font module. Add special key definitions to DriverPygame.
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   358
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
   359
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
   360
    ## cursor ##
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
   361
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
   362
    def showcursor(self, x, y):
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
   363
        '''Set cursor to be shown at x, y coordinates.'''
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
   364
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
   365
    def hidecursor(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
   366
        '''Hide cursor.'''
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
   367
29
c0cdef06fd16 Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   368
c0cdef06fd16 Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   369
driverclass = DriverPygame
c0cdef06fd16 Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   370