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://www.wtfpl.net/ 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 array_xy = [ (x, y) for x, y in zip(array_x(*x), array_y(*y))] 55 if thin: 56 self.cv.draw_thin_polyline(array_xy) 57 else: 58 self.cv.draw_polyline(array_xy, '#') 59 60 def do_circle(self): 61 x = self.cv.get_width() // 2 62 y = self.cv.get_height() // 2 63 radius = 5 64 self.cv.draw_circle(x, y, radius, '@') 65 66 def do_ellipse(self, thin=False): 67 x = self.cv.get_width() // 2 68 y = self.cv.get_height() // 2 69 a = 7 70 b = 3 71 if thin: 72 self.cv.draw_thin_ellipse(x, y, a, b) 73 else: 74 self.cv.draw_ellipse(x, y, a, b, '@') 75 76 def do_box(self, thin=False): 77 if thin: 78 self.cv.draw_thin_box(0, 0, self.cv.get_width(), self.cv.get_height()) 79 else: 80 self.cv.draw_box(0, 0, self.cv.get_width(), self.cv.get_height(), '#') 81 82if __name__ == "__main__": 83 cv = Canvas() 84 dp = Display(cv) 85 ev = Event() 86 draw = Drawing(cv) 87 88 while True: 89 cv.clear() 90 draw.do_menu() 91 dp.refresh() 92 if dp.get_event(caca.EVENT_KEY_PRESS, ev, 0): 93 ch = ev.get_key_ch() 94 cv.clear() 95 if ch == ord('q'): 96 sys.exit() 97 elif ch == ord('1'): 98 draw.do_line() 99 elif ch == ord('2'): 100 draw.do_line(thin=True) 101 elif ch == ord('3'): 102 draw.do_polyline() 103 elif ch == ord('4'): 104 draw.do_polyline(thin=True) 105 elif ch == ord('5'): 106 draw.do_circle() 107 elif ch == ord('6'): 108 draw.do_ellipse() 109 elif ch == ord('7'): 110 draw.do_ellipse(thin=True) 111 elif ch == ord('8'): 112 draw.do_box() 113 elif ch == ord('9'): 114 draw.do_box(thin=True) 115 dp.refresh() 116 time.sleep(2) 117 118