Observer trong SketchUp
SketchUp có triển khai mô hình lập trình observer pattern, một đối tượng A thông báo cho đối tượng B về sự thay đổi trạng thái của nó.
Một đối tượng phát sinh thông báo khi có sự kiện xảy ra gọi là notifier, đối tượng nhận thông báo sự kiện gọi là observer, có các thao tác cần thực hiện:
- Một observer gọi phương thức
add_observercủanotifierđể đăng ký nhận thông báo notifiersẽ thêmobservervào danh sách nhận thông báo- Mỗi khi sự kiện xảy ra, notifier thông báo bằng cách gọi phương thức mà observer yêu cầu
- Nếu
observerkhông muốn nhận tin nữa, nó sẽ gọi phương thứcremove_observercủa notifier
Trong SketchUp các lớp observer và notifier thường đi theo cặp với tên dạng:
- Entity/EntityObserver tương ứng cho các đối tượng Entity
- Selection/SelectionObserver tương ứng việc lựa chọn đối tượng của người dùng
- Materials/MaterialsObserver tương ứng khi một vật liệu thêm vào, xóa bỏ khỏi tập hợp ...
EntityObserver trong SketchUp
EntityObserver được tạo ra để nhận thông tin khi Entity thay đổi, xóa. Có ba bước để triển khai:
- Định nghĩa một lớp kế thừa
EntityObserver - Thêm các phương thức instance tương ứng với các sự kiện
onChangeEntity(entity) ⇒ nil,onEraseEntity(entity) ⇒ nil - Tạo đối tượng từ lớp định nghĩa với phương thức
new
# 23.ent_observer.rb
# load "/Users/xuanthulab/Desktop/learn-ruby/sketchup/23.ent_observer.rb"
require 'sketchup.rb'
require 'extensions.rb'
class MyEntityObserver < Sketchup::EntityObserver
# Phương thức gọi khi thay đổi Entity
def onChangeEntity(entity)
puts entity.to_s
puts entity.typename + " changed"
end
# Nhận thông báo khi xóa Entity
def onEraseEntity(entity)
puts entity.typename + " erased"
end
end
def set_all_enties_observer
# Thiết lập đăng ký Entity
entites = Sketchup.active_model.entities
obs = MyEntityObserver.new
entites.each {
|ent|
is_ent = ent.class.ancestors.include? Sketchup::Entity
if is_ent
puts ent.class.to_s
ent.add_observer obs
end
}
end
SelectionObserver trong SketchUp
Đối tượng Selection chứa các đối tượng Entity mà người dùng chọn. Thông thường để truy cập thì sử dụng phương thức selection của lớp Model. Khi có đối tượng lớp Selection có thể thi hành các tác vụ như: clear - loại bỏ đối tượng lựa chọn, toggle chuyển đối trạng thái chọn, empty?, sinngle_object?, is_curver?, is_surface? ...
Có 2 phương thức hay dùng: add thêm Entity vào Selection, shift loại Entity đầu tiên
Lớp SelectionObserver để lắng nghe các sự kiện lựa chọn, cần xây dựng lớp kế thừa và định nghĩa những phương thức bắt sự kiện nếu muốn:
onSelectionAdded(Selection, Entity)- một Entity chọnonSelectionRemoved(Selection, Entity- một Entity hủy chọnonSelectedCleared(Selection)- không còn chọnonSelectionBulkChange(Selection)- chọn thay đổi
# 24.selection_obs.rb
# load "/Users/xuanthulab/Desktop/learn-ruby/sketchup/24.selection_obs.rb"
require 'sketchup.rb'
require 'extensions.rb'
class SelObserver < Sketchup::SelectionObserver
# Gọi khi một Entity thêm vào Selection
def onSelectionAdded(selection, entity)
puts "onSelectionAdded: #{entity}"
end
def onSelectionBulkChange(selection)
selection.each {|e| puts e}
end
# Khi selection rỗng
def onSelectionCleared(selection)
puts "Không chọn Entity nào"
end
end
sel = Sketchup.active_model.selection
selob = SelObserver.new
sel.add_observer selob
# sel.remove_observer selob
