Class: LifeGame::FieldArea
- Inherits:
-
Gtk::DrawingArea
- Object
- Gtk::DrawingArea
- LifeGame::FieldArea
- Defined in:
- lib/kaki-lifegame.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#areah ⇒ Object
readonly
Returns the value of attribute areah.
-
#areaw ⇒ Object
readonly
Returns the value of attribute areaw.
-
#label1 ⇒ Object
writeonly
Sets the attribute label1.
-
#label2 ⇒ Object
writeonly
Sets the attribute label2.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#time_goes ⇒ Object
writeonly
Sets the attribute time_goes.
Instance Method Summary collapse
- #area_size_change ⇒ Object
- #back ⇒ Object
- #background_renewal ⇒ Object
- #change_window_size(size) ⇒ Object
- #clear ⇒ Object
- #each_cell ⇒ Object
- #faster ⇒ Object
- #go_on_one_step ⇒ Object
- #grid ⇒ Object
-
#initialize(field) ⇒ FieldArea
constructor
A new instance of FieldArea.
- #load_file(file_name) ⇒ Object
- #preserve ⇒ Object
- #redraw ⇒ Object
- #renewal_one_place(x, y) ⇒ Object
- #reset_cell(x, y) ⇒ Object
- #restore ⇒ Object
- #save_file(file_name) ⇒ Object
- #scatter ⇒ Object
- #set_cell(x, y) ⇒ Object
- #set_cell_color(color) ⇒ Object
- #set_color(color_name) ⇒ Object
- #set_wait_time ⇒ Object
- #show_step ⇒ Object
- #show_wait_time ⇒ Object
- #slower ⇒ Object
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. reset_cell(x, y) if e.state. end end set_wait_time end |
Instance Attribute Details
#areah ⇒ Object (readonly)
Returns the value of attribute areah.
196 197 198 |
# File 'lib/kaki-lifegame.rb', line 196 def areah @areah end |
#areaw ⇒ Object (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
195 196 197 |
# File 'lib/kaki-lifegame.rb', line 195 def label1=(value) @label1 = value end |
#label2=(value) ⇒ Object (writeonly)
Sets the attribute label2
195 196 197 |
# File 'lib/kaki-lifegame.rb', line 195 def label2=(value) @label2 = value end |
#size ⇒ Object (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
195 196 197 |
# File 'lib/kaki-lifegame.rb', line 195 def time_goes=(value) @time_goes = value end |
Instance Method Details
#area_size_change ⇒ Object
342 343 344 |
# File 'lib/kaki-lifegame.rb', line 342 def area_size_change @areaw, @areah = @f.width * L, @f.height * L end |
#back ⇒ Object
326 327 328 329 330 |
# File 'lib/kaki-lifegame.rb', line 326 def back @f.back redraw show_step end |
#background_renewal ⇒ Object
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 |
#clear ⇒ Object
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_cell ⇒ Object
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 |
#faster ⇒ Object
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_step ⇒ Object
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 |
#grid ⇒ Object
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 |
#preserve ⇒ Object
229 230 231 |
# File 'lib/kaki-lifegame.rb', line 229 def preserve @f.preserve end |
#redraw ⇒ Object
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 |
#restore ⇒ Object
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 |
#scatter ⇒ Object
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_time ⇒ Object
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_step ⇒ Object
239 240 241 |
# File 'lib/kaki-lifegame.rb', line 239 def show_step @label1.set_text("Step : #{@f.step}") end |
#show_wait_time ⇒ Object
301 302 303 |
# File 'lib/kaki-lifegame.rb', line 301 def show_wait_time @label2.set_text("#{@wait_time} ミリ秒") end |
#slower ⇒ Object
305 306 307 308 309 |
# File 'lib/kaki-lifegame.rb', line 305 def slower @wait_time += WaitChange set_wait_time show_wait_time end |