Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

drawing.py 4.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. """ 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. dp.refresh()
  88. time.sleep(2)
  89. elif ch == ord('2'):
  90. draw.do_line(thin=True)
  91. dp.refresh()
  92. time.sleep(2)
  93. elif ch == ord('3'):
  94. draw.do_polyline()
  95. dp.refresh()
  96. time.sleep(2)
  97. elif ch == ord('4'):
  98. draw.do_polyline(thin=True)
  99. dp.refresh()
  100. time.sleep(2)
  101. elif ch == ord('5'):
  102. draw.do_circle()
  103. dp.refresh()
  104. time.sleep(2)
  105. elif ch == ord('6'):
  106. draw.do_ellipse()
  107. dp.refresh()
  108. time.sleep(2)
  109. elif ch == ord('7'):
  110. draw.do_ellipse(thin=True)
  111. dp.refresh()
  112. time.sleep(2)
  113. elif ch == ord('8'):
  114. draw.do_box()
  115. dp.refresh()
  116. time.sleep(2)
  117. elif ch == ord('9'):
  118. draw.do_box(thin=True)
  119. dp.refresh()
  120. time.sleep(2)