Wiz Programming Tutorials

You can set any object to be a MouseInterest, and when the user clicks on the object, you'll get a notification.

Here's an example, which you'd put in a file called mousy.py:

<span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>classes<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>StaticObject<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>commands<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>MouseCommandEvent<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>properties<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>MouseInterest<span class='keyword'></span><br><br><span class='keyword'>class</span> <span class='function'>mousy</span>(StaticObject, MouseInterest):<br><br>    <span class='keyword'>def</span> <span class='function'>initialize</span>(self):<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>super__initialize<span class='keyword'></span>()<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setImage<span class='keyword'></span>('<span class='keyword'></span>monsters<span class='keyword'></span>/<span class='keyword'></span>animal<span class='keyword'></span>'/<span class='keyword'></span>rat<span class='keyword'></span>)<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setProperty<span class='keyword'></span>('<span class='keyword'></span>short<span class='keyword'></span>', '<span class='keyword'></span>mouse<span class='keyword'></span>')<br><br>    <span class='keyword'>def</span> <span class='function'>mouseClicked</span>(self, event):<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>broadcast<span class='keyword'></span>('<span class='keyword'></span>The<span class='keyword'></span> <span class='keyword'></span>mouse<span class='keyword'></span> <span class='keyword'></span>squeaks<span class='keyword'></span>.')<br>        <span class='keyword'></span>event<span class='keyword'></span>.<span class='keyword'></span>consume<span class='keyword'></span>()

To run the example, we cloned the mouse, dropped it, stepped away, and clicked on it.

You don't get mouse events if the user clicks on the object in their Ground View, although if you need it, we can always change it.

We called event.consume() so that we don't also get the default handling for the event. If we hadn't put in that line, the player would have run to the mouse (after the mouse squeaked). You can leave the line out if you want the system to handle the mouse after you're done with it.

MouseCommandEvent

The event that's passed into mouseClicked() contains some useful information:

  • getAgent() returns the player who clicked the mouse.

  • getButton() returns "left", "middle", or "right", depending on which mouse button the user pressed.

  • getMapCoords() returns a wyvern.lib.Point containing the map coordinates where the user clicked.

  • getX() and getY() return the coordinates of the mouse press relative the the player's viewport. So if the player clicked in the upper-left corner tile, it would be (0, 0). You probably won't need to use this function very often.

Map MouseInterest

If you create your own GameMap implementation in Python, you can have the map implement MouseInterest to get all clicks in the map.

Here's the absolute simplest example — a map that tells you were you clicked in it:

<span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>kernel<span class='keyword'></span>.<span class='keyword'></span>maps<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>DenseMap<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>properties<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>MouseInterest<span class='keyword'></span><br><br><span class='keyword'>class</span> <span class='function'>mousemap</span>(DenseMap, MouseInterest):<br><br><span class='comment'>    # this is required for DenseMap subclasses</span><br>    <span class='keyword'>def</span> <span class='function'>__init__</span>(self, width, height):<br>        <span class='keyword'></span>DenseMap<span class='keyword'></span>.<span class='keyword'></span>__init__<span class='keyword'></span>(<span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>, <span class='keyword'></span>width<span class='keyword'></span>, <span class='keyword'></span>height<span class='keyword'></span>)<br><br>    <span class='keyword'>def</span> <span class='function'>mouseClicked</span>(self, event):<br>        <span class='keyword'></span>agent<span class='keyword'></span> = <span class='keyword'></span>event<span class='keyword'></span>.<span class='keyword'></span>getAgent<span class='keyword'></span>()<br>        <span class='keyword'></span>loc<span class='keyword'></span> = <span class='keyword'></span>event<span class='keyword'></span>.<span class='keyword'></span>getMapCoords<span class='keyword'></span>()<br>        <span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span>('<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>clicked<span class='keyword'></span> <span class='keyword'></span>at<span class='keyword'></span> ' + <span class='keyword'></span>str<span class='keyword'></span>(<span class='keyword'></span>loc<span class='keyword'></span>) )<br>        <span class='keyword'></span>event<span class='keyword'></span>.<span class='keyword'></span>consume<span class='keyword'></span>()

The corresponding mouse.map map file is:

&lt;map&gt;<br>&lt;header class=&quot;wiz/rhialto/python/mousemap.py&quot; width=&quot;10&quot; height=&quot;10&quot;&gt;<br>  &lt;string name=&quot;name&quot; value=&quot;mouse map&quot;/&gt;<br>  &lt;int name=&quot;startX&quot; value=&quot;0&quot;/&gt;<br>  &lt;int name=&quot;startY&quot; value=&quot;0&quot;/&gt;<br>  &lt;arch name=&quot;terrain&quot; path=&quot;terrain/wood_floor&quot;/&gt;<br>&lt;/header&gt;<br>&lt;/map&gt;

To try this example, goto wiz/rhialto/python/mouse and click on the floor.

<< Previous Chapter Next Chapter >>