rutui 0.1 → 0.2

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.
Files changed (2) hide show
  1. data/{rutui.rb → lib/rutui.rb} +209 -47
  2. metadata +20 -41
@@ -7,7 +7,7 @@
7
7
  # Author: Roman Pramberger (roman.pramberger@gmail.com)
8
8
  # License: MIT
9
9
  #
10
- # Son Jul 22 16:26:42 CEST 2012
10
+ # Sam Okt 27 19:04:33 CEST 2012
11
11
  #
12
12
  class RuTui
13
13
  class Color
@@ -62,6 +62,57 @@ class Utils
62
62
  system("tput cnorm")
63
63
  end
64
64
  end
65
+ class Pixel
66
+ attr_accessor :fg, :bg, :symbol
67
+ def initialize foreground = 15, background = nil, symbol = " "
68
+ @fg = foreground
69
+ @bg = background
70
+ @symbol = symbol
71
+ end
72
+ def self.random sym = "#"
73
+ Pixel.new(rand(255),rand(255),sym)
74
+ end
75
+ end
76
+ class Theme
77
+ @@themes = {}
78
+ @@use = :default
79
+ # init themes
80
+ def self.init
81
+ @@themes[:default] = {
82
+ :background => Pixel.new(236,234,":"),
83
+ :border => Pixel.new(144,234,"-"),
84
+ :textcolor => 11,
85
+ :rainbow => [1,3,11,2,4,5]
86
+ }
87
+ @@themes[:light] = {
88
+ :background => Pixel.new(251,253,":"),
89
+ :border => Pixel.new(237,253,"-"),
90
+ :textcolor => 0,
91
+ :rainbow => [1,3,11,2,4,5]
92
+ }
93
+ end
94
+ # Create new theme
95
+ def self.create name, theme
96
+ @@themes[name] = theme
97
+ end
98
+ # Delete theme
99
+ def self.delete name
100
+ @@themes.delete(name)
101
+ end
102
+ # Set value from current theme
103
+ def self.set key, val
104
+ @@themes[@@use][key] = val
105
+ end
106
+ # Get value from current theme
107
+ def self.get val
108
+ @@themes[@@use][val]
109
+ end
110
+ # set current theme
111
+ def self.use name
112
+ @@use = name if !@@themes[name].nil?
113
+ end
114
+ end
115
+ Theme.init
65
116
  class BaseObject
66
117
  attr_accessor :x, :y, :width, :height, :obj
67
118
  # Ex.: BaseObject.new({ :x => 1, :y => 20, :obj => [[Pixel.new(1),Pixel.new(2,4,"#")]] })
@@ -81,11 +132,19 @@ class BaseObject
81
132
  def set_pixel x, y, pixel
82
133
  @obj[x][y] = pixel if !@obj[x].nil? and !@obj[x][y].nil?
83
134
  end
135
+ # Get pixel by position
136
+ def get_pixel x, y
137
+ @obj[x][y] if !@obj[x].nil? and !@obj[x][y].nil?
138
+ end
84
139
  # "special" each methods for objects
85
- def each
86
- @obj.each_with_index do |row,row_count|
87
- row.each_with_index do |pixel,col_count|
88
- yield row_count, col_count, pixel
140
+ def each
141
+ if !@obj.nil?
142
+ @obj.each_with_index do |row,row_count|
143
+ if !row.nil?
144
+ row.each_with_index do |pixel,col_count|
145
+ yield row_count, col_count, pixel
146
+ end
147
+ end
89
148
  end
90
149
  end
91
150
  end
@@ -103,9 +162,10 @@ class Box < RuTui::BaseObject
103
162
  @vertical = options[:vertical]
104
163
  @horizontal = options[:horizontal]
105
164
  @corner = options[:corner]
106
- @horizontal = Pixel.new(3,0,"-") if options[:horizontal].nil?
107
- @vertical = Pixel.new(3,0,"|") if options[:vertical].nil?
108
- @corner = Pixel.new(3,0,"*") if options[:corner].nil?
165
+ ref = Theme.get(:border)
166
+ @horizontal = Pixel.new(ref.fg,ref.bg,"-") if options[:horizontal].nil?
167
+ @vertical = Pixel.new(ref.fg,ref.bg,"|") if options[:vertical].nil?
168
+ @corner = Pixel.new(ref.fg,ref.bg,"*") if options[:corner].nil?
109
169
  @width = 3 if @width < 3
