ModraUp Ruby API Documentation Ruby API Reference

Class: ModraUp::Overlay

Inherits:
Object
  • Object
show all

Overview

Viewport overlay layer. Not a ModraUp::Entity. Subclasses implement draw / mouse callbacks, then add to the model via ModraUp::OverlaysManager#add. The overlay layer is enabled by checking it in the 'Window › Tray Window › Overlays' menu; once #enabled= is true, the viewport calls draw(view) every time it refreshes. The overlay layer's drawings cannot be picked or exported.

Each model has a ModraUp::OverlaysManager. To appear in all documents, create an instance in ModraUp::AppObserver and add it in onNewModel / onOpenModel.

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo", description: "Write in the upper-left corner of the viewport")
  end

  def draw(view)
    view.draw_text([20, 20, 0], "Demo overlay")
  end
end
ov = DemoOverlay.new
ModraUp.active_model.overlays.add(ov)

Class Method Summary # collapse

Instance Method Summary # collapse

  • #overlay_id ⇒ String
    The unique ID passed during construction.
  • #name ⇒ String
    Panel display name.
  • #description ⇒ String
    Panel description, displayed below the list when this line is selected.
  • #description= ⇒ String
    Modify the description. The overlay refreshes in the viewport when added to the model.
  • #enabled? ⇒ Boolean
    Enabled or not. It is false before ModraUp::OverlaysManager#add.
  • #enabled= ⇒ Boolean
    Toggle the overlay layer on or off, which is the same as the checkbox in the palette. Call #start when turning on and #stop when turning off; do nothing if the state does not change.
  • #valid? ⇒ Boolean
    After removing an overlay with ModraUp::OverlaysManager#remove, its validity becomes false and it cannot be added back.
  • #source ⇒ String
    The overlay layer comes from which extension: determined by the SketchupExtension#name found at the time of construction. It is displayed in the tray as "Extension: Name". If not in any registered extension's folder (e.g., created in the Ruby console), it is an empty string.
  • #start ⇒ nil
    Called when the overlay is enabled through its checkbox or #enabled=. Attach observers and prepare caches here. The base implementation does nothing.
  • #stop ⇒ nil
    Called when the overlay is disabled or removed through ModraUp::OverlaysManager#remove while enabled. Detach observers and clear caches here. The base implementation does nothing.
  • #draw(view) ⇒ nil
    Callback (subclass implementation): Called every time the viewport is refreshed while the overlay is active. Only draw the cached data and do not perform any recomputation here.
  • #onMouseMove(flags, x, y, view) ⇒ nil
    Implement this callback to track pointer motion within the viewport while the overlay is enabled, regardless of the active tool. Coordinates are in logical pixels. Keep this frequent callback lightweight.
  • #onMouseEnter(flags, x, y, view) ⇒ nil
    Called when the pointer enters the viewport while the overlay is enabled. Implement this callback in a subclass.
  • #onMouseLeave(view) ⇒ nil
    Called when the pointer leaves the viewport while the overlay is enabled. Implement this callback in a subclass.
  • #inspect ⇒ String
    Returns a string of the form #<Sketchup::Overlay:0x...>, omitting instance variables.

Class Method Details

Overlay.new(id, name, description: "") ⇒ overlay

Construct with source inferred by call stack #source: take the extension name of the registered file folder where the calling file is located.

Parameters:

  • id (String) Unique globally, suggest company.extension.overlay
  • name (String) Short name on the panel
  • description (String) Panel description, defaults to empty.

Returns:

  • (ModraUp::Overlay)

Raises:

  • [ArgumentError] id or name is empty

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
end

Instance Method Details

overlay.overlay_id ⇒ String

The unique ID passed during construction.

Returns:

  • (String)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
end
DemoOverlay.new.overlay_id  #=> "demo.overlay"

overlay.name ⇒ String

Panel display name.

Returns:

  • (String)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
end
DemoOverlay.new.name  #=> "Demo"

