Merge games and plugins.
[kaya.git] / lib / plugins / shogi / shogi.rb
blobcf70ff983f422503c385a1c9887118c328c7e088
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          :interface => :board,
16          :keywords => %w(shogi)
17         
18   def initialize(opts = {})
19     @squares = opts[:board_size] || opts[:game].size
20     @background = opts[:background] || 'kaya'
21   end
22   
23   def pixmap(size)
24     Qt::Image.painted(Qt::Point.new(size.x * @squares.x, size.y * @squares.y)) do |p|
25       if @background
26         bg = Qt::Image.new(File.join(BASE_DIR, @background + '.png'))
27         p.draw_tiled_pixmap(Qt::Rect.new(0, 0, size.x * @squares.x, size.y * @squares.y), bg.to_pix)
28       else
29         (0...@squares.x).each do |x|
30           (0...@squares.y).each do |y|
31             rect = Qt::RectF.new(size.x * x, size.y * y, size.x, size.y)
32             p.fill_rect(rect, Qt::Brush.new(BACKGROUND_COLOR))
33           end
34         end
35       end
36       pen = p.pen
37       pen.width = 2
38       pen.color = LINE_COLOR
39       p.pen = pen
40       (0..@squares.x).each do |x|
41         p.draw_line(x * size.x, 0, x * size.x, @squares.y * size.y)
42       end
43       (0..@squares.y).each do |y|
44         p.draw_line(0, y * size.y, size.x * @squares.x, y * size.y)
45       end
46     end.to_pix
47   end
48 end
50 class ShogiTheme
51   include Plugin
52   include Shadower
53   
54   BASE_DIR = File.dirname(__FILE__)
55   TYPES = { :knight => 'n' }
56   NUDE_TILE = File.join(BASE_DIR, 'nude_tile.svg')
57   RATIOS = {
58     :king => 1.0,
59     :rook => 0.96,
60     :bishop => 0.93,
61     :gold => 0.9,
62     :silver => 0.9,
63     :knight => 0.86,
64     :lance => 0.83,
65     :pawn => 0.8 }
67   plugin :name => 'Shogi Pieces',
68          :interface => :pieces,
69          :keywords => %w(shogi)
71   def initialize(opts = {})
72     @loader = lambda do |piece, size|
73       tile = Qt::SvgRenderer.new(NUDE_TILE)
74       kanji = Qt::SvgRenderer.new(filename(piece))
75       ratio = RATIOS[piece.type] || 0.9
76       img = Qt::Image.painted(size) do |p|
77         p.scale(ratio, ratio)
78         p.translate(size * (1 - ratio) / 2)
79         if (piece.color == :white) ^ @flipped
80           p.translate(size)
81           p.rotate(180)
82         end
83         tile.render(p)
84         kanji.render(p)
85       end
86     end
87     if opts.has_key? :shadow
88       @loader = with_shadow(@loader)
89     end
90     @flipped = false
91   end
93   def pixmap(piece, size)
94     @loader[piece, size].to_pix
95   end
96   
97   def filename(piece)
98     color = piece.color.to_s[0, 1]
99 #     type = TYPES[piece.type] || piece.type.to_s[0, 1]
100     name = piece.type.to_s.gsub(/^promoted_/, 'p') + ".svg"
101     File.join(BASE_DIR, name)
102   end
103   
104   def flip(value)
105     @flipped = value
106   end