rutui 0.4 → 0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,285 +1,344 @@
1
1
  # This class contains different Objects
2
2
 
3
- ## Base Object Class
4
- # Dynamic Objects that get drawn on the Screen
5
- # This class is used as basic class for other objects
6
- #
7
- class BaseObject
8
- attr_accessor :x, :y, :width, :height, :obj
9
- # Ex.: BaseObject.new({ :x => 1, :y => 20, :obj => [[Pixel.new(1),Pixel.new(2,4,"#")]] })
10
- def initialize options
11
- @obj = options[:obj]
12
- @obj = Array.new(height){ Array.new(width) } if @obj.nil?
13
-
14
- @x = options[:x]
15
- @y = options[:y]
16
- @x = 1 if @x.nil?
17
- @y = 1 if @y.nil?
18
-
19
- @height = @obj.size
20
- @width = @obj[0].size
21
- end
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
- # Move object X and Y (modifiers like: -2, 2)
24
- def move x, y; @x += x; @y += y; end
25
- # Set object position X Y
26
- def set_position x, y; @x = x; @y = y; end
27
- # Set pixel on position on object
28
- def set_pixel x, y, pixel
29
- @obj[x][y] = pixel if !@obj[x].nil? and !@obj[x][y].nil?
30
- end
31
- # Get pixel by position
32
- def get_pixel x, y
33
- @obj[x][y] if !@obj[x].nil? and !@obj[x][y].nil?
34
- end
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
- # "special" each methods for objects
37
- def each
38
- @obj.each_with_index do |row,row_count|
39
- row.each_with_index do |pixel,col_count|
40
- yield row_count, col_count, pixel
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
- attr_accessor :fill, :width, :height, :vertical, :horizontal, :corner
64
-
65
- # initialize object (see above)
66
- def initialize options
67
- @x = options[:x]
68
- @y = options[:y]
69
- @width = options[:width]
70
- @height = options[:height]
71
-
72
- return if @x.nil? or @y.nil? or @width.nil? or @height.nil?
73
-
74
- @fill = options[:fill]
75
-
76
- @vertical = options[:vertical]
77
- @horizontal = options[:horizontal]
78
- @corner = options[:corner]
79
- ref = Theme.get(:border)
80
- @horizontal = Pixel.new(ref.fg,ref.bg,"-") if options[:horizontal].nil?
81
- @vertical = Pixel.new(ref.fg,ref.bg,"|") if options[:vertical].nil?
82
- @corner = Pixel.new(ref.fg,ref.bg,"*") if options[:corner].nil?
83
-
84
- @width = 3 if @width < 3
85
- @height = 3 if @height < 3
86
-
87
- create
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
- (@height-2).times do |i|
106
- obj[i+1][0] = @vertical
107
- obj[i+1][@width-1] = @vertical
108
- end
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
- obj[0][0] = @corner
111
- obj[0][@width-1] = @corner
112
- obj[@height-1][0] = @corner
113
- obj[@height-1][@width-1] = @corner
102
+ (@width-2).times do |i|
103
+ obj[0][i+1] = @horizontal
104
+ obj[@height-1][i+1] = @horizontal
105
+ end
114
106
 
115
- @height = obj.size
116
- @width = obj[0].size
117
- @obj = obj
118
- end
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
- ## Line Class
123
- # draw a straight line
124
- #
125
- # Attributes (all accessible)
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
- # Create Line
153
- # can be recalled any time on attribute changes
154
- def create
155
- if @direction == :horizontal
156
- @obj = [Array.new(@length){ @pixel }]
157
- else
158
- @obj = Array.new(@length){ [@pixel] }
159
- end
160
- if !@endpixel.nil?
161
- @obj[0][0] = @endpixel
162
- @obj[0][@length-1] = @endpixel
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
- ## Circle Class
168
- # draw a circle
169
- #
170
- # thx to: http://rubyquiz.strd6.com/quizzes/166-circle-drawing
171
- #
172
- # Attributes (all accessible):
173
- # :x MUST
174
- # :y MUST
175
- # :radius - Circle radius
176
- # :pixel - Circle Pixel
177
- # :fill - Actually just fills the box not the circle...
178
- #
179
- class Circle < BaseObject
180
- attr_accessor :radius, :pixel, :fill
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
- # Create Circle
200
- # can be recalled any time on attribute changes
201
- def create
202
- obj = []
203
- (0..(@radius*2)).each do |x|
204
- (0..(@radius*2)).each do |y|
205
- obj[y] = [] if obj[y].nil?
206
- obj[y][x] = distance_from_center(x,y).round == @radius ? @pixel : @fill
207
- end
208
- end
209
- @obj = obj
210
- end
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
- private
213
- def distance_from_center(x,y)
214
- a = calc_side(x)
215
- b = calc_side(y)
216
- return Math.sqrt(a**2 + b**2)
217
- end
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
- def calc_side(z)
220
- z < @radius ? (@radius - z) : (z - @radius)
221
- end
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
- end
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
- # Create Text
257
- # can be recalled any time on attribute changes
258
- def create
259
- if @do_rainbow
260
- rainbow = 0
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
- @width = @text.size
263
- @obj = []
264
- tmp = []
265
- @text.split("").each do |t|
278
+
279
+ # Create Text
280
+ # can be recalled any time on attribute changes
281
+ def create
266
282
  if @do_rainbow
267
- tmp << Pixel.new(@@rainbow[rainbow],@bg,t)
268
- rainbow += 1
269
- rainbow = 0 if rainbow >= @@rainbow.size
270
- else
271
- tmp << Pixel.new(@fg,@bg,t)
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
- def set_text text
278
- @text = text
279
- create
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
- def get_text
283
- @text
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