I've created an application with the User Interface being a Graphic with lines, polygons etc. The functionality I need is:
- to be able to place the objects anywhere in {x,y} coords of the Graphic.
- to change the size of the object depending on how its used.
- to click each object and trigger color changes and execution of various functions.
The application works fine when there are only a few (say 20 objects) but noticeably slows as the number of objects increase. Any ideas why, and can it be made more responsive?
Note in the code below that the Print[] statement at line 91 inside the DynamicModule is currently commented out, and performance is still poor, when its made active performance gets a lot worse .
function1[p_, scale_, state_] :=
Module[
{body, ghost},
{ax, by} = scale {1, 0.7};
body = Polygon[
{
{-ax, -by} + p, {-ax, by} + p, {ax, -by} +
p, {ax, by} + p
}];
ghost = Rectangle[{-ax, -by} + p, {ax, by} + p];
{
FaceForm[state],
EdgeForm[Black],
body,
EdgeForm[None],
Opacity[0.],
ghost,
FaceForm[None]
}
]
function2[p_, scale_, state_] :=
Module[
{body, plug, ghost},
{ax, by} = scale {1, 0.7};
body = Polygon[
{
{-ax, -by} + p, {-ax, by} + p, {ax, -by} +
p, {ax, by} + p
}];
plug = Polygon[
{
{0, -by/2} + p, {-0.4, 0} + p, {0., by/2} +
p, {0.4, 0} + p, {0., -by/2} + p
}];
ghost = Rectangle[{-ax, -by} + p, {ax, by} + p];
{
FaceForm[state],
EdgeForm[Black],
body,
plug,
EdgeForm[None],
Opacity[0.],
ghost,
FaceForm[None]
}
]
function3[p_, scale_] := Module[
{actuator, stem},
{ax, by} = scale {0.75, 0.75};
actuator =
{
FaceForm[None], EdgeForm[Black],
Polygon[
{
{-ax, 1.5 by} + p, {ax, 1.5 by} + p, {ax, 3. by} + p, {-ax, 3. by} + p
}],
Line[{{-ax, 2.25 by} + p, {ax, 2.25 by} + p}]
};
stem = Line[{p, {0, 2.25 by} + p}];
{actuator, stem}
]
function13[p_, scale_, rotation_, state_: White] :=
Rotate[
Flatten@{function3[p, scale], function1[p, scale, state]},
rotation Degree, p
]
function23[p_, scale_, rotation_, state_: White] :=
Rotate[
Flatten@{function3[p, scale], function2[p, scale, state]},
rotation Degree, p
]
myCol = Black;
list = Table[Null, {i, 1, 100}];
objectDynamic[valveType_, p_, scale_, orientation_ , ID_Integer] :=
DynamicModule[
{myCol = Black},
EventHandler[
Dynamic@valveType[p, scale, orientation, myCol],
{
"MouseDown" :>
(
myCol =
myCol /. {Black -> Green, Green -> Red, Red -> Black};
list[[ID]] = myCol; (*Print[p]*)
)
},
Method -> "Queued", PassEventsDown -> False, PassEventsUp -> False
]
]
tableOfObjects =
Table[
{
objectDynamic[function13, {5 i , 5 j}, 1., 0. , 1 ],
objectDynamic[function23, {5 i , 5 j + 10}, 1., 0. , 1 ]}, {i, 0, 9}, {j, 0, 4}];
CreateDocument[
Dynamic[
Graphics[
Flatten@{
Line[{{0, -5}, {0,0}, {4,0}, {4,4}, {8,4}, {12,4}, {12,0}}],
tableOfObjects
},
Frame -> True, ImageSize -> {700, 500}],
SynchronousUpdating -> False
],
WindowSize -> {1600, 1000}
];
PassEventsDown
andPassEventsUp
toFalse
? $\endgroup$Graphics[ DynamicModule[{col = 1}, EventHandler[{Dynamic@GrayLevel[col], Disk[#, 1/2]}, {"MouseClicked" :> (col = 1 - col)}] ] & /@ Tuples[Range[10], 2], Background -> Gray ]
. It is important to see the details of your code. $\endgroup$Dynamic[valveType[p, scale, orientation, myCol], TrackedSymbols :> {myCol}]
. $\endgroup$