I am working on something where I need to know which locator out of many is currently being changed.
(* edit, I cross posted here: https://mathematica.stackexchange.com/questions/156022/ subsequent edit, there are two nice answers posted at this URL. *)
Here is some working code that uses the second argument to Dynamic to identify which locator is active for a limited set of Locators. I'd like to extend this to many Locators. In this code, the Locators are hard-coded. I'd like to extend this programmatically so I can slot in the Locators and the Manipulator controls. I've tried various combinations of Hold* without really knowing what I am doing.
Does anyone know how I can achieve my goal of finding which is the active locator?
See some additional comments below the working code about an attempt to do this with LocatorPane.
DynamicModule[
{active = 0, p},
Manipulate[
Column[
{active,
Deploy@Graphics[
{
{
Locator@
Dynamic[p[1, 1,
1], {((p[1, 1, 1] = {-1, Last[#]};
active = {1, 1, 1}) &), ((active = 0) &)}],
Locator@
Dynamic[p[1, 1,
2], {((p[1, 1, 2] = #;
active = {1, 1, 2}) &), ((active = 0) &)}],
Locator@
Dynamic[p[1, 1,
3], {((p[1, 1, 3] = {1, Last[#]};
active = {1, 1, 3}) &), (active = 0) &}]
},
Line[{p[1, 1, 1], p[1, 1, 2], p[1, 1, 3]}]
},
PlotRange -> 2
]
}
],
{{p[1, 1, 1], {-1, -1}}, ControlType -> None},
{{p[1, 1, 2], {0, 0}}, ControlType -> None},
{{p[1, 1, 3], {1, 1}}, ControlType -> None}
]
]
Trying to do the same thing with a LocatorPane and Nearest. It is not clear to me that the 2 argument form of Dynamic is working here:
DynamicModule[{active = False, pt1 = {1, 1}/2, pt2 = {-1, 1}/2,
pt3 = {1, -1}/2},
Row[
{active,
LocatorPane[
{
Dynamic[
pt1, {((active = Nearest[pt1, #, 1][[1]]);
pt1 = {0, 1} #) &, ((active = 0) &)}],
Dynamic[
pt2, {((active = Nearest[pt2, #, 1][[1]]);
pt2 = #) &, ((active = 0) &)}]
},
Graphics[{Yellow, Disk[{0, 0}, 2]},
PlotRange -> 2
]
]
}
]
]