Class: Scratchpad::SheetView

Inherits:
Gtk::DrawingArea
  • Object
show all
Includes:
Scratchpad
Defined in:
lib/scratchpad.rb

Constant Summary

Constants included from Scratchpad

VERSION

Instance Method Summary collapse

Methods included from Scratchpad

#chess_distance, #color_bytes_to_floats, #distance, #dot_product, #midpoint, #plus, #scalar_times, #vec_lerp, #vector

Constructor Details

#initializeSheetView

Returns a new instance of SheetView.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/scratchpad.rb', line 255

def initialize()
  super()
  self.app_paintable = true

  @dirty = true

  set_size_request(800, 600)
  signal_connect('expose-event') do
    draw
    true
  end

  # モーションノティファイ。
  @last_point = nil
  signal_connect('motion-notify-event') do |self_, ev|
    handle_motion_notify_event(ev)
  end

  # ボタンプレス。このイベントは同じ座標へのモーションノティファイの
  # 後に来る。
  signal_connect('button-press-event') do |self_, ev|
    # デバッグ描画。ボタン押下座標に赤の四角を描く。
    c = Cairo::Context.new(@model.debug_surface)
    c.set_source_rgba([1, 0, 0])
    c.rectangle(ev.x - 2, ev.y - 2, 4, 4)
    c.fill
    c.destroy

    if ev.button == 1
      @model.pen_down(ev)
    elsif ev.button == 2
      # 押し間違いを防ぐ。
      unless @model.pen.down?
        @model.clear
      end
    elsif ev.button == 3
      # 押し間違いを防ぐ。
      unless @model.pen.down?
        menu_popup(ev)
      end
    end
  end

  # ボタンリリース。このイベントは同じ座標へのモーションノティファイ
  # の後に来る。
  signal_connect('button-release-event') do |self_, ev|
    # デバッグ描画。ボタンリリース座標に赤の四角を描く。
    c = Cairo::Context.new(@model.debug_surface)
    c.set_source_rgba([1, 0, 0])
    c.rectangle(ev.x - 2, ev.y - 2, 4, 4)
    c.fill
    c.destroy

    if ev.button == 1 && @model.pen.down?
      @model.pen_move(ev)
      @model.pen_up(ev)
    end
  end

  @model = SheetModel.new(screen.width, screen.height)
  @model.signal_connect('changed') do
    @dirty = true
  end

  # POINTER_MOTION_HINT_MASKを付けるとこちらの反応が遅い場合にポイン
  # ター座標を省略する。
  self.events |= Gdk::Event::BUTTON_PRESS_MASK |
                 Gdk::Event::BUTTON_RELEASE_MASK |
                 # Gdk::Event::POINTER_MOTION_HINT_MASK |
                 Gdk::Event::POINTER_MOTION_MASK

end

Instance Method Details

#create_context_menuObject



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/scratchpad.rb', line 371

def create_context_menu
  menu = Gtk::Menu.new

  item1 = Gtk::MenuItem.new('クリア')
  item1.signal_connect('activate') do
    @model.clear
  end
  menu.append(item1)

  menu.append(Gtk::MenuItem.new)

  item2 = Gtk::MenuItem.new('')
  item2.signal_connect('activate') do 
    @model.pen.color = Pen::BLUE
  end
  menu.append(item2)

  item3 = Gtk::MenuItem.new('オレンジ')
  item3.signal_connect('activate') do
    @model.pen.color = Pen::ORANGE
  end
  menu.append(item3)

  item4 = Gtk::MenuItem.new('')
  item4.signal_connect('activate') do
    @model.pen.color = Pen::GREEN
  end
  menu.append(item4)

  menu.show_all

  return menu
end

#dirty?Boolean

Returns:

  • (Boolean)


367
368
369
# File 'lib/scratchpad.rb', line 367

def dirty?
  @dirty
end

#drawObject



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/scratchpad.rb', line 410

def draw
  cr = window.create_cairo_context

  cr.set_operator(Cairo::OPERATOR_SOURCE)
  cr.set_source_rgba(1.0, 1.0, 1.0, 0.5)
  cr.paint 

  cr.set_operator(Cairo::OPERATOR_OVER)

  cr.set_source(@model.outline_surface)
  cr.paint

  cr.set_source(@model.surface)
  cr.paint

  if $DEBUG
    cr.set_source(@model.debug_surface)
    cr.paint
  end

  cr.destroy
end

#handle_motion_notify_event(ev) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/scratchpad.rb', line 338

def handle_motion_notify_event(ev)
  @last_motion_notify_event = ev.dup
  @last_motion_notify_event_time = Time.now

  ev.x = ev.x + 0.5
  ev.y = ev.y + 0.5

  # デバッグ描画。ポインター座標に緑の四角を描く。
  c = Cairo::Context.new(@model.debug_surface)
  c.set_source_rgba([0, 1, 0])
  c.rectangle(ev.x - 1, ev.y - 1, 2, 2)
  c.fill
  c.destroy

  if @last_point == nil
    @last_point = [ev.x, ev.y]
    @model.pen_move(ev)
  else
    # 1サンプル時間あたり距離30以上移動した場合は完全にポインター
    # 座標にならう。
    d = distance(@last_point, [ev.x, ev.y])
    alpha = if d <= 5.0 then 0.30 else 0.60 end
    p [d, alpha] if $DEBUG
    @last_point = vec_lerp(alpha, [ev.x, ev.y], @last_point)
    ev.x, ev.y = @last_point
    @model.pen_move(ev)
  end
end

#invalidateObject



433
434
435
436
437
# File 'lib/scratchpad.rb', line 433

def invalidate
  window.invalidate(window.clip_region, true)
  window.process_updates(true)
  @dirty = false
end


405
406
407
408
# File 'lib/scratchpad.rb', line 405

def menu_popup(button_event)
  menu = create_context_menu
  menu.popup(nil, nil, button_event.button, button_event.time)
end

#tickObject



328
329
330
331
332
333
334
335
336
# File 'lib/scratchpad.rb', line 328

def tick
  return if @last_motion_notify_event.nil?

  t = Time.now
  if (t - @last_motion_notify_event_time) > 0.033
    handle_motion_notify_event(@last_motion_notify_event)
    @last_motion_notify_event_time = t
  end
end