Check if a graphics object is visible to the user
23 views (last 30 days)
Justin Solomon on 18 Jun 2015
The short version: Is there an easy way to check if a given graphics object is visible on screen? (Note that its visible property will not necessarily tell me this since its parent could be invisible while the object itself is visible).
The long version: Imagine you have a GUI with tabs (each tab containing an axes) and you want to add a callback that is invoked when the user moves the mouse scroll wheel. The callback needs to act differently depending upon which axes the mouse is currently positioned over. However, if the axes are positioned in the same location on screen in the different tabs, then the mouse is actually positioned over all of them. Thus I need to know which object is actually visible to the user and have the callback act accordingly. Also, because I'm trying to make this callback as generalizable as possible, the callback itself is ambivalent to the whole structure of the GUI so I can't just check which tab is currently selected. (I'm doing it this way so that I can reuse this code for other projects where the GUI design may be different from this specific case).
Any ideas would be appreciated. I could probably go handle diving and recursively figure out if an object is visible on screen, but I'd rather not have to do that if there's a simpler (and faster) way. Thanks!
Alfonso Nieto-Castanon on 18 Jun 2015
While it is simple to check which object is visible, e.g.:
function ok = isvisible(handle)
ok = true;
ok = strcmp(get(handle,'visible'),'on');
handle = get(handle,'parent');
things become much more complicated when you have several objects that may be all visible over the same position of the screen and you need to figure out which one is displayed "on top". This depends on the specific Matlab version and nature of the objects, so it is rather complicated as far as I know to make it work generally.
An alternative approach would be to have your GUI explicitly indicate which objects should be considered "active" at any time, for example by setting the 'tag' or 'userdata' properties on the objects themselves, or by invoking some function which keeps/updates a list of currently active objects. The latter approach is typically more general, since specific GUIs might want to reuse these same object property to other ends, and perhaps more flexible as well (e.g. you may want to define an invisible axes that still captures the user actions over an arbitrary area). Depending on how fancy you want your code to get, you may want to define a class wrapping your graphic object handles which keeps track of this sort of things for you.
More Answers (1)
Walter Roberson on 18 Jun 2015