Class: LifeGame::FieldArea

Inherits:
Gtk::DrawingArea
  • Object
show all
Defined in:
lib/kaki-lifegame.rb

Constant Summary collapse

CellSize =
11
Space =
1
L =
CellSize + Space * 2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ FieldArea

Returns a new instance of FieldArea.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/kaki-lifegame.rb', line 116

def initialize(field)
  super()
  set_size_request(AreaW, AreaH)
  
  @f = field
  
  @colormap = Gdk::Colormap.system
  @color = {black: Gdk::Color.new(0, 0, 0), green: Gdk::Color.new(0xc784, 0xffff, 0x52c4),
            orange: Gdk::Color.new(0xffff, 0xbb85, 0xf7a),
            grid_color: Gdk::Color.new(0x4b79, 0x4b79, 0x4b79)}
  @color.each {|_, v| @colormap.alloc_color(v, false, true)}
  @cell_color = :green
  
  @grid = false
  
  signal_connect("expose_event") do
    @gc = Gdk::GC.new(window)
    set_color(@grid ? :grid_color : :black)
    window.draw_rectangle(@gc, true, 0, 0, AreaW, AreaH)
    redraw
  end
  
  @time_goes = false
  add_events(Gdk::Event::BUTTON_PRESS_MASK | Gdk::Event::BUTTON_MOTION_MASK)
  
  signal_connect("button_press_event") do |w, e|
    unless @time_goes
      x, y = e.x.to_i / L, e.y.to_i / L
      c = @f.get_cell(x, y)
      c.zero? ? set_cell(x, y) : reset_cell(x, y)
    end
  end
  
  signal_connect('motion_notify_event') do |w, e|
    unless @time_goes
      x, y = e.x.to_i / L, e.y.to_i / L
      set_cell(x, y)   if e.state.button1_mask?
      reset_cell(x, y) if e.state.button3_mask?
    end
  end
  
  Gtk.timeout_add(500) do
    go_on_one_step if @time_goes
    true
  end
end

Instance Attribute Details

#label=(value) ⇒ Object (writeonly)

Sets the attribute label

Parameters:

  • value

    the value to set the attribute label to.



162
163
164
# File 'lib/kaki-lifegame.rb', line 162

def label=(value)
  @label = value
end

#time_goes=(value) ⇒ Object (writeonly)

Sets the attribute time_goes

Parameters:

  • value

    the value to set the attribute time_goes to.



162
163
164
# File 'lib/kaki-lifegame.rb', line 162

def time_goes=(value)
  @time_goes = value
end

Instance Method Details

#clearObject



219
220
221
222
223
224
# File 'lib/kaki-lifegame.rb', line 219

def clear
  @f.clear
  redraw
  @f.step_reset
  show_step
end

#each_cellObject



213
214
215
216
217
# File 'lib/kaki-lifegame.rb', line 213

def each_cell
  Field::Height.times do |y|
    Field::Width.times {|x| yield(x, y)}
  end
end

#go_on_one_stepObject



180
181
182
183
184
185
186
187
188
189
# File 'lib/kaki-lifegame.rb', line 180

def go_on_one_step
  @f.next
  
  each_cell do |x, y|
    renewal_one_place(x, y) if @f.renewal?(x, y)
  end
  
  @f.count
  show_step
end

#gridObject



243
244
245
246
247
248
# File 'lib/kaki-lifegame.rb', line 243

def grid
  @grid = !@grid
  set_color(@grid ? :grid_color : :black)
  window.draw_rectangle(@gc, true, 0, 0, AreaW, AreaH)
  redraw
end

#load_file(file_name) ⇒ Object



237
238
239
240
241
# File 'lib/kaki-lifegame.rb', line 237

def load_file(file_name)
  @f.load(file_name)
  redraw
  show_step
end

#preserveObject



195
196
197
# File 'lib/kaki-lifegame.rb', line 195

def preserve
  @f.preserve
end

#redrawObject



209
210
211
# File 'lib/kaki-lifegame.rb', line 209

def redraw
  each_cell {|x, y| renewal_one_place(x, y)}
end

#renewal_one_place(x, y) ⇒ Object



191
192
193
# File 'lib/kaki-lifegame.rb', line 191

def renewal_one_place(x, y)
  @f.get_cell(x, y).zero? ? reset_cell(x, y) : set_cell(x, y)
end

#reset_cell(x, y) ⇒ Object



174
175
176
177
178
# File 'lib/kaki-lifegame.rb', line 174

def reset_cell(x, y)
  set_color(:black)
  @f.reset_cell(x, y)
  window.draw_rectangle(@gc, true, L * x + Space, L * y + Space, CellSize, CellSize)
end

#restoreObject



199
200
201
202
203
# File 'lib/kaki-lifegame.rb', line 199

def restore
  @f.restore
  redraw
  show_step
end

#save_file(file_name) ⇒ Object



233
234
235
# File 'lib/kaki-lifegame.rb', line 233

def save_file(file_name)
  @f.save(file_name)
end

#scatterObject



226
227
228
229
230
231
# File 'lib/kaki-lifegame.rb', line 226

def scatter
  100.times do
    x, y = rand(Field::Width), rand(Field::Height)
    set_cell(x, y) if @f.get_cell(x, y).zero?
  end
end

#set_cell(x, y) ⇒ Object



168
169
170
171
172
# File 'lib/kaki-lifegame.rb', line 168

def set_cell(x, y)
  set_color(@cell_color)
  @f.set_cell(x, y)
  window.draw_rectangle(@gc, true, L * x + Space, L * y + Space, CellSize, CellSize)
end

#set_cell_color(color) ⇒ Object



250
251
252
253
# File 'lib/kaki-lifegame.rb', line 250

def set_cell_color(color)
  @cell_color = color
  redraw
end

#set_color(color_name) ⇒ Object



164
165
166
# File 'lib/kaki-lifegame.rb', line 164

def set_color(color_name)
  @gc.rgb_fg_color = @color[color_name]
end

#show_stepObject



205
206
207
# File 'lib/kaki-lifegame.rb', line 205

def show_step
  @label.set_text("Step : #{@f.step}")
end