-
Notifications
You must be signed in to change notification settings - Fork 3
/
cursorUpdater.m
114 lines (102 loc) · 3.1 KB
/
cursorUpdater.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function fhndl = cursorUpdater(cursors)
if nargin<1 || isempty(cursors)
fhndl.update = @(~,~) 1; % dummy function that swallows arguments and is noop
fhndl.updateCursors = @(~,~) 1;
fhndl.reset = @(~,~) 1;
return;
end
usingPoly = isfield(cursors,'poly');
cursorPolys = [];
cursorRects = [];
nAOI = [];
nElemPerAOI = [];
cursorLooks = [];
cursorIdxs = [];
qCursorReset= [];
cursorReset = [];
updateCursors(cursors);
currCursor = nan;
fhndl.update = @update;
fhndl.updateCursors = @updateCursors;
fhndl.reset = @reset;
function update(x,y)
if usingPoly
idx = [];
% get in which poly, if any. If polys overlap, first in the
% list is used
for p=1:nAOI
if inPoly([x y],cursorPolys{p})
idx = p;
break;
end
end
else
if isempty(cursorRects)
idx = [];
else
% get in which rect, if any. If rects overlap, first in the list is
% used
idx = find(inRect([x y],cursorRects),1);
end
end
% get corresponding cursor
if isempty(idx)
curr = cursorLooks(end);
else
curr = cursorLooks(cursorIdxs(idx));
end
% see if need to change
if currCursor ~= curr
if curr==-1
HideCursor();
else
ShowCursor(curr);
end
currCursor = curr;
end
end
function updateCursors(cursors)
% process rects/polys
usingPoly = isfield(cursors,'poly');
if usingPoly
cursorPolys = cursors.poly;
nAOI = length(cursorPolys);
nElemPerAOI = ones(1,nAOI);
else
cursorRects = [cursors.rect{:}];
nAOI = length(cursors.rect);
nElemPerAOI = cellfun(@(x) size(x,2),cursors.rect);
end
% cursor looks are numbered IDs as eaten by ShowMouse. -1 means hide cursor
cursorLooks = [cursors.cursor cursors.other];
cursorIdxs = SmartVec(1:nAOI,nElemPerAOI,0);
% optional (default on) reset of cursor when calling reset(). Have it as an
% option as some function out of the reach of the user always call reset
% upon exit, user can here configure if it actually does something
qCursorReset = true;
if isfield(cursors,'qReset')
qCursorReset = cursors.qReset;
end
% if resetting, indicate what cursor to reset to. if empty, we reset to
% cursors.other.
cursorReset = [];
if isfield(cursors,'reset')
cursorReset = cursors.reset;
end
end
function reset()
if ~qCursorReset
return;
end
if ~isempty(cursorReset)
curr = cursorReset;
else
curr = cursorLooks(end);
end
if curr==-1
HideCursor();
else
ShowCursor(curr);
end
end
end