Improve the draw_arrowhead API.
[tagua/yd.git] / data / themes / pieces / ShogiTTF / theme.lua
blob2a371cb72a470ee466102e4aa39ad1bb51302f00
1 import("piece_theme.lua")
2 shadow=7.0
3 shadow_color="#404050"
4 shadow_offset_x=6
5 shadow_offset_y=4
6 shadow_grow=5
8 theme.options = OptList {
9 BoolOpt("moves_overlay", "Moves overlay", true)
12 function tile(white, ratio)
13 return function(size)
14 local i = Image(size,size)
15 i:clear()
17 local g = RadialGradient(Point(size*0.5,size*0.5), size*0.5, Point(size*0.3,size*0.3))
18 g[0] = "#ffeda2"
19 g[1] = "#c0a870"
20 local b = Brush(g)
22 if white then
23 i:translate(-size, -size)
24 i:rotate(180)
25 b:translate(-size, -size)
26 b:rotate(180)
27 end
29 if ratio then
30 i:scale(ratio, ratio)
31 i:translate(size*(1-ratio)*0.5, size*(1-ratio)*0.5)
32 b:scale(ratio, ratio)
33 b:translate(size*(1-ratio)*0.5, size*(1-ratio)*0.5)
34 end
36 i:draw_glyph(Rect(0,0,size,size), "Shogi.ttf", "0x2c", "black", b)
37 return i
38 end
39 end
40 function addChar(char, promoted)
41 return function(i, size)
42 i:draw_glyph(Rect(0,0,size,size), "Shogi.ttf", char,
43 promoted and "#d00000" or "#004000",
44 "#fff3c8", 4, false)
45 return i
46 end
47 end
49 function draw_arrowhead(i, p, vx,vy, length, rwidth, linecolor,linewidth)
50 -- normalize to 1 the direction vector
51 local vl = (vx*vx + vy*vy)^0.5
52 vx,vy = vx/vl, vy/vl
53 -- the component vector along the arrow direction
54 local vxdirect, vydirect = -vx*length, -vy*length
55 -- an orthogonal vector of requested relative width
56 local vxortho,vyortho = vydirect*rwidth, -vxdirect*rwidth
58 i:draw_line(p,Point(p.x+vxdirect+vxortho,p.y-vydirect-vyortho), linecolor,linewidth)
59 i:draw_line(p,Point(p.x+vxdirect-vxortho,p.y-vydirect+vyortho), linecolor,linewidth)
60 end
62 function shogi_moves(...)
63 local moves = {...}
64 return function(i,size)
65 if not theme.options.moves_overlay.value then
66 return i
67 end
69 local index, move
70 --local xcenter, ycenter, width = size/2, size/2, size/4
71 local xcenter, ycenter, width = 0.8*size, 0.82*size, 0.08*size
72 local tilesize,shortsize = 0.8*width, 0.3*width
73 local arrowlen,arrowrwidth = 0.2*width, 0.5
74 local centerpoint = Point(xcenter,ycenter)
75 local b = Brush("red")
76 for index,move in ipairs(moves) do
77 local x,y,long = move[1],move[2],move[3]
78 if long then
79 local p2 = Point(xcenter+1.5*x*width, ycenter-1.5*y*width)
80 i:draw_line(centerpoint,p2, "purple",1)
81 draw_arrowhead(i, p2, x,y, arrowlen,arrowrwidth, "purple",1)
82 else
83 i:draw_line(centerpoint, Point(xcenter+x*width,ycenter-y*width),"red",1)
84 i:fill_rect(Rect(xcenter+x*width-shortsize/2, ycenter-y*width-shortsize/2,
85 shortsize, shortsize), b)
86 end
87 end
89 -- the tile
90 i:fill_rect(Rect(xcenter-tilesize/2,ycenter-tilesize/2,
91 tilesize,tilesize), b)
93 return i
94 end
95 end
97 function shogi_piece(char, white, promoted, ratio, ...)
98 return addShadow(overlay(tile(white, ratio),
99 shogi_moves(...),
100 addChar(char, promoted)))
103 theme.black_king = shogi_piece("0x6B", false, false, 1,
104 {-1,1},{0,1},{1,1},
105 {-1,0},{1,0},
106 {-1,-1},{0,-1},{1,-1})
107 theme.black_rook = shogi_piece("0x72", false, false, 0.96,
108 {-1,0,1},{1,0,1},{0,-1,1},{0,1,1})
109 theme.black_p_rook = shogi_piece("0x52", false, true, 0.96,
110 {-1,0,1},{1,0,1},{0,-1,1},{0,1,1},
111 {-1,-1},{1,-1},{-1,1},{1,1})
112 theme.black_bishop = shogi_piece("0x62", false, false, 0.93,
113 {-1,-1,1},{1,-1,1},{-1,1,1},{1,1,1})
114 theme.black_p_bishop = shogi_piece("0x42", false, true, 0.93,
115 {-1,-1,1},{1,-1,1},{-1,1,1},{1,1,1},
116 {-1,0},{1,0},{0,-1},{0,1})
117 theme.black_gold = shogi_piece("0x67", false, false, 0.9,
118 {-1,1},{0,1},{1,1},
119 {-1,0},{1,0},{0,-1})
120 theme.black_silver = shogi_piece("0x73", false, false, 0.9,
121 {-1,1},{0,1},{1,1},
122 {-1,-1},{1,-1})
123 theme.black_p_silver = shogi_piece("0x53", false, true, 0.9,
124 {-1,1},{0,1},{1,1},
125 {-1,0},{1,0},{0,-1})
126 theme.black_knight = shogi_piece("0x68", false, false, 0.86,
127 {-1,2},{1,2})
128 theme.black_p_knight = shogi_piece("0x48", false, true, 0.86,
129 {-1,1},{0,1},{1,1},
130 {-1,0},{1,0},{0,-1})
131 theme.black_lance = shogi_piece("0x6C", false, false, 0.83,
132 {0,1,1})
133 theme.black_p_lance = shogi_piece("0x4C", false, true, 0.83,
134 {-1,1},{0,1},{1,1},
135 {-1,0},{1,0},{0,-1})
136 theme.black_pawn = shogi_piece("0x70", false, false, 0.8,
137 {0,1})
138 theme.black_p_pawn = shogi_piece("0x50", false, true, 0.8,
139 {-1,1},{0,1},{1,1},
140 {-1,0},{1,0},{0,-1})
142 theme.white_king = shogi_piece("0x6B", true, false, 1,
143 {-1,1},{0,1},{1,1},
144 {-1,0},{1,0},
145 {-1,-1},{0,-1},{1,-1})
146 theme.white_rook = shogi_piece("0x72", true, false, 0.96,
147 {-1,0,1},{1,0,1},{0,-1,1},{0,1,1})
148 theme.white_p_rook = shogi_piece("0x52", true, true, 0.96,
149 {-1,0,1},{1,0,1},{0,-1,1},{0,1,1},
150 {-1,-1},{1,-1},{-1,1},{1,1})
151 theme.white_bishop = shogi_piece("0x62", true, false, 0.93,
152 {-1,-1,1},{1,-1,1},{-1,1,1},{1,1,1})
153 theme.white_p_bishop = shogi_piece("0x42", true, true, 0.93,
154 {-1,-1,1},{1,-1,1},{-1,1,1},{1,1,1},
155 {-1,0},{1,0},{0,-1},{0,1})
156 theme.white_gold = shogi_piece("0x67", true, false, 0.9,
157 {-1,1},{0,1},{1,1},
158 {-1,0},{1,0},{0,-1})
159 theme.white_silver = shogi_piece("0x73", true, false, 0.9,
160 {-1,1},{0,1},{1,1},
161 {-1,-1},{1,-1})
162 theme.white_p_silver = shogi_piece("0x53", true, true, 0.9,
163 {-1,1},{0,1},{1,1},
164 {-1,0},{1,0},{0,-1})
165 theme.white_knight = shogi_piece("0x68", true, false, 0.86,
166 {-1,2},{1,2})
167 theme.white_p_knight = shogi_piece("0x48", true, true, 0.86,
168 {-1,1},{0,1},{1,1},
169 {-1,0},{1,0},{0,-1})
170 theme.white_lance = shogi_piece("0x6C", true, false, 0.83,
171 {0,1,1})
172 theme.white_p_lance = shogi_piece("0x4C", true, true, 0.83,
173 {-1,1},{0,1},{1,1},
174 {-1,0},{1,0},{0,-1})
175 theme.white_pawn = shogi_piece("0x70", true, false, 0.8,
176 {0,1})
177 theme.white_p_pawn = shogi_piece("0x50", true, true, 0.8,
178 {-1,1},{0,1},{1,1},
179 {-1,0},{1,0},{0,-1})
181 -- To be able to adapt this theme to chess too
182 -- should use a Free King (\u5954\u738b) instead
183 theme.black_queen = theme.black_gold
184 theme.white_queen = theme.white_gold