rutui 0.4 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/rutui.rb +3 -21
- data/lib/rutui/ansi.rb +46 -0
- data/lib/rutui/axx.rb +66 -66
- data/lib/rutui/figlet.rb +126 -127
- data/lib/rutui/objects.rb +309 -250
- data/lib/rutui/pixel.rb +20 -18
- data/lib/rutui/screen.rb +125 -83
- data/lib/rutui/screenmanager.rb +73 -60
- data/lib/rutui/sprites.rb +50 -49
- data/lib/rutui/table.rb +183 -180
- data/lib/rutui/textfield.rb +122 -99
- data/lib/rutui/theme.rb +53 -52
- metadata +16 -17
- data/lib/rutui/util.rb +0 -78
data/lib/rutui/objects.rb
CHANGED
@@ -1,285 +1,344 @@
|
|
1
1
|
# This class contains different Objects
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
3
|
+
module RuTui
|
4
|
+
## Base Object Class
|
5
|
+
# Dynamic Objects that get drawn on the Screen
|
6
|
+
# This class is used as basic class for other objects
|
7
|
+
#
|
8
|
+
class BaseObject
|
9
|
+
attr_accessor :x, :y, :width, :height, :obj
|
10
|
+
# Ex.: BaseObject.new({ :x => 1, :y => 20, :obj => [[Pixel.new(1),Pixel.new(2,4,"#")]] })
|
11
|
+
def initialize options
|
12
|
+
@obj = options[:obj]
|
13
|
+
@obj = Array.new(height){ Array.new(width) } if @obj.nil?
|
14
|
+
|
15
|
+
@x = options[:x]
|
16
|
+
@y = options[:y]
|
17
|
+
@x = 1 if @x.nil?
|
18
|
+
@y = 1 if @y.nil?
|
19
|
+
|
20
|
+
@height = @obj.size
|
21
|
+
@width = @obj[0].size
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
24
|
+
# Move object X and Y (modifiers like: -2, 2)
|
25
|
+
def move x, y; @x += x; @y += y; end
|
26
|
+
# Set object position X Y
|
27
|
+
def set_position x, y; @x = x; @y = y; end
|
28
|
+
# Set pixel on position on object
|
29
|
+
def set_pixel x, y, pixel
|
30
|
+
@obj[x][y] = pixel if !@obj[x].nil? and !@obj[x][y].nil?
|
31
|
+
end
|
32
|
+
# Get pixel by position
|
33
|
+
def get_pixel x, y
|
34
|
+
@obj[x][y] if !@obj[x].nil? and !@obj[x][y].nil?
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
# "special" each methods for objects
|
38
|
+
def each
|
39
|
+
return if @obj.nil?
|
40
|
+
@obj.each_with_index do |row,row_count|
|
41
|
+
row.each_with_index do |pixel,col_count|
|
42
|
+
yield row_count, col_count, pixel
|
43
|
+
end
|
41
44
|
end
|
42
45
|
end
|
43
46
|
end
|
44
|
-
end
|
45
47
|
|
46
|
-
## Box Class
|
47
|
-
# draw a box with borders
|
48
|
-
#
|
49
|
-
# Attributes (all accessible):
|
50
|
-
# :width * Box width
|
51
|
-
# :height * Box height
|
52
|
-
# :x * Box X MUST
|
53
|
-
# :y * Box Y MUST
|
54
|
-
# :horizontal Horizontal Pixel
|
55
|
-
# :vertical Vertical Pixel
|
56
|
-
# :corner Corner Pixel
|
57
|
-
# :fill Between the border lines (Color)
|
58
|
-
#
|
59
|
-
# Example:
|
60
|
-
# Box.new({ :x => 1, :y => 42, :width => 10, :height => 5, :corner => Pixel.new(1,2,"#"), :fill => Pixel.new(4,6,":") })
|
61
|
-
#
|
62
|
-
class Box < BaseObject
|
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
|
-
end
|
89
|
-
|
90
|
-
# Create Box
|
91
|
-
# can be recalled any time on attribute changes
|
92
|
-
def create
|
93
|
-
if !@fill.nil?
|
94
|
-
backpixel = Pixel.new(@fill.fg,@fill.bg,@fill.symbol)
|
95
|
-
obj = Array.new(@height){ Array.new(@width) { backpixel } }
|
96
|
-
else
|
97
|
-
obj = Array.new(@height){ Array.new(@width) }
|
98
|
-
end
|
99
|
-
|
100
|
-
(@width-2).times do |i|
|
101
|
-
obj[0][i+1] = @horizontal
|
102
|
-
obj[@height-1][i+1] = @horizontal
|
48
|
+
## Box Class
|
49
|
+
# draw a box with borders
|
50
|
+
#
|
51
|
+
# Attributes (all accessible):
|
52
|
+
# :width * Box width
|
53
|
+
# :height * Box height
|
54
|
+
# :x * Box X MUST
|
55
|
+
# :y * Box Y MUST
|
56
|
+
# :horizontal Horizontal Pixel
|
57
|
+
# :vertical Vertical Pixel
|
58
|
+
# :corner Corner Pixel
|
59
|
+
# :fill Between the border lines (Color)
|
60
|
+
#
|
61
|
+
# Example:
|
62
|
+
# Box.new({ :x => 1, :y => 42, :width => 10, :height => 5, :corner => Pixel.new(1,2,"#"), :fill => Pixel.new(4,6,":") })
|
63
|
+
#
|
64
|
+
class Box < BaseObject
|
65
|
+
attr_accessor :fill, :width, :height, :vertical, :horizontal, :corner
|
66
|
+
|
67
|
+
# initialize object (see above)
|
68
|
+
def initialize options
|
69
|
+
@x = options[:x]
|
70
|
+
@y = options[:y]
|
71
|
+
@width = options[:width]
|
72
|
+
@height = options[:height]
|
73
|
+
|
74
|
+
return if @x.nil? or @y.nil? or @width.nil? or @height.nil?
|
75
|
+
|
76
|
+
@fill = options[:fill]
|
77
|
+
|
78
|
+
@vertical = options[:vertical]
|
79
|
+
@horizontal = options[:horizontal]
|
80
|
+
@corner = options[:corner]
|
81
|
+
ref = Theme.get(:border)
|
82
|
+
@horizontal = Pixel.new(ref.fg,ref.bg,"-") if options[:horizontal].nil?
|
83
|
+
@vertical = Pixel.new(ref.fg,ref.bg,"|") if options[:vertical].nil?
|
84
|
+
@corner = Pixel.new(ref.fg,ref.bg,"*") if options[:corner].nil?
|
85
|
+
|
86
|
+
@width = 3 if @width < 3
|
87
|
+
@height = 3 if @height < 3
|
88
|
+
|
89
|
+
create
|
103
90
|
end
|
104
91
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
92
|
+
# Create Box
|
93
|
+
# can be recalled any time on attribute changes
|
94
|
+
def create
|
95
|
+
if !@fill.nil?
|
96
|
+
backpixel = Pixel.new(@fill.fg,@fill.bg,@fill.symbol)
|
97
|
+
obj = Array.new(@height){ Array.new(@width) { backpixel } }
|
98
|
+
else
|
99
|
+
obj = Array.new(@height){ Array.new(@width) }
|
100
|
+
end
|
109
101
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
102
|
+
(@width-2).times do |i|
|
103
|
+
obj[0][i+1] = @horizontal
|
104
|
+
obj[@height-1][i+1] = @horizontal
|
105
|
+
end
|
114
106
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
end
|
107
|
+
(@height-2).times do |i|
|
108
|
+
obj[i+1][0] = @vertical
|
109
|
+
obj[i+1][@width-1] = @vertical
|
110
|
+
end
|
120
111
|
|
112
|
+
obj[0][0] = @corner
|
113
|
+
obj[0][@width-1] = @corner
|
114
|
+
obj[@height-1][0] = @corner
|
115
|
+
obj[@height-1][@width-1] = @corner
|
121
116
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
# :x MUST
|
127
|
-
# :y MUST
|
128
|
-
# :direction :vertical|:horizontal (Default: :horizontal)
|
129
|
-
# :length
|
130
|
-
# :pixel
|
131
|
-
# :endpixel
|
132
|
-
#
|
133
|
-
class Line < BaseObject
|
134
|
-
attr_accessor :length, :pixel, :endpixel, :direction
|
135
|
-
|
136
|
-
def initialize options
|
137
|
-
@x = options[:x]
|
138
|
-
@y = options[:y]
|
139
|
-
@length = options[:length]
|
140
|
-
@direction = options[:direction]
|
141
|
-
@direction = :horizontal if @direction.nil?
|
142
|
-
|
143
|
-
return if @x.nil? or @y.nil? or @width.nil?
|
144
|
-
@pixel = options[:pixel]
|
145
|
-
@endpixel = options[:endpixel]
|
146
|
-
|
147
|
-
@pixel = Theme.get(:border) if @pixel.nil?
|
148
|
-
|
149
|
-
create
|
117
|
+
@height = obj.size
|
118
|
+
@width = obj[0].size
|
119
|
+
@obj = obj
|
120
|
+
end
|
150
121
|
end
|
151
122
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
123
|
+
|
124
|
+
## Line Class
|
125
|
+
# draw a straight line
|
126
|
+
#
|
127
|
+
# Attributes (all accessible)
|
128
|
+
# :x MUST
|
129
|
+
# :y MUST
|
130
|
+
# :direction :vertical|:horizontal (Default: :horizontal)
|
131
|
+
# :length
|
132
|
+
# :pixel
|
133
|
+
# :endpixel
|
134
|
+
#
|
135
|
+
class Line < BaseObject
|
136
|
+
attr_accessor :length, :pixel, :endpixel, :direction
|
137
|
+
|
138
|
+
def initialize options
|
139
|
+
@x = options[:x]
|
140
|
+
@y = options[:y]
|
141
|
+
@length = options[:length]
|
142
|
+
@direction = options[:direction]
|
143
|
+
@direction = :horizontal if @direction.nil?
|
144
|
+
|
145
|
+
return if @x.nil? or @y.nil? or @length.nil?
|
146
|
+
@pixel = options[:pixel]
|
147
|
+
@endpixel = options[:endpixel]
|
148
|
+
|
149
|
+
@pixel = Theme.get(:border) if @pixel.nil?
|
150
|
+
|
151
|
+
create
|
163
152
|
end
|
164
|
-
end
|
165
|
-
end
|
166
153
|
|
167
|
-
|
168
|
-
#
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
# Initialize circle (see above)
|
182
|
-
def initialize options
|
183
|
-
@x = options[:x]
|
184
|
-
@y = options[:y]
|
185
|
-
@radius = options[:radius]
|
186
|
-
@pixel = options[:pixel]
|
187
|
-
@fill = options[:fill_pixel]
|
188
|
-
|
189
|
-
@pixel = Theme.get(:border) if @pixel.nil?
|
190
|
-
|
191
|
-
return if @x.nil? or @y.nil? or @radius.nil?
|
192
|
-
|
193
|
-
@width = options[:radius]*2 # needed?
|
194
|
-
@height = @width # needed?
|
195
|
-
|
196
|
-
create
|
154
|
+
# Create Line
|
155
|
+
# can be recalled any time on attribute changes
|
156
|
+
def create
|
157
|
+
@obj = []
|
158
|
+
if @direction == :horizontal
|
159
|
+
@obj = [Array.new(@length){ @pixel }]
|
160
|
+
else
|
161
|
+
@obj = Array.new(@length){ [@pixel] }
|
162
|
+
end
|
163
|
+
if !@endpixel.nil?
|
164
|
+
@obj[0][0] = @endpixel
|
165
|
+
@obj[0][@length-1] = @endpixel
|
166
|
+
end
|
167
|
+
end
|
197
168
|
end
|
198
169
|
|
199
|
-
|
200
|
-
#
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
170
|
+
## Circle Class
|
171
|
+
# draw a circle
|
172
|
+
#
|
173
|
+
# thx to: http://rubyquiz.strd6.com/quizzes/166-circle-drawing
|
174
|
+
#
|
175
|
+
# Attributes (all accessible):
|
176
|
+
# :x MUST
|
177
|
+
# :y MUST
|
178
|
+
# :radius - Circle radius
|
179
|
+
# :pixel - Circle Pixel
|
180
|
+
# :fill - Actually just fills the box not the circle...
|
181
|
+
#
|
182
|
+
class Circle < BaseObject
|
183
|
+
attr_accessor :radius, :pixel, :fill
|
184
|
+
# Initialize circle (see above)
|
185
|
+
def initialize options
|
186
|
+
@x = options[:x]
|
187
|
+
@y = options[:y]
|
188
|
+
@radius = options[:radius]
|
189
|
+
@pixel = options[:pixel]
|
190
|
+
@fill = options[:fill_pixel]
|
191
|
+
|
192
|
+
@pixel = Theme.get(:border) if @pixel.nil?
|
193
|
+
|
194
|
+
return if @x.nil? or @y.nil? or @radius.nil?
|
195
|
+
|
196
|
+
@width = options[:radius]*2 # needed?
|
197
|
+
@height = @width # needed?
|
198
|
+
|
199
|
+
create
|
200
|
+
end
|
211
201
|
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
202
|
+
# Create Circle
|
203
|
+
# can be recalled any time on attribute changes
|
204
|
+
def create
|
205
|
+
obj = []
|
206
|
+
(0..(@radius*2)).each do |x|
|
207
|
+
(0..(@radius*2)).each do |y|
|
208
|
+
obj[y] = [] if obj[y].nil?
|
209
|
+
obj[y][x] = distance_from_center(x,y).round == @radius ? @pixel : @fill
|
210
|
+
end
|
211
|
+
end
|
212
|
+
@obj = obj
|
213
|
+
end
|
218
214
|
|
219
|
-
|
220
|
-
|
221
|
-
|
215
|
+
private
|
216
|
+
def distance_from_center(x,y)
|
217
|
+
a = calc_side(x)
|
218
|
+
b = calc_side(y)
|
219
|
+
return Math.sqrt(a**2 + b**2)
|
220
|
+
end
|
222
221
|
|
223
|
-
|
222
|
+
def calc_side(z)
|
223
|
+
z < @radius ? (@radius - z) : (z - @radius)
|
224
|
+
end
|
224
225
|
|
225
|
-
## Text Object Class
|
226
|
-
# Creates an Text element
|
227
|
-
#
|
228
|
-
# Attributes (all accessible):
|
229
|
-
# :x MUST
|
230
|
-
# :y MUST
|
231
|
-
# :text - the text to draw
|
232
|
-
# :background - Background color (0-255)
|
233
|
-
# :foreground - Foreground color (0-255)
|
234
|
-
# :rainbow - true|false|none Rainbow text
|
235
|
-
#
|
236
|
-
class Text < BaseObject
|
237
|
-
attr_accessor :bg, :fg, :text, :do_rainbow
|
238
|
-
@@rainbow = nil
|
239
|
-
|
240
|
-
def initialize options
|
241
|
-
@@rainbow = Theme.get(:rainbow) if @@rainbow.nil?
|
242
|
-
@do_rainbow = options[:rainbow]
|
243
|
-
@text = options[:text]
|
244
|
-
@x = options[:x]
|
245
|
-
@y = options[:y]
|
246
|
-
@bg = options[:background]
|
247
|
-
@fg = options[:foreground]
|
248
|
-
@bg = Theme.get(:background).bg if @bg.nil?
|
249
|
-
@fg = Theme.get(:textcolor) if @fg.nil?
|
250
|
-
return if @x.nil? or @y.nil?
|
251
|
-
|
252
|
-
@height = 1
|
253
|
-
create
|
254
226
|
end
|
255
227
|
|
256
|
-
|
257
|
-
#
|
258
|
-
|
259
|
-
|
260
|
-
|
228
|
+
## Text Object Class
|
229
|
+
# Creates an Text element
|
230
|
+
#
|
231
|
+
# Attributes (all accessible):
|
232
|
+
# :x MUST
|
233
|
+
# :y MUST
|
234
|
+
# :text - the text to draw
|
235
|
+
# :background - Background color (0-255)
|
236
|
+
# :foreground - Foreground color (0-255)
|
237
|
+
# :max_width - with max_width defined text breaks to next line automatically
|
238
|
+
# :rainbow - true|false|none Rainbow text
|
239
|
+
# :bold - true|false|nil Bold text
|
240
|
+
# :italic - true|false|nil Italic text (Not supported everywhere)
|
241
|
+
# :thin - true|false|nil Thin text (Not supported everywhere)
|
242
|
+
# :underline - true|false|nil Underline text (Not supported everywhere)
|
243
|
+
# :blink - true|false|nil Blink text (Not supported everywhere)
|
244
|
+
#
|
245
|
+
class Text < BaseObject
|
246
|
+
attr_accessor :bg, :fg, :text, :do_rainbow, :bold,
|
247
|
+
:thin, :italic, :underline, :blink, :max_width, :pixel
|
248
|
+
@@rainbow = nil
|
249
|
+
|
250
|
+
def initialize options
|
251
|
+
@do_rainbow = options[:rainbow]
|
252
|
+
if @do_rainbow
|
253
|
+
@@rainbow = Theme.get(:rainbow) if @@rainbow.nil?
|
254
|
+
end
|
255
|
+
@text = options[:text]
|
256
|
+
@x = options[:x]
|
257
|
+
@y = options[:y]
|
258
|
+
@pixel = options[:pixel]
|
259
|
+
@bg = options[:background]
|
260
|
+
@fg = options[:foreground]
|
261
|
+
if !@pixel.nil?
|
262
|
+
@bg = @pixel.bg
|
263
|
+
@fg = @pixel.fg
|
264
|
+
end
|
265
|
+
@bg = -1 if @bg.nil?
|
266
|
+
@fg = Theme.get(:textcolor) if @fg.nil?
|
267
|
+
@bold = options[:bold] || false
|
268
|
+
@thin = options[:thin] || false
|
269
|
+
@italic = options[:italic] || false
|
270
|
+
@underline = options[:underline] || false
|
271
|
+
@blink = options[:blink] || false
|
272
|
+
@max_width = options[:max_width]
|
273
|
+
return if @x.nil? or @y.nil?
|
274
|
+
|
275
|
+
@height = 1
|
276
|
+
create
|
261
277
|
end
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
278
|
+
|
279
|
+
# Create Text
|
280
|
+
# can be recalled any time on attribute changes
|
281
|
+
def create
|
266
282
|
if @do_rainbow
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
283
|
+
rainbow = 0
|
284
|
+
end
|
285
|
+
@width = @text.size
|
286
|
+
@obj = []
|
287
|
+
|
288
|
+
texts = [@text]
|
289
|
+
if !@max_width.nil?
|
290
|
+
texts = @text.chars.each_slice(@max_width).map(&:join)
|
291
|
+
end
|
292
|
+
|
293
|
+
texts.each do |l|
|
294
|
+
tmp = []
|
295
|
+
l.split("").each do |t|
|
296
|
+
t = RuTui::Ansi.bold(t) if @bold
|
297
|
+
t = RuTui::Ansi.thin(t) if @thin
|
298
|
+
t = RuTui::Ansi.italic(t) if @italic
|
299
|
+
t = RuTui::Ansi.underline(t) if @underline
|
300
|
+
t = RuTui::Ansi.blink(t) if @blink
|
301
|
+
if @do_rainbow
|
302
|
+
tmp << Pixel.new(@@rainbow[rainbow],@bg,t)
|
303
|
+
rainbow += 1
|
304
|
+
rainbow = 0 if rainbow >= @@rainbow.size
|
305
|
+
else
|
306
|
+
tmp << Pixel.new(@fg,@bg,t)
|
307
|
+
end
|
308
|
+
end
|
309
|
+
@obj << tmp
|
272
310
|
end
|
273
311
|
end
|
274
|
-
@obj << tmp
|
275
|
-
end
|
276
312
|
|
277
|
-
|
278
|
-
|
279
|
-
|
313
|
+
def set_text text
|
314
|
+
@text = text
|
315
|
+
create
|
316
|
+
end
|
317
|
+
|
318
|
+
def get_text
|
319
|
+
@text
|
320
|
+
end
|
280
321
|
end
|
281
322
|
|
282
|
-
|
283
|
-
|
323
|
+
## Dot Object Class
|
324
|
+
# Single pixel element
|
325
|
+
#
|
326
|
+
# Attributes (all accessible):
|
327
|
+
# :x MUST
|
328
|
+
# :y MUST
|
329
|
+
# :pixel MUST - Pixel
|
330
|
+
#
|
331
|
+
class Dot < BaseObject
|
332
|
+
def initialize options
|
333
|
+
@x = options[:x]
|
334
|
+
@y = options[:y]
|
335
|
+
@pixel = options[:pixel]
|
336
|
+
return if @x.nil? or @y.nil? or @pixel.nil?
|
337
|
+
create
|
338
|
+
end
|
339
|
+
|
340
|
+
def create
|
341
|
+
@obj = [[ @pixel ]]
|
342
|
+
end
|
284
343
|
end
|
285
344
|
end
|