source: trunk/src/mouse_down.m @ 159

Last change on this file since 159 was 156, checked in by sommeria, 13 years ago

many bug repairs and corrections for mouse action
create_grid: option for black marjkers for grid detection

File size: 18.3 KB
Line 
1%'mouse_down': function activated when the mouse button is pressed on a figure (callback for 'WindowButtonDownFcn'
2%--------------------------------------------------------------
3% xy=mouse_down(hObject,eventdata)
4% activated by the command:
5% set(hObject,'WindowButtonDownFcn',{'mouse_down'}),
6% where hObject is the handle of the figure
7%
8%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
9%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
10%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
11%     This file is part of the toolbox UVMAT.
12%
13%     UVMAT is free software; you can redistribute it and/or modify
14%     it under the terms of the GNU General Public License as published by
15%     the Free Software Foundation; either version 2 of the License, or
16%     (at your option) any later version.
17%
18%     UVMAT is distributed in the hope that it will be useful,
19%     but WITHOUT ANY WARRANTY; without even the implied warranty of
20%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
22%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
23
24function xy=mouse_down(hObject,eventdata)
25
26%MouseAction='none'; %default
27huvmat=findobj(allchild(0),'tag','uvmat');%find the uvmat interface handle which controls theoption of  mouse action
28if isempty(huvmat)
29    return
30end
31hhuvmat=guidata(huvmat);%handles of elements in uvmat
32UvData=get(huvmat,'UserData');
33%MouseAction='none'; %default
34currentfig=hObject;
35hhcurrentfig=guidata(currentfig);
36test_zoom=get(hhcurrentfig.zoom,'Value');%test for zoom action, first priority
37% if isfield(UvData,'MouseAction')
38%     MouseAction=UvData.MouseAction;% get the mouse action from the uvmat GUI: options:
39% end
40
41%test_cal=get(handles.cal,'Value');
42% test_cal=strcmp(MouseAction,'calib');
43test_ruler=isequal(get(hhuvmat.MenuRuler,'checked'),'on');%test for ruler  action, second priority
44%test_ruler=strcmp(MouseAction,'ruler');
45%test_edit=strcmp(MouseAction,'edit_object');
46test_edit=get(hhuvmat.edit_object,'Value');%test for object editing, third priority
47%test_edit_vect=strcmp(MouseAction,'edit_vect');%test for vector editing,  priority 4
48test_edit_vect=get(hhuvmat.edit_vect,'Value');%test for vector editing,  priority 4
49test_create=isequal(get(hhuvmat.MenuObject,'checked'),'on');% test for object creation,  priority 5
50if test_create
51    hset_object=findobj(allchild(0),'tag','set_object');
52    test_create=~isempty(hset_object)&&~test_edit;
53end
54test_cal=isequal(get(hhuvmat.MenuCalib,'checked'),'on');% test for calibration
55if test_cal% test for calibration popints,  priority 6
56    h_calib=findobj(allchild(0),'tag','geometry_calib');
57    if isempty(h_calib)
58        test_cal=0;
59        set(hhuvmat.MenuCalib,'checked','off');% test for calibration off
60    else
61        hh_calib=guidata(h_calib);
62        test_cal=get(hh_calib.edit_append,'Value');
63    end
64end
65%test_create=~test_zoom && strcmp(MouseAction,'create_object')&&~test_edit && ~test_edit_vect ;% || isequal(MouseAction,'create_mask'));
66xdisplay=[];%default
67ydisplay=[];%default
68AxeData=[];%default
69
70%% edit an existing point or line if found
71hcurrentobject=gco;% current object handle (selected by the mouse)
72hcurrentfig=hObject;% current figure handle
73fig_tag=get(hcurrentfig,'Tag');
74tag_obj=get(gco,'Tag');
75xy=[];%default
76xy_fig=get(hcurrentfig,'CurrentPoint');% current point of the current figure (gcbo)
77hchild=get(hcurrentfig,'Children');%handles of all objects in the current figure
78haxes=[];
79
80%% loop on all the objects in the current figure (selected by the last mouse click)
81for ichild=1:length(hchild)
82    obj_pos=get(hchild(ichild),'Position');%position of the object
83    if xy_fig(1) >=obj_pos(1) & xy_fig(2) >= obj_pos(2)& xy_fig(1) <=obj_pos(1)+obj_pos(3) & xy_fig(2) <= obj_pos(2)+obj_pos(4);
84        htype=get(hchild(ichild),'Type');%type of object child of the current figure
85        %if the mouse is over an axis, look at the data
86        if isequal(htype,'axes')
87            y_lim=get(hchild(ichild),'YLim');
88            x_lim=get(hchild(ichild),'XLim');
89            haxes=hchild(ichild);
90            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
91            if xy(1,1)>x_lim(1) && xy(1,1)<x_lim(2) && xy(1,2)>y_lim(1) && xy(1,2)<y_lim(2)
92                AxeData=get(haxes,'UserData');% data attached to the axis
93                AxeData.CurrentOrigin=[xy(1,1) xy(1,2)];% The current point set by the mouse becomes the current origin
94                if test_edit_vect && ~isequal(tag_obj,'proj_object') & ~test_create
95                    ivec=[];
96                    FigData=get(hcurrentfig,'UserData');
97                    tagaxes=get(haxes,'tag');
98                    if isfield(FigData,tagaxes)
99                        eval(['Field=FigData.' tagaxes ';'])
100                        [CellVarIndex,NbDim,VarType]=find_field_indices(Field);%analyse the physical fields contained in Field
101                        for icell=1:numel(CellVarIndex)%look for all physical fields
102                            if NbDim(icell)==2 % select 2D field
103                                if  isfield(Field,'Mesh') && ~isempty(Field.Mesh)&& ~isempty(VarType{icell}.coord_x) && ~isempty(VarType{icell}.coord_y)%case of unstructured data
104                                    eval(['X=Field.' Field.ListVarName{VarType{icell}.coord_x} ';'])
105                                    eval(['Y=Field.' Field.ListVarName{VarType{icell}.coord_y} ';'])
106                                    flag_vec=(X<(xy(1,1)+Field.Mesh/4) & X>(xy(1,1)-Field.Mesh/4)) & ...%flagx=1 for the vectors with x position selected by the mouse
107                                        (Y<(xy(1,2)+Field.Mesh/4) & Y>(xy(1,2)-Field.Mesh/4));%f
108                                    ivec=find(flag_vec,1);% search the (first) selected vector index ivec
109                                end
110                            end
111                        end
112                    end
113                end
114            else
115                haxes=[];%mouse out of axes
116            end
117        end
118    end
119end
120test2D=0;
121if isfield(AxeData,'NbDim')
122    if isequal(AxeData.NbDim,2)
123        test2D=1;
124    end
125end
126if ~test2D     %desable  object creation and vector editing if NbDim different from 2
127    test_create=0;
128    test_edit_vect=0;
129end
130%delete the current zoom rectangle
131if isfield(AxeData,'CurrentRectZoom') & ishandle(AxeData.CurrentRectZoom)
132    delete(AxeData.CurrentRectZoom)
133    AxeData.CurrentRectZoom=[];
134end   
135
136%% zoom has first priority
137if test_zoom %&& ~test_create && ~test_edit && ~test_edit_vect && exist('xy','var')
138     AxeData.Drawing='zoom'; %initiate drawing mode
139     AxeData.CurrentObject=[];%unselect objects
140     set(haxes,'UserData',AxeData);
141     return
142end
143if isempty(huvmat)%further options require the uvmat GUI
144    return
145end
146
147%% ruler has second priority
148if test_ruler
149    AxeData.RulerCoord(1,1)=xy(1,1);
150    AxeData.RulerCoord(1,2)=xy(1,2);
151    AxeData.RulerHandle=line([xy(1,1) xy(1,1)],[xy(1,2) xy(1,2)],'Color','m','Tag','ruler');
152    AxeData.Drawing='ruler';
153    set(haxes,'UserData',AxeData);
154    return
155end
156
157%% selection of an existing projection object (third priority)
158if  test_edit && (isequal(tag_obj,'proj_object')||isequal(tag_obj,'DeformPoint'))
159    if ~(isfield(AxeData,'Drawing') && isequal(AxeData.Drawing,'create'))
160        userdata=get(hcurrentobject,'UserData');
161        if ishandle(userdata)%the selected line depends on a parent line
162            AxeData.CurrentObject=userdata;% the parent object becomes the current one
163        else
164            AxeData.CurrentObject=hcurrentobject;% the selected object becomes the current one
165        end
166        ObjectData=get(AxeData.CurrentObject,'UserData');
167        if test_edit && isfield(ObjectData,'IndexObj')
168            hother=findobj('Tag','proj_object','Type','line');%find all the proj objects
169            set(hother,'Color','b');%reset all the proj objects in 'blue' by default
170            set(hother,'Selected','off')
171            hother=findobj('Tag','proj_object','Type','rectangle');
172            set(hother,'EdgeColor','b');
173            set(hother,'Selected','off');
174            hother=findobj('Tag','proj_object','Type','image');
175            for iobj=1:length(hother)
176                   Acolor=get(hother(iobj),'CData');
177                   Acolor(:,:,1)=zeros(size(Acolor,1),size(Acolor,2));
178                   set(hother(iobj),'CData',Acolor);
179            end
180            hother=findobj('Tag','DeformPoint');
181            set(hother,'Color','b');
182            set(hother,'Selected','off')   
183            if isequal(get(AxeData.CurrentObject,'Type'),'line')
184                set(AxeData.CurrentObject,'Color','m'); %set the selected object to magenta color
185            elseif isequal(get(AxeData.CurrentObject,'Type'),'rectangle')
186                 set(AxeData.CurrentObject,'EdgeColor','m'); %set the selected object to magenta color
187            end
188            if isfield(ObjectData,'SubObject')& ishandle(ObjectData.SubObject)
189                for iobj=1:length(ObjectData.SubObject)
190                    hsub=ObjectData.SubObject(iobj);
191                    if isequal(get(hsub,'Type'),'rectangle')
192                        set(hsub,'EdgeColor','m'); %set the selected object to magenta color
193                    elseif isequal(get(hsub,'Type'),'image')
194                       Acolor=get(hsub,'CData');
195                       Acolor(:,:,1)=Acolor(:,:,3);
196                       set(hsub,'CData',Acolor);
197                    else
198                        set(hsub,'Color','m')
199                    end
200                end
201            end
202            if isequal(tag_obj,'DeformPoint')
203                 set(hcurrentobject,'Color','m'); %set the selected DeformPoint to magenta color
204            end
205            IndexObj=ObjectData.IndexObj;
206                    %indicate on the list of the GUI uvmat which object has been selected
207            if strcmp(get(hcurrentfig,'tag'),'uvmat') %if the uvmat graph has been selected, object projection is on the other frame view_field
208                set(hhuvmat.list_object_2,'Value',IndexObj);
209                list_str=get(hhuvmat.list_object_2,'String');
210                UvData.Object{IndexObj}.Name=list_str{IndexObj};
211            else
212                set(hhuvmat.list_object_1,'Value',IndexObj);
213                list_str=get(hhuvmat.list_object_1,'String');
214                UvData.Object{IndexObj}.Name=list_str{IndexObj};
215            end
216            h_set_object=findobj(allchild(0),'Tag','set_object');
217            if ~isempty(h_set_object)
218                delete(h_set_object)
219            end
220            set_object(UvData.Object{IndexObj})
221            axes(haxes);%set back the current axes haxes
222            testdeform=0;
223            set(gcbo,'Pointer','circle');
224            AxeData.Drawing='deform';
225            if isequal(tag_obj,'DeformPoint')       
226               if isfield(ObjectData,'DeformPoint')
227                   set(hcurrentobject,'Selected','on')
228                   for ipt=1:length(ObjectData.DeformPoint)
229                       if isequal(ObjectData.DeformPoint(ipt),hcurrentobject)
230                            AxeData.CurrentIndex=ipt;
231                            testdeform=1;
232                       end
233                   end
234               end
235            end
236            if testdeform==0
237                AxeData.Drawing='translate';
238                set(AxeData.CurrentObject,'Selected','on')
239                set(gcbo,'Pointer','fleur');
240            end
241        end
242    end
243end
244
245%%  create new projection  object
246if  test_create && ~isempty(xy) && ~(isfield(AxeData,'Drawing')&& isequal(AxeData.Drawing,'create'))
247        hset_object=findobj(allchild(0),'tag','set_object');
248        if ~isempty(hset_object)
249            sethandles=guidata(hset_object);
250            ObjectData=read_set_object(sethandles);
251            ObjectData.Coord=[]; %reset previous object coordinates
252            ObjectData.Coord(1,1)=xy(1,1);
253            ObjectData.Coord(1,2)=xy(1,2);
254            ObjectData.Coord(1,3)=0;
255            if isfield(AxeData,'ObjectCoord') & size(AxeData.ObjectCoord,2)==3
256                 ObjectData.Coord(1,3)=AxeData.ObjectCoord(1,3); %generaliser au cas avec angle
257            end
258            AxeData.CurrentObject=plot_object(ObjectData,[],haxes,'m');%draw the object and its handle becomes AxeData.CurrentObject
259            if isfield(UvData,'Object')
260                IndexObj=length(UvData.Object)+1;% add the object as index IndexObj on the list of the interface
261            else
262                IndexObj=2;
263            end 
264            UvData.Object{IndexObj}=ObjectData;       
265            list_str=get(hhuvmat.list_object_1,'String');
266            object_name=get(sethandles.TITLE,'String');
267            if isempty(object_name)|| strcmp(object_name,'')
268                list_str{IndexObj}=[num2str(IndexObj) '-' ObjectData.Style];
269                set(sethandles.TITLE,'String',list_str{IndexObj})
270            else
271               list_str{IndexObj}=object_name;
272            end
273            set(hhuvmat.list_object_1,'String',list_str)
274            list_str{end+1}='...';
275            set(hhuvmat.list_object_2,'String',list_str)
276            if strcmp(fig_tag,'view_field')%we are in view_field plot
277                  set(hhuvmat.list_object_1,'Value',IndexObj)% the projection field will be plotted in uvmat frame
278                  UvData.Object{IndexObj}.DisplayHandle_uvmat=[];
279                  UvData.Object{IndexObj}.DisplayHandle_view_field=AxeData.CurrentObject;       
280            else%we are in uvmat plot
281                set(hhuvmat.list_object_2,'Value',IndexObj)
282                UvData.Object{IndexObj}.DisplayHandle_uvmat=AxeData.CurrentObject;
283                UvData.Object{IndexObj}.DisplayHandle_view_field=[];
284            end
285            set(huvmat,'UserData',UvData)
286            PlotData=get(AxeData.CurrentObject,'UserData');
287            PlotData.IndexObj=IndexObj;
288            set(AxeData.CurrentObject,'UserData',PlotData); %record the object index in the graph
289            AxeData.Drawing='create';
290        end
291end
292
293% create calibration points if the GUI geometry_calib is opened, if the main axes axes3 of uvmat has ben selected
294if ~test_zoom && test_cal && ~isempty(haxes) && strcmp(get(haxes,'tag'),'axes3')
295    h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
296    hh_geometry_calib=guidata(h_geometry_calib);
297    h_edit_append=hh_geometry_calib.edit_append;%findobj(h_geometry_calib,'Tag','edit_append');
298    if isequal(get(h_edit_append,'Value'),1) && ~isempty(haxes)
299        h_ListCoord=hh_geometry_calib.ListCoord; %findobj(h_geometry_calib,'Tag','ListCoord');
300        coord_value=get(hhuvmat.transform_fct,'Value');% set uvmat to pixel coordinates, run it again if not
301        if ~(isequal(coord_value,1)||isequal(coord_value,3)); %active only with no transform or px (no phys)
302            set(hhuvmat.transform_fct,'Value',1)
303            uvmat('transform_fct_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat
304            set(hhuvmat.FixedLimits,'Value',0)% put FixedLimits option to 'off'
305            set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
306            return
307        end
308        Coord=get(h_ListCoord,'String');
309        data=read_geometry_calib(Coord);%transform char cell to numbers
310        xlim=get(haxes,'XLim');
311        ind_range_x=abs((xlim(2)-xlim(1))/50);
312        ylim=get(haxes,'YLim');
313        ind_range_y=abs((ylim(2)-ylim(1))/50);
314        ind_range=sqrt(ind_range_x*ind_range_y);
315        test_newpoint=1;
316        if size(data.Coord,2)>=5 %if calibration points already exist
317            XCoord=(data.Coord(:,4));
318            YCoord=(data.Coord(:,5));
319            index_point=find((XCoord<xy(1,1)+ind_range) & (XCoord>xy(1,1)-ind_range) & ...%flagx=1 for the vectors with x position selected by the mouse
320                          (YCoord<xy(1,2)+ind_range) & (YCoord>xy(1,2)-ind_range),1);%find the first calibration point in the neighborhood of the mouse
321            test_newpoint=isempty(index_point);%test for no existing calibration point near the mouse position
322        end
323        val=get(h_ListCoord,'Value');
324        %create a new calib point if we are not close to an existing one
325        if test_newpoint                 
326             strline=[ '    |    '  '    |    '  '    |    ' num2str(xy(1,1),4) '    |    ' num2str(xy(1,2),4)];
327           
328             if length(Coord)>=val
329                 Coord(val+1:length(Coord)+1)=Coord(val:length(Coord));% push the list forward beyond the current point
330             end
331             Coord{val}=strline;
332             set(h_ListCoord,'String',Coord)
333             data=read_geometry_calib(Coord);%transform char cell to numbers
334             XCoord=data.Coord(:,4);
335             YCoord=data.Coord(:,5);
336        end
337        hh=findobj('Tag','calib_points');%look for handle of calibration points           
338        if isempty(hh)
339            hh=line(XCoord,YCoord,'Color','m','Tag','calib_points','LineStyle','.','Marker','+');
340        else
341            set(hh,'XData',XCoord)
342            set(hh,'YData',YCoord)
343        end
344        set(hh,'UserData',val)% flag the points to edit mode
345        hhh=findobj('Tag','calib_marker');%look for handle of point marker (circle)
346        if ~isempty(hhh)
347            set(hhh,'Position',[xy(1,1)-ind_range/2 xy(1,2)-ind_range/2 ind_range ind_range])
348        else
349            rectangle('Curvature',[1 1],...
350                  'Position',[xy(1,1)-ind_range/2 xy(1,2)-ind_range/2 ind_range ind_range],'EdgeColor','m',...
351                  'LineStyle','-','Tag','calib_marker');
352           % line([xy(1,1) xy(1,1)],[xy(1,2) xy(1,2)],'Color','m','Tag','calib_marker','LineStyle','.','Marker','o','MarkerSize',ind_range);
353        end
354        AxeData.Drawing='calibration';
355    end
356end
357
358% edit vectors
359if test_edit_vect && ~isempty(ivec)
360    %create the error flag FF if it does not exist
361    if ~isfield(Field,'FF')
362        Field.ListVarName=[Field.ListVarName 'FF'];
363        Field.VarDimName=[Field.VarDimName Field.VarDimName{VarType{icell}.coord_x}];
364        nbvar=length(Field.ListVarName);
365        Field.VarAttribute{nbvar}.Role='errorflag';
366        Field.FF=zeros(size(Field.X));
367    end
368    if isequal(Field.FF(ivec),0)
369        Field.FF(ivec)=100 %mark vector #ivec as false
370    else
371        Field.FF(ivec)=0;
372    end
373    PlotParam=read_plot_param(hhcurrentfig);
374    plot_field(Field,haxes,PlotParam);
375    eval(['FigData.' tagaxes '=Field;'])%record the modified field in FigData
376    set(hcurrentfig,'UserData',FigData);
377end   
378set(haxes,'UserData',AxeData);
379
Note: See TracBrowser for help on using the repository browser.