Class: MatrixManager

Inherits:
Object
  • Object
show all
Defined in:
lib/matrix_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, config) ⇒ MatrixManager

Returns a new instance of MatrixManager.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/matrix_manager.rb', line 8

def initialize(width, height, config)
  @show_cov = config['show_cov']
  @cov_zoom = config['cov_zoom']
  @zoom = config['zoom']
  @left_col = config['left_col']
  @top_row = config['top_row']
  @offset_x = config['offset_x']
  @offset_y = config['offset_y']
  @width = width
  @height = height
  create_matrix
end

Instance Attribute Details

#cov_zoomObject (readonly)

Returns the value of attribute cov_zoom.



6
7
8
# File 'lib/matrix_manager.rb', line 6

def cov_zoom
  @cov_zoom
end

#left_colObject (readonly)

Returns the value of attribute left_col.



6
7
8
# File 'lib/matrix_manager.rb', line 6

def left_col
  @left_col
end

#offset_xObject (readonly)

Returns the value of attribute offset_x.



6
7
8
# File 'lib/matrix_manager.rb', line 6

def offset_x
  @offset_x
end

#offset_yObject (readonly)

Returns the value of attribute offset_y.



6
7
8
# File 'lib/matrix_manager.rb', line 6

def offset_y
  @offset_y
end

#show_covObject (readonly)

Returns the value of attribute show_cov.



6
7
8
# File 'lib/matrix_manager.rb', line 6

def show_cov
  @show_cov
end

#top_rowObject (readonly)

Returns the value of attribute top_row.



6
7
8
# File 'lib/matrix_manager.rb', line 6

def top_row
  @top_row
end

#zoomObject (readonly)

Returns the value of attribute zoom.



6
7
8
# File 'lib/matrix_manager.rb', line 6

def zoom
  @zoom
end

Instance Method Details

#configObject



21
22
23
24
25
26
27
# File 'lib/matrix_manager.rb', line 21

def config
  result = {}
  %w(left_col top_row offset_x offset_y zoom show_cov cov_zoom).each do |attr|
    result[attr] = self.send(attr)
  end
  result
end

#cov_valueObject



137
138
139
# File 'lib/matrix_manager.rb', line 137

def cov_value
  show_cov && cov_zoom
end

#coverage_zoom_inObject



69
70
71
72
73
74
# File 'lib/matrix_manager.rb', line 69

def coverage_zoom_in
  if cov_zoom - zoom < MAX_ZOOM_DIFF
    @cov_zoom += 1
    recreate_coverage
  end
end

#coverage_zoom_outObject



76
77
78
79
80
81
# File 'lib/matrix_manager.rb', line 76

def coverage_zoom_out
  if cov_zoom - zoom > 1
    @cov_zoom -= 1
    recreate_coverage
  end
end

#draw(dc) ⇒ Object



49
50
51
52
53
# File 'lib/matrix_manager.rb', line 49

def draw(dc)
  @matrix.each do |col, row, tile|
    tile.draw(dc, TILE_WIDTH * (col - 1) + @offset_x, TILE_WIDTH * (row - 1) + @offset_y)
  end
end

#draw_tile(dc, the_tile) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/matrix_manager.rb', line 55

def draw_tile(dc, the_tile)
  @matrix.each do |col, row, tile|
    if tile == the_tile
      tile.draw(dc, TILE_WIDTH * (col - 1) + @offset_x, TILE_WIDTH * (row - 1) + @offset_y)
      break
    end
  end
end

#pan(dx, dy) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/matrix_manager.rb', line 83

def pan(dx, dy)
  @offset_x += dx
  @offset_y += dy

  if @offset_x < 0
    @offset_x = TILE_WIDTH + @offset_x
    @left_col += 1
    @matrix.shift_left(get_tiles(:column, :last) )
  elsif @offset_x >= TILE_WIDTH
    @offset_x -= TILE_WIDTH
    @left_col -= 1
    @matrix.shift_right(get_tiles(:column, :first) )
  end

  if @offset_y < 0
    @offset_y = TILE_WIDTH + @offset_y
    @top_row += 1
    @matrix.shift_up(get_tiles(:row, :last) )
  elsif @offset_y >= TILE_WIDTH
    @offset_y -= TILE_WIDTH
    @top_row -= 1
    @matrix.shift_down(get_tiles(:row, :first) )
  end
end

#resize(viewport_width, viewport_height) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/matrix_manager.rb', line 108

def resize(viewport_width, viewport_height)
  @width = viewport_width
  @height = viewport_height
  new_matrix_width = (viewport_width.to_f / TILE_WIDTH).ceil + 2
  new_matrix_height = (viewport_height.to_f / TILE_WIDTH).ceil + 2
  if new_matrix_width < @matrix.width || new_matrix_height < @matrix.height
    @matrix.reduce(new_matrix_width, new_matrix_height)
  else
    if new_matrix_width > @matrix.width
      (@left_col + @matrix.width..@left_col + new_matrix_width - 1).each do |col|
        column = []
        (@top_row..@top_row + @matrix.height - 1).each do |row|
          column << get_tile(col, row, @zoom)
        end
        @matrix.add_column(column)
      end
    end
    if new_matrix_height > @matrix.height
      (@top_row + @matrix.height..@top_row + new_matrix_height - 1).each do |r|
        row = []
        (@left_col..@left_col + new_matrix_width - 1).each do |col|
          row << get_tile(col, r, @zoom)
        end
        @matrix.add_row(row)
      end
    end
  end
end

#toggle_coverageObject



64
65
66
67
# File 'lib/matrix_manager.rb', line 64

def toggle_coverage
  @show_cov = !show_cov
  recreate_coverage
end

#zoom_in(x, y) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/matrix_manager.rb', line 29

def zoom_in(x, y)
  if @zoom < MAX_ZOOM
    @zoom += 1
    @cov_zoom = zoom + 1 if cov_zoom - zoom < 1
    @offset_x, @left_col = calc_start_and_offset_zoom_in(@left_col, x, @offset_x, @width)
    @offset_y, @top_row = calc_start_and_offset_zoom_in(@top_row, y, @offset_y, @height)
    create_matrix
  end
end

#zoom_out(x, y) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/matrix_manager.rb', line 39

def zoom_out(x, y)
  if @zoom > 0
    @zoom -= 1
    @cov_zoom = zoom + MAX_ZOOM_DIFF if cov_zoom - zoom > MAX_ZOOM_DIFF
    @offset_x, @left_col = calc_start_and_offset_zoom_out(@left_col, x, @offset_x, @width)
    @offset_y, @top_row = calc_start_and_offset_zoom_out(@top_row, y, @offset_y, @height)
    create_matrix
  end
end