Class: GridCanvas

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

Constant Summary collapse

GRIDSIZE =
25

Instance Method Summary collapse

Constructor Details

#initialize(frame, cell_grid) ⇒ GridCanvas

Returns a new instance of GridCanvas.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fx_grid_canvas.rb', line 9

def initialize(frame, cell_grid)
  super(frame, nil, 0, FRAME_RAISED|LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_TOP|LAYOUT_LEFT)
  @cell_grid = cell_grid
  @width = @cell_grid.column_count * GRIDSIZE
  @height = @cell_grid.row_count * GRIDSIZE
  @grid_background = FXColor::DarkGray
  @grid_foreground = FXColor::LightGray
  @live_cell_color = FXColor::SpringGreen
  @dragging = false
  connect(SEL_PAINT) do |sender, sel, event|
    draw_grid
  end
  
  # the dragging stuff here is almost certainly not the
  # "correct" way to git-r-dun... pending enlightenment
  connect(SEL_LEFTBUTTONPRESS) do |sender, sel, event|
    toggle_cell_at_location(event.click_x, event.click_y)
    @dragging = true
  end
  connect(SEL_LEFTBUTTONRELEASE) do |sender, sel, event|
    @dragging = false
  end
  connect(SEL_MOTION) do |sender, sel, event|
    handle_drag(event.win_x, event.win_y) if @dragging
  end
end

Instance Method Details

#cell_at_location(x, y) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/fx_grid_canvas.rb', line 82

def cell_at_location(x, y)
  cell = nil
  column = x / GRIDSIZE
  row = y / GRIDSIZE
  cell = @cell_grid.cell_at(row, column) if column < @cell_grid.column_count && row < @cell_grid.row_count
  cell
end

#draw_gridObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fx_grid_canvas.rb', line 36

def draw_grid
  FXDCWindow.new(self) do |dc|
    self.backColor = @grid_background
    dc.foreground = self.backColor
    dc.fillRectangle(0, 0, width, height)
    dc.foreground = @grid_foreground
    0.step(@width, GRIDSIZE) do |column|
      dc.drawLine(column, 0, column, @height)
    end
    0.step(@height, GRIDSIZE) do |row|
      dc.drawLine(0, row, @width, row)
    end
    dc.foreground = @live_cell_color
    for row in 0...@cell_grid.row_count
      for column in 0...@cell_grid.column_count
        cell = @cell_grid.cell_at(row, column)
        if(cell.cell_state == :live)
          x = (column * GRIDSIZE) + (GRIDSIZE / 2)
          y = (row * GRIDSIZE) + (GRIDSIZE / 2)
          dc.fillCircle(x, y, GRIDSIZE / 3)
        end
      end
    end
  end
end

#handle_drag(x, y) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/fx_grid_canvas.rb', line 62

def handle_drag(x, y)
  cell = cell_at_location(x, y)
  if cell && cell.cell_state == :dead
    cell.cell_state = :live
    draw_grid
  end
end

#toggle_cell_at_location(x, y) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fx_grid_canvas.rb', line 70

def toggle_cell_at_location(x, y)
  cell = cell_at_location(x, y)
  if cell
    if cell.cell_state == :dead
      cell.cell_state = :live
    else
      cell.cell_state = :dead
    end
    draw_grid
  end
end