110
170
  @height = 3 if @height < 3
111
171
  create
@@ -147,8 +207,8 @@ class Line < BaseObject
147
207
  return if @x.nil? or @y.nil? or @width.nil?
148
208
  @pixel = options[:pixel]
149
209
  @endpixel = options[:endpixel]
150
- @pixel = Pixel.new(2) if @pixel.nil?
151
- create
210
+ @pixel = Theme.get(:border) if @pixel.nil?
211
+ self.create
152
212
  end
153
213
  # Create Line
154
214
  # can be recalled any time on attribute changes
@@ -173,6 +233,7 @@ class Circle < BaseObject
173
233
  @radius = options[:radius]
174
234
  @pixel = options[:pixel]
175
235
  @fill = options[:fill_pixel]
236
+ @pixel = Theme.get(:border) if @pixel.nil?
176
237
  return if @x.nil? or @y.nil? or @radius.nil?
177
238
  @width = options[:radius]*2 # needed?
178
239
  @height = @width # needed?
@@ -202,16 +263,17 @@ class Circle < BaseObject
202
263
  end
203
264
  class Text < BaseObject
204
265
  attr_accessor :bg, :fg, :text, :do_rainbow
205
- @@rainbow = [1,3,11,2,4,5]
266
+ @@rainbow = nil
206
267
  def initialize options
268
+ @@rainbow = Theme.get(:rainbow) if @@rainbow.nil?
207
269
  @do_rainbow = options[:rainbow]
208
270
  @text = options[:text]
209
271
  @x = options[:x]
210
272
  @y = options[:y]
211
273
  @bg = options[:background]
212
274
  @fg = options[:foreground]
213
- @bg = 236 if @bg.nil?
214
- @fg = 245 if @fg.nil?
275
+ @bg = Theme.get(:background).bg if @bg.nil?
276
+ @fg = Theme.get(:textcolor) if @fg.nil?
215
277
  return if @x.nil? or @y.nil?
216
278
  @height = 1
217
279
  create
@@ -241,20 +303,9 @@ class Text < BaseObject
241
303
  create
242
304
  end
243
305
  end
244
- class Pixel
245
- attr_accessor :fg, :bg, :symbol
246
- def initialize foreground = 15, background = nil, symbol = " "
247
- @fg = foreground
248
- @bg = background
249
- @symbol = symbol
250
- end
251
- def self.random sym = "#"
252
- Pixel.new(rand(255),rand(255),sym)
253
- end
254
- end
255
306
  class Screen
256
307
  # Initialize me with a default pixel, if you want
257
- def initialize default_pixel = Pixel.new(238,236,":")
308
+ def initialize default_pixel = Theme.get(:background)
258
309
  size = Utils.winsize
259
310
  @smap = Array.new(size[0]){ Array.new(size[1]) }
260
311
  @map = @smap.dup
@@ -286,9 +337,17 @@ class Screen
286
337
  def add_static object
287
338
  @statics << object if !@statics.include? object
288
339
  object.each do |ri,ci,pixel|
289
- @smap[object.y+ri][object.x+ci] = pixel if !pixel.nil? and object.y+ri > 0 and object.y+ci > 0
340
+ @smap[object.y+ri][object.x+ci] = pixel if !pixel.nil? and object.y+ri >= 0 and object.x+ci >= 0
290
341
  end
291
342
  end
343
+ # Set pixel on map
344
+ def set_pixel x,y,val
345
+ @smap[y][x] = val
346
+ end
347
+ # Get pixel from map
348
+ def get_pixel x,y
349
+ @smap[y][x]
350
+ end
292
351
  # add dynamic object
293
352
  def add object
294
353
  @objects << object
@@ -357,10 +416,9 @@ class ScreenManager
357
416
  @@screens[name]
358
417
  end
359
418
  # Refit screen size
360
- def refit
419
+ def self.refit
361
420
  size = Utils.winsize
362
421
  if @autofit or size != @lastsize
363
- File.open("log.log", 'w') {|f| f.write("#{size[0]}-#{size[1]}\n") }
364
422
  @@screens[@@current].rescreen
365
423
  @lastsize = size
366
424
  end
@@ -375,6 +433,7 @@ class ScreenManager
375
433
  def self.loop options
