Skip to content

Commit

Permalink
Handle onLightErased event.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelTallet committed Nov 25, 2019
1 parent f510e43 commit bba0048
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 13 deletions.
53 changes: 53 additions & 0 deletions source/pbr/entity_observer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Physically-Based Rendering extension for SketchUp 2017 or newer.
# Copyright: © 2019 Samuel Tallet <samuel.tallet arobase gmail.com>
#
# 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
5 changes: 5 additions & 0 deletions source/pbr/light.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

require 'sketchup'
require 'pbr/shapes'
require 'pbr/entity_observer'

# PBR plugin namespace.
module PBR
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions source/pbr/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions source/pbr/material_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

require 'sketchup'
require 'pbr/html_dialogs'
require 'pbr/viewport'

# PBR plugin namespace.
module PBR
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion source/pbr/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 0 additions & 7 deletions source/pbr/model_observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion source/pbr/toolbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions source/pbr/viewport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?']
Expand Down

0 comments on commit bba0048

Please sign in to comment.