classdef MeterUI < handle
% hMeterUI

% Copyright 2015-2021 The MathWorks, Inc.
    
    properties
        pVisual  
    end
    
    methods
        function obj = MeterUI(varargin)
            backgroundColor = [0,0,0];
            panelColor      = [0,0.7,0.6];
            valueColor      = [0,0.8,0.5];
            
            panelFontSize = 0.16;
            textFontSize  = 0.75;
            
            vertFrac = 1/14;
            
            figureName = varargin{1};
            
            [~,figureTag,~] = fileparts(tempname);
            
            width = 0.1*(nargin-1)+ 0.01*(nargin+1);
            parentFig = figure(...
                'Units','normalized',...
                'Position',[0.1 0.75 width 0.1], ...
                'Visible',get(0,'defaultfigureVisible'),...
                'Color',backgroundColor,...
                'MenuBar','none',...
                'Name',figureName,...
                'NumberTitle','off',...
                'Tag',figureTag,...
                'HandleVisibility','callback',...
                'IntegerHandle', 'off'...          
                );

            textPosition = [0.25, 0.25, 0.5, 0.5];
            
            horzFrac = (1/(nargin-1))-0.01;
            offset = 0.01;
            for i = nargin:-1:2
                panel{i-1} = uipanel(...
                    'Parent',parentFig,...
                    'FontUnits','normalized',...
                    'Units','normalized',...
                    'ForegroundColor',panelColor,...
                    'HighlightColor',panelColor,...
                    'ShadowColor',panelColor,...
                    'Title',varargin{i},...
                    'Tag',['panel',num2str(i-1)],...
                    'Position',[offset, 2*vertFrac, horzFrac, 10*vertFrac], ... %this will depend on input number
                    'BackgroundColor',backgroundColor,...
                    'FontSize',panelFontSize);
                
                uicontrol(...
                    'Parent',panel{i-1},...
                    'FontUnits','normalized',...
                    'Units','normalized',...
                    'Style','text',...
                    'Position',textPosition, ... % this will depend on input number
                    'BackgroundColor',backgroundColor,...
                    'ForegroundColor',valueColor,...
                    'FontSize',textFontSize,...
                    'FontWeight','bold',...
                    'Tag',['text',num2str(i-1)],...
                    'String','TEST');
                offset = offset + horzFrac + 0.01;
            end
            
            obj.pVisual = struct('parentFig',parentFig,'numPane',nargin-1);
%             reset(obj);
        end
        %------------------------------------------------------------------
        function update(obj, varargin)
            % If figure is open, display the values
            if isFigureValid(obj)
                for i = 1:nargin-1
                    obj.pVisual.parentFig.Children(i).Children.String = varargin{i};
                end
                drawnow limitrate
            end
        end
        %------------------------------------------------------------------
        function flag = isFigureValid(obj)
            flag = isvalid(obj.pVisual.parentFig);
        end
        %------------------------------------------------------------------
        function h = getFigureHandle(obj)
            h = obj.pVisual.parentFig;
        end
        %------------------------------------------------------------------
        function show(obj)
            if isFigureValid(obj)
                figure(obj.pVisual.parentFig);
            end
        end
        %------------------------------------------------------------------
        function reset(obj)
            k = obj.pVisual.numPane;
            for i = 1:k
                obj.pVisual.Panel{i}.String = '';
            end
            drawnow limitrate
        end
        %------------------------------------------------------------------
        function delete(obj)
            if isFigureValid(obj)
                close(obj.pVisual.parentFig)
            end
        end
    end
    
end