-
Notifications
You must be signed in to change notification settings - Fork 0
/
createButtonLabel.m
188 lines (179 loc) · 6.94 KB
/
createButtonLabel.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
function buttonIcon = createButtonLabel(string,varargin)
% createButtonLabel: creates a tightly cropped RGB image (button-label) of a simple text string
%
% This is very useful for labeling uicontrols (pushbuttons), with the syntax:
%
% set(hndl,'cdata',buttonIcon)
%
% All valid Parameter-Value pairs, INCLUDING TEXT ROTATION, are
% supported. Note that this function requires the Image Processing
% Toolbox, and that it triggers the creation of a temporary
% figure, which will be momentarily visible during
% button-label creation.
%
% Allows easy creation of multi-line text for labeling
% pushbuttons.
%
% SYNTAX: BUTTONICON = CREATEBUTTONLABEL(STRING,PVs,FIGOPT);
%
% USAGE: buttonIcon = createButtonLabel(string)
% Create a tightly cropped RGB image of a text object of the
% simple string indicated in input STRING. Uses default
% properties (fontname, fontsize, fontweight, fontcolor,
% bgcolor, rotation, etc.) for text object.
%
% buttonIcon = createButtonLabel(string,PVs)
% Provide additional Parameter-Value pairs, either in a cell
% array of strings, or in a structure of parameters. All PV
% pairs valid for text objects are supported.
%
% buttonIcon = createButtonLabel(string,PVs,figOpt)
% Provides optional argument for controlling the (requisite)
% temporary figure. By default, the temporary figure will be
% (visibly) created and destroyed within this function.
% However, if you will be calling CREATEBUTTONLABEL multiply (labeling
% tabs in a tabPanel, for instance), it might be less
% distracting to reuse the same figure, and to destroy the
% figure after the last call. This parameter will allow you to
% do so.
%
% ARGUMENTS:
%
% BUTTONICON: Output RGB image
%
% STRING: The string to be captured as a tightly-cropped RGB
% Image.
%
% PVs (OPTIONAL): Cell array or structure of any parameter-value pairs
% valid for TEXT object. (See 'Text Properties' for details.)
% Particularly useful: {'rotation',90} or {'rotation',270}.
%
% FIGOPT (OPTIONAL): Control parameter for temporary figure. Valid options:
% 1: REUSE/DESTROY (DEFAULT): reuse existing temporary figure, or create
% one if it doesn't exist. Destroy figure on completion.
% 2: REUSE existing temporary figure, or create one if it
% doesn't exist. Do not destroy figure.
%
% EXAMPLES:
%
% 1) Create cdata to label a vertically-oriented pushbutton, apply
% structure of text parameters, destroy figure.
%
% txt.rotation = 270;
% txt.color = 'r';
% txt.backgroundcolor = 'y';
% buttonIcon = createButtonLabel('PRESS',txt);
%
% 2) Create cdata, apply PV pairs, leave figure open for re-use.
%
% buttonIcon = createButtonLabel('PUSH',{'fontname','Helvetica','fontweight','bold'},2);
%
% 3) Multi-line text on a pushbutton:
% buttonIcon = createButtonLabel({'PUSH', 'ME!'},...
% {'fontname','Helvetica','fontweight',...
% 'bold','horizontalAlignment','center',...
% 'color','r'});
%
% SO: Why is this useful? Try this (and then try it without createButtonLabel!):
%
% 4)
% x = createButtonLabel('PRESS',...
% {'rotation',270,'fontsize',18,'fontweight','b',...
% 'color','c','backgroundcolor','y'});
% figure;uicontrol('style','pushbutton','units','pixels',...
% 'pos',[10 10 60 100],'callback','disp(''I was pushed!'')',...
% 'backgroundcolor','y','cdata',x);
% Created by Brett Shoelson
% brett.shoelson@mathworks.com
% 05/14/07
%
% Modifications:
% 02/29/08 Force figure into 'normal' (not docked) state
% 06/22/09 Modified at the suggestion of Dave Forstot to allow
% the creation of multi-line text buttons. (Thanks, Dave.)
% 08/01/2014 Windowstyle=normal
% 08/07/2014 Re-written for R2014b compliance.
% 10/03/2014 Removed 'range' function to eliminate Stats Toolbox dependency
%
% Copyright 2010-2013 The Mathworks Inc
% Validate and parse inputs
string = parseValidateInputs(string,varargin{:});
% Depending on figOpt parameter, branch to use/reuse/destroy temporary button-label figure
ExistFig = findobj('tag','tmpcreateButtonLabelfig');
if ~isempty(ExistFig)
figure(ExistFig);
clf;
else
figure('windowstyle','normal',...
'units','pixels',...
'position',[100 100 500 500],...
'menubar','none',...
'tag','tmpcreateButtonLabelfig');
end
ax = axes('units','pixels','position',[0 0 500 500],...
'visible','on','xtick',[],'ytick',[],'color','w');
tmp = text(250,250,string,'units','pixels','fontunits','pixels');
buffer = 2;
% Apply any input Parameter-Value pairs
applyPVs(tmp,PVs);
% Temporarily change fontcolor to black for detection, then return it to
% intended color
currColor = get(tmp,'color');
set(tmp,'color','k');
% Create button-label
drawnow
F = getframe(ax,[10 10 480 480]);
im = im2double(F.cdata(:,:,1));
im = im < 1;
[y,x] = find(im);
minmaxXY = [min(x(:)),max(x(:)),min(y(:)),max(y(:))];
bb = [minmaxXY(1)-buffer minmaxXY(3)-buffer (max(x)-min(x))+2*buffer (max(y)-min(y))+2*buffer];
set(tmp,'color',currColor);
set(ax,'color',get(tmp,'backgroundcolor'));
%axes(ax)
buttonIcon = getframe(ax,[10 10 480 480]);
buttonIcon = im2double(buttonIcon.cdata);
buttonIcon = imcrop(buttonIcon,bb);
%
if figOpt == 1
delete(findobj('tag','tmpcreateButtonLabelfig'));
end
% NESTED FUNCTIONS
function string = parseValidateInputs(string,varargin)
error(nargchk(1,3,nargin))
error(nargoutchk(0,1,nargout))
if ~(ischar(string) || iscellstr(string))
try
string = num2str(string);
catch
error('CREATEBUTTONLABEL: Requires at a minimum a single input, which must be a string.')
end
end
% Defaults
figOpt = 1;
PVs = [];
if ~isempty(varargin)
if ~isempty(varargin{1})
PVs = varargin{1};
end
if nargin == 3
figOpt = varargin{2};
end
if ~ismember(figOpt,1:2)
error('CREATEBUTTONLABEL: Invalid option for figOpt parameter.')
end
end
end
function applyPVs(obj,pvarray)
if isempty(pvarray)
return;
end
if isstruct(pvarray)
set(obj,pvarray);
else %Cell
for ii = 1:2:numel(pvarray)
set(obj,pvarray{ii},pvarray{ii+1});
end
end
end
end