Edit 3: As a third and (hopefully) final edit, thanks to Lukas Lang's answer, I was able to solve the original question. Now I just need to define several toggler-type meshes of the same shape. One naive attempt is simply

Which naturally doesn't yield meshes with the same shape, due to the randomness in defining the points. How can I solve this? I have tried to define the mesh outside, then I lose the dynamic update of the mesh-dependent control. I would like something like the following, where I'm able to independently update similarly shaped meshes

$\begingroup$This has such a control. It seems to be written in V8, so no mesh. The control does several things, so it's more complicated than you probably need. It uses ClickPane to handle the mouse clicks and toggling.$\endgroup$
– Michael E2Jan 10 at 15:32

$\begingroup$Apparently I also wrote this, which someone just upvoted (thanks!). You could use it to un/highlight the cell clicked I suppose.$\endgroup$
– Michael E2Jan 10 at 15:37

$\begingroup$It's somewhat frowned upon to keep updating an old Q&A with new questions, especially there is already an accepted answer. -- Anyway, all you have to do is pass the same mesh to MeshTogglerBar's first argument.$\endgroup$
– Michael E2Jan 15 at 23:06

1

$\begingroup$I'm not sure exactly what you want, but you could put mesh=VoronoiMesh@RandomReal[{0, 1}, {n, 2}] in the TrackingFunction for n. If you want the same mesh every time you come back to the same n, then you'll need to do something else, like define mesh[n_] := mesh[n] = VoronoiMesh@RandomReal[{0, 1}, {n, 2}] or use SeedRandom[] or meshTogglerBar[VoronoiMesh@Take[myBigListOfPoints, n]] and so forth.$\endgroup$
– Michael E2Jan 16 at 0:39

3 Answers
3

Here are implementations for a MeshTogglerBar and MeshSetterBar based on my answer here (code below). Both implementations use Mouseover and EventHandler to handle detection of the polygon below the cursor for you. Compared to the NearestFunction approach, this is far more performant (since it is done by the front-end), it also works nicely for other types of meshes, where the cell below the cursor is not necessarily the one with the closest center.

Notes

Some notes on the implementation (you can find some more in my answer linked above):

Since everything is handled by the front-end, these controls will have excellent performance

For the MeshTogglerBar, we have to generate a list of state variables (one per cell). This is because the front-end cannot manipulate lists, so each cell needs a separate variable

The default values of the state variables are set in the Initialization property of the DynamicModule to ensure that the values are not prematurely inserted anywhere.

The dynamic styling is done via TagBoxOptions -> {BaseStyle -> {...}}. This is done since we need to set the styles via an option for the front-end-only solution to work. The Annotation[...]/TagBoxOptions trick is to ensure that any type of primitive is styled, not only Polygons.

The controlled variables are kept separate from the DynamicModule variables used to store the state of the control. This ensures that the front-end ↔ kernel communication is kept to a minimum (i.e. only when a click has happened is the kernel variable updated).

For the MeshTogglerBar, we trigger on both "MouseEntered" and "MouseDown" to enable dragging over many elements to toggle them. The state of the first element is stored in dragAction, to ensure that dragging sets all elements to the same state instead of toggling them back and forth

The iMeshTogglerBar/iMeshSetterBar functions are there so the control can be easily used inside Manipulate:

Manipulate[
x,
{{x, 3}, MeshSetterBar[mesh]}
]

Similarly, the Dynamic[MeshSetterBar[_]]/Dynamic[MeshTogglerBar[_]] type definitions are to ensure that the controls work inside of Manipulate when the controls depend on other variables:

The additional definition is necessary, since Manipulate wraps control specifications in Dynamic if any other manipulate variables occur in the specifications. This prevents Manipulator from seeing the Function expression, since it is not evaluated. The additional upvalue forces evaluation into something with an explicit Function in those cases.

$\begingroup$Thank you! I just have one question: would it be possible to hold the mouse and select/unselect several cells just by dragging the mouse over them?$\endgroup$
– sam wolfeJan 14 at 10:56

$\begingroup$@samwolfe see the updated code - now you can hold the mouse button down and drag over other elements to set them all to the same state$\endgroup$
– Lukas LangJan 14 at 12:22

$\begingroup$That is great! I'm just getting a message in the beginning: "MapThread: incompatible dimensions of objects at positions {2,1} and {2,2} of (...)". Once I drag the mouse over it, it's gone. Any idea how to fix this small issue?$\endgroup$
– sam wolfeJan 14 at 12:27

1

$\begingroup$@samwolfe Sorry, I missed a line when updating the code - should be fixed now$\endgroup$
– Lukas LangJan 14 at 13:42

$\begingroup$Thank you. I've just added a new edit section, because Manipulate doesn't seem to be working properly if I vary the mesh size with another Control. Can you please take a quick look?$\endgroup$
– sam wolfeJan 14 at 16:09

$\begingroup$Nice! This more properly takes care of the "toggler" functionality.$\endgroup$
– MarcoBJan 10 at 16:43

$\begingroup$Note that the NearestFunction will return the wrong cell in some cases for non-voronoi-type meshes. Try for example to select one of the narrow cells in your DelaunyMesh example with the cursor close to the corner$\endgroup$
– Lukas LangJan 11 at 10:27

$\begingroup$@samwolfe It's an initialization problem when you first evaluate because selected has not yet been assigned any value. Just click any cell and it will work after that. Otherwise, add e.g. Initialization :> (selected = {1}) as an option to Dynamic.$\endgroup$
– MarcoBJan 10 at 16:39

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.