
    ը	f/                     @    d Z ddlZddlZddlZddlmZ  G d d      Zy)a=  
@package nviz.animation

@brief Nviz (3D view) animation

Classes:
 - animation::Animation

(C) 2011 by the GRASS Development Team

This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.

@author Anna Kratochvilova <kratochanna gmail.com>
    N)Signalc                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)	Animationa  Class represents animation as a sequence of states (views).
    It enables to record, replay the sequence and finally generate
    all image files. Recording and replaying is based on timer events.
    There is no frame interpolation like in the Tcl/Tk based Nviz.
    c                    t        d      | _        t        d      | _        g | _        || _        || _        | j                  | j                  d| _        ddg| _	        d| _
        d| _        d| _        d	| _        d| _        d| _        y
)a  Animation constructor

        Signals:
            animationFinished - emitted when animation finished
                              - attribute 'mode'
            animationUpdateIndex - emitted during animation to update gui
                                 - attributes 'index' and 'mode'

        :param mapWindow: glWindow where rendering takes place
        :param timer: timer for recording and replaying
        zAnimation.animationFinishedzAnimation.animationUpdateIndex)recordplaytifppmr   Fr      N)r   animationFinishedanimationUpdateIndexanimationListtimer	mapWindowRecordPlayactionsformatsmodepausedcurrentFramefps
stopSavinganimationSaved)selfr   r   s      //usr/lib/grass83/gui/wxpython/nviz/animation.py__init__zAnimation.__init__    s     "((E!F$*+K$L!
""&++tyyAu~	#    c                 V    | j                   j                  | j                                y)zStart recording/playingN)r   StartGetIntervalr   s    r   r    zAnimation.Start<   s    

))+,r   c                 8    | j                   j                          y)zPause recording/playingN)r   Stopr"   s    r   PausezAnimation.Pause@   s    

r   c                 X    | j                   j                          | j                          y)zStop recording/playingN)r   r$   PostFinishedEventr"   s    r   r$   zAnimation.StopD   s    

 r   c                 @     | j                   | j                             y)z,Record/play next view state (on timer event)N)r   r   r"   s    r   UpdatezAnimation.UpdateI   s    TYY!r   c                 L   | j                   j                  t        j                  | j                  j
                        t        j                  | j                  j                        d       | xj                  dz  c_        | j                  | j                         d| _	        y)zRecord new view stateviewiview   indexFN)
