2 # StrokeFont library by eto 2003-0311
9 class StrokesRenderer #======================================================================
17 def init() @start_time = Time.now; end
18 def set_strokes(strokes)
23 @strokes.strokes.each_with_index {|stroke, index|
24 draw_delay(stroke, index)
27 def draw_alpha(stroke, time)
31 stroke.points.each {|x, y|
33 colorHSV(@hsv[0], @hsv[1], @hsv[2], a*100.0)
34 line(px, py, x, y) if (px != 0 || py != 0) #最初の点ではない
39 def draw_delay(stroke, index)
41 @start_time = Time.now if @start_time == nil
42 diff = now - @start_time #開始からの秒数がはいる
43 draw_alpha(stroke, diff - index*0.3)
47 class Stroke #====================================================================== 一本の線
57 return @length if @length
62 len += Math.sqrt((x-px)*(x-px)+(y-py)*(y-py))
71 class Strokes #====================================================================== 複数の線
74 @px1, @py1, @px2, @py2 = 0, 0, 0, 0
75 @x1, @y1, @x2, @y2 = 0, 0, 0, 0
79 def add_line(x1, y1, x2, y2)
80 if (@px != x1 || @py != y1) #以前の点とつながっていなかったら、
81 @strokes << Stroke.new
82 @strokes.last.add_point(x1, y1)
84 @strokes.last.add_point(x2, y2)
89 class CodeSelector #======================================================================
90 WIDTH, HEIGHT = 256, 256
92 def initialize(cx=0, cy=0)
95 @x1, @y1 = @cx-@s*WIDTH/2, @cy-@s*HEIGHT/2
96 @x2, @y2 = @cx+@s*WIDTH/2, @cy+@s*HEIGHT/2
97 @px, @py = @cx, @cy #とりあえず中心が開始点
98 @pw, @ph, @pr = 30, 30, 10
106 colorHSV(0, 0, 100, 10) #まずは下敷きになる枠を書きます。
107 rect(@x1, @y1, @x2, @y2)
109 colorHSV(0, 0, 100, 50)
110 b = 8; s = @s*WIDTH/b
112 line(@x1, @y1+n*s, @x2, @y1+n*s)
113 line(@x1+n*s, @y1, @x1+n*s, @y2)
115 colorHSV(0, 100, 100, 100) # 次にポインターを書きます
116 circle(@px, @py, @pr)
117 line(@px-@pw/2, @py, @px+@pw/2, @py)
118 line(@px, @py-@ph/2, @px, @py+@ph/2)
124 if @dragging || @onkeydown
126 @px, @py = x, y #p [x, y]
127 @px = @x1 if @px < @x1
128 @py = @y1 if @py < @y1
129 @px = @x2-1 if @x2-1 < @px
130 @py = @y2-1 if @y2-1 < @py
137 x = ((@px - @x1)/@s).to_i
138 y = HEIGHT-1 - ((@py - @y1)/@s).to_i
144 printf("%02x %02x %04x\n", x, y, @code)
150 colorHSV(0, 100, 100, 100)
152 x, y = code_to_xy(code)
158 cy = HEIGHT - (code / WIDTH) #intになる?
163 def length(x, y) Math.sqrt(x*x + y*y) end
164 def onMouseDown(x, y)
165 if length(@px-x, @py-y) < @pr
169 def onMouseUp(x, y) @dragging = false end
187 if $0 == __FILE__ #======================================================================
188 $LOAD_PATH << '../ruby/src'
196 window(-300,-300,300,300)
199 @cs = CodeSelector.new
200 @csf1 = CSFFont.new() #普通の文字
201 @csf2 = CSFFont.new(CSF_KOUKOTSU_FILE) #甲骨文字
203 @kage = KageFont.new()
208 @changed = @cs.onMouse(mouseX, mouseY) #変化があったか?
210 @cs.show_list(@kage.cache_list)
223 char = Character.get(code)
225 @kage.init(code) if @changed
226 @kage.print(code) if @changed
231 char = Character.get(ucs)
233 j = char.japanese_jisx0208
236 csf = @key == 1 ? @csf1 : @csf2
237 csf.init(code) if @changed
238 csf.print(code) if @changed
242 def onMouseDown(x, y) @cs.onMouseDown(x, y)end
243 def onMouseUp(x, y) @cs.onMouseUp(x, y)end
251 #----------------------------------------------------------------------end.