Class: Contrek::Bitmaps::ChunkyBitmap

Inherits:
Bitmap
  • Object
show all
Defined in:
lib/contrek/bitmaps/chunky_bitmap.rb

Instance Method Summary collapse

Methods inherited from Bitmap

#clear!, #copy_rect, #scan

Methods included from Painting

#bitmap_colors, direct_draw_polygons, #draw_line

Constructor Details

#initialize(data, mod) ⇒ ChunkyBitmap

Returns a new instance of ChunkyBitmap.



4
5
6
7
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 4

def initialize(data, mod)
  @raw = data.dup
  @module = mod
end

Instance Method Details

#clear(val = "0") ⇒ Object



9
10
11
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 9

def clear(val = "0")
  @raw = val * @module * h
end

#draw_numbered_polygons(polygons) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 60

def draw_numbered_polygons(polygons)
  polygons.each do |polygon|
    color = "A"
    polygon[:outer].each_with_index do |position, index|
      value_set(position[:x], position[:y], color)
      color = next_color(color)
    end
    polygon[:inner].each do |sequence|
      color = "a"
      sequence.each_with_index do |position, index|
        value_set(position[:x], position[:y], color)
        color = next_color(color)
      end
    end
  end
end

#draw_polygons(polygons) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 46

def draw_polygons(polygons)
  polygons.each do |polygon|
    [[:outer, "o"], [:inner, "i"]].each do |side, color|
      sequences = polygon[side]
      sequences = [sequences] if side == :outer
      sequences.each do |sequence|
        sequence.each do |position|
          value_set(position[:x], position[:y], color)
        end
      end
    end
  end
end

#draw_rect(x:, y:, width:, height:, color: "o", filled: true) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 36

def draw_rect(x:, y:, width:, height:, color: "o", filled: true)
  (y...(y + height)).each do |row|
    (x...(x + width)).each do |col|
      if filled || row == y || row == y + height - 1 || col == x || col == x + width - 1
        value_set(col, row, color)
      end
    end
  end
end

#dup!Object



32
33
34
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 32

def dup!
  ChunkyBitmap.new(@raw, @module)
end

#hObject



17
18
19
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 17

def h
  @raw.size / @module
end

#to_terminal(label = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 77

def to_terminal(label = nil)
  puts label if label
  puts "  " + (0...@module).map { |i| (i % 10).to_s }.join
  n = 0
  @raw.scan(/.{1,#{@module}}/).each do |line|
    colored_line = line.chars.map { |c| colorize_char(c) }.join
    puts "#{n} #{colored_line}"
    n += 1
    n = 0 if n >= 10
  end
  puts
end

#value_at(x, y) ⇒ Object



21
22
23
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 21

def value_at(x, y)
  @raw[y * @module + x]
end

#value_set(x, y, value) ⇒ Object



25
26
27
28
29
30
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 25

def value_set(x, y, value)
  return if y >= h
  return if x >= w

  @raw[y * @module + x] = value
end

#wObject



13
14
15
# File 'lib/contrek/bitmaps/chunky_bitmap.rb', line 13

def w
  @module
end