首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在SketchUp Ruby中创建和设置自定义游标

如何在SketchUp Ruby中创建和设置自定义游标
EN

Stack Overflow用户
提问于 2022-03-11 09:33:39
回答 1查看 144关注 0票数 1

我正在SketchUp中创建一个自定义工具,并且希望能够设置自定义游标来帮助用户识别他们正在使用的工具。下面是我的当前代码,以防万一你想看看我到目前为止有什么。

代码语言:javascript
复制
#Some presets
system "clear"
model = Sketchup.active_model
entities = model.active_entities
selection=Sketchup.active_model.selection
materials=Sketchup.active_model.materials
layer_array=Sketchup.active_model.layers

module Examples
  module CustomTool

    class LineTool

      def activate
    @mouse_ip = Sketchup::InputPoint.new
        @picked_first_ip = Sketchup::InputPoint.new
    #num = "2"
    #UI.messagebox((num))
        update_ui
      end

      def deactivate(view)
        view.invalidate
      end

      def resume(view)
        update_ui
        view.invalidate
      end

      def suspend(view)
        view.invalidate
      end

      def onCancel(reason, view)
        reset_tool
        view.invalidate
      end

      def onMouseMove(flags, x, y, view)
        if picked_first_point?
          @mouse_ip.pick(view, x, y, @picked_first_ip)
      UI.messagebox("One")
        else
          @mouse_ip.pick(view, x, y)
      UI.messagebox("Two")
        end
        view.tooltip = @mouse_ip.tooltip if @mouse_ip.valid?
        view.invalidate
      end

      def onLButtonDown(flags, x, y, view)
        if picked_first_point? && create_edge > 0
          reset_tool
        else
          @picked_first_ip.copy!(@mouse_ip)
      #UI.messagebox("Test")
        end
        update_ui
        view.invalidate
      end

      #CURSOR_PENCIL = 641
      #def onSetCursor
        #UI.set_cursor(CURSOR_PENCIL)
      #end

       cursor_id = nil
       cursor_path = Sketchup.find_support_file("Pointer.png", "Plugins")
       if cursor_path
         cursor_id = UI.create_cursor(cursor_path, 0, 0)
       end

       def onSetCursor
         UI.set_cursor(cursor_id)
       end

    end # class LineTool

    def self.activate_line_tool
      Sketchup.active_model.select_tool(LineTool.new)
    end

    unless file_loaded?(__FILE__)
      menu = UI.menu('Plugins')
      menu.add_item('Darrian\'s Point Maker Tool') {
        self.activate_line_tool
      }
      file_loaded(__FILE__)
    end

  end # module CustomTool
end # module Examples

这最初来自于github上的一个示例,其中一个善良的用户正在演示如何创建自定义工具。我试着给它加点旋转,但使我陷入了严重的纠缠之中。不过,我想重点讨论的代码是以下这段代码.

代码语言:javascript
复制
   cursor_id = nil
   cursor_path = Sketchup.find_support_file("Pointer.png", "Plugins")
   if cursor_path
     cursor_id = UI.create_cursor(cursor_path, 0, 0)
   end

   def onSetCursor
     UI.set_cursor(cursor_id)
   end

使用这部分代码,我尝试将自定义游标设置为“Pointer.png”。图片在我电脑上的插件文件夹中,我有一个图像来验证。

https://i.stack.imgur.com/6aGGD.png

当我运行完整的代码时,我会看到一个额外的工具被添加到我的SketchUp窗口顶部的扩展选项卡上,名为“”。当我单击此工具时,我希望光标会更改为“Pointer.png”上的图像。但是,它作为白色箭头游标保留下来。

如果你好奇的话,这张照片是“我的世界”中的钻石剑(我目前正处于实验阶段)。我从这里得到了图像..。

https://minecraft.fandom.com/wiki/Sword

我所面临的问题是什么,如何纠正?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-11 21:37:12

示例代码对光标图形使用PNG文件格式,这是可以的,但我建议使用矢量图形。

