function varargout = GestMovs(varargin)
%GESTMOVS M-file for GestMovs.fig
%      GESTMOVS, by itself, creates a new GESTMOVS or raises the existing
%      singleton*.
%
%      H = GESTMOVS returns the handle to a new GESTMOVS or the handle to
%      the existing singleton*.
%
%      GESTMOVS('Property','Value',...) creates a new GESTMOVS using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to GestMovs_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      GESTMOVS('CALLBACK') and GESTMOVS('CALLBACK',hObject,...) call the
%      local function named CALLBACK in GESTMOVS.M with the given input
%      arguments.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help GestMovs

% Last Modified by GUIDE v2.5 10-May-2007 01:09:21

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GestMovs_OpeningFcn, ...
                   'gui_OutputFcn',  @GestMovs_OutputFcn, ...
                   'gui_LayoutFcn',  [], ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
   gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before GestMovs is made visible.
function GestMovs_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

% Choose default command line output for GestMovs
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes GestMovs wait for user response (see UIRESUME)
handles.DM = varargin{1};
handles.output = handles.DM;
init(handles);
uiwait(handles.GestMovs);


% --- Outputs from this function are returned to the command line.
function varargout = GestMovs_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure

varargout{1} = handles.output;

delete(hObject);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fim das definições automáticas do GUI-----------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
%       Inicio

    function init(handles)
        set(handles.axes_pre_vis, 'Visible', 'Off');
        handles = populateList(handles);
        
        default = 'Sem_titulo';
        default_actual = default;
        if(isstruct(handles.MovsList))
            lista = fieldnames(handles.MovsList);
            for i=1:inf,
                    if(~sum(strcmp(default_actual, lista)))
                        break
                    end
                    default_actual = [default num2str(i)];
            end
        end
        handles.nome = default_actual;
        set(handles.text_nome, 'String', handles.nome);
        guidata(handles.GestMovs, handles)
        
        
        
    function handles = populateList(handles)
            if(~exist('MovsList.mat'))
                handles.MovsList = [];
            else
                str = open('MovsList.mat');
                handles.MovsList = str.MovsList;
                elementos = fieldnames(handles.MovsList);
                set(handles.movimentos, 'String', elementos, 'Value', 1);
            end
            
            
function nome_Callback(hObject, eventdata, handles)
        handles.nome = get(handles.text_nome, 'String');
        guidata(hObject, handles);
        

function guardar_Callback(hObject, eventdata, handles)
        nome = handles.nome;
        handles.MovsList = setfield(handles.MovsList, nome, handles.output);
        save 'MovsList' -struct handles MovsList;
        handles = populateList(handles);
        guidata(hObject, handles);
        

function movimentos_Callback(hObject, eventdata, handles)
        value = get(handles.movimentos, 'Value');
        nome = get(handles.movimentos, 'String');
        nome = nome{value};
        
        handles.nome = nome;
        set(handles.text_nome, 'String', handles.nome);
        
        handles.DM = getfield(handles.MovsList, nome);
        handles.DM.fase = 1;
        
        A = CinDir20dof( handles.DM ); 
        handles.GP.def = CoordJuntas( A,handles.DM );
        handles.GP.obj = DesenharRoboPZ( handles.GP, handles.axes_pre_vis );
        guidata(hObject, handles);
        

function abrir_Callback(hObject, eventdata, handles)
        value = get(handles.movimentos, 'Value');
        nome = get(handles.movimentos, 'String');
        nome = nome{value};
        handles.DM = getfield(handles.MovsList, nome);
        handles.output = handles.DM;
        guidata(hObject, handles);
        uiresume(handles.GestMovs);
        
function remover_Callback(hObject, eventdata, handles)
        value = get(handles.movimentos, 'Value');
        nome = get(handles.movimentos, 'String');
        nome = nome{value};
        handles.MovsList = rmfield(handles.MovsList, nome);
        save 'MovsList' -struct handles MovsList;
        handles = populateList(handles);
        guidata(hObject, handles);
        
        

function novo_Callback(hObject, eventdata, handles)
        handles.DM = [];
        handles.output = handles.DM;
        guidata(hObject, handles);
        uiresume(handles.GestMovs);

function pre_vis_Callback(hObject, eventdata, handles)
        while(handles.DM.fase<size(handles.DM.q,1))
            handles.DM.fase = handles.DM.fase+1;
            A = CinDir20dof( handles.DM ); 
            handles.GP.def = CoordJuntas( A,handles.DM );
            ActualizaRobo(handles.GP)
            pause(.5);
        end



% --- Executes when user attempts to close GestMovs.
function GestMovs_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to GestMovs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
uiresume(handles.GestMovs);