376
434
  autodraw = options[:autodraw]
377
435
  @autofit = options[:autofit]
436
+ @autofit = true if @autofit.nil?
378
437
  @lastsize = nil
379
438
  Utils.init
380
439
  ScreenManager.draw
@@ -385,17 +444,35 @@ class ScreenManager
385
444
  ScreenManager.draw if autodraw
386
445
  end
387
446
  end
447
+ def self.draw_loop options
448
+ autofit = options[:autofit]
449
+ while true
450
+ key = Utils.gets
451
+ yield
452
+ ScreenManager.refit if @autofit
453
+ ScreenManager.draw
454
+ end
455
+ end
456
+ def self.input_loop options
457
+ Thread.new {
458
+ while true
459
+ key = Utils.gets
460
+ yield key
461
+ end
462
+ }
463
+ end
388
464
  end
389
465
  class Figlet < BaseObject
390
466
  attr_accessor :text, :rainbow, :font, :colors
391
- # Figlet font: '!" #$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz[|]~ÄÖÜäöüß'
392
- @@chars = '!!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz[|]~ÄÖÜäöüß'.split("")
393
- @@rainbow = [1,3,11,2,4,5]
467
+ # Figlet font: '!" #$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz[|]'
468
+ @@chars = '!!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz[|]'.split("")
469
+ @@rainbow = nil
394
470
  @@fonts = {}
395
471
  # Initialize (see above)
396
472
  def initialize options
397
473
  @x = options[:x]
398
474
  @y = options[:y]
475
+ @@rainbow = Theme.get(:rainbow) if @@rainbow.nil?
399
476
  return if @x.nil? or @y.nil?
400
477
  @font = options[:font]
401
478
  @text = options[:text]
@@ -403,7 +480,7 @@ class Figlet < BaseObject
403
480
  @space = options[:space]
404
481
  @space = 0 if @space.nil?
405
482
  @colors = options[:colors]
406
- @colors = [Pixel.new(15)] if @colors.nil?
483
+ @colors = [Pixel.new(Theme.get(:textcolor))] if @colors.nil?
407
484
  create
408
485
  end
409
486
 
@@ -517,34 +594,119 @@ class Axx < BaseObject
517
594
  end
518
595
  class Axx < BaseObject
519
596
  def initialize options
520
- @x = options[:x]
521
- @y = options[:y]
522
597
  @file = options[:file]
523
- @attr, @xx, @yy, @save_x, @save_y, @attr = 0,0,0,0,0,0
524
- return if @x.nil? or @y.nil? or @file.nil?
598
+ return if @file.nil?
525
599
  return if !File.exists? @file
526
600
  @img = File.open(@file).read
527
- p @img
528
601
  parse
529
602
  end
530
603
  def parse
531
604
  out = []
532
605
  @img.split("\n").each_with_index do |line,li|
