如何在 SketchUp 中创建和设置自定义光标 Ruby
How to create and set a custom cursor in SketchUp Ruby
我正在 SketchUp 中创建自定义工具 Ruby 并希望能够设置自定义光标以帮助用户识别他们正在使用的工具。下面是我现有的代码,以防你想看看我目前拥有的代码。
#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 上的一个示例,其中一位好心的用户正在演示如何创建自定义工具。我试图给它添加一个旋转,但让自己陷入了严重的困境。然而,我想关注的代码是下面显示的这段代码吗...
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'。图片在我电脑的plugins文件夹里,我有一张图片可以验证一下。
https://i.stack.imgur.com/6aGGD.png
当我 运行 完整代码时,我得到一个额外的工具被添加到我的 SketchUp window 顶部的扩展选项卡中,名为“Darrian's Point Maker Tool”。当我单击此工具时,我希望光标更改为 'Pointer.png' 上的图像。然而,它仍然是一个白色箭头光标。
如果你很好奇,这张图片是一把来自我的世界的钻石剑(我目前正处于试验阶段)。我从这里得到了图像...
https://minecraft.fandom.com/wiki/Sword
我面临的问题是什么,我该如何解决?
您的示例代码使用 PNG 文件格式作为光标图形,这没问题,但我建议改用矢量图形。
以后SketchUp将在'Mac'和'Windows'上采用SVG格式。但是,截至今天,我们的开发人员需要为 Mac 使用 PDF,为 Windows 使用 SVG。
我将图标路径更改为相对于您的“.rb”文件,而不是插件文件夹路径。
建议创建一个文件夹并在其中添加您的资产。我没有将此包含在下面的 'Possible solution' 代码示例中。
Plugins Folder
↓
_____________________________________
↓ ↓
set-cursor.rb (Folder)
set-cursor
↓
(Folder)
assets
↓
_______________________________________
↓ ↓ ↓
(Folder) (Folder) (Folder)
pdf svg png
↓ ↓ ↓
Pointer.pdf Pointer.svg Pointer.png
# 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 的参数值以与光标图形配合使用。
可能的解决方案:
如何测试下面的代码:
- 创建 .rb 文件(例如将其命名为 'set-cursor.rb')并复制并粘贴下面的代码。然后将文件放在 SketchUp Plugins 文件夹中。
- 通过转到
Plugins or Extension Menu
-> Darrian's Point Maker Tool
激活 Sketchup 中的工具
- 完成!如果插件文件夹中有一个名为 'Pointer.png' 的 PNG 文件,那么此脚本将创建一个光标。
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
我正在 SketchUp 中创建自定义工具 Ruby 并希望能够设置自定义光标以帮助用户识别他们正在使用的工具。下面是我现有的代码,以防你想看看我目前拥有的代码。
#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 上的一个示例,其中一位好心的用户正在演示如何创建自定义工具。我试图给它添加一个旋转,但让自己陷入了严重的困境。然而,我想关注的代码是下面显示的这段代码吗...
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'。图片在我电脑的plugins文件夹里,我有一张图片可以验证一下。
https://i.stack.imgur.com/6aGGD.png
当我 运行 完整代码时,我得到一个额外的工具被添加到我的 SketchUp window 顶部的扩展选项卡中,名为“Darrian's Point Maker Tool”。当我单击此工具时,我希望光标更改为 'Pointer.png' 上的图像。然而,它仍然是一个白色箭头光标。
如果你很好奇,这张图片是一把来自我的世界的钻石剑(我目前正处于试验阶段)。我从这里得到了图像...
https://minecraft.fandom.com/wiki/Sword
我面临的问题是什么,我该如何解决?
您的示例代码使用 PNG 文件格式作为光标图形,这没问题,但我建议改用矢量图形。
以后SketchUp将在'Mac'和'Windows'上采用SVG格式。但是,截至今天,我们的开发人员需要为 Mac 使用 PDF,为 Windows 使用 SVG。
我将图标路径更改为相对于您的“.rb”文件,而不是插件文件夹路径。
建议创建一个文件夹并在其中添加您的资产。我没有将此包含在下面的 'Possible solution' 代码示例中。
Plugins Folder
↓
_____________________________________
↓ ↓
set-cursor.rb (Folder)
set-cursor
↓
(Folder)
assets
↓
_______________________________________
↓ ↓ ↓
(Folder) (Folder) (Folder)
pdf svg png
↓ ↓ ↓
Pointer.pdf Pointer.svg Pointer.png
# 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 的参数值以与光标图形配合使用。
可能的解决方案:
如何测试下面的代码:
- 创建 .rb 文件(例如将其命名为 'set-cursor.rb')并复制并粘贴下面的代码。然后将文件放在 SketchUp Plugins 文件夹中。
- 通过转到
Plugins or Extension Menu
->Darrian's Point Maker Tool
激活 Sketchup 中的工具
- 完成!如果插件文件夹中有一个名为 'Pointer.png' 的 PNG 文件,那么此脚本将创建一个光标。
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