n.c.
[chise/ruby.git] / chise / codeviewer.rb
1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2
3 require "chise/stroke"
4
5 module StrokeFont
6   class CodeViewer
7     WIDTH, HEIGHT = 256, 256
8     SCALE = 2
9
10     def initialize(cx=0, cy=0)
11       @cx, @cy = cx, cy
12       @s = SCALE
13       @x1, @y1 = @cx-@s*WIDTH/2, @cy-@s*HEIGHT/2
14       @x2, @y2 = @cx+@s*WIDTH/2, @cy+@s*HEIGHT/2
15       @px, @py = @cx, @cy #とりあえず中心が開始点
16       @pw, @ph, @pr = 30, 30, 10
17       @dragging = false
18       @onkeydown = false
19       @code = 0
20       calc_code
21     end
22     attr_reader :code
23
24     def draw
25       colorHSV(0, 0, 100, 10) #まずは下敷きになる枠を書きます。
26       rect(@x1, @y1, @x2, @y2)
27       lineWidth(1)
28       colorHSV(0, 0, 100, 50)
29       b = 8; s = @s*WIDTH/b
30       (0..b).each {|n|
31         line(@x1, @y1+n*s, @x2, @y1+n*s)
32         line(@x1+n*s, @y1, @x1+n*s, @y2)
33       }
34       colorHSV(0, 100, 100, 100) # 次にポインターを書きます
35       circle(@px, @py, @pr)
36       line(@px-@pw/2, @py, @px+@pw/2, @py)
37       line(@px, @py-@ph/2, @px, @py+@ph/2)
38     end
39
40     def onMouse(x, y)
41       if @onkeydown
42         x, y = @px, @py
43       end
44       if @dragging || @onkeydown
45         @onkeydown = false
46         @px, @py = x, y      #p [x, y]
47         @px = @x1 if @px < @x1
48         @py = @y1 if @py < @y1
49         @px = @x2-1 if @x2-1 < @px
50         @py = @y2-1 if @y2-1 < @py
51         return calc_code
52       else
53         return false
54       end
55     end
56
57     def calc_code()
58       x = ((@px - @x1)/@s).to_i
59       y = HEIGHT-1 - ((@py - @y1)/@s).to_i
60       code = x + y*WIDTH
61       if @code != code
62         @code = code
63         return true #changed
64         #p [x, y, code]
65         printf("%02x %02x %04x\n", x, y, @code)
66       else
67         return false
68       end
69     end
70
71     def show_list(list)
72       colorHSV(0, 100, 100, 100)
73       list.each {|code|
74         x, y = code_to_xy(code)
75         rect(x, y, x+2, y-2)
76       }
77     end
78
79     def code_to_xy(code)
80       cx = code % WIDTH
81       cy = HEIGHT - (code / WIDTH) #intになる?
82       x = cx * SCALE + @x1
83       y = cy * SCALE + @y1
84       return x, y
85     end
86
87     def length(x, y)    Math.sqrt(x*x + y*y)  end
88
89     def onMouseDown(x, y)
90       if length(@px-x, @py-y) < @pr
91         @dragging = true
92       end
93     end
94
95     def onMouseUp(x, y)    @dragging = false  end
96
97     def onKeyDown(key)
98       @onkeydown = true
99       case key
100       when 273
101         @py += @s
102       when 274
103         @py -= @s
104       when 276
105         @px -= @s
106       when 275
107         @px += @s
108       end
109     end
110   end
111 end
112
113 if $0 == __FILE__
114   $LOAD_PATH << "../../src"
115   require "chise"
116   include CHISE
117   require "chise/stroke"
118   include StrokeFont
119
120   def setup
121     useSmooth()
122     window(-300,-300,300,300)
123     background 0,0,20
124     useFramerate(30)
125     @cs = CodeViewer.new
126     @csf1 = CSFFont.new()  #普通の文字
127     @csf2 = CSFFont.new(CSF_KOUKOTSU_FILE) #甲骨文字
128     @key = 1
129     @kage = KageFont.new()
130     @changed = nil
131   end
132
133   def display
134     @changed = @cs.onMouse(mouseX, mouseY) #変化があったか?
135     @cs.draw
136     @cs.show_list(@kage.cache_list)
137     code = @cs.code
138
139     push
140     scale 0.2
141     translate -500,-500
142     lineWidth(2)
143     draw_kage(code)
144     draw_csf(code)
145     pop
146   end
147
148   def draw_kage(code)
149     char = Character.get(code)
150     return if char.nil?
151     @kage.init(code) if @changed
152     @kage.print(code) if @changed
153     @kage.draw(code)
154   end
155
156   def draw_csf(ucs)
157     char = Character.get(ucs)
158     return if char.nil?
159     j = char.japanese_jisx0208
160     return if j.nil?
161     code = j
162     csf = @key == 1 ? @csf1 : @csf2
163     csf.init(code) if @changed
164     csf.print(code) if @changed
165     csf.draw(code)
166   end
167
168   def onMouseDown(x, y)  @cs.onMouseDown(x, y)end
169   def onMouseUp(x, y)  @cs.onMouseUp(x, y)end
170   def onKeyDown(key)
171     @key = key
172     @cs.onKeyDown(key)
173   end
174   mainloop
175 end