📄 animate.cpp
字号:
return;
m_savedBackground.Create(rect.width, rect.height);
wxMemoryDC memDC;
memDC.SelectObject(m_savedBackground);
if (m_useParentBackground && GetWindow()->GetParent())
{
wxWindow* parent = GetWindow()->GetParent();
wxClientDC dc(parent);
// Translate the point to coordinates in the
// parent's client area, going via screen coordinates
wxPoint pt(rect.x, rect.y);
wxPoint screenPt = GetWindow()->ClientToScreen(pt);
wxPoint parentPt = parent->ScreenToClient(screenPt);
memDC.Blit(0, 0, rect.width, rect.height, & dc, parentPt.x, parentPt.y);
}
else
{
wxClientDC dc(GetWindow());
memDC.Blit(0, 0, rect.width, rect.height, & dc, rect.x, rect.y);
}
memDC.SelectObject(wxNullBitmap);
}
// Draw this frame
void wxAnimationPlayer::DrawFrame(int frame, wxDC& dc, const wxPoint& pos)
{
wxASSERT_MSG( (m_animation != NULL), _T("Animation not present in wxAnimationPlayer"));
wxASSERT_MSG( (m_frames.GetCount() != 0), _T("Animation cache not present in wxAnimationPlayer"));
wxASSERT_MSG( !!m_frames.Item(frame), _T("Image not present in wxAnimationPlayer::DrawFrame"));
wxBitmap* bitmap = (wxBitmap*) m_frames.Item(frame)->GetData() ;
wxRect rect = GetFrameRect(frame);
dc.DrawBitmap(*bitmap, pos.x + rect.x, pos.y + rect.y, (bitmap->GetMask() != NULL));
}
void wxAnimationTimer::Notify()
{
m_player->PlayFrame();
}
/*
* wxAnimationBase
*/
IMPLEMENT_ABSTRACT_CLASS(wxAnimationBase, wxObject)
/*
* wxGIFAnimation
*/
IMPLEMENT_CLASS(wxGIFAnimation, wxAnimationBase)
wxGIFAnimation::wxGIFAnimation()
{
m_decoder = (wxGIFDecoder*) NULL;
}
wxGIFAnimation::~wxGIFAnimation()
{
delete m_decoder;
}
int wxGIFAnimation::GetFrameCount() const
{
wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
return m_decoder->GetNumberOfFrames();
}
wxImage* wxGIFAnimation::GetFrame(int i) const
{
wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
m_decoder->GoFrame(i + 1);
wxImage* image = new wxImage;
m_decoder->ConvertToImage(image);
return image;
}
wxAnimationDisposal wxGIFAnimation::GetDisposalMethod(int i) const
{
wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
m_decoder->GoFrame(i + 1);
int disposalMethod = m_decoder->GetDisposalMethod();
return (wxAnimationDisposal) disposalMethod;
}
wxRect wxGIFAnimation::GetFrameRect(int i) const
{
wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
m_decoder->GoFrame(i + 1);
wxRect rect(m_decoder->GetLeft(), m_decoder->GetTop(), m_decoder->GetWidth(), m_decoder->GetHeight());
return rect;
}
int wxGIFAnimation::GetDelay(int i) const
{
wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
m_decoder->GoFrame(i + 1);
return m_decoder->GetDelay();
}
wxSize wxGIFAnimation::GetLogicalScreenSize() const
{
wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
return wxSize(m_decoder->GetLogicalScreenWidth(), m_decoder->GetLogicalScreenHeight());
}
bool wxGIFAnimation::GetBackgroundColour(wxColour& col) const
{
wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
int i = m_decoder->GetBackgroundColour();
if (i == -1)
return false;
const unsigned char *pal = m_decoder->GetPalette();
bool result = (pal != NULL);
if (result)
col = wxColour(pal[3*i + 0], pal[3*i + 1], pal[3*i + 2]);
return result;
}
bool wxGIFAnimation::GetTransparentColour(wxColour& col) const
{
wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
int i = m_decoder->GetTransparentColour();
if (i == -1)
return false;
const unsigned char *pal = m_decoder->GetPalette();
bool result = (pal != NULL);
if (result)
col = wxColour(pal[3*i + 0], pal[3*i + 1], pal[3*i + 2]);
return result;
}
bool wxGIFAnimation::IsValid() const
{
return ((m_decoder != NULL) && (m_decoder->IsAnimation()));
}
bool wxGIFAnimation::LoadFile(const wxString& filename)
{
if (!wxFileExists(filename))
return false;
bool result = true;
if (m_decoder)
{
delete m_decoder;
m_decoder = NULL;
}
{
wxFileInputStream stream(filename);
if (stream.GetLength() > 0)
m_decoder = new wxGIFDecoder(&stream, true);
result = ((m_decoder != NULL) && (m_decoder->ReadGIF() == wxGIF_OK));
if (result)
result = m_decoder->IsAnimation();
}
if (!result && (m_decoder != NULL))
{
delete m_decoder;
m_decoder = NULL;
}
return result;
}
/*
* wxAnimationCtrlBase
* Abstract base class for format-specific animation controls.
* This class implements most of the functionality; all a derived
* class has to do is create the appropriate animation class on demand.
*/
IMPLEMENT_ABSTRACT_CLASS(wxAnimationCtrlBase, wxControl)
BEGIN_EVENT_TABLE(wxAnimationCtrlBase, wxControl)
EVT_PAINT(wxAnimationCtrlBase::OnPaint)
END_EVENT_TABLE()
bool wxAnimationCtrlBase::Create(wxWindow *parent, wxWindowID id,
const wxString& filename, const wxPoint& pos,
const wxSize& size, long style, const wxString& name)
{
m_animation = NULL;
m_filename = filename;
if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
return false;
SetBackgroundColour(parent->GetBackgroundColour());
m_animationPlayer.SetCustomBackgroundColour(GetBackgroundColour());
// give the impression of transparency by painting
// with the parent background
// if (parent)
// m_animationPlayer.UseParentBackground(true);
m_animationPlayer.SetWindow(this);
m_animationPlayer.SetPosition(wxPoint(0, 0));
m_animationPlayer.SetDestroyAnimation(false);
LoadFile(filename);
return true;
}
wxAnimationCtrlBase::~wxAnimationCtrlBase()
{
if (m_animationPlayer.IsPlaying())
m_animationPlayer.Stop();
m_animationPlayer.SetAnimation(NULL, false);
delete m_animation;
}
bool wxAnimationCtrlBase::LoadFile(const wxString& filename)
{
if (m_animationPlayer.IsPlaying())
m_animationPlayer.Stop();
wxString filename1(filename);
if (filename1.IsEmpty())
{
filename1 = m_filename;
if (filename1.IsEmpty())
return false;
}
if (m_animation)
{
delete m_animation;
m_animation = NULL;
}
m_animation = DoCreateAnimation(filename1);
if (!m_animation)
return false;
if (!m_animation->LoadFile(filename) || !m_animation->IsValid())
{
delete m_animation;
m_animation = NULL;
return false;
}
m_animationPlayer.SetAnimation(m_animation, false);
if (GetWindowStyle() & wxAN_FIT_ANIMATION)
FitToAnimation();
return true;
}
bool wxAnimationCtrlBase::Play(bool looped)
{
return m_animationPlayer.Play(*this, wxPoint(0, 0), looped);
}
wxSize wxAnimationCtrlBase::DoGetBestSize() const
{
if (m_animationPlayer.HasAnimation() && (GetWindowStyle() & wxAN_FIT_ANIMATION))
return m_animationPlayer.GetLogicalScreenSize();
else
return GetSize();
}
void wxAnimationCtrlBase::FitToAnimation()
{
if (!m_animationPlayer.HasAnimation())
return;
wxSize sz = m_animationPlayer.GetLogicalScreenSize();
SetClientSize(sz);
}
void wxAnimationCtrlBase::OnPaint(wxPaintEvent& WXUNUSED(event))
{
wxPaintDC dc(this);
if (GetPlayer().IsPlaying())
GetPlayer().Draw(dc);
}
/*
* wxGIFAnimationCtrl
* Provides a GIF animation class when required.
*/
IMPLEMENT_ABSTRACT_CLASS(wxGIFAnimationCtrl, wxAnimationCtrlBase)
wxAnimationBase* wxGIFAnimationCtrl::DoCreateAnimation(const wxString& WXUNUSED(filename))
{
return new wxGIFAnimation;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -