Class: Moon::Painter2

Inherits:
Object show all
Defined in:
lib/moon/packages/data_painter/painter2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Painter2


7
8
9
# File 'lib/moon/packages/data_painter/painter2.rb', line 7

def initialize(target)
  @target = target
end

Instance Attribute Details

#targetMoon::Tabular (readonly)


4
5
6
# File 'lib/moon/packages/data_painter/painter2.rb', line 4

def target
  @target
end

Instance Method Details

#blit(table, x, y, rect) ⇒ self #blit(table, x, y, sx, sy, sw, sh) ⇒ self


141
142
143
144
145
146
147
148
149
150
151
# File 'lib/moon/packages/data_painter/painter2.rb', line 141

def blit(*args, &block)
  case args.size
  when 4
    blit_rect(*args, &block)
  when 7
    blit_xywh(*args, &block)
  else
    raise ArgumentError,
          "wrong argument count #{args.size} (expected 4:(table, x, y, rect) or 7:(table, x, y, sx, sy, sw, sh))"
  end
end

#blit_rect(table, x, y, rect, &block) ⇒ self


133
134
135
# File 'lib/moon/packages/data_painter/painter2.rb', line 133

def blit_rect(table, x, y, rect, &block)
  blit_xywh(table, x, y, rect.x, rect.y, rect.w, rect.h, &block)
end

#blit_xywh(table, x, y, sx, sy, sw, sh) ⇒ self


118
119
120
121
122
123
124
# File 'lib/moon/packages/data_painter/painter2.rb', line 118

def blit_xywh(*args, &block)
  if block_given?
    blit_xywh_with_block(*args, &block)
  else
    blit_xywh_without_block(*args)
  end
end

#clear(n = nil) ⇒ Object


17
18
19
# File 'lib/moon/packages/data_painter/painter2.rb', line 17

def clear(n = nil)
  fill(n || target.default)
end

#fill(n) ⇒ Object


12
13
14
# File 'lib/moon/packages/data_painter/painter2.rb', line 12

def fill(n)
  target.map_with_xy { |old_n, x, y| n }
end

#fill_rect(rect, value) ⇒ self #fill_rect(x, y, w, h, value) ⇒ self


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/moon/packages/data_painter/painter2.rb', line 59

def fill_rect(*args)
  case args.size
  when 2
    r, n = *args
    fill_rect_xywh(r.x, r.y, r.w, r.h, n)
  when 5
    fill_rect_xywh(*args)
  else
    raise ArgumentError,
          "wrong argument count #{args.size} (expected 2:(rect, value) or 5:(x, y, w, h, value))"
  end
end

#fill_rect_xywh(x, y, w, h, v) ⇒ self


45
46
47
# File 'lib/moon/packages/data_painter/painter2.rb', line 45

def fill_rect_xywh(x, y, w, h, v)
  map_rect(x, y, w, h) { v }
end

#map_rect(rect) ⇒ self #map_rect(x, y, w, h) ⇒ self


29
30
31
32
33
34
35
36
37
# File 'lib/moon/packages/data_painter/painter2.rb', line 29

def map_rect(*args)
  x, y, w, h = *Rect.extract(args.size > 1 ? args : args.first)
  h.times do |j|
    w.times do |i|
      target[x + i, y + j] = yield target[x + i, y + j], i, j
    end
  end
  self
end

#replace(a, b) ⇒ self

Replaces all ocurrences of (a) with (b)


171
172
173
# File 'lib/moon/packages/data_painter/painter2.rb', line 171

def replace(a, b)
  replace_map({ a => b })
end

#replace_map(rmap) ⇒ self

Replaces all ocurrences of (rmap.key) with (rmap.value)


158
159
160
161
162
163
# File 'lib/moon/packages/data_painter/painter2.rb', line 158

def replace_map(rmap)
  target.map_with_xy do |n, x, y|
    rmap[n] || n
  end
  self
end

#replace_select(selection) {|Integer| ... } ⇒ self

Replaces all ocurrences that appear in (selection) with the result from the block

Yields:


181
182
183
184
185
186
187
# File 'lib/moon/packages/data_painter/painter2.rb', line 181

def replace_select(selection)
  map_with_xy do |n, x, y|
    n = yield n if selection.include?(n)
    n
  end
  self
end