Handling Unknown Arguments
-
When testing code for Observers, I was curious about what exactly was being passed to the Observer event methods. I would normally trust the documentation, but in this case... So anyway, here's a little trick. If you prefix an asterisk (*) to the variable name in the method def, every argument passed to the method gets put in the variable as an array element.
The ToolObsever methods below normally accept 3 parameters, according to the API docs;
- A Tools object
- The tool name
- The toolid.
In the code below, the args variable is prefixed with an asterisk, which make it into an array and each argument passed to the method are added to the array. This makes an easy way to inspect all of the arguments.
class MyToolsObserver < Sketchup;;ToolsObserver def onActiveToolChanged(*args) print "onActiveToolChanged; " p args end def onToolStateChanged(*args) print "onToolStateChanged; " p args end end Sketchup.active_model.tools.add_observer(MyToolsObserver.new)
And a look at some output:
onToolStateChanged; [#<Sketchup;;Tools;0xc681940>, "CircleTool", 21096, 0] onToolStateChanged; [#<Sketchup;;Tools;0xc681940>, "CircleTool", 21096, 0] onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "CircleTool", 21096] onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "CircleTool", 21096] onToolStateChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094, 0] onToolStateChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094, 0] onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094] onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094] onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "CameraOrbitTool", 10508] onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "CameraOrbitTool", 10508] onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094] onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094] onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "SelectionTool", 21022] onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "SelectionTool", 21022]
So the output is mostly right according to the docs. In some cases, there is a fourth argument passed to the methods. I can only guess what it is - perhaps a flag, or indicator of the tool state.
-
I am pretty sure it's a tool state that cannot be relied upon. I discussed this with Google some time ago. While it does seem to work for some tools, it completely doesn't for others.
For instance, the line tool. State 1 might be a down and release of the LMB, and state 2 might be the second click to complete the line after you've moved the mouse. However, you can also down left click, move the mouse, and release the mouse to draw the line, in which case, the (hypothetical) state rule above does not apply.
The net of it all is that right now, we cannot depend on the tool state flag for anything, either because we don't have enough additional information to go along with it (like how the line tool is being used), or, it's simply residual information that is being push over through the API and is meaningless. (And that fact that "we're not supposed to be using it - it wasn't put in for us mere mortals")
Perhaps in the future it can be relied upon.
Todd
Advertisement