📄 animate.cpp
字号:
///////////////////////////////////////////////////////////////////////////////
// Name: animate.cpp
// Purpose: Implementation of wxAnimation classes
// Author: Julian Smart and Guillermo Rodriguez Garcia
// Modified by:
// Created: 13/8/99
// RCS-ID: $Id: animate.cpp,v 1.13 2005/12/30 18:40:20 vell Exp $
// Copyright: (c) Julian Smart and Guillermo Rodriguez Garcia
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "wx/log.h"
#include "wx/wfstream.h"
#include "wx/image.h"
#include "wx/gifdecod.h"
#include "wx/dcmemory.h"
#include "wx/dc.h"
#include "wx/dcclient.h"
#include "wx/animate/animate.h"
/*
* wxAnimationPlayer
*/
IMPLEMENT_CLASS(wxAnimationPlayer, wxObject)
wxAnimationPlayer::wxAnimationPlayer(wxAnimationBase *animation, bool destroyAnimation)
{
m_animation = animation;
m_destroyAnimation = destroyAnimation;
m_customBackgroundColour = wxColour(0, 0, 0);
m_currentFrame = 0;
m_window = (wxWindow*) NULL;
m_position = wxPoint(0, 0);
m_looped = true;
m_isPlaying = false;
m_useBackgroundColour = false;
m_useCustomBackgroundColour = false;
m_useParentBackground = false;
m_timer.SetPlayer(this);
}
wxAnimationPlayer::~wxAnimationPlayer()
{
Stop();
ClearCache();
if (m_destroyAnimation)
delete m_animation;
}
void wxAnimationPlayer::SetAnimation(wxAnimationBase* animation, bool destroyAnimation)
{
ClearCache();
if (m_destroyAnimation)
delete m_animation;
m_animation = animation;
m_destroyAnimation = destroyAnimation;
}
// Play
bool wxAnimationPlayer::Play(wxWindow& window, const wxPoint& pos, bool WXUNUSED(looped))
{
m_window = & window;
if (!m_animation || !m_animation->IsValid())
return false;
wxSize sz = GetLogicalScreenSize();
wxRect rect(pos, sz);
SaveBackground(rect);
if (m_frames.GetCount() == 0)
{
if (!Build())
{
wxLogWarning(_T("wxAnimationPlayer::Play: could not build the image cache."));
return false;
}
}
m_currentFrame = 0;
// Create the backing store
m_backingStore.Create(sz.x, sz.y);
PlayFrame();
return true;
}
// Build animation (list of wxImages). If not called before Play
// is called, Play will call this automatically.
bool wxAnimationPlayer::Build()
{
ClearCache();
if (!m_animation)
return false;
int i, n;
n = GetFrameCount();
for (i = 0; i < n; i++)
{
wxImage* image = GetFrame(i);
if (image == NULL)
return false;
// If the frame has transparency,
// set the colour so converting to a bitmap
// will create a mask
wxColour transparentColour;
if (GetTransparentColour(transparentColour))
image->SetMaskColour(transparentColour.Red(), transparentColour.Green(), transparentColour.Blue());
wxBitmap* bitmap = new wxBitmap(*image);
delete image;
if (bitmap == NULL)
return false;
m_frames.Append(bitmap);
}
return true;
}
// Stop the animation
void wxAnimationPlayer::Stop()
{
m_timer.Stop();
m_isPlaying = false;
}
// Draw the current view of the animation into this DC.
// Call this from your OnPaint, for example.
void wxAnimationPlayer::Draw(wxDC& dc)
{
dc.DrawBitmap(m_backingStore, m_position.x, m_position.y);
}
int wxAnimationPlayer::GetFrameCount() const
{
if (m_animation)
return m_animation->GetFrameCount();
else
return 0;
}
wxImage* wxAnimationPlayer::GetFrame(int i) const
{
if (m_animation)
return m_animation->GetFrame(i);
else
return (wxImage*) NULL;
}
wxAnimationDisposal wxAnimationPlayer::GetDisposalMethod(int i) const
{
if (m_animation)
return m_animation->GetDisposalMethod(i);
else
return wxANIM_UNSPECIFIED;
}
wxRect wxAnimationPlayer::GetFrameRect(int i) const
{
if (m_animation)
return m_animation->GetFrameRect(i);
else
return wxRect(0, 0, 0, 0);
}
int wxAnimationPlayer::GetDelay(int i) const
{
if (m_animation)
return m_animation->GetDelay(i);
else
return 0;
}
wxSize wxAnimationPlayer::GetLogicalScreenSize() const
{
if (m_animation)
return m_animation->GetLogicalScreenSize();
else
return wxSize(0, 0);
}
bool wxAnimationPlayer::GetBackgroundColour(wxColour& col) const
{
if (m_animation)
return m_animation->GetBackgroundColour(col);
else
return false;
}
bool wxAnimationPlayer::GetTransparentColour(wxColour& col) const
{
if (m_animation)
return m_animation->GetTransparentColour(col);
else
return false;
}
// Play the frame
bool wxAnimationPlayer::PlayFrame(int frame, wxWindow& window, const wxPoint& WXUNUSED(pos))
{
wxMemoryDC dc;
dc.SelectObject(m_backingStore);
// Draw the background: colour or area beneath animation
wxColour col(255, 255, 255);
if (UsingBackgroundColour())
{
if (UsingCustomBackgroundColour())
col = GetCustomBackgroundColour();
else
GetBackgroundColour(col);
// Draw the background colour loaded from the animation
// (or set by the user)
DrawBackground(dc, wxPoint(0, 0), col);
}
else
{
// Draw background we saved
dc.DrawBitmap(m_savedBackground, 0, 0);
}
// Draw all intermediate frames that haven't been removed from the animation
int i;
for (i = 0; i < frame; i++)
{
if ((GetDisposalMethod(i) == wxANIM_DONOTREMOVE) || (GetDisposalMethod(i) == wxANIM_UNSPECIFIED))
DrawFrame(i, dc, wxPoint(0, 0));
}
DrawFrame(frame, dc, wxPoint(0, 0));
dc.SelectObject(wxNullBitmap);
// Draw from backing bitmap onto window
wxClientDC clientDC(& window);
Draw(clientDC);
return true;
}
bool wxAnimationPlayer::PlayFrame()
{
m_isPlaying = true;
PlayFrame(GetCurrentFrame(), * GetWindow(), GetPosition());
// Set the timer for the next frame
int delay = GetDelay(GetCurrentFrame());
if (delay == 0)
delay = 1; // 0 is invalid timeout for wxTimer.
m_timer.Start(delay);
m_currentFrame ++;
if (m_currentFrame == GetFrameCount())
{
// Should a non-looped animation display the last frame?
if (!m_looped)
{
m_timer.Stop();
m_isPlaying = false;
}
else
m_currentFrame = 0;
}
return true;
}
// Clear the wxImage cache
void wxAnimationPlayer::ClearCache()
{
wxList::compatibility_iterator node = m_frames.GetFirst();
while (node)
{
wxList::compatibility_iterator next = node->GetNext();
wxBitmap* bitmap = (wxBitmap*) node->GetData();
delete bitmap;
m_frames.Erase(node);
node = next;
}
}
// Draw the background colour
void wxAnimationPlayer::DrawBackground(wxDC& dc, const wxPoint& pos, const wxColour& colour)
{
wxASSERT_MSG( (m_animation != NULL), _T("Animation not present in wxAnimationPlayer"));
wxASSERT_MSG( (m_frames.GetCount() > 0), _T("Animation cache not present in wxAnimationPlayer"));
// Optimization: if the first frame fills the whole area, and is non-transparent,
// don't bother drawing the background
wxBitmap* firstBitmap = (wxBitmap*) m_frames.GetFirst()->GetData() ;
wxSize screenSize = GetLogicalScreenSize();
if (!firstBitmap->GetMask() && (firstBitmap->GetWidth() == screenSize.x) && (firstBitmap->GetHeight() == screenSize.y))
return;
wxBrush brush(colour, wxSOLID);
wxPen pen(colour, 1, wxSOLID);
dc.SetBrush(brush);
dc.SetPen(pen);
dc.SetLogicalFunction(wxCOPY);
dc.DrawRectangle(pos.x, pos.y, screenSize.x, screenSize.y);
}
// Save the pertinent area of the window so we can restore
// it if drawing transparently
void wxAnimationPlayer::SaveBackground(const wxRect& rect)
{
wxASSERT( (GetWindow() != NULL) );
if (!GetWindow())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -