Issues with the onMouseMove method within a Ruby Tool
-
Hi all,
I've got some troubles to understand the view.invalidate command implemented inside the onMouseMove method within a Tool Ruby script.
This method is called to refresh the view and to allow the tool to draw some temporary graphics.
If I want the draw method to draw something different either I clicked once or twice, can I create two draw methods (draw1 and draw2) ?
Or should I use a variable (first_click=true or false) to tell the draw method which graphic it must draw ?Eventually, I don't want the draw method to draw simple lines in real time, but I want a component attribute to be modified (LenX) and immediatly displayed when the onMouseMove method is active.
It is a bit like the Push-Pull method : can I tell the draw view to update the LenX attribute's value in real time and to display immediatly the component with the good dimensions ? Or should I use another method than the draw method to allow my onMouseMove method to do that in real time ?Thanks a lot for your help,
Marie
-
dagobo, to start, you could add a variable @first_click = false, then in the onLButtonDown or onRButtonDown methods toggle it by @first_click = !@first_click. In the draw method, use a if then else statement to draw different things given the state of @first_click.
-
SketchUp tools are event-driven - you do not decide when when the methods (defined by the Tool interface) are called - SketchUp calls them. What you need to do is decide how to react depending on the state of your tool.
Here's a very basic example:
class MyTool def activate @state = 0 end def onLButtonDown(flags, x, y, view) @state += 1 end def onMouseMove(flags, x, y, view) case @state when 1; # Do this when 2; # Do that when 3; # do the other thing end end end -
Thanks Jim.
I am actually trying to implement my draw method within my tool ruby script that way :
def draw view @component.set_attribute("dynamic_attributes","angle_h", @angle_horizontal_degres) @component.set_attribute("dynamic_attributes","angle_v", @angle_vertical_degres) $dc_observers.get_latest_class.redraw_with_undo(@component) endThe draw method is called within my onMouseMove method.
What I am trying to get is to make my tool draw and direct my component (@component) towards the right direction (given by both the attributes "angle_h" and "angle_v").
For now, my script doesn't return any error message, but it doesn't do the right operation.
My component is directed toward the same direction each time I compute the script, which is not the good direction.The lines I use to direct the component are perfectly working within my onLButtonDown method, but not within my onMouseMove method (through the draw method) as you can see.
How could I fix this ?
Thanks a lot,
Marie
-
def draw(view)
is a standard method within a Tool class that is auto-called by Sketchup when any event happens that needs a [re]draw - like a onMouseMove ... you don't need to specifically 'call' it in your own code
http://code.google.com/apis/sketchup/docs/ourdoc/tool.html#draw
Don't use draw but rather a new method called saydef update()that is explicitly called from your onMouseMove method when certain conditions are met like so '@state' checking tests... Leave 'draw' to do it's own thing like draw some temporary graphics etc... -
OK so if I make a simple draw method that way :
def draw(view) view.line_width = 3 endand if I create a draw_beam method that way :
def draw_beam() @component.set_attribute("dynamic_attributes","angle_h", @angle_horizontal_degres) @component.set_attribute("dynamic_attributes","angle_v", @angle_vertical_degres) $dc_observers.get_latest_class.redraw_with_undo(@component) endwhich I can call inside my onMouseMove, will it work correctly ?
-
Why ask me?
You are the one with all of the code...
Just test it.You need to call your method
self.draw_beam()within the onMouseMove method - probably after you've test for a certain 'state' being reached - e.g. perhaps initially 'draw_beam' doesn't kick in with @state=0, but after you have done some other stuff when you've then set@state=1- then in the onMouseMove method the line would beself.draw_beam() if @state==1etc, later on, perhaps after you click again, you'd set @state=2 so then the 'draw_beam' stops...
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register LoginAdvertisement