File: drawing.py

package info (click to toggle)
libcaca 0.99.beta20-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,264 kB
  • sloc: ansic: 25,091; php: 2,763; python: 2,637; cs: 1,213; cpp: 1,127; java: 916; objc: 836; makefile: 543; perl: 505; sh: 472; asm: 297; ruby: 215; xml: 33
file content (133 lines) | stat: -rwxr-xr-x 4,109 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# libcaca       Colour ASCII-Art library
#               Python language bindings
# Copyright (c) 2010 Alex Foulon <alxf@lavabit.com>
#               All Rights Reserved
#
# This library is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What the Fuck You Want
# to Public License, Version 2, as published by Sam Hocevar. See
# http://www.wtfpl.net/ for more details.
#

""" Libcaca Python bindings """

import ctypes
import sys
import time

import caca
from caca.canvas import Canvas
from caca.display import Display, Event

class Drawing(object):
    def __init__(self, canvas):
        self.cv = canvas

    def do_menu(self):
        self.cv.put_str(0, 1, "Drawing Menu")
        self.cv.put_str(0, 2, "------------")
        self.cv.put_str(0, 4, "1) line")
        self.cv.put_str(0, 5, "2) thin line")
        self.cv.put_str(0, 6, "3) polyline")
        self.cv.put_str(0, 7, "4) thin polyline")
        self.cv.put_str(0, 8, "5) circle")
        self.cv.put_str(0, 9, "6) ellipse")
        self.cv.put_str(0, 10, "7) thin ellipse")
        self.cv.put_str(0, 11, "8) box")
        self.cv.put_str(0, 12, "9) thin box")

    def do_line(self, thin=False):
        if thin:
            self.cv.draw_thin_line(0, 0, self.cv.get_width(), 1)
        else:
            self.cv.draw_line(0, 0, self.cv.get_width(), 1, '#')

    def do_polyline(self, thin=False):
        x = [0, self.cv.get_width() - 1, self.cv.get_width() - 1]
        y = [0, self.cv.get_height(), 0]
        array_x = ctypes.c_int * (len(x) + 1)
        array_y = ctypes.c_int * (len(y) + 1)
        array_xy = [ (x, y) for x, y in zip(array_x(*x), array_y(*y))]
        if thin:
            self.cv.draw_thin_polyline(array_xy)
        else:
            self.cv.draw_polyline(array_xy, '#')

    def do_circle(self):
        x = self.cv.get_width() // 2
        y = self.cv.get_height() // 2
        radius = 5
        self.cv.draw_circle(x, y, radius, '@')

    def do_ellipse(self, thin=False):
        x = self.cv.get_width() // 2
        y = self.cv.get_height() // 2
        a = 7
        b = 3
        if thin:
            self.cv.draw_thin_ellipse(x, y, a, b)
        else:
            self.cv.draw_ellipse(x, y, a, b, '@')

    def do_box(self, thin=False):
        if thin:
            self.cv.draw_thin_box(0, 0, self.cv.get_width(), self.cv.get_height())
        else:
            self.cv.draw_box(0, 0, self.cv.get_width(), self.cv.get_height(), '#')

if __name__ == "__main__":
    cv = Canvas()
    dp = Display(cv)
    ev = Event()
    draw = Drawing(cv)

    while True:
        cv.clear()
        draw.do_menu()
        dp.refresh()
        if dp.get_event(caca.EVENT_KEY_PRESS, ev, 0):
            ch = ev.get_key_ch()
            cv.clear()
            if ch == ord('q'):
                sys.exit()
            elif ch == ord('1'):
                draw.do_line()
                dp.refresh()
                time.sleep(2)
            elif ch == ord('2'):
                draw.do_line(thin=True)
                dp.refresh()
                time.sleep(2)
            elif ch == ord('3'):
                draw.do_polyline()
                dp.refresh()
                time.sleep(2)
            elif ch == ord('4'):
                draw.do_polyline(thin=True)
                dp.refresh()
                time.sleep(2)
            elif ch == ord('5'):
                draw.do_circle()
                dp.refresh()
                time.sleep(2)
            elif ch == ord('6'):
                draw.do_ellipse()
                dp.refresh()
                time.sleep(2)
            elif ch == ord('7'):
                draw.do_ellipse(thin=True)
                dp.refresh()
                time.sleep(2)
            elif ch == ord('8'):
                draw.do_box()
                dp.refresh()
                time.sleep(2)
            elif ch == ord('9'):
                draw.do_box(thin=True)
                dp.refresh()
                time.sleep(2)