function varargout = plotFig(varargin)
% UTMFIG Application M-file for plotFig.fig
%    FIG = UTMFIG launch plotFig GUI.
%    UTMFIG('callback_name', ...) invoke the named callback.

% Last Modified by GUIDE v2.0 23-Feb-2001 13:12:29
	
if nargin == 0  % LAUNCH GUI

	fig = openfig(mfilename,'new');
	set(fig,'toolbar','figure')

	% Use system color scheme for figure:
	set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));

	% Generate a structure of handles to pass to callbacks, and store it. 
	handles = guihandles(fig);
	guidata(fig, handles);

	if nargout > 0
		varargout{1} = fig;
	end
	
	% Add icons
	set(handles.sonar_button,'CData',double(imread('sonar.bmp'))/255)
	set(handles.auv_button,'CData',double(imread('auv.bmp'))/255)
	set(handles.measure_button,'CData',double(imread('label.bmp'))/255)
	set(handles.addline_button,'CData',double(imread('line.bmp'))/255)
	set(handles.move_button,'CData',double(imread('move.bmp'))/255)		
	set(handles.saveas_button,'CData',double(imread('saveas.bmp'))/255)
	set(handles.mission_button,'CData',double(imread('mission.bmp'))/255)
	
elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK

	try
		[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
	catch, errorbox, end

end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = measure_button_Callback(h, eventdata, handles, varargin)
[dist,az] = measure;
set(handles.distance_text,'String',[sprintf('%.2f',dist) ' m'])
set(handles.azimuth_text,'String',[sprintf('%.2f',az) ' deg'])


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = move_button_Callback(h, eventdata, handles, varargin)
movePoint('MyTag','utm')
	

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = figure_window_ResizeFcn(h, eventdata, handles, varargin)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = button_MotionFcn(h, eventdata, handles, varargin)
mouseUtmTip(handles)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = createPlot_Callback(h, eventdata, handles, varargin)
	global origin
origin = [];
h = utmFig;
if isempty(handles), handles = guidata(h); end
plot_Callback(h, eventdata, handles, varargin)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = plot_Callback(h, eventdata, handles, varargin)
	global mission
try, 
	set(gcbf,'Pointer','watch')
	cla
	if get(handles.chart_checkbox,'Value'), viewMap('utm'), end
	if get(handles.nav_box,'Value'), navQuiver('navigation_log','utm'); end
	if get(handles.renav_box,'Value'), navQuiver('renavigation_log','utm'); end
	if ~isempty(mission), plotMission(mission,'utm',handles), end
	delete(colorbar)
catch, errorbox, end
set(gcbf,'Pointer','arrow')


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = plotNav_Callback(h, eventdata, handles, varargin)
	global mission
try,
	set(gcbf,'Pointer','watch')
	cla
	if get(handles.nav_box,'Value'),
		x = get(gca,'XLim');
		y = get(gca,'YLim');
		set(handles.renav_box,'Value',0)
		if get(handles.chart_checkbox,'Value'), viewMap('utm'), end
		navQuiver('navigation_log','utm');
		if ~isempty(mission), plotMission(mission,'utm',handles), end
		delete(colorbar)
		set(gca,'XLim',x)
		set(gca,'YLim',y)		
	else
		plot_Callback(h, eventdata, handles, varargin)
	end
catch, errorbox, end
set(gcbf,'Pointer','arrow')


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = plotRenav_Callback(h, eventdata, handles, varargin)
	global mission
try,
	set(gcbf,'Pointer','watch')
	cla
	if get(handles.renav_box,'Value'),
		x = get(gca,'XLim');
		y = get(gca,'YLim');	
		set(handles.nav_box,'Value',0)
		if get(handles.chart_checkbox,'Value'), viewMap('utm'), end
		navQuiver('renavigation_log','utm');
		if ~isempty(mission), plotMission(mission,'utm',handles), end
		delete(colorbar)
		set(gca,'XLim',x)
		set(gca,'YLim',y)		
	else
		plot_Callback(h, eventdata, handles, varargin)
	end
catch, errorbox, end
set(gcbf,'Pointer','arrow')


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = sonar_button_Callback(h, eventdata, handles, varargin)
	global mission
try,

	if ~isempty(mission),
		plotSonar;
	else
		disp('sonar_button_Callback: mission is empty')
	end
catch, errorbox('sonar_button_Callback failed'), end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = mission_button_Callback(h, eventdata, handles, varargin)	
	global mission
try,
	if ~isempty(mission),
		button = questdlg(strvcat('Are you sure you want to redo this mission? ', ...
			'Your current mission will not be saved.'));
		if ~strcmp(button,'Yes'), return, end
		if ~isfield(mission{1},'missiontype'),
			errorbox('Mission type ("missiontype") not found in mission{1}.')
			return
		end

		bmission = mission{1};
		mfunction = [getfield(mission{1},'missiontype') 'Mission'];
		bnotes = behaviorNotes;
		bnotes = bnotes{findin(bnotes,'name',getfield(mission{1},'missiontype'))};
	else
		bmission = [];
		str = { 'Basic' 'Survey' 'SurveyBox' 'SurveyArea' 'SurveyAreaEZ' 'SurveyAreaOneway' ...
			'CrissCross' 'QRoute' 'QRouteRelative' 'CompassStar' 'SurveyAreaRaw' };

		%str = { 'Survey' 'SurveyBox' 'SurveyArea' 'SurveyAreaEZ' 'SurveyAreaOneway' ...
		%	'SurveyArea4Leaf' 'SurveyAreaRaw' 'QRoute' 'QRouteRelative' 'CompassCircle' ...
		%	'CompassStar' };
		[s,v] = listdlg('PromptString','Select a mission:','SelectionMode','single',...
			'ListString',str,'InitialValue',4);
		if isempty(s), return, end
		
		bdefault = behaviorDefaults;
		bmission = bdefault{findin(bdefault,'name',lower(str{s}))};
		mfunction = [lower(str{s}) 'mission'];
		bnotes = behaviorNotes;
		bnotes = bnotes{findin(bnotes,'name',lower(str{s}))};
	end
	bmission = setSurveyPoints(bmission,bnotes,'utm');
	if isempty(bmission), return, end
	mission = editor(mfunction,[],[],bmission,[]);
	h = editor(mission);
	
	global mission, global origin
	cvt2utm
	if ~isempty(mission),
		x = get(gca,'XLim');
		y = get(gca,'YLim');
		cla,plotMission(mission,'utm',handles)
		global fighandles
		if ~isempty(fighandles),
			set(fighandles.number_checkbox,'Value',0),
			set(fighandles.chart_checkbox,'Value',0),
			set(fighandles.sonar_checkbox,'Value',0),
		end
		delete(colorbar)
		set(gca,'XLim',x)
		set(gca,'YLim',y)		
	end
catch, errorbox('mission_button_Callback failed'), end

