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.
 
 
 
 
 
 

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