r   appendcopydeepcopyr   r,   r-   r   PostUpdateIndexEventr   r"   s    r   r   zAnimation.RecordM   sy    !!dnn&9&9:t~~';';<	
 	Q!!(9(9!:#r   c                     | j                   s| j                          y	 | j                          y# t        $ r | j                          Y yw xY w)zRender next frameN)r   r$   IterAnimation
IndexErrorr"   s    r   r   zAnimation.PlayY   s@    !!IIK	  	IIK	s   0 AAc                     | j                   | j                     }| j                  |       | xj                  dz  c_        | j                  | j                         y )Nr.   r/   )r   r   
UpdateViewr4   )r   paramss     r   r6   zAnimation.IterAnimationd   sL    ##D$5$56Q!!(9(9!:r   c                    | j                   j                         }|j                  |d   |d          | j                   j                          d| j                   j                  d<   | j                   j                  d       y)z)Update view data in map window and renderr,   r-   r+   TquickFN)r   
GetToolWinUpdateStater9   renderRefresh)r   r:   toolWins      r   r9   zAnimation.UpdateViewk   se    ..++-vgG!!#)-g&u%r   c                 6    | j                   j                         S )zTest if timer is running)r   	IsRunningr"   s    r   rC   zAnimation.IsRunningu   s    zz##%%r   c                     || _         y)zWStart animation mode

        :param mode: animation mode (record, play, save)
        Nr   )r   r   s     r   SetModezAnimation.SetModey   s    
 	r   c                     | j                   S )z'Get animation mode (record, play, save)rE   r"   s    r   GetModezAnimation.GetMode   s    yyr   c                     | j                   S )zTest if animation is pausedr   r"   s    r   IsPausedzAnimation.IsPaused   s    {{r   c                     || _         y )NrJ   )r   pauses     r   SetPausezAnimation.SetPause   s	    r   c                 ,    t        | j                        S )z)Returns if an animation has been recorded)boolr   r"   s    r   ExistszAnimation.Exists   s    D&&''r   c                 ,    t        | j                        S )z Return number of recorded frames)lenr   r"   s    r   GetFrameCountzAnimation.GetFrameCount   s    4%%&&r   c                      g | _         d| _        y)zClear all recordsr   N)r   r   r"   s    r   ClearzAnimation.Clear   s    r   c                     |t        | j                        k\  ry|| _        | j                  | j                     }| j                  |       y)zRender frame of given indexN)rS   r   r   r9   )r   r0   r:   s      r   	GoToFramezAnimation.GoToFrame   sB    C**++!##D$5$56r   c                 P    | j                   j                  | j                         y)zAnimation endsrE   N)r   emitr   r"   s    r   r'   zAnimation.PostFinishedEvent   s    ###3r   c                 >    | j                  || j                         y)z'Frame index changed, update tool window)r0   r   N)r   r   )r   r0   s     r   r4   zAnimation.PostUpdateIndexEvent   s    !!DII!>r   c                     d| _         y)zAbort image files generationTN)r   r"   s    r   
StopSavingzAnimation.StopSaving   s	    r   c                     | j                   S )z,Test if animation has been saved (to images))r   r"   s    r   IsSavedzAnimation.IsSaved   s    """r   c                 V   | j                   j                         }| j                   j                         }d}t        | j                        }|dk  rd}n|dk  rd}n|dk  rd}d| _        d	| _        | j                  D ]  }| j                  s| j                  |       d
|z   dz   j                  | j
                        }	dj                  ||	| j                  |         }
t        j                  j                  ||
      }| j                   j                  || j                  |   |d   |d          | xj
                  dz  c_        t        j                          j#                          |j%                  | j
                  d       d| _         n d| _        | j)                          y)zGenerate image files

        :param path: path to directory
        :param prefix: file prefix
        :param format: index of image file format
        z:04.0f
   z:01.0fd   z:02.0fi  z:03.0fr   savez{frame})framez{prefix}_{number}.{ext})prefixnumberextr.   )FileNameFileTypewidthheightF)r0   	goToFrameTN)r   GetClientSizer=   rS   r   r   r   r   r9   formatr   ospathjoin
SaveToFilewxGetAppYieldUpdateFrameIndexr   r'   )r   rq   rf   ro   sizerA   	formatternr:   rg   filenamefilepaths               r   SaveAnimationFilezAnimation.SaveAnimationFile   s    ~~++-..++-	""#r6 IW IX I	(( 	F??'"Y.4<<4CTCT<U4;;!&dll66J <  77<<h7))%!\\&1q'7	 *  !!Q&!		!!#((t/@/@E(R"')	* # r   c                     || _         y)zJSet Frames Per Second value
        :param fps: frames per second
        Nr   )r   r   s     r   SetFPSzAnimation.SetFPS   s     r   c                      d| j                   z  S )zReturn timer interval in msg     @@r   r"   s    r   r!   zAnimation.GetInterval   s      r   N)__name__
__module____qualname____doc__r   r    r%   r$   r)   r   r   r6   r9   rC   rF   rH   rK   rN   rQ   rT   rV   rX   r'   r4   r]   r_   r}   r   r!    r   r   r   r      s    $8-!
"
$	;&&('
 4?#+!Z!r   r   )r   rp   r2   rt   grass.pydispatch.signalr   r   r   r   r   <module>r      s$     
  	 *M! M!r   