classdef VisualizeResultsState < handle
    % Holds the current state of the Visualize PDE Results LET. This is the
    % minimum data required to generate code, support undo/redo, and support
    % the "restore default values" selection.
    %
    % FOR INTERNAL USE ONLY -- This feature is intentionally undocumented.
    % Its behavior may change, or it may be removed in a future release.

    % Copyright 2022 The MathWorks, Inc.

    properties (Access = public)

        % Track version history
        Version (1, 1) double = 1;

        % Select results
        %
        % Workspace variable name of the selected results object
        Results (1, 1) string = pde.internal.task.utils.Constants.UnsetDropDownValue;
        % Class type of the selected results object
        ResultsClass (1, 1) string = pde.internal.task.visualizeresults.utils.Constants.DefaultResultsClass;
        % Geometry dimensions of the results object: 2D or 3D
        ResultsDimensions (1, 1) string = pde.internal.task.visualizeresults.utils.Constants.DefaultResultsDimensions;

        % Specify data
        %
        % Nodal data type to visualize, e.g.: Displacement, Stress, Strain, ...
        NodalDataType (1, 1) string = pde.internal.task.visualizeresults.utils.Constants.DefaultNodalDataType;
        % Direction of nodal data to visualize, e.g.: Magnitude, X, Y, Z, ...
        NodalDataComponent (1, 1) string = pde.internal.task.visualizeresults.utils.Constants.DefaultNodalDataComponent;

        % Index of interest into nodal data, e.g.: "1", "2", etc.
        FieldIndex (1, 1) string = pde.internal.task.visualizeresults.utils.Constants.DefaultFieldIndex;
        % List of all field value labels for the results object, e.g.: ["1 Hz, 10 Hz, ...]
        FieldItems (1, :) string = pde.internal.task.visualizeresults.utils.Constants.DefaultFieldItems;

        % Time index to visualize
        TimeIndex (1, 1) double = pde.internal.task.visualizeresults.utils.Constants.DefaultTimeIndex;
        % Max number of time indices
        MaxTimeIndex (1, 1) double = pde.internal.task.visualizeresults.utils.Constants.DefaultMaxTimeIndex;
        % Phase angle to visualize
        PhaseAngle (1, 1) double = pde.internal.task.visualizeresults.utils.Constants.DefaultPhaseAngle;
        % Animation logical
        Animate (1, 1) logical = pde.internal.task.visualizeresults.utils.Constants.DefaultAnimate;

        % Scale deformation to help emphasize results
        ScaleDeformation (1, 1) logical = pde.internal.task.visualizeresults.utils.Constants.DefaultScaleDeformation;
        % The auto value to scale deformation, determined based on the selected results object.
        AutoDeformationScaleFactor (1, 1) double = pde.internal.task.visualizeresults.utils.Constants.DefaultAutoDeformationScaleFactor;
        % Limits on the axes given the deformation data and scale factor.
        % A struct with the following fields:
        % MinX (1, 1) double
        % MaxX (1, 1) double
        % MinY (1, 1) double
        % MaxY (1, 1) double
        % MinZ (1, 1) double
        % MaxZ (1, 1) double
        AxesLimits (1, 1) struct = pde.internal.task.visualizeresults.utils.Constants.DefaultAxesLimits;

        % Display options
        %
        % Show axes
        AxesVisible (1, 1) logical = pde.internal.task.visualizeresults.utils.Constants.DefaultAxesVisible;
        % Show colorbar
        ColorbarVisible (1, 1) logical = pde.internal.task.visualizeresults.utils.Constants.DefaultColorbarVisible;
        % Show mesh wireframe
        MeshVisible (1, 1) logical = pde.internal.task.visualizeresults.utils.Constants.DefaultMeshVisible;
        % Show title in figure
        ShowTitle (1, 1) logical = pde.internal.task.visualizeresults.utils.Constants.DefaultShowTitle;
        % Active/specified colorbar limits. A struct with the following fields:
        % Min (1, 1) double - Range minimum
        % Max (1, 1) double - Range maximum
        ColorLimits (1, 1) struct = pde.internal.task.visualizeresults.utils.Constants.DefaultColorLimits;
        % Auto colorbar limits. These value are specific to the current nodal data,
        % component, field index, time index, etc. A struct with the following fields:
        % Min (1, 1) double - Auto range minimum
        % Max (1, 1) double - Auto range maximum
        AutoColorLimits (1, 1) struct = pde.internal.task.visualizeresults.utils.Constants.DefaultColorLimits;
        % These color limits correspond to the "default" nodal data selections
        % of a given results object. This is needed when users select
        % "Restore Default Values" from the task context menu. We need memory
        % of the color limits associated with the default nodal data selections
        % that we will revert to.
        % Min (1, 1) double - Default range minimum
        % Max (1, 1) double - Default range maximum
        DefaultResultsColorLimits (1, 1) struct = pde.internal.task.visualizeresults.utils.Constants.DefaultColorLimits;
        % Mesh transparency
        Transparency (1, 1) double = pde.internal.task.visualizeresults.utils.Constants.DefaultTransparency;
    end

    methods (Access = public)

        function updateState(this, stateStruct)

            % Assign property values to corresponding struct field values
            propNames = properties(this);
            for ct = 1:numel(propNames)
                this.(propNames{ct}) = stateStruct.(propNames{ct});
            end
        end

        function resetSelections(this)

            % Reset data and options selections for the current results object.
            % This is called when users select "Restore Default Values"
            % from the task contect menu
            this.resetDataSelections();
            this.resetOptionsSelections();
        end

        function resetDataSelections(this)

            % Reset data selections. This method is public because it is called
            % independent of resetOptionsSelections() whenever the results
            % object changes.
            this.NodalDataType = pde.internal.task.visualizeresults.utils.Constants.DefaultNodalDataType;
            this.NodalDataComponent = pde.internal.task.visualizeresults.utils.Constants.DefaultNodalDataComponent;
            this.FieldIndex = pde.internal.task.visualizeresults.utils.Constants.DefaultFieldIndex;
            this.TimeIndex = pde.internal.task.visualizeresults.utils.Constants.DefaultTimeIndex;
            this.PhaseAngle = pde.internal.task.visualizeresults.utils.Constants.DefaultPhaseAngle;
            this.Animate = pde.internal.task.visualizeresults.utils.Constants.DefaultAnimate;
            this.ScaleDeformation = pde.internal.task.visualizeresults.utils.Constants.DefaultScaleDeformation;
            % Note, do NOT reset FieldItems, MaxTimeIndex, AutoDeformationScaleFactor or AxesLimits
            % to default. The current values in the task are specific to the selected
            % results object. The default values are arbitrary.
        end
    end

    methods (Access = protected)

        function resetOptionsSelections(this)

            % Reset option selections
            this.AxesVisible = pde.internal.task.visualizeresults.utils.Constants.DefaultAxesVisible;
            this.ColorbarVisible = pde.internal.task.visualizeresults.utils.Constants.DefaultColorbarVisible;
            this.MeshVisible = pde.internal.task.visualizeresults.utils.Constants.DefaultMeshVisible;
            this.ShowTitle = pde.internal.task.visualizeresults.utils.Constants.DefaultShowTitle;
            this.Transparency = pde.internal.task.visualizeresults.utils.Constants.DefaultTransparency;
            % Reset ColorLimits and AutoColorLimits to the auto-color limits
            % stored for the default nodal data of the selected results object.
            this.ColorLimits = this.DefaultResultsColorLimits;
            this.AutoColorLimits = this.DefaultResultsColorLimits;
        end
    end
end
