Factor common code out of xboard and gnushogi engines.
[kaya/ydirson.git] / lib / board / square_tag.rb
blob7e5a8ea6ff5bfec9f4d30ec3dbb0f65403bac5ea
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 TaggableSquares
9   TAGS_ZVALUE = -2
10   
11   module ClassMethods
12     def square_tag(name, element, opts = {})
13       define_method(name) do
14         instance_variable_get("@#{name}")
15       end
16       
17       define_method("#{name}=") do |val|
18         instance_variable_set("@#{name}", val)
19         if val
20           options = { :pos => to_real(val),
21                    :z => TAGS_ZVALUE }.merge(opts)
22           square_tag_container.add_item(
23             name, 
24             theme.board.send(element, unit), 
25             options)
26         else
27           square_tag_container.remove_item(name)
28         end
29       end
30     end
31   end
32   
33   def self.included(klass)
34     klass.extend ClassMethods
35   end
36   
37   alias :tag :send
38   
39   def set_tag(name, value)
40     send("#{name}=", value)
41   end
42   
43   def square_tag_container
44     self
45   end
46 end