function plcdemo_ladder_elevator_gui(method, varargin)
%Elevator gui

persistent fig carWidth carHeight floorHeight;
persistent carX carPos car carDoor carAxes;

if isempty(fig) || ~ishandle(fig)
    switch method
    case 'init'
        % go through
    case 'close'
        % Already closed. Do nothing.
        return;
    otherwise
        % Figure closed. Stop simulation
        %set_param('sf_elevator', 'SimulationCommand', 'Stop');
        return;
    end
end
        
switch method
case 'init'
    % Initialize the elevator example GUI figure
    fig = figure('Name','Ladder Diagram Controlled Elevator', 'DoubleBuffer', 'on'); 
    oldUnits = get(fig,'Units');
    set(fig,'Units','normalized');
    ratioXY = 200/700;
    height = 0.80;
    width = ratioXY * height;
    left = (1 - width) / 2;
    top = (1 - height)/ 2;
    set(fig,'Position',[left top width height]);
    set(fig,'Units',oldUnits);
    
    % The axes to layout floor levels
    axeFloor = axes('Color','w', ...
                    'XLim',[1 200], ...
                    'YLim',[1 700], ...
                    'DataAspectRatio',[1 1 1], ...
                    'XTick',[], ...
                    'YTick',[], ...
                    'Position',[0 1/8 1 7/8]);
                
    % The axes to layout elevator internal view
    axes('Color','k', ...
        'XLim',[1 200], ...
        'YLim',[1 100], ...
        'DataAspectRatio',[1 1 1], ...
        'XTick',[], ...
        'YTick',[], ...
        'Position',[0 0 1 1/8]);
    
    numFloor = 4;
    floorHeight = 700/numFloor;
    signSize = 10;
    
    % Draw each floor level, the floor number sign, and the elevator request button        
    for f = 1:numFloor
        floorBaseY = floorHeight*(f-1);
        signPosX = 200/2-8/2;
        signPosY = floorBaseY + 7*floorHeight/8;        
                            
        % Floor number signs
        text('Parent',axeFloor, ...
             'Position',[signPosX signPosY], ...
             'String',['F' num2str(f)], ...
             'BackgroundColor','k', ...
             'Color','w', ...
             'FontName','FixedWidth', ...
             'FontSize',signSize, ...
             'FontWeight','bold');
         
        % Floor levels
        line([1 200],[floorBaseY floorBaseY],'Parent',axeFloor);
    end

    % Draw the elevator and its door
    carWidth = 60;
    carHeight = floorHeight*5/8;
    carAxes = 200/2;    
    carX = carAxes-carWidth/2;    
    carPos = 0;
    car = rectangle('Parent',axeFloor, ...
                     'Position',[carAxes-carWidth/2 carPos carWidth carHeight], ...
                     'FaceColor','g', ...
                     'LineWidth',1, ...
                     'EdgeColor','k');
                 
    carDoor = rectangle('parent',axeFloor, ...
                         'Position',[carAxes carPos 0 carHeight], ...
                         'FaceColor','y', ...
                         'LineWidth',1, ...
                         'EdgeColor','k');                         

case 'paint_car'
    % Animation is realized by painting the elevators at new position when
    % they are moving.
    newPos = varargin{1} * 700;    
    if newPos ~= carPos
        carPos = newPos;
        set(car, 'Position', [carX carPos carWidth carHeight]);
        set(carDoor, 'Position', [carAxes carPos 0 carHeight]);
    end
    pause(0.001);
        
case 'paint_door'
    % Animate opening elevator doors
    doorWidth = carWidth/2-10;
    doorWidth = varargin{1}*doorWidth;
    if doorWidth > 0
        set(carDoor, 'Position', [carAxes-doorWidth carPos doorWidth*2 carHeight]);    
    end
    pause(0.001);
    
case 'close'
    % Close UI figure
    close(fig);
end
