From bba00487cb86631fc714ae635f882db017471131 Mon Sep 17 00:00:00 2001 From: Samuel Tallet Date: Mon, 25 Nov 2019 02:36:46 +0100 Subject: [PATCH] Handle onLightErased event. --- source/pbr/entity_observer.rb | 53 +++++++++++++++++++++++++++++++++++ source/pbr/light.rb | 5 ++++ source/pbr/load.rb | 3 ++ source/pbr/material_editor.rb | 4 +-- source/pbr/menu.rb | 2 +- source/pbr/model_observer.rb | 7 ----- source/pbr/toolbar.rb | 2 +- source/pbr/viewport.rb | 4 +-- 8 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 source/pbr/entity_observer.rb diff --git a/source/pbr/entity_observer.rb b/source/pbr/entity_observer.rb new file mode 100644 index 0000000..f4c7df5 --- /dev/null +++ b/source/pbr/entity_observer.rb @@ -0,0 +1,53 @@ +# Physically-Based Rendering extension for SketchUp 2017 or newer. +# Copyright: © 2019 Samuel Tallet +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3.0 of the License, or +# (at your option) any later version. +# +# If you release a modified version of this program TO THE PUBLIC, +# the GPL requires you to MAKE THE MODIFIED SOURCE CODE AVAILABLE +# to the program's users, UNDER THE GPL. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# Get a copy of the GPL here: https://www.gnu.org/licenses/gpl.html + +raise 'The PBR plugin requires at least Ruby 2.2.0 or SketchUp 2017.'\ + unless RUBY_VERSION.to_f >= 2.2 # SketchUp 2017 includes Ruby 2.2.4. + +require 'sketchup' +require 'pbr/light' +require 'pbr/viewport' + +# PBR plugin namespace. +module PBR + + # Observes SketchUp entity events and reacts. + class EntityObserver < Sketchup::EntityObserver + + # rubocop: disable Naming/MethodName + + # When a SketchUp entity was erased. + def onEraseEntity(entity) + + # If it was a PBR artificial light: + if SESSION[:lights_objects_ids].include?(entity.object_id.to_i) + + Viewport.update_model_and_reopen + + SESSION[:lights_objects_ids].delete(entity.object_id.to_i) + + end + + end + + # rubocop: enable Naming/MethodName + + end + +end diff --git a/source/pbr/light.rb b/source/pbr/light.rb index 32dbe37..7030850 100644 --- a/source/pbr/light.rb +++ b/source/pbr/light.rb @@ -22,6 +22,7 @@ require 'sketchup' require 'pbr/shapes' +require 'pbr/entity_observer' # PBR plugin namespace. module PBR @@ -39,6 +40,10 @@ def initialize @light = Shapes.create_sphere('30cm', 10, 8, LAYER_NAME) + SESSION[:lights_objects_ids].push(@light.object_id.to_i) + + @light.add_observer(EntityObserver.new) + end end diff --git a/source/pbr/load.rb b/source/pbr/load.rb index 0eb28ea..235e4d6 100644 --- a/source/pbr/load.rb +++ b/source/pbr/load.rb @@ -42,6 +42,9 @@ module PBR # Storage for Chromium process ID. SESSION[:viewport_pid] = 0 + # Memory of artificial lights IDs. + SESSION[:lights_objects_ids] = [] + # Plug PBR menu into SketchUp UI. Menu.new( UI.menu('Plugins') # parent_menu diff --git a/source/pbr/material_editor.rb b/source/pbr/material_editor.rb index c506dc1..ab0c019 100644 --- a/source/pbr/material_editor.rb +++ b/source/pbr/material_editor.rb @@ -22,6 +22,7 @@ require 'sketchup' require 'pbr/html_dialogs' +require 'pbr/viewport' # PBR plugin namespace. module PBR @@ -159,8 +160,7 @@ def show @dialog.close - # Update and refresh PBR Viewport model. - Viewport.reopen if Viewport.update_model + Viewport.update_model_and_reopen end diff --git a/source/pbr/menu.rb b/source/pbr/menu.rb index 0f4b556..1eb101f 100644 --- a/source/pbr/menu.rb +++ b/source/pbr/menu.rb @@ -126,7 +126,7 @@ def initialize(parent_menu) return PBR.open_required_plugin_page unless PBR.required_plugin_exist? - Viewport.reopen_if_model_updated + Viewport.update_model_and_reopen end diff --git a/source/pbr/model_observer.rb b/source/pbr/model_observer.rb index 452615a..b21ba41 100644 --- a/source/pbr/model_observer.rb +++ b/source/pbr/model_observer.rb @@ -38,13 +38,6 @@ def onPreSaveModel(_model) end - # After a SketchUp model has been saved to disk. - def onPostSaveModel(_model) - - Viewport.open - - end - # rubocop: enable Naming/MethodName end diff --git a/source/pbr/toolbar.rb b/source/pbr/toolbar.rb index 815c1a3..a2b5c70 100644 --- a/source/pbr/toolbar.rb +++ b/source/pbr/toolbar.rb @@ -105,7 +105,7 @@ def initialize return PBR.open_required_plugin_page unless PBR.required_plugin_exist? - Viewport.reopen_if_model_updated + Viewport.update_model_and_reopen end diff --git a/source/pbr/viewport.rb b/source/pbr/viewport.rb index 083e182..e139391 100644 --- a/source/pbr/viewport.rb +++ b/source/pbr/viewport.rb @@ -145,10 +145,10 @@ def self.reopen end - # Reopens Viewport if model updated. + # Updates Viewport glTF model then reopens Viewport? # # @return [nil] - def self.reopen_if_model_updated + def self.update_model_and_reopen return Menu.propose_help( TRANSLATE['glTF export failed. Do you want help?']