533
- out[li] << []
534
- line.split("|").each_with_index do |elem,ei|
535
- ele = elem.split(";")
536
- if ele.size == 3
537
- out[li][ei] = Pixel.new(elem[1].to_i,elem[2].to_i,elem[0])
538
- elsif ele.size == 2
539
- out[li][ei] = Pixel.new(elem[1].to_i,nil,elem[0])
540
- else
541
- out[li][ei] = Pixel.new(nil,nil,elem[0])
606
+ if !line.match(/^#/)
607
+ out[li] = []
608
+ line.split("|").each_with_index do |elem,ei|
609
+ ele = elem.split(":")
610
+ if ele.size == 3
611
+ out[li][ei] = Pixel.new(ele[1].to_i,ele[2].to_i,ele[0])
612
+ elsif ele.size == 2
613
+ out[li][ei] = Pixel.new(ele[1].to_i,nil,ele[0])
614
+ else
615
+ if ele[0] == "nil"
616
+ out[li][ei] = nil
617
+ else
618
+ out[li][ei] = Pixel.new(nil,nil,ele[0])
619
+ end
620
+ end
542
621
  end
543
622
  end
544
623
  end
624
+ out.each do |o|
625
+ out.delete(o) if o.nil?
626
+ end
545
627
  @obj = out
546
628
  @height = out.size
547
- @width = out[0].size
629
+ @width = out.size
630
+ end
631
+ def self.parse data
632
+ out = []
633
+ data.split("\n").each_with_index do |line,li|
634
+ if !line.match(/^#/)
635
+ out[li] = []
636
+ line.split("|").each_with_index do |elem,ei|
637
+ ele = elem.split(":")
638
+ if ele.size == 3
639
+ out[li][ei] = Pixel.new(ele[1].to_i,ele[2].to_i,ele[0])
640
+ elsif ele.size == 2
641
+ out[li][ei] = Pixel.new(ele[1].to_i,nil,ele[0])
642
+ else
643
+ if ele[0] == "nil"
644
+ out[li][ei] = nil
645
+ else
646
+ out[li][ei] = Pixel.new(nil,nil,ele[0])
647
+ end
648
+ end
649
+ end
650
+ end
651
+ end
652
+ out.each do |o|
653
+ out.delete(o) if o.nil? or o == []
654
+ end
655
+ return out
656
+ end
657
+ end
658
+ class Sprite < BaseObject
659
+ attr_accessor :tick, :current, :sprites, :ticks, :object
660
+ @@spr = []
661
+ def initialize options
662
+ @x = options[:x]
663
+ @y = options[:y]
664
+ @x = 0 if @x.nil?
665
+ @y = 0 if @y.nil?
666
+ @tick = 0
667
+ @object = options[:obj]
668
+ @file = options[:file]
669
+ @current = nil
670
+ @@spr << self
671
+ @sprites = {}
672
+ create_from_file if !@file.nil?
673
+ end
674
+ def create_from_file
675
+ if File.exists? @file
676
+ data = File.open(@file).read
677
+ # AXX Format
678
+ if data.include? '---' and data.include? '| :'
679
+ out = []
680
+ data = data.split("---")
681
+ while data.size > 0
682
+ while data[0].match(/^#/)
683
+ data.shift
684
+ end
685
+ name = data.shift
686
+ content = data.shift
687
+ @sprites[name] = [] if @sprites[name].nil?
688
+ @sprites[name] << Axx.parse(content)
689
+ @current = name if @current.nil? # first sprite gets default
690
+ @obj = @sprites[@current][0]
691
+ end
692
+ end
693
+ end
694
+ end
695
+ # set current animation
696
+ def set_current name
697
+ @current = name
698
+ @obj = @sprites[@current][0]
699
+ end
700
+ # Add sprite
701
+ def add name, images
702
+ @current = name if @current.nil? # first sprite gets default
703
+ @sprites[name] = images
704
+ end
705
+ # Update ticker
706
+ def update
707
+ @tick += 1
708
+ @tick = 0 if @tick >= ( @sprites[@current].size )
709
+ @obj = @sprites[@current][@tick]
548
710
  end
549
711
  end
550
712
  end
metadata CHANGED
@@ -1,66 +1,45 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rutui
3
- version: !ruby/object:Gem::Version
4
- hash: 9
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- version: "0.1"
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Roman Pramberger
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2012-07-22 00:00:00 +02:00
18
- default_executable:
12
+ date: 2012-07-22 00:00:00.000000000 Z
19
13
  dependencies: []
20
-
21
14
  description: Create Pure Ruby textbased interfaces of all kinds (Unix only)
22
15
  email: roman.pramberger@gmail.com
23
16
  executables: []
24
-
25
17
  extensions: []
26
-
27
18
  extra_rdoc_files: []
28
-
29
- files:
30
- - rutui.rb
31
- has_rdoc: true
19
+ files:
20
+ - lib/rutui.rb
32
21
  homepage: http://rubygems.org/gems/rutui
33
22
  licenses: []
34
-
35
23
  post_install_message:
36
24
  rdoc_options: []
37
-
38
- require_paths:
25
+ require_paths:
39
26
  - lib
40
- required_ruby_version: !ruby/object:Gem::Requirement
27
+ required_ruby_version: !ruby/object:Gem::Requirement
41
28
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 3
46
- segments:
47
- - 0
48
- version: "0"
49
- required_rubygems_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
34
  none: false
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- hash: 3
55
- segments:
56
- - 0
57
- version: "0"
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
58
39
  requirements: []
59
-
60
40
  rubyforge_project:
61
- rubygems_version: 1.3.7
41
+ rubygems_version: 1.8.23
62
42
  signing_key:
63
43
  specification_version: 3
64
44
  summary: RUby Textbased User Interface
65
45
  test_files: []
66
-