mousemove interface¶
This page explains the mousemove
interface.
What interface is this?¶
The mousemove
interface binds the mouse moving event handler to any DisplayObject
instance. If you move the mouse cursor on that instance, the interface calls the registered handler.
See also¶
The following page describes the basic mouse event interfaces.
Basic usage¶
Each DisplayObject
instance has the mousemove
method, and you can bind handlers by that.
The following example binds the mouse move event handler to the circle. So if you move a mouse cursor on that, the circle follows the cursor position.
import apysc as ap
def on_mousemove(e: ap.MouseEvent[ap.Circle], options: dict) -> None:
"""
The handler that the circle calls when mousemove.
Parameters
----------
e : MouseEvent
Event instance.
options : dict
Optional arguments dictionary.
"""
circle: ap.Circle = e.this
circle.x = e.stage_x
circle.y = e.stage_y
ap.Stage(
background_color=ap.Color("#333"),
stage_width=200,
stage_height=200,
stage_elem_id="stage",
)
sprite: ap.Sprite = ap.Sprite()
sprite.graphics.begin_fill(color=ap.Color("#0af"))
circle: ap.Circle = sprite.graphics.draw_circle(x=100, y=100, radius=100)
circle.mousemove(on_mousemove)
ap.save_overall_html(dest_dir_path="mousemove_basic_usage/")
Unbind interfaces¶
unbind_mousemove
interface can remove the binding of the mouse move event from the DisplayObject
.
In the following example, the interface removes the mouse move event handler if you click the circle.
import apysc as ap
def on_mousemove(e: ap.MouseEvent[ap.Circle], options: dict) -> None:
"""
The handler that the circle calls when mousemove.
Parameters
----------
e : MouseEvent
Event instance.
options : dict
Optional arguments dictionary.
"""
circle: ap.Circle = e.this
circle.x = e.stage_x
circle.y = e.stage_y
def on_click(e: ap.MouseEvent[ap.Circle], options: dict) -> None:
"""
The handler that the circle calls when clicked.
Parameters
----------
e : MouseEvent
Event instance.
options : dict
Optional arguments dictionary.
"""
circle: ap.Circle = e.this
circle.unbind_mousemove(handler=on_mousemove)
ap.Stage(
background_color=ap.Color("#333"),
stage_width=200,
stage_height=200,
stage_elem_id="stage",
)
sprite: ap.Sprite = ap.Sprite()
sprite.graphics.begin_fill(color=ap.Color("#0af"))
circle: ap.Circle = sprite.graphics.draw_circle(x=100, y=100, radius=100)
circle.mousemove(on_mousemove)
circle.click(on_click)
ap.save_overall_html(dest_dir_path="mousemove_unbind_interface/")
There are also existing the unbind_mousemove_all
interface. This interface unbinds all the handlers from the target DisplayObject
instance.
mousemove API¶
Note: the document build script generates and updates this API document section automatically. Maybe this section is duplicated compared with previous sections.
[Interface signature] mousemove(self, handler: Callable[[apysc._event.mouse_event.MouseEvent, ~_Options], NoneType], *, options: Union[~_Options, NoneType] = None) -> str
[Interface summary]
Add mouse move event listener setting.
[Parameters]
handler
: _HandlerCallable that would be called when mousemove on this instance.
options
: dict or None, default NoneOptional arguments dictionary to be passed to a handler.
[Returns]
name
: strHandler’s name.
[Examples]
>>> import apysc as ap
>>> def on_mousemove(e: ap.MouseEvent[ap.Rectangle], options: dict) -> None:
... stage_x: ap.Int = e.stage_x
... ap.trace("stage_x:", stage_x)
>>> stage: ap.Stage = ap.Stage()
>>> sprite: ap.Sprite = ap.Sprite()
>>> sprite.graphics.begin_fill(color=ap.Color("#0af"))
>>> rectangle: ap.Rectangle = sprite.graphics.draw_rect(
... x=50, y=50, width=50, height=50
... )
>>> _ = rectangle.mousemove(on_mousemove)
[References]
unbind_mousemove API¶
Note: the document build script generates and updates this API document section automatically. Maybe this section is duplicated compared with previous sections.
[Interface signature] unbind_mousemove(self, handler: Callable[[apysc._event.mouse_event.MouseEvent, ~_Options], NoneType]) -> None
[Interface summary]
Unbind a specified handler’s mouse move event.
[Parameters]
handler
: _HandlerUnbinding target Callable.
[Examples]
>>> import apysc as ap
>>> def on_mousemove(e: ap.MouseEvent[ap.Rectangle], options: dict) -> None:
... stage_x: ap.Int = e.stage_x
... ap.trace("stage_x:", stage_x)
>>> def on_click(e: ap.MouseEvent[ap.Rectangle], options: dict) -> None:
... rectangle: ap.Rectangle = e.this
... rectangle.unbind_mousemove(on_mousemove)
>>> stage: ap.Stage = ap.Stage()
>>> sprite: ap.Sprite = ap.Sprite()
>>> sprite.graphics.begin_fill(color=ap.Color("#0af"))
>>> rectangle: ap.Rectangle = sprite.graphics.draw_rect(
... x=50, y=50, width=50, height=50
... )
>>> _ = rectangle.mousemove(on_mousemove)
>>> _ = rectangle.click(on_click)
unbind_mousemove_all API¶
Note: the document build script generates and updates this API document section automatically. Maybe this section is duplicated compared with previous sections.
[Interface signature] unbind_mousemove_all(self) -> None
[Interface summary]
Unbind all mouse move events.
[Examples]
>>> import apysc as ap
>>> def on_mousemove(e: ap.MouseEvent[ap.Rectangle], options: dict) -> None:
... stage_x: ap.Int = e.stage_x
... ap.trace("stage_x:", stage_x)
>>> def on_click(e: ap.MouseEvent[ap.Rectangle], options: dict) -> None:
... rectangle: ap.Rectangle = e.this
... rectangle.unbind_mousemove_all()
>>> stage: ap.Stage = ap.Stage()
>>> sprite: ap.Sprite = ap.Sprite()
>>> sprite.graphics.begin_fill(color=ap.Color("#0af"))
>>> rectangle: ap.Rectangle = sprite.graphics.draw_rect(
... x=50, y=50, width=50, height=50
... )
>>> _ = rectangle.mousemove(on_mousemove)
>>> _ = rectangle.click(on_click)