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