source: libcaca/trunk/python/examples/drawing.py @ 4398

Last change on this file since 4398 was 4398, checked in by Sam Hocevar, 11 years ago

New improved ctypes-based Python bindings, by Alex Foulon <alxf@lavabit.com>.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# libcaca       Colour ASCII-Art library
5#               Python language bindings
6# Copyright (c) 2010 Alex Foulon <alxf@lavabit.com>
7#               All Rights Reserved
8#
9# This library is free software. It comes without any warranty, to
10# the extent permitted by applicable law. You can redistribute it
11# and/or modify it under the terms of the Do What The Fuck You Want
12# To Public License, Version 2, as published by Sam Hocevar. See
13# http://sam.zoy.org/wtfpl/COPYING for more details.
14#
15
16""" Libcaca Python bindings """
17
18import ctypes
19import sys
20import time
21
22import caca
23from caca.canvas import Canvas
24from caca.display import Display, Event
25
26class Drawing(object):
27    def __init__(self, canvas):
28        self.cv = canvas
29
30    def do_menu(self):
31        self.cv.put_str(0, 1, "Drawing Menu")
32        self.cv.put_str(0, 2, "------------")
33        self.cv.put_str(0, 4, "1) line")
34        self.cv.put_str(0, 5, "2) thin line")
35        self.cv.put_str(0, 6, "3) polyline")
36        self.cv.put_str(0, 7, "4) thin polyline")
37        self.cv.put_str(0, 8, "5) circle")
38        self.cv.put_str(0, 9, "6) ellipse")
39        self.cv.put_str(0, 10, "7) thin ellipse")
40        self.cv.put_str(0, 11, "8) box")
41        self.cv.put_str(0, 12, "9) thin box")
42
43    def do_line(self, thin=False):
44        if thin:
45            self.cv.draw_thin_line(0, 0, self.cv.get_width(), 1)
46        else:
47            self.cv.draw_line(0, 0, self.cv.get_width(), 1, '#')
48
49    def do_polyline(self, thin=False):
50        x = [0, self.cv.get_width() - 1, self.cv.get_width() - 1]
51        y = [0, self.cv.get_height(), 0]
52        array_x = ctypes.c_int * (len(x) + 1)
53        array_y = ctypes.c_int * (len(y) + 1)
54        ax = array_x(*x)
55        ay = array_y(*y)
56        if thin:
57            self.cv.draw_thin_polyline(ax, ay, len(x) + 1)
58        else:
59            self.cv.draw_polyline(ax, ay, len(x) + 1, '#')
60
61    def do_circle(self):
62        x = self.cv.get_width() / 2
63        y = self.cv.get_height() / 2
64        radius = 5
65        self.cv.draw_circle(x, y, radius, '@')
66
67    def do_ellipse(self, thin=False):
68        x = self.cv.get_width() / 2
69        y = self.cv.get_height() / 2
70        a = 7
71        b = 3
72        if thin:
73            self.cv.draw_thin_ellipse(x, y, a, b)
74        else:
75            self.cv.draw_ellipse(x, y, a, b, '@')
76
77    def do_box(self, thin=False):
78        if thin:
79            self.cv.draw_thin_box(0, 0, self.cv.get_width(), self.cv.get_height())
80        else:
81            self.cv.draw_box(0, 0, self.cv.get_width(), self.cv.get_height(), '#')
82
83if __name__ == "__main__":
84    cv = Canvas()
85    dp = Display(cv)
86    ev = Event()
87    draw = Drawing(cv)
88
89    while True:
90        cv.clear()
91        draw.do_menu()
92        dp.refresh()
93        if dp.get_event(caca.EVENT_KEY_PRESS, ev, 0):
94            ch = ev.get_key_ch()
95            cv.clear()
96            if ch == ord('q'):
97                sys.exit()
98            elif ch == ord('1'):
99                draw.do_line()
100            elif ch == ord('2'):
101                draw.do_line(thin=True)
102            elif ch == ord('3'):
103                draw.do_polyline()
104            elif ch == ord('4'):
105                draw.do_polyline(thin=True)
106            elif ch == ord('5'):
107                draw.do_circle()
108            elif ch == ord('6'):
109                draw.do_ellipse()
110            elif ch == ord('7'):
111                draw.do_ellipse(thin=True)
112            elif ch == ord('8'):
113                draw.do_box()
114            elif ch == ord('9'):
115                draw.do_box(thin=True)
116            dp.refresh()
117            time.sleep(2)
118
Note: See TracBrowser for help on using the repository browser.