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