我正在 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”。該影像位于我計算機上的插件檔案夾中,我有一個影像來驗證這一點。
https://i.stack.imgur.com/6aGGD.png
當我運行完整的代碼時,我會在我的 SketchUp 視窗頂部的擴展選項卡中添加一個額外的工具,名為“Darrian's Point Maker Tool”。當我單擊此工具時,我希望游標變為“Pointer.png”上的影像。然而,它仍然是一個白色箭頭游標。
如果你很好奇,圖片是來自我的世界的鉆石劍(我目前處于試驗階段)。我從這里得到影像...
https://minecraft.fandom.com/wiki/Sword
我面臨的問題是什么,我該如何糾正?
uj5u.com熱心網友回復:
您的示例代碼對游標圖形使用 PNG 檔案格式,這沒關系,但我建議改用矢量圖形。
未來,SketchUp 將在“Mac”和“Windows”上采用 SVG 格式。但是,從今天開始,我們的開發人員需要在 Mac 上使用 PDF,在 Windows 上使用 SVG。
我將圖示路徑更改為相對于您的“.rb”檔案,而不是插件檔案夾路徑。
建議創建一個檔案夾并在其中添加您的資產。我沒有將其包含在下面的“可能的解決方案”代碼示例中。
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->激活 Sketchup 中的工具Darrian's Point Maker Tool - 完畢!如果您在插件檔案夾中有一個名為“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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/445018.html
