dracus17 [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=55199 Full Mouse Interface <p> <table><tr><td> <a href="/bbs/?pid=93499#p"> <img src="/bbs/thumbs/pico8_dracus17mouse-1.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=93499#p"> dracus17's Mouse Interface</a><br><br> by <a href="/bbs/?uid=55199"> dracus17</a> <br><br><br> <a href="/bbs/?pid=93499#p"> [Click to Play]</a> </td></tr></table> </p> <p>So I was making a project which needed some mouse support, and I decided to make an interface that was universally applicable. After some work to generalize the code, I created this example to share with you people.</p> <h2>The Interface</h2> <p>Usage is pretty simple, tab 1 in the cart above contains all one needs to be able to implement this, so just copy it over to a new tab in your cart.</p> <p>Insert <strong>init_mouse</strong> in your <strong>_init</strong>, with a table containing your interactable elements (we'll come back to this table). <strong>update_mouse</strong> and <strong>draw_mouse</strong> are quite self-explanatory.</p> <p>The mouse calls the function <strong>mouse_down</strong> whenever you left click, and <strong>mouse_up</strong> when released (I left right and middle clicking out to support people on mobile or other platforms where mouses have less buttons, but they're easy enough to implement if needed).</p> <p>The <strong>draw_mouse</strong> function is easy enough to modify to allow for multi-sprite cursors or to remove multiple cursor sprites altogether.</p> <h3>The widgets</h3> <p>The table you feed to <strong>init_mouse</strong> can contain widgets which differ greatly from one another, the only things the mouse really needs from the elements are their coordinates (in pixels) and dimensions (in sprites). <em>If you have widgets with non-sprite dimensions, just edit the code which checks if the mouse is over a widget in the <strong>in_bounds_of</strong> function.</em></p> <p>The elements you can add which add behavior are: (*needed to work properly)</p> <ul> <li><strong>x</strong> * (x coordinate of left corner in pixels)</li> <li><strong>y</strong> * (y coordinate of left corner in pixels)</li> <li><strong>w</strong> * (width in sprites)</li> <li><strong>h</strong> * (height in sprites)</li> <li><strong>clickable</strong><br /> A boolean which, if true, makes the mouse change its sprite when hovering over the widget.</li> <li><strong>on_down</strong><br /> A function which is called when the mouse clicks on the widget.</li> <li><strong>on_up</strong><br /> A function which is called when the mouse releases after having clicked on the widget.</li> <li><strong>on_hover</strong><br /> A function which is called every frame the mouse hovers over the widget.</li> </ul> <p>By implementing these, you can achieve pretty much any behavior one might need from a mouse interface. I made this as general as possible, so some simplification is always useful to reduce token count.</p> <p>Do read over the code in the cart. The example shows of how to use the interface in case of any doubt.</p> <h3>Final notes</h3> <ul> <li>I do use the term &quot;widget&quot; very liberally here, yes.</li> <li>This was made for a personal project of mine, so it might not be as useful in your specific case, whereas in mine, it was.</li> <li>I shared this because I figured it might be useful to someone who, like me, needs mouse support in pico-8 and can't find other sources of help. I made this myself and shared it so others don't have to reinvent the wheel. </li> <li>I acknowledge that there must certainly be other implementations out there, definitely better than mine, but I was too lazy to find them.</li> <li>Do criticize (constructively): I love to learn!</li> </ul> https://www.lexaloffle.com/bbs/?tid=43344 https://www.lexaloffle.com/bbs/?tid=43344 Tue, 15 Jun 2021 02:40:10 UTC