You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

119 line
3.5 KiB

  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. """ Libcaca Python bindings """
  16. import ctypes
  17. import sys
  18. import time
  19. import caca
  20. from caca.canvas import Canvas
  21. from caca.display import Display, Event
  22. class Drawing(object):
  23. def __init__(self, canvas):
  24. self.cv = canvas
  25. def do_menu(self):
  26. self.cv.put_str(0, 1, "Drawing Menu")
  27. self.cv.put_str(0, 2, "------------")
  28. self.cv.put_str(0, 4, "1) line")
  29. self.cv.put_str(0, 5, "2) thin line")
  30. self.cv.put_str(0, 6, "3) polyline")
  31. self.cv.put_str(0, 7, "4) thin polyline")
  32. self.cv.put_str(0, 8, "5) circle")
  33. self.cv.put_str(0, 9, "6) ellipse")
  34. self.cv.put_str(0, 10, "7) thin ellipse")
  35. self.cv.put_str(0, 11, "8) box")
  36. self.cv.put_str(0, 12, "9) thin box")
  37. def do_line(self, thin=False):
  38. if thin:
  39. self.cv.draw_thin_line(0, 0, self.cv.get_width(), 1)
  40. else:
  41. self.cv.draw_line(0, 0, self.cv.get_width(), 1, '#')
  42. def do_polyline(self, thin=False):
  43. x = [0, self.cv.get_width() - 1, self.cv.get_width() - 1]
  44. y = [0, self.cv.get_height(), 0]
  45. array_x = ctypes.c_int * (len(x) + 1)
  46. array_y = ctypes.c_int * (len(y) + 1)
  47. ax = array_x(*x)
  48. ay = array_y(*y)
  49. if thin:
  50. self.cv.draw_thin_polyline(ax, ay, len(x) + 1)
  51. else:
  52. self.cv.draw_polyline(ax, ay, len(x) + 1, '#')
  53. def do_circle(self):
  54. x = self.cv.get_width() / 2
  55. y = self.cv.get_height() / 2
  56. radius = 5
  57. self.cv.draw_circle(x, y, radius, '@')
  58. def do_ellipse(self, thin=False):
  59. x = self.cv.get_width() / 2
  60. y = self.cv.get_height() / 2
  61. a = 7
  62. b = 3
  63. if thin:
  64. self.cv.draw_thin_ellipse(x, y, a, b)
  65. else:
  66. self.cv.draw_ellipse(x, y, a, b, '@')
  67. def do_box(self, thin=False):
  68. if thin:
  69. self.cv.draw_thin_box(0, 0, self.cv.get_width(), self.cv.get_height())
  70. else:
  71. self.cv.draw_box(0, 0, self.cv.get_width(), self.cv.get_height(), '#')
  72. if __name__ == "__main__":
  73. cv = Canvas()
  74. dp = Display(cv)
  75. ev = Event()
  76. draw = Drawing(cv)
  77. while True:
  78. cv.clear()
  79. draw.do_menu()
  80. dp.refresh()
  81. if dp.get_event(caca.EVENT_KEY_PRESS, ev, 0):
  82. ch = ev.get_key_ch()
  83. cv.clear()
  84. if ch == ord('q'):
  85. sys.exit()
  86. elif ch == ord('1'):
  87. draw.do_line()
  88. elif ch == ord('2'):
  89. draw.do_line(thin=True)
  90. elif ch == ord('3'):
  91. draw.do_polyline()
  92. elif ch == ord('4'):
  93. draw.do_polyline(thin=True)
  94. elif ch == ord('5'):
  95. draw.do_circle()
  96. elif ch == ord('6'):
  97. draw.do_ellipse()
  98. elif ch == ord('7'):
  99. draw.do_ellipse(thin=True)
  100. elif ch == ord('8'):
  101. draw.do_box()
  102. elif ch == ord('9'):
  103. draw.do_box(thin=True)
  104. dp.refresh()
  105. time.sleep(2)