ModraUp Ruby API Documentation Ruby API Reference

Class: ModraUp::Tool

Inherits:
Object
  • Object
show all

Overview

Callback list for custom tools. SketchUp's official API does not have this class (cannot be written as class MyTool < ModraUp::Tool), so it is implemented by duck typing, providing the necessary methods and then passing them to ModraUp::Model#select_tool.

ModraUp makes the same protocol into a browsable class for internal training: no inheritance is required; missing methods are skipped by the host. When determining if a tool has implemented a callback, it ignores the documentation stub methods in this class, consistent with SketchUp's duck typing. Dynamic Components' DCInteractTool is an example of such a regular Ruby class.

Currently, it forwards: activate, deactivate, suspend, resume, onSetCursor, draw (calls getExtents before drawing), onMouseMove, left/middle/right mouse clicks and double-clicks, onMouseWheel (returns true to prevent viewport scaling), onMouseEnter, onMouseLeave, onKeyDown, onKeyUp, onCancel, onReturn, enableVCB? / onUserText, getMenu. Middle mouse button still navigates the viewport. getInstructorContentDirectory is queried at activate, and the HTML files in that directory are loaded when the "Instructor" window menu is opened.

Modifier key constants are found at VK_SHIFT, CONSTRAIN_MODIFIER_MASK, COPY_MODIFIER_MASK.

Note: ModraUp extension. SketchUp does not define the ModraUp::Tool class; this class is only used as a documentation index.

Examples:

duck type (recommended, similar to SketchUp)

class TipTool
  def activate
    ModraUp.set_status_text "tip"
  end
  def onMouseMove(flags, x, y, view)
    view.pick_helper.do_pick(x, y)
  end
end
defined?(ModraUp::Tool)  #=> true
# ModraUp.active_model.select_tool(TipTool.new)

Instance Method Summary # collapse

Instance Method Details

tool.activate ⇒ nil

This method is called when the tool is selected by ModraUp::Model#select_tool. It is suitable for initializing the tool's state.

Returns:

  • (nil)

Examples:

class TipTool
  def activate
    ModraUp.set_status_text "tip"
  end
end

tool.deactivate(view) ⇒ nil

Called when switching to another tool or by select_tool(nil).

Parameters:

  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def deactivate(view)
    view.invalidate
  end
end

tool.draw(view) ⇒ nil

Redraw temporary graphics in the viewport when the tool is drawn. SketchUp requires implementing #getExtents when drawing outside the model's bounding box; #getExtents is called before drawing.

Parameters:

  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def draw(view)
    view.invalidate
  end
end

tool.enableVCB? ⇒ Boolean

Controls input in the Measurements box (VCB). Implementing #onUserText enables input. Implement this method to return false when the tool is not ready, for example before the first click. If omitted, the box stays enabled when the tool implements #onUserText. Empty callbacks inherited from ModraUp::Tool do not count as implementations.

Returns:

  • (Boolean)

Examples:

Turn off before selecting.

class LengthTool
  def enableVCB?
    @ready == true
  end
end

tool.getExtents ⇒ Geom::BoundingBox, nil

Tell SketchUp how large the temporary graphics are to avoid being clipped. This can be omitted if there is no temporary drawing.

Returns:

  • (Geom::BoundingBox, nil)

Examples:

class TipTool
  def getExtents
    Geom::BoundingBox.new.add(ORIGIN)
  end
end

tool.getInstructorContentDirectory ⇒ String, nil

Directory where the instructor panel HTML content is located. It is queried when activated; the directory is loaded when the "Window / Instructor" opens.

Returns:

  • (String, nil)

Examples:

class TipTool
  def getInstructorContentDirectory
    File.join(__dir__, "instructor")
  end
end

tool.getMenu(menu) ⇒ niltool.getMenu(menu, flags, x, y, view) ⇒ nil

Provides a tool's own right-click menu. If implemented, only the items added by this method are displayed, and they do not mix with the default menu or UI.add_context_menu_handler. If not implemented, the default menu is displayed based on the hit object or blank area.

Parameters:

  • menu (ModraUp::Menu)
  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def getMenu(menu)
    menu.add_item("Cancel") { ModraUp.active_model.select_tool(nil) }
  end
end

tool.onCancel(reason, view) ⇒ nil

Cancel the current operation. reason: 0 Esc, 1 reselecting the same tool, 2 undo (before an undo operation). If #onKeyDown returns true for Esc, this method is not called.

