Add licence and installation instructions.
[kaya.git] / lib / plugins / shadow.rb
bloba3789dcc840bb693dd2b67977b166363612ad535
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 module Shadower
9   ShadowData = Struct.new(:radius, :color, :grow, :offset)
10   DEFAULT_SHADOW_DATA = 
11     ShadowData.new(7.0, Qt::Color.new(0x40, 0x40, 0x50), 5, Qt::PointF.new(6, 4))
12   
13   def with_shadow(loader, data = DEFAULT_SHADOW_DATA)
14     lambda do |piece, size|
15       isz = size * 100 / (100 + data.grow) + Qt::Point.new(0.5, 0.5)
16       off = Qt::Point.new(
17         data.offset.x * isz.x / 200.0,
18         data.offset.y * isz.x / 200.0)
19       img = loader[piece, isz]
20       scaled_data = ShadowData.new(data.radius * isz.x / 100.0,
21                                    data.color,
22                                    size.x - isz.x,
23                                    off)
24       s = shadow(img, scaled_data)
25       Qt::Painter.new(s).paint do |p|
26         dst = Qt::Rect.new((size.x - isz.x) / 2 - off.x,
27                             (size.y - isz.y) / 2 - off.y,
28                             isz.x, isz.y)
29         p.draw_image(dst, img, Qt::Rect.new(Qt::Point.new(0, 0), isz))
30       end
31       s
32     end
33   end
34   
35   private
36   
37   def shadow(img, data)
38     img = Qt::Image.painted(Qt::Point.new(img.width + data.grow, 
39                                           img.height + data.grow)) do |p|
40       px = (data.grow * 0.5 + data.offset.x).to_i
41       py = (data.grow * 0.5 + data.offset.y).to_i
42   
43       p.composition_mode = Qt::Painter::CompositionMode_Source
44       p.fill_rect Qt::Rect.new(px, py, img.width, img.height), data.color
45       p.composition_mode = Qt::Painter::CompositionMode_DestinationAtop
46       p.draw_image Qt::Point.new(px, py), img
47     end
48     img.exp_blur(data.radius)
49     img
50   end
51 end