3
$\begingroup$

I have some locators in a LocatorPane and want to let users remove them. Unfortunately, it seems doing this conflicts with the Appearance option. Here's an example of the issue:

DynamicModule[{pts = {{-1, 1}/2, {1, 1}/2}, lbls = {"A", "B"}},
    Column @ {
         Dynamic @ pts, 
         LocatorPane[Dynamic @ pts, Framed @ Graphics @ {}, 
             LocatorAutoCreate -> {0, Length[pts]}, 
             Appearance -> lbls
         ]
     }
]

enter image description here

As you can see if hold the alt key (CMD on MacOS) and click on locator A, the label of the other locator B is incorrectly changed to A. Any simple workarounds to this?

Analysis:

This is the form of LocatorPane we're using:

enter image description here

According to the docs, the option LocatorAutoCreate allows deletion of locators by CMD-clicking on them.

![![enter image description here

I think that the proper index is deleted in first arg Dynamic@pts but the corresponding list of labels in the Appearance option is not (this I believe is the bug):

enter image description here

I'm not attached to using LocatorPane, perhaps EventHandler + Locator's is a way to go?

$\endgroup$
6
  • 1
    $\begingroup$ Can everyone reproduce the above behavior? I’m on Mac 10.15.7 v12.1 $\endgroup$
    – M.R.
    Sep 30, 2020 at 0:11
  • $\begingroup$ This is normal behavior. Appearance is just a list used to display locators in the order they appear in pts. If the first locator is removed, the point disappears from pts, which will now contain only one point, to be associated with the first point in lbls. $\endgroup$ Sep 30, 2020 at 2:14
  • $\begingroup$ If it’s not a bug it’s bad design, can you please think of a solution for my use case $\endgroup$
    – M.R.
    Sep 30, 2020 at 2:15
  • $\begingroup$ @M.R. On Windows 10 version 12.1 this code calculates some expression so that we see on a screen different numbers like {{-0.515,0.43},{0.55,0.45}} not {{-1/2,1/2},{1/2,1/2}}. What actually do you try to reproduce with this code? $\endgroup$ Oct 1, 2020 at 19:21
  • $\begingroup$ When you cmd click on A, B turns into A, I want to avoid that $\endgroup$
    – M.R.
    Oct 1, 2020 at 19:31

1 Answer 1

5
+125
$\begingroup$

Does it fit your needs:

DynamicModule[{
  nodes = Transpose@{{{-1, 1}/2, {1, 1}/2}, {"A", "B"}}, length = 2
  }, Deploy@Column@{Dynamic@nodes[[All, 1]],
    Graphics[
     Dynamic[
      Table[myLocator[Dynamic @ nodes, i], {i, length}], 
      TrackedSymbols :> {length}
      ]
     , Frame -> True, PlotRange -> 1
     ]},
 Initialization :> (
   myLocator[Dynamic@nodes_, i_] := EventHandler[
     Inset[nodes[[i, 2]], Dynamic@nodes[[i, 1]]],
     {"MouseClicked" :> If[
        CurrentValue["AltKey"],
        nodes = Delete[nodes, i]; length = Length@nodes
        ]
      ,
      "MouseDragged" :> (
        nodes[[i, 1]] = CurrentValue[{"MousePosition", "Graphics"}]
        )
      },
     PassEventsDown -> True
     ]
   
   )]

Could probably be shorter with Locator instead of MouseDragged etc but I encountered a bug that prevented me from using Locator.

$\endgroup$
2
  • $\begingroup$ Nice, but this pinks for me in wolfram cloud $\endgroup$
    – M.R.
    Oct 2, 2020 at 1:33
  • 1
    $\begingroup$ @M.R. ah, that's important piece, we can only count on LocatorPane there. I will try to look at it but I can't promise any timeframe. $\endgroup$
    – Kuba
    Oct 2, 2020 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.