Parameters:

  • reason (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onCancel(reason, view)
    ModraUp.active_model.select_tool(nil) if reason == 0
  end
end

tool.onKeyDown(key, repeat, flags, view) ⇒ Boolean, nil

Press the keyboard. To read a value from VCB, implement #onUserText instead of this method. Return true for Esc to prevent triggering #onCancel. Use top-level VK_SHIFT instead of writing ModraUp::VK_SHIFT.

Parameters:

  • key (Integer) Virtual keys, such as VK_ESCAPE / VK_SHIFT
  • repeat (Integer) Pressing once returns 1; holding down automatically repeats with 2.
  • flags (Integer)
  • view (ModraUp::View)

Returns:

  • (Boolean, nil)

Examples:

class TipTool
  def onKeyDown(key, repeat, flags, view)
    key == VK_ESCAPE || key == VK_SHIFT
  end
end

tool.onKeyUp(key, repeat, flags, view) ⇒ Boolean, nil

Release the keyboard.

Parameters:

  • key (Integer)
  • repeat (Integer) Normal release is 1 (consistent with SketchUp).
  • flags (Integer)
  • view (ModraUp::View)

Returns:

  • (Boolean, nil)

Examples:

class TipTool
  def onKeyUp(key, repeat, flags, view)
  end
end

tool.onLButtonDoubleClick(flags, x, y, view) ⇒ nil

Left mouse button double-click.

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onLButtonDoubleClick(flags, x, y, view)
    view.pick_helper.do_pick(x, y)
  end
end

tool.onLButtonDown(flags, x, y, view) ⇒ nil

Left mouse button down. flags contains MK_LBUTTON, CONSTRAIN_MODIFIER_MASK, etc.

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onLButtonDown(flags, x, y, view)
    view.pick_helper.do_pick(x, y)
  end
end

tool.onLButtonUp(flags, x, y, view) ⇒ nil

Left mouse button released. DC Interact performs onclick here.

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onLButtonUp(flags, x, y, view)
    view.invalidate
  end
end

tool.onMButtonDoubleClick(flags, x, y, view) ⇒ nil

Mouse middle button double-click.

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onMButtonDoubleClick(flags, x, y, view)
  end
end

tool.onMButtonDown(flags, x, y, view) ⇒ nil

Mouse middle button down.

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onMButtonDown(flags, x, y, view)
  end
end

tool.onMButtonUp(flags, x, y, view) ⇒ nil

Mouse middle button up.

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onMButtonUp(flags, x, y, view)
  end
end

tool.onMouseEnter(view) ⇒ nil

The mouse pointer entered the viewport.

Parameters:

  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onMouseEnter(view)
    view.invalidate
  end
end

tool.onMouseLeave(view) ⇒ nil

The mouse pointer has left the viewport.

Parameters:

  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onMouseLeave(view)
    view.tooltip = ""
  end
end

tool.onMouseMove(flags, x, y, view) ⇒ nil

Called when the pointer moves within the viewport. Use ModraUp::View#pick_helper for picking.

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onMouseMove(flags, x, y, view)
    view.pick_helper.do_pick(x, y)
  end
end

tool.onMouseWheel(flags, delta, x, y, view) ⇒ Boolean, nil

Mouse wheel. Returns true if the tool has handled the event.

Parameters:

  • flags (Integer)
  • delta (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (Boolean, nil)

Examples:

class TipTool
  def onMouseWheel(flags, delta, x, y, view)
    false
  end
end

tool.onRButtonDoubleClick(flags, x, y, view) ⇒ nil

Mouse right button double-click.

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onRButtonDoubleClick(flags, x, y, view)
  end
end

tool.onRButtonDown(flags, x, y, view) ⇒ nil

Right mouse button down. Implement with #getMenu to customize the menu.

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onRButtonDown(flags, x, y, view)
  end
end

tool.onRButtonUp(flags, x, y, view) ⇒ nil

Right button up.

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onRButtonUp(flags, x, y, view)
  end
end

tool.onReturn(view) ⇒ nil

The user presses Return. When the measurement box is open, pressing Return exits it without calling this method. #onUserText

Parameters:

  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def onReturn(view)
    view.invalidate
  end
end

tool.onSetCursor ⇒ Boolean, nil

The host calls this after a mouse event to change the cursor using UI.set_cursor.

Returns:

  • (Boolean, nil)

Examples:

class TipTool
  def onSetCursor
    UI.set_cursor(0)
  end
end

tool.onUserText(text, view) ⇒ nil

The user inputs a measurement in the measurement box and presses Enter. Calling this method opens the measurement box (similar to SketchUp's engine). Pass the current text within the box, including the value set by ModraUp.vcb_value= which should be entered by pressing Enter.

Parameters:

  • text (String)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class LengthTool
  def onUserText(text, view)
    text.to_l
  end
end

tool.resume(view) ⇒ nil

The tool becomes the current tool again after ModraUp::Tools#pop_tool.

Parameters:

  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def resume(view)
    view.invalidate
  end
end

tool.suspend(view) ⇒ nil

Called when ModraUp::Tools#push_tool pushes this tool onto the stack.

Parameters:

  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class TipTool
  def suspend(view)
    view.invalidate
  end
end