UI::Tool protoclass

Moderator: Jim

UI::Tool protoclass

Postby Dan Rathbun » Fri Dec 21, 2012 3:34 am


    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
    0


    Last bumped by Dan Rathbun on Fri Dec 21, 2012 3:34 am.
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      SketchUcation One-Liner Adverts

      by Ad Machine » 5 minutes ago



      Ad Machine 
      Robot
       


       

      Return to Skx Extension Library

      Who is online

      Users browsing this forum: No registered users and 1 guest

      Visit our sponsors: