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
WaitChange =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ FieldArea

Returns a new instance of FieldArea.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/kaki-lifegame.rb', line 149

def initialize(field)
  super()
  
  @areaw, @areah = field.width * L, field.height * L
  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_value {|v| @colormap.alloc_color(v, false, true)}
  @cell_color = :green
  
  @grid = false
  @wait_time = 500
  
  signal_connect("expose_event") do
    @gc = Gdk::GC.new(window)
    background_renewal
  end
  
  @time_goes = false
  add_events(Gdk::Event::BUTTON_PRESS_MASK | Gdk::Event::BUTTON_MOTION_MASK)
  
  event_to_xy = ->(e) {[e.x.to_i / L, e.y.to_i / L]}
  
  signal_connect("button_press_event") do |w, e|
    unless @time_goes
      x, y = event_to_xy[e]
      @f.get_cell(x, y).zero? ? set_cell(x, y) : reset_cell(x, y)
    end
  end
  
  signal_connect('motion_notify_event') do |w, e|
    unless @time_goes
      x, y = event_to_xy[e]
      set_cell(x, y)   if e.state.button1_mask?
      reset_cell(x, y) if e.state.button3_mask?
    end
  end
  
  set_wait_time
end

Instance Attribute Details

#areahObject (readonly)

Returns the value of attribute areah.



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

def areah
  @areah
end

#areawObject (readonly)

Returns the value of attribute areaw.



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

def areaw
  @areaw
end

#label1=(value) ⇒ Object (writeonly)

Sets the attribute label1

Parameters:

  • value

    the value to set the attribute label1 to.



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

def label1=(value)
  @label1 = value
end

#label2=(value) ⇒ Object (writeonly)

Sets the attribute label2

Parameters:

  • value

    the value to set the attribute label2 to.



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

def label2=(value)
  @label2 = value
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#time_goes=(value) ⇒ Object (writeonly)

Sets the attribute time_goes

Parameters:

  • value

    the value to set the attribute time_goes to.



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

def time_goes=(value)
  @time_goes = value
end

Instance Method Details

#area_size_changeObject



342
343
344
# File 'lib/kaki-lifegame.rb', line 342

def area_size_change
  @areaw, @areah = @f.width * L, @f.height * L
end

#backObject



326
327
328
329
330
# File 'lib/kaki-lifegame.rb', line 326

def back
  @f.back
  redraw
  show_step
end

#background_renewalObject



285
286
287
288
289
# File 'lib/kaki-lifegame.rb', line 285

def background_renewal
  set_color(@grid ? :grid_color : :black)
  window.draw_rectangle(@gc, true, 0, 0, @areaw, @areah)
  redraw
end

#change_window_size(size) ⇒ Object



332
333
334
335
336
337
338
339
340
# File 'lib/kaki-lifegame.rb', line 332

def change_window_size(size)
  if @f.change_window_size(size)
    area_size_change
    show_step
    true
  else
    false
  end
end

#clearObject



253
254
255
256
257
258
# File 'lib/kaki-lifegame.rb', line 253

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

#each_cellObject



247
248
249
250
251
# File 'lib/kaki-lifegame.rb', line 247

def each_cell
  @f.height.times do |y|
    @f.width.times {|x| yield(x, y)}
  end
end

#fasterObject



311
312
313
314
315
316
# File 'lib/kaki-lifegame.rb', line 311

def faster
  @wait_time -= WaitChange
  @wait_time = 50 if @wait_time < 50
  set_wait_time
  show_wait_time
end

#go_on_one_stepObject



214
215
216
217
218
219
220
221
222
223
# File 'lib/kaki-lifegame.rb', line 214

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



291
292
293
294
# File 'lib/kaki-lifegame.rb', line 291

def grid
  @grid = !@grid
  background_renewal
end

#load_file(file_name) ⇒ Object



277
278
279
280
281
282
283
# File 'lib/kaki-lifegame.rb', line 277

def load_file(file_name)
  is_change = @f.load(file_name)
  area_size_change if is_change
  redraw
  show_step
  return is_change
end

#preserveObject



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

def preserve
  @f.preserve
end

#redrawObject



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

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

#renewal_one_place(x, y) ⇒ Object



225
226
227
# File 'lib/kaki-lifegame.rb', line 225

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



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

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



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

def restore
  @f.restore
  redraw
  show_step
end

#save_file(file_name) ⇒ Object



273
274
275
# File 'lib/kaki-lifegame.rb', line 273

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

#scatterObject



260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/kaki-lifegame.rb', line 260

def scatter
  co1 = co2 = 0
  while co1 < 100 and co2 < 5000
    x, y = rand(@f.width), rand(@f.height)
    if @f.get_cell(x, y).zero?
      set_cell(x, y)
      co1 += 1
    else
      co2 += 1
    end
  end
end

#set_cell(x, y) ⇒ Object



202
203
204
205
206
# File 'lib/kaki-lifegame.rb', line 202

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



296
297
298
299
# File 'lib/kaki-lifegame.rb', line 296

def set_cell_color(color)
  @cell_color = color
  redraw
end

#set_color(color_name) ⇒ Object



198
199
200
# File 'lib/kaki-lifegame.rb', line 198

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

#set_wait_timeObject



318
319
320
321
322
323
324
# File 'lib/kaki-lifegame.rb', line 318

def set_wait_time
  Gtk.timeout_remove(@timer_id) if @timer_id
  @timer_id = Gtk.timeout_add(@wait_time) do
    go_on_one_step if @time_goes
    true
  end
end

#show_stepObject



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

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

#show_wait_timeObject



301
302
303
# File 'lib/kaki-lifegame.rb', line 301

def show_wait_time
  @label2.set_text("#{@wait_time} ミリ秒")
end

#slowerObject



305
306
307
308
309
# File 'lib/kaki-lifegame.rb', line 305

def slower
  @wait_time += WaitChange
  set_wait_time
  show_wait_time
end