UI::Tool protoclass
Not sure if anyone has proposed a UI::Tool protoclass yet... but it's something I've been missing in the API, and I believe the API needs, IF we ever get our oft professed wish that the Sketchup::ToolsObserver work with Ruby tools.
so here's a start:
- Code: Select all
class UI::Tool
### CLASS METHODS
alias_method(:create,:new)
private_class_method( :create )
public
# self.new()
#
# The public constructor method.
# Calls the private method initialize(), in the new instance,
# after creating it, passing it all arguments it received.
#
def self.new(*args)
obj = self.create(*args)
obj.instance_eval('@first_activation=true')
obj.instance_eval('@tool_id=0')
end
### INSTANCE METHODS
private
def first_activation?()
@first_activation
end
def first_activate()
model = Sketchup.active_model()
tset = model.tools()
@tool_id = tset.active_tool_id()
#
@first_activation = false
end
# initialize()
#
# Instance method called in the new instance itself, by the class
# constructor method, after it has instantiated the instance.
# ! This method prototype must be included in any UI::Tool template.
#
def initialize(*args)
#
# NOTE: @tool_id will be set upon first activation.
#
@tool_name = "" # always English.
@local_name = "" # name in localized language (optional.)
@tool_state = 0 # can use @state instead, if so inclined.
#
# Your additional initialization code here ...
#
end
public
# activate()
#
# Callback: called by model.select_tool handler.
# ! This method prototype must be included in any UI::Tool template.
#
def activate()
# do not remove the following statement!
first_activate if first_activation?
#
# Add your activation code here ...
#
end
# local_name()
#
# Return the localized name of the tool.
#
def local_name()
@local_name
end # def
# tool_id()
#
# Return the [Integer] tool_id of the tool.
#
def tool_id()
@tool_id
end # def
# tool_name()
#
# Return the "Ruby" English name of the tool.
#
def tool_name()
@tool_name
end # def
# tool_state()
#
# Return the [Integer] state of the tool.
#
def tool_state()
if defined?(@tool_state)
@tool_state
elsif defined?(@state)
@state
else
return 0
end
end # def
end # class UI::Tool