Restore game actions.
[kaya.git] / lib / nine_patch_item.rb
blob165947a69da72da9dcd747c207ed2acb86087b7a
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 require 'toolkit'
10 # A NinePatchItem is rectangular item composed of 9 subitems, 
11 # 4 at the sides, 4 at the corners, and one in the center.
12 # Subitems are specified in the items hash, with keys: 
13 # :n, :w, :s, :e for the sides; 
14 # :ne, :nw, :sw, :se for the corners;
15 # :center for the center.
16 # Subitems must respond to set_geometry.
17
18 class NinePatchItem < Qt::GraphicsItemGroup
19   attr_reader :bsize
20   
21   def initialize(parent, scene, items)
22     super(parent, scene)
23     @items = items
24     @bsize = 1
25     @items.each do |key, item|
26       item.group = self
27     end
28   end
29   
30   def bsize=(value)
31     @bsize = value.to_i
32   end
33   
34   def set_geometry(rect)
35     self.pos = rect.top_left.to_f
36     g = lambda do |key, x, y, w, h|
37       if @items[key]
38         @items[key].set_geometry(Qt::Rect.new(x, y, w, h))
39       end
40     end
41     g[:nw, 0, 0, @bsize, @bsize]
42     g[:n, @bsize, 0, rect.width - @bsize * 2, @bsize]
43     g[:ne, rect.width - @bsize, 0, @bsize, @bsize]
45     g[:sw, 0, rect.height - @bsize, @bsize, @bsize]
46     g[:s, @bsize, rect.height - @bsize, rect.width - @bsize * 2, @bsize]
47     g[:se, rect.width - @bsize, rect.height - @bsize, @bsize, @bsize]
49     g[:w, 0, @bsize, @bsize, rect.height - @bsize * 2]
50     g[:center, @bsize, @bsize, rect.width - @bsize * 2, rect.height - @bsize * 2]
51     g[:e, rect.width - @bsize, @bsize, @bsize, rect.height - @bsize * 2]
52   end
53 end