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