overlay.description ⇒ String

Panel description, displayed below the list when this line is selected.

Returns:

  • (String)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo", description: "hi")
  end
end
DemoOverlay.new.description  #=> "hi"

overlay.description = text ⇒ String

Modify the description. The overlay refreshes in the viewport when added to the model.

Parameters:

  • text (String)

Returns:

  • (String)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
end
ov = DemoOverlay.new
ov.description = "x"

overlay.enabled? ⇒ Boolean

Enabled or not. It is false before ModraUp::OverlaysManager#add.

Returns:

  • (Boolean)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
end
DemoOverlay.new.enabled?  #=> false

overlay.enabled = flag ⇒ Boolean

Toggle the overlay layer on or off, which is the same as the checkbox in the palette. Call #start when turning on and #stop when turning off; do nothing if the state does not change.

Parameters:

  • flag (Boolean)

Returns:

  • (Boolean)

Raises:

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
end
ov = DemoOverlay.new
ModraUp.active_model.overlays.add(ov)
ov.enabled = true

overlay.valid? ⇒ Boolean

After removing an overlay with ModraUp::OverlaysManager#remove, its validity becomes false and it cannot be added back.

Returns:

  • (Boolean)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
end
DemoOverlay.new.valid?  #=> true

overlay.source ⇒ String

The overlay layer comes from which extension: determined by the SketchupExtension#name found at the time of construction. It is displayed in the tray as "Extension: Name". If not in any registered extension's folder (e.g., created in the Ruby console), it is an empty string.

Returns:

  • (String)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
end
DemoOverlay.new.source  #=> "" (created in the console)

overlay.start ⇒ nil

Called when the overlay is enabled through its checkbox or #enabled=. Attach observers and prepare caches here. The base implementation does nothing.

Returns:

  • (nil)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
  def start
    @cache = []
  end
end

overlay.stop ⇒ nil

Called when the overlay is disabled or removed through ModraUp::OverlaysManager#remove while enabled. Detach observers and clear caches here. The base implementation does nothing.

Returns:

  • (nil)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
  def stop
    @cache = nil
  end
end

overlay.draw(view) ⇒ nil

Callback (subclass implementation): Called every time the viewport is refreshed while the overlay is active. Only draw the cached data and do not perform any recomputation here. Overlay layers are drawn in the order of the tray list from top to bottom, with later-drawn layers overlaying earlier-drawn ones.

Parameters:

  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
  def draw(view)
    view.drawing_color = "red"
    view.draw2d(GL_LINES, [[10, 10, 0], [200, 10, 0]])
  end
end

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

Implement this callback to track pointer motion within the viewport while the overlay is enabled, regardless of the active tool. Coordinates are in logical pixels. Keep this frequent callback lightweight.

Parameters:

  • flags (Integer) mouse buttons and modifier keys
  • x (Integer)
  • y (Integer)
  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
  def onMouseMove(flags, x, y, view)
    @mouse = [x, y, 0]
    view.invalidate
  end
end

overlay.onMouseEnter(flags, x, y, view) ⇒ nil

Called when the pointer enters the viewport while the overlay is enabled. Implement this callback in a subclass.

Parameters:

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

Returns:

  • (nil)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
  def onMouseEnter(flags, x, y, view)
    @inside = true
  end
end

overlay.onMouseLeave(view) ⇒ nil

Called when the pointer leaves the viewport while the overlay is enabled. Implement this callback in a subclass.

Parameters:

  • view (ModraUp::View)

Returns:

  • (nil)

Examples:

class DemoOverlay < ModraUp::Overlay
  def initialize
    super("demo.overlay", "Demo")
  end
  def onMouseLeave(view)
    @inside = false
    view.invalidate
  end
end

overlay.inspect ⇒ String

Returns a string of the form #<Sketchup::Overlay:0x...>, omitting instance variables.

Returns:

  • (String)

Examples:

ModraUp::Overlay.new("demo.overlay", "Demo").inspect