将来,SketchUp将在'Mac‘和'Windows’上都采用SVG格式。但是,到目前为止,我们的开发人员需要在Mac上使用PDF,对于Windows需要使用SVG。

我将图标路径更改为相对于您的“.rb”文件,而不是插件文件夹路径。

建议创建一个文件夹并在其中添加您的资产。我没有在下面的“可能的解决方案”代码示例中包括这一点。

代码语言:javascript
复制
                Plugins Folder 
                       ↓
     _____________________________________
     ↓                                   ↓
set-cursor.rb                        (Folder)
                                    set-cursor
                                         ↓
                                      (Folder)
                                       assets
                                         ↓
                      _______________________________________
                      ↓                  ↓                  ↓
                   (Folder)           (Folder)           (Folder)
                     pdf                svg                png
                      ↓                  ↓                  ↓
                  Pointer.pdf       Pointer.svg        Pointer.png
代码语言:javascript
复制
# Example on how to get cursor path for PNG format
# If you follow the file structure in the above example.
# -------------------------------------------------------
# Finds path relative to your ruby script file.
path = File.dirname(__FILE__)
cursor_path = File.join(path, 'set-cursor/assets/png/Pointer.png')

另外,在调用下面的UI.create_cursor(cursor_path, 8, 0)时,请确保您调整了x,y的参数值,以便很好地处理光标图形。

可能的解决方案:

如何测试下面的代码:

  1. 创建.rb文件(例如,将其命名为“set-cursor.rb”),并复制和粘贴下面的代码。然后,将该文件放入folder.
  2. Activate插件中,该工具位于SketchUp中,方法是转到Plugins or Extension Menu -> Darrian's Point Maker Tool
  3. Done!如果插件文件夹中有一个名为“Pointer.png”的PNG文件,那么这个脚本将创建一个游标.

代码语言:javascript
复制
module Examples
  module CustomTool
    MAC = RUBY_PLATFORM =~ /(darwin)/i ? true : false
    WIN = RUBY_PLATFORM =~ /(mswin|mingw)/i ? true : false
    class LineTool
      def activate
        # Using PDF format for Mac and SVG format for Windows.
        if MAC
          icon_format = '.pdf'
        else
          icon_format = '.svg'
        end
        # Option for use Vector graphics for the icon.
        cursor_name = "Pointer#{icon_format}"
        # OR use PNG, but I recomend using PDF for MAC & SVG for Windows.
        cursor_name = 'Pointer.png'
        # Finds path relative to your ruby script file.
        path = File.dirname(__FILE__)
        # Pointer.png needs to be in same path as your .rb file
        cursor_path = File.join(path, cursor_name)
        @result = File.file?(cursor_path)
        unless @result
          msg = "Can't find the 'cursor icon' path"
          UI.messagebox(msg, MB_OK)
          return
        end

        # ----------------------------------------------------------------------
        # The create_cursor method is used to create a cursor from an image
        # file at the specified location. This must be called from within a
        # custom Tool.
        # ----------------------------------------------------------------------
        # Since SketchUp 2016 it is possible to provide vector images
        # for the cursors. SVG format for Windows and PDF format for OS X.
        # ----------------------------------------------------------------------
        # .create_cursor(filename, hot_x, hot_y) # => Integer

        @cursor_id = UI.create_cursor(cursor_path, 8, 0)
      end

      def onSetCursor
        # ----------------------------------------------------------------------
        # The 'set_cursor' method is used to change the cursor to a new cursor
        # with a given cursor id. See UI.create_cursor and the Tool class for
        # details on creating your own tools with arbitrary cursors.

        UI.set_cursor(@cursor_id) if @result
      end
      # # #
    end # class LineTool
    unless defined?(@loaded)
      UI.menu('Plugins').add_item('Darrian\'s Point Maker Tool') do
        Sketchup.active_model.select_tool(Examples::CustomTool::LineTool.new)
      end
      @loaded = true
    end
  end # module CustomTool
end # module Examples
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71436416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档