Class: Scratchpad::SheetView
- Inherits:
-
Gtk::DrawingArea
- Object
- Gtk::DrawingArea
- Scratchpad::SheetView
- Includes:
- Scratchpad
- Defined in:
- lib/scratchpad.rb
Constant Summary
Constants included from Scratchpad
Instance Method Summary collapse
- #create_context_menu ⇒ Object
- #dirty? ⇒ Boolean
- #draw ⇒ Object
- #handle_motion_notify_event(ev) ⇒ Object
-
#initialize ⇒ SheetView
constructor
A new instance of SheetView.
- #invalidate ⇒ Object
- #menu_popup(button_event) ⇒ Object
- #tick ⇒ Object
Methods included from Scratchpad
#chess_distance, #color_bytes_to_floats, #distance, #dot_product, #midpoint, #plus, #scalar_times, #vec_lerp, #vector
Constructor Details
#initialize ⇒ SheetView
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. == 1 @model.pen_down(ev) elsif ev. == 2 # 押し間違いを防ぐ。 unless @model.pen.down? @model.clear end elsif ev. == 3 # 押し間違いを防ぐ。 unless @model.pen.down? (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. == 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_menu ⇒ Object
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 = Gtk::Menu.new item1 = Gtk::MenuItem.new('クリア') item1.signal_connect('activate') do @model.clear end .append(item1) .append(Gtk::MenuItem.new) item2 = Gtk::MenuItem.new('青') item2.signal_connect('activate') do @model.pen.color = Pen::BLUE end .append(item2) item3 = Gtk::MenuItem.new('オレンジ') item3.signal_connect('activate') do @model.pen.color = Pen::ORANGE end .append(item3) item4 = Gtk::MenuItem.new('緑') item4.signal_connect('activate') do @model.pen.color = Pen::GREEN end .append(item4) .show_all return end |
#dirty? ⇒ Boolean
367 368 369 |
# File 'lib/scratchpad.rb', line 367 def dirty? @dirty end |
#draw ⇒ Object
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 |
#invalidate ⇒ Object
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 |
#menu_popup(button_event) ⇒ Object
405 406 407 408 |
# File 'lib/scratchpad.rb', line 405 def () = .popup(nil, nil, ., .time) end |
#tick ⇒ Object
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 |