Code covered by the BSD License  

Highlights from
Fireworks GUI

image thumbnail

Fireworks GUI

by

 

30 Dec 2010 (Updated )

Enjoy some fireworks (with sound) with this interactive GUI

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

helper.FireworksGlitter
classdef FireworksGlitter < helper.Fireworks
    % FireworksGlitter      Class for creating fireworks objects
    %
    % This class is subclassed from Fireworks. The difference is that it has
    % some glitters after the explosion. It is used by the FireworksAxes class,
    % and should not be called by itself.
    %
    % See also helper.Fireworks, FireworksAxes, FireworksAxes.newFireworks.
    
    % Author: Jiro Doke
    % Date: Jan 29, 2011
    % Copyright 2010-2012 The MathWorks, Inc.

    properties (Access = public, Hidden)
        CrackleSound
        
        GlitterVals = [30 10];
        GlitterCount = 1;
    end
        
    methods
        
        function obj = FireworksGlitter(FWAxes, pr)
            
            obj = obj@helper.Fireworks(FWAxes, pr);
            
            obj.CrackleSound = loadSound(obj, 'crackle');
                            
            if ~ismember(pr.FireworksType, {'symmetric', 'asymmetric'})
                obj.GlitterVals = [20 5];
            end

        end
        
        function success = step(obj)
            
            % Treat method call to an array of objects
            if numel(obj) > 1
                success = false(size(obj));
                for id = 1:numel(obj)
                    success(id) = step(obj(id));
                end
                drawnow;
                return;
            end
            
            if ~isOperable(obj)
                success = false;
                return;
            end
            
            if ~ishandle(obj.FWAxes.AxH)
                delete(obj);
                success = false;
                return;
            end

            switch obj.Stage
                case 4
                    checkFinishedFcn(obj);
                    success = false;
                case {0, 1, 2}
                    % Call the superclass method for the first part of the
                    % fireworks.
                    success = step@helper.Fireworks(obj);
                case 3
                    
                    success = true;
                    
                    et = etime(clock, obj.StartTime)*12;
                    
                    et2 = et - (obj.T_Stem(end)*obj.SpeedFactor + obj.T_Petals(end) + obj.PetalTailTime);
                    numPetals = length(obj.H_Petals);
                    if obj.GlitterCount == 1
                        if obj.HaveSound
                            try %#ok<TRYNC>
                                play(obj.CrackleSound);
                            end
                        end
                        
                        % First time through, randomly choose points around
                        % each tip
                        set(obj.H_Petals, ...
                            'LineStyle', 'none', ...
                            'LineWidth', 0.5, ...
                            'Marker', 'o', ...
                            {'XData', 'YData'}, ...
                            [...
                            mat2cell(bsxfun(@plus, obj.GlitterVals(1)*randn(numPetals, 4), obj.Y_Petals(end, 1:4:end)'), ones(1, numPetals), 4), ...
                            mat2cell(bsxfun(@plus, obj.GlitterVals(1)*randn(numPetals, 4), obj.Y_Petals(end, 3:4:end)'), ones(1, numPetals), 4)]);
                        
                        obj.GlitterCount = 1.5;
                        
                    elseif ceil(et2/1) > obj.GlitterCount && et2 <= 30
                        % Update only every 1 count (or so)
                        
                        % For subsequent calls, tweak the last location by
                        % some small amount. Also make it fall (by 10
                        % points) to account for gravity (YData)
                        obj.GlitterCount = ceil(et2/1);
                        
                        c1 = obj.getColor(obj.PetalColorIndex, et2/30/1);
                        c2 = obj.getColor(obj.PetalColorIndex, et2/30*2);
                        
                        d = get(obj.H_Petals, {'XData', 'YData'});
                        d(:, 1) = mat2cell(cell2mat(d(:, 1))+obj.GlitterVals(2)*randn(numPetals, 4), ...
                            ones(1, numPetals), 4);
                        d(:, 2) = mat2cell(cell2mat(d(:, 2))+obj.GlitterVals(2)*randn(numPetals, 4)-10, ...
                            ones(1, numPetals), 4);
                        c1_cell = mat2cell(c1, ones(1, numPetals), 3);
                        c2_cell = mat2cell(c2, ones(1, numPetals), 3);
                        set(obj.H_Petals, ...
                            {'XData', 'YData', 'MarkerFaceColor', 'MarkerEdgeColor'}, ...
                            [d, c1_cell, c2_cell]);
                        
                    elseif et2 > 30
                        set(obj.H_Petals, 'XData', NaN, 'YData', NaN);
                        obj.Stage = 4;
                    end
            end
            
        end
        
    end
    
    methods (Access = protected)
        
    end
    
end

Contact us