But you do NOT use a hardcoded validation Proc.
Instead you access a module variable that holds a nested hash of the check states.
Outer hash has model objects as keys, inner hash has command symbols as keys.
module Author::SomePlugin
extend self
@@loaded ||= false
@@command ||= {}
@@checkstate ||= Hash::new(MF_UNCHECKED)
def nifty_thing()
# command code
end
def expectsStartupModelNotifications
return true
end
def onActivateModel(model) # Mac ONLY
unless @@checkstate.empty?
@@checkstate.delete_if {|model_key,h| !model_key.valid? }
end
end
def onNewModel(model)
reset_checkstates(model)
end
def onOpenModel(model)
reset_checkstates(model)
end
def reset_checkstates(model)
if @@checkstate[model] == MF_UNCHECKED || !@@checkstate[model].is_a?(Hash)
@@checkstate[model]= Hash::new(MF_UNCHECKED)
end
@@checkstate.delete_if { |model_key,h| !model_key.valid? }
@@checkstate[model].each do |model_key,command_checkstates|
command_checkstates.each do |command,check|
check = MF_UNCHECKED
end
end
end
unless @@loaded
# Declare a command called "NiftyThing":
@@command[:NiftyThing]= UI::Command::new("NiftyThing") {
nifty_thing()
}.set_validation_proc {
@@checkstate[Sketchup::active_model][:NiftyThing] rescue MF_UNCHECKED
}
# set icons for @@command[:NiftyThing]
# ... other command declarations ...
# Attach this module itself as an AppObserver:
Sketchup.add_observer(self)
@@loaded = true
end
end
Added a rescue clause in modifier position to the validation proc, just in case during startup that the active model object is not ready when the UI wants to call validation on a toolbar command button, etc.
Simple answer, … I’ll quote the code in the example, and add a # <<<---<<<< comment …
# Declare a command called "NiftyThing":
@@command[:NiftyThing]= UI::Command::new("NiftyThing") {
nifty_thing() # <<<------<<<< The command's block object CALLS a ...
# method named nifty_thing() thats has all the command's control code.
}.set_validation_proc {
@@checkstate[Sketchup::active_model][:NiftyThing] rescue MF_UNCHECKED
}
Incorrect, because …
(1) there is no class in the example.
(2) It is a module, and the last line of the code closes the module edit.
(I loaded the above example into Ruby with no error except “add_observer” mispelled.)
(3) At the top of the module, there is the statement: extend self
which extends the module using itself as a mixin module.
(This allows other methods in the module to call each other without the self. qualification.)
class MyAppObserver < Sketchup::AppObserver
def onNewModel(model)
A.send :unchek
model.selection.add_observer(MyAppObserver.new)
end
end
Sketchup.add_observer(MyAppObserver.new)
class A
class << self
def unchek
@@pro_01.set_validation_proc{MF_UNCHECKED}
end
def message
UI.messagebox("Icon checked, open a new template to uncheck it. ;-)")
@@pro_01.set_validation_proc{MF_CHECKED}
end
tb = UI::Toolbar.new(%Q(TESTE2))
@@pro_01 = UI::Command.new("MESSAGE CHECKED"){ A.message}
@@pro_01.set_validation_proc{MF_UNCHECKED}
tb.add_item(@@pro_01)
tb.restore
end
end
You can copy and paste the code into the ruby console to test.