Theme -> Plugin.
[kaya.git] / lib / plugins / shogi / shogi.rb
blob3e856537e04e1f6c62c63a88a19800844f2ed2f2
1 require 'qtutils'
2 require 'plugins/plugin'
3 require 'plugins/shadow'
4 require 'plugins/background'
6 class ShogibanBackground
7   include Plugin
8   include Background
9   
10   BACKGROUND_COLOR = Qt::Color.new(0xeb, 0xd6, 0xa0)
11   LINE_COLOR = Qt::Color.new(0x9c, 0x87, 0x55)
12   BASE_DIR = File.dirname(__FILE__)
13   
14   plugin :name => 'Shogiban',
15          :keywords => %w(shogi board)
16         
17   def initialize(opts = {})
18     @squares = opts[:board_size] || opts[:game].size
19     @background = opts[:background] || 'kaya'
20   end
21   
22   def pixmap(size)
23     Qt::Image.painted(Qt::Point.new(size.x * @squares.x, size.y * @squares.y)) do |p|
24       if @background
25         bg = Qt::Image.new(File.join(BASE_DIR, @background + '.png'))
26         p.draw_tiled_pixmap(Qt::Rect.new(0, 0, size.x * @squares.x, size.y * @squares.y), bg.to_pix)
27       else
28         (0...@squares.x).each do |x|
29           (0...@squares.y).each do |y|
30             rect = Qt::RectF.new(size.x * x, size.y * y, size.x, size.y)
31             p.fill_rect(rect, Qt::Brush.new(BACKGROUND_COLOR))
32           end
33         end
34       end
35       pen = p.pen
36       pen.width = 2
37       pen.color = LINE_COLOR
38       p.pen = pen
39       (0..@squares.x).each do |x|
40         p.draw_line(x * size.x, 0, x * size.x, @squares.y * size.y)
41       end
42       (0..@squares.y).each do |y|
43         p.draw_line(0, y * size.y, size.x * @squares.x, y * size.y)
44       end
45     end.to_pix
46   end
47 end
49 class ShogiTheme
50   include Plugin
51   include Shadower
52   
53   BASE_DIR = File.dirname(__FILE__)
54   TYPES = { :knight => 'n' }
55   NUDE_TILE = File.join(BASE_DIR, 'nude_tile.svg')
56   RATIOS = {
57     :king => 1.0,
58     :rook => 0.96,
59     :bishop => 0.93,
60     :gold => 0.9,
61     :silver => 0.9,
62     :knight => 0.86,
63     :lance => 0.83,
64     :pawn => 0.8 }
66   plugin :name => 'Shogi',
67          :keywords => %w(shogi pieces)
69   def initialize(opts = {})
70     @loader = lambda do |piece, size|
71       tile = Qt::SvgRenderer.new(NUDE_TILE)
72       kanji = Qt::SvgRenderer.new(filename(piece))
73       ratio = RATIOS[piece.type] || 0.9
74       img = Qt::Image.painted(size) do |p|
75         p.scale(ratio, ratio)
76         p.translate(size * (1 - ratio) / 2)
77         if piece.color == :white
78           p.translate(size)
79           p.rotate(180)
80         end
81         tile.render(p)
82         kanji.render(p)
83       end
84     end
85     if opts.has_key? :shadow
86       @loader = with_shadow(@loader)
87     end
88   end
90   def pixmap(piece, size)
91     @loader[piece, size].to_pix
92   end
93   
94   def filename(piece)
95     color = piece.color.to_s[0, 1]
96 #     type = TYPES[piece.type] || piece.type.to_s[0, 1]
97     name = piece.type.to_s.gsub(/^promoted_/, 'p') + ".svg"
98     File.join(BASE_DIR, name)
99   end