Log In  

Cart #dracus17mouse-1 | 2021-06-15 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

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.

The Interface

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.

Insert init_mouse in your _init, with a table containing your interactable elements (we'll come back to this table). update_mouse and draw_mouse are quite self-explanatory.

The mouse calls the function mouse_down whenever you left click, and mouse_up 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).

The draw_mouse function is easy enough to modify to allow for multi-sprite cursors or to remove multiple cursor sprites altogether.

The widgets

The table you feed to init_mouse 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). If you have widgets with non-sprite dimensions, just edit the code which checks if the mouse is over a widget in the in_bounds_of function.

The elements you can add which add behavior are: (*needed to work properly)

  • x * (x coordinate of left corner in pixels)
  • y * (y coordinate of left corner in pixels)
  • w * (width in sprites)
  • h * (height in sprites)
  • clickable
    A boolean which, if true, makes the mouse change its sprite when hovering over the widget.
  • on_down
    A function which is called when the mouse clicks on the widget.
  • on_up
    A function which is called when the mouse releases after having clicked on the widget.
  • on_hover
    A function which is called every frame the mouse hovers over the widget.

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.

Do read over the code in the cart. The example shows of how to use the interface in case of any doubt.

Final notes

  • I do use the term "widget" very liberally here, yes.
  • 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.
  • 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.
  • I acknowledge that there must certainly be other implementations out there, definitely better than mine, but I was too lazy to find them.
  • Do criticize (constructively): I love to learn!
P#93499 2021-06-15 02:40 ( Edited 2021-06-15 21:10)

[Please log in to post a comment]