Bläddra i källkod

* Add examples: colors, driver and event.

tags/v0.99.beta18
Alex Foulon alxf 14 år sedan
förälder
incheckning
77c94579be
4 ändrade filer med 282 tillägg och 1 borttagningar
  1. +8
    -1
      python/Makefile.am
  2. +76
    -0
      python/examples/colors.py
  3. +67
    -0
      python/examples/driver.py
  4. +131
    -0
      python/examples/event.py

+ 8
- 1
python/Makefile.am Visa fil

@@ -12,8 +12,15 @@ endif

EXTRA_DIST = \
setup.py \
examples/blit.py \
examples/cacainfo.py \
examples/colors.py \
examples/drawing.py \
examples/driver.py \
examples/event.py \
examples/figfont.py \
examples/frames.py \
examples/img2txt.py \
examples/gol.py
examples/gol.py \
examples/text.py


+ 76
- 0
python/examples/colors.py Visa fil

@@ -0,0 +1,76 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# colors display all possible libcaca colour pairs
# Copyright (c) 2010 Alex Foulon <alxf@lavabit.com>
#
# This file is a Python port of "examples/colors.c"
# which is:
# Copyright (c) 2003-2010 Sam Hocevar <sam@hocevar.net>
# All Rights Reserverd
#
# This library is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#

import sys

import caca
from caca.canvas import Canvas, CanvasError
from caca.display import Display, DisplayError, Event

def main():
""" Main function. """

try:
cv = Canvas(80, 24)
dp = Display(cv)
except (CanvasError, DisplayError), err:
sys.stderr.write("%s\n" % err)
sys.exit(127)

cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
cv.clear()

for i in xrange(0, 16):
if i >= 8:
y = i + 3
else:
y = i + 2

cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
cv.printf(3, y, "ANSI %i", i)

for j in xrange(0, 16):
if j >= 8:
x = 13 + (j * 4)
else:
x = 12 + (j * 4)
if i >= 8:
y = i + 3
else:
y = i + 2

cv.set_color_ansi(i, j)
cv.put_str(x, y, "Aaホ")

cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
cv.put_str(3, 20, "This is bold This is blink This is italics This is underline")
cv.set_attr(caca.STYLE_BOLD)
cv.put_str(3 + 8, 20, "bold")
cv.set_attr(caca.STYLE_BLINK)
cv.put_str(3 + 24, 20, "blink")
cv.set_attr(caca.STYLE_ITALICS)
cv.put_str(3 + 41, 20, "italics")
cv.set_attr(caca.STYLE_UNDERLINE)
cv.put_str(3 + 60, 20, "underline")

dp.refresh()
dp.get_event(caca.EVENT_KEY_PRESS, Event(), -1)

if __name__ == "__main__":
main()


+ 67
- 0
python/examples/driver.py Visa fil

@@ -0,0 +1,67 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# driver libcaca test drivers program
# Copyright (c) 2010 Alex Foulon <alxf@lavabit.com>
#
# This file is a Python port of "examples/driver.c"
# which is:
# Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
# All Rights Reserverd
#
# This library is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#

import sys

import caca
from caca.canvas import Canvas, CanvasError
from caca.display import Display, DisplayError, Event

def main():
""" Main function. """

lst = caca.get_display_driver_list()
cur = 0

try:
cv = Canvas(0, 0)
dp = Display(cv)
except (CanvasError, DisplayError), err:
sys.stderr.write("%s\n" % err)
sys.exit(127)

cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)

while True:
cv.put_str(1, 0, "Available drivers:")
cur_driver = dp.get_driver()
n = 0
for driver, desc in lst:
if driver == cur_driver:
cv.put_str(2, n + 2, "%s %s (%s)" % ('*', driver, desc))
else:
cv.put_str(2, n + 2, "%s %s (%s)" % (' ', driver, desc))
n += 1

cv.put_str(2, n + 3, "Switching driver in 5 seconds")
dp.refresh()

if dp.get_event(caca.EVENT_KEY_PRESS, Event(), 5000000):
break

cur += 1
if cur < len(lst) and lst[cur][0] == "raw":
cur += 1
if cur >= len(lst):
cur = 0

