demo-pixbufs.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local GLib = lgi.GLib
  4. local Gtk = lgi.Gtk
  5. local Gdk = lgi.Gdk
  6. local GdkPixbuf = lgi.GdkPixbuf
  7. local cairo = lgi.cairo
  8. local assert = lgi.assert
  9. -- Load pixbuf images.
  10. local background = assert(GdkPixbuf.Pixbuf.new_from_file(
  11. dir:get_child('background.jpg'):get_path()))
  12. local back_width, back_height = background.width, background.height
  13. local images = {}
  14. for _, name in ipairs {
  15. 'apple-red.png', 'gnome-applets.png', 'gnome-calendar.png',
  16. 'gnome-foot.png', 'gnome-gmush.png', 'gnome-gimp.png', 'gnome-gsame.png',
  17. 'gnu-keys.png' } do
  18. images[#images + 1] = assert(GdkPixbuf.Pixbuf.new_from_file(
  19. dir:get_child(name):get_path()))
  20. end
  21. local window = Gtk.Window {
  22. title = "Pixbufs",
  23. resizable = false,
  24. Gtk.DrawingArea {
  25. id = 'area',
  26. width = back_width,
  27. height = back_height,
  28. },
  29. }
  30. local frame = GdkPixbuf.Pixbuf.new('RGB', false, 8, back_width, back_height)
  31. local area = window.child.area
  32. function area:on_draw(cr)
  33. cr:set_source_pixbuf(frame, 0, 0)
  34. cr:paint()
  35. return true
  36. end
  37. local FRAME_DELAY = 50
  38. local CYCLE_LEN = 60
  39. local frame_num = 0
  40. local sin, cos, pi, floor, abs, min, max
  41. = math.sin, math.cos, math.pi, math.floor, math.abs, math.min, math.max
  42. local timeout_id = GLib.timeout_add(
  43. GLib.PRIORITY_DEFAULT, FRAME_DELAY, function()
  44. background:copy_area(0, 0, back_width, back_height, frame, 0, 0)
  45. local f = (frame_num % CYCLE_LEN) / CYCLE_LEN
  46. local xmid, ymid = back_width / 2, back_height / 2
  47. local radius = min(xmid, ymid) / 2
  48. local r1 = Gdk.Rectangle()
  49. local r2 = Gdk.Rectangle { x = 0, y = 0,
  50. width = back_width, height = back_height }
  51. for i = 1, #images do
  52. local ang = 2 * pi * (i / #images - f)
  53. local iw, ih = images[i].width, images[i].height
  54. local r = radius + (radius / 3) * sin(2 * pi * f)
  55. local xpos = floor(xmid + r * cos(ang) - iw / 2 + 0.5)
  56. local ypos = floor(ymid + r * sin(ang) - ih / 2 + 0.5)
  57. local k = (i % 2 == 0) and sin(f * 2 * pi) or cos(f * 2 * pi)
  58. k = max(2 * k * k, 0.25)
  59. r1.x = xpos
  60. r1.y = ypos
  61. r1.width = iw * k
  62. r1.height = ih * k
  63. local dest = Gdk.Rectangle.intersect(r1, r2)
  64. if dest then
  65. local alpha = (i % 1 == 0) and sin(f * 2 * pi) or cos(f * 2 * pi)
  66. images[i]:composite(frame, dest.x, dest.y, dest.width, dest.height,
  67. xpos, ypos, k, k,
  68. 'NEAREST', max(127, abs(alpha)))
  69. end
  70. end
  71. area:queue_draw()
  72. frame_num = frame_num + 1
  73. return GLib.SOURCE_CONTINUE
  74. end)
  75. function window:on_destroy()
  76. GLib.source_remove(timeout_id)
  77. end
  78. window:show_all()
  79. return window
  80. end,
  81. "Pixbufs",
  82. table.concat {
  83. [[A Gdk.Pixbuf represents an image, normally in RGB or RGBA format. ]],
  84. [[Pixbufs are normally used to load files from disk and perform image ]],
  85. [[scaling.
  86. ]],
  87. [[This demo is not all that educational, but looks cool. It was ]],
  88. [[written by Extreme Pixbuf Hacker Federico Mena Quintero. It also ]],
  89. [[shows off how to use Gtk.DrawingArea to do a simple animation.
  90. ]],
  91. [[Look at the Image demo for additional pixbuf usage examples.]],
  92. }