-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlui.rb
186 lines (175 loc) · 3.97 KB
/
lui.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
module TestShow
module_function
def create_background
bitmap = Bitmap.new(640, 480)
color = [Color.new(63,63,63,63), Color.new(127,127,127,63)]
k = 0
(640 / 32).times{|x|
(480 / 32).times{|y|
bitmap.fill_rect(x * 32, y * 32, 32, 32, color[k])
k ^= 1
}
}
bitmap
end
def show(bitmap)
t = Sprite.new
t.bitmap = create_background
s = Sprite.new
s.bitmap = bitmap
loop do
Graphics.update
Input.update
s.update
yield if block_given?
break if Input.trigger?(Input::B)
end
end
end
class Target < Bitmap
attr_accessor :clip
def roi # region of interest
self.clip || self.rect
end
def text(text, align = 1)
draw_text( self.roi, text, align )
end
def blt(target, opacity = 255)
super( self.roi.x, self.roi.y, target, target.roi, opacity )
end
def stretch_blt(target, opacity = 255)
super( self.roi, target, target.roi, opacity )
end
def fill(color)
fill_rect( self.roi, color )
end
def clone_roi
roi = self.roi
target = Target.new(roi.width, roi.height)
target.blt(self)
target
end
def boundary
self.roi
end
end
class UI
def parse
@chars.each do |x|
case x
when 0
@list.push @text
@list.push :draw
@text = ""
when 1
@list.push @text
@list.push :justpush
@text = ""
when 2
@list.push @text
@list.push :teval
@text = ""
when 3
@list.push @text
@list.push :seval
@text = ""
when 4
@list.push @text
@list.push :sevalesc
@text = ""
when 5
@list.push :blt
when 10
@list.push @text
@list.push :newline
@text = ""
else
@text << x.chr
end
end
end
def do_render
@stack = []
@rects = []
@rects_named = {}
@id = 0
@cx, @cy = 0, 0
@cw, @ch = nil, nil
@hw = 0
@list.each{|x|
case x
when :draw
text = @stack.pop
rect = text_size(text)
rect.x, rect.y = @cx, @cy
if @cw && @ch
rect.width, rect.height = @cw, @ch
@cw = @ch = nil
end
unless @nofocus
@rects[@id] = rect
@id += 1
if @name
@rects_named[@name] = rect
@name = nil
end
end
@cx += rect.width
@hw = [rect.height, @hw].max
@target.clip = rect
@target.text(text)
when :teval
@target.instance_eval @stack.pop
when :seval
instance_eval @stack.pop
when :sevalesc
instance_eval escape @stack.pop
when :newline
@cy += @hw
@cx = 0
@hw = 0
when :blt
name = @stack.pop
newtarget = Target.new(name)
rect = text_size(newtarget)
rect.x, rect.y = @cx, @cy
if @cw && @ch
rect.width, rect.height = @cw, @ch
newtarget.clip = Rect.new(0, 0, @cw, @ch)
@cw = @ch = nil
end
unless @nofocus
@rects[@id] = rect
@id += 1
if @name
@rects_named[@name] = rect
@name = nil
end
end
@cx += rect.width
@hw = [rect.height, @hw].max
@target.clip = rect
@target.blt(newtarget)
newtarget.dispose
when :justpush
else
@stack.push x
end
}
end
def text_size(x)
if x.respond_to?(:boundary)
x.boundary
else
@target.text_size(x)
end
end
def render(target, text)
@target = target
@chars = text.unpack('C*')
@list = []
@text = ""
parse
do_render
end
end