Move match inside match_info in MatchHandler.
[kaya.git] / lib / item.rb
blobe7d35df1c38617d4ef37a9d995a0f24726188403
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 class Item < Qt::GraphicsPixmapItem
11   attr_reader :name, :item
12   attr_reader :opacity
13   
14   # name is whatever information the caller needs
15   # to recreate this piece with a different size
16   # 
17   def initialize(name, pixmap, parent)
18     super(pixmap || Qt::Pixmap.new, parent)
19     @name = name
20     @opacity = 1.0
21   end
22   
23   def paint(p, options, widget)
24     if pixmap
25       p.saving do |p|
26         p.opacity = @opacity
27         super p, options, widget
28       end
29     end
30   end
31   
32   def opacity=(value)
33     @opacity = value
34     update
35   end
36   
37   def remove
38     scene.remove_item self
39   end
40 end
42 class ReloadableItem < Item
43   def initialize(name, reloader, parent)
44     super(name, nil, parent)
45     @reloader = reloader
46   end
47   
48   def reload(key)
49     @reloader[key, self]
50   end
51 end
53 class AutoreloadableItem < Item
54   def initialize(name, pix_loader, parent)
55     super(name, nil, parent)
56     @pix_loader = pix_loader
57   end
58   
59   def set_geometry(rect)
60     self.pos = rect.top_left.to_f
61     self.pixmap = @pix_loader[rect.size]
62   end
63 end
65 module ItemUtils
66   BACKGROUND_ZVALUE = -10
67   TEMP_ZVALUE = 10
68   
69   def create_item(opts)
70     item_factory, arg = if opts[:reloader]
71       [ReloadableItem, opts[:reloader]]
72     elsif opts[:pix_loader]
73       [AutoreloadableItem, opts[:pix_loader]]
74     else
75       [Item, opts[:pixmap]]
76     end
77     
78     item = item_factory.new(opts[:name], arg, opts.fetch(:parent, item_parent))
79     item.pos = opts[:pos] || Qt::PointF.new(0, 0)
80     item.z_value = opts[:z] || 0
81     item.visible = false if opts[:hidden]
82     item
83   end
84   
85   def destroy_item(item)
86     scene.remove_item item
87   end
88   
89   def raise(item)
90     item.z_value = TEMP_ZVALUE
91   end
92   
93   def lower(item)
94     item.z_value = 0
95   end
96   
97   def item_parent
98     self
99   end