dp.set_driver(lst[cur][0])

if __name__ == "__main__":
main()


+ 131
- 0
python/examples/event.py Visa fil

@@ -0,0 +1,131 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# event event lister for libcaca
# Copyright (c) 2010 Alex Foulon <alxf@lavabit.com>
#
# This file is a Python port of "examples/event.c"
# which is:
# Copyright (c) 2004-2010 Sam Hocevar <sam@hocevar.net>
# All Rights Reserverd
#
# This library is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#

import sys

import caca
from caca.canvas import Canvas, CanvasError
from caca.display import Display, DisplayError, Event

def print_event(cv, x, y, event):
""" Print event name.

:cv: libcaca canvas
:x: X coordinate
:y: Y coordinate
:event: event to display
"""

if event.get_type() == caca.EVENT_NONE:
cv.put_str(x, y, "CACA_EVENT_NONE")
elif event.get_type() == caca.EVENT_KEY_PRESS:
character = event.get_key_ch()
if character > 0x1f and character < 0x80:
cv.put_str(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%s)" % (character, chr(character)))
else:
cv.put_str(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%s)" % (character, '?'))
elif event.get_type() == caca.EVENT_KEY_RELEASE:
character = event.get_key_ch()
if character > 0x1f and character < 0x80:
cv.put_str(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%s)" % (character, chr(character)))
else:
cv.put_str(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%s)" % (character, '?'))
elif event.get_type() == caca.EVENT_MOUSE_MOTION:
cv.put_str(x, y, "CACA_EVENT_MOUSE_MOTION %u %u" % (event.get_mouse_x(),
event.get_mouse_y()))
elif event.get_type() == caca.EVENT_MOUSE_PRESS:
cv.put_str(x, y, "CACA_EVENT_MOUSE_PRESS %u" % (event.get_mouse_button()))
elif event.get_type() == caca.EVENT_MOUSE_RELEASE:
cv.put_str(x, y, "CACA_EVENT_MOUSE_RELEASE %u" % (event.get_mouse_button()))
elif event.get_type() == caca.EVENT_RESIZE:
cv.put_str(x, y, "CACA_EVENT_RESIZE %u %u" % (event.get_resize_width(),
event.get_resize_height()))
elif event.get_type() == caca.EVENT_QUIT:
cv.put_str(x, y, "CACA_EVENT_QUIT")
else:
cv.put_str(x, y, "CACA_EVENT_UNKNOWN")

def main():
""" Main function. """

events = []
quit = 0
quit_string = ["", "q", "qu", "qui", "quit"]

try:
cv = Canvas(80, 24)
dp = Display(cv)
except (CanvasError, DisplayError), err:
sys.stderr.write("%s\n" % err)
sys.exit(127)

h = cv.get_height() - 1

cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
cv.draw_line(0, 0, cv.get_width() - 1, 0, ' ')
cv.draw_line(0, h, cv.get_width() - 1, h, ' ')
cv.put_str(0, h, "Type \"quit\" to exit")

dp.refresh()

while quit < 4:
ev = Event()
if dp.get_event(caca.EVENT_ANY, ev, -1):
if ev.get_type() == caca.EVENT_KEY_PRESS:
key = ev.get_key_ch()
if key == ord('u') and quit == 1:
quit += 1
elif key == ord('i') and quit == 2:
quit += 1
elif key == ord('t') and quit == 3:
quit += 1
elif key == ord('q'):
quit = 1
else:
quit = 0

events.append(ev)

cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
cv.clear()

#print current event
cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
cv.draw_line(0, 0, cv.get_width() - 1, 0, ' ')
if events:
print_event(cv, 0, 0, events[-1])

cv.draw_line(0, h, cv.get_width() - 1, h, ' ')
cv.put_str(0, h, "Type \"quit\" to exit: %s" % quit_string[quit])

#print previous events
cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)
counts = range(0, len(events) - 1)
counts.reverse()
if len(events) > 1:
j = 0
for i in counts:
if j < h - 1 and events[i].get_type():
print_event(cv, 0, ((len(events) - 1) - i), events[i])
j += 1

dp.refresh()

if __name__ == "__main__":
main()


Laddar…
Avbryt
Spara