Change inactive bitmap to in wxBitmapBundle wxAnimationCtrl

This commit is contained in:
Alexander Koshelev 2022-01-18 08:45:52 +03:00 committed by Vadim Zeitlin
parent 2b94729a33
commit 3f3561e2c6
5 changed files with 37 additions and 23 deletions

View file

@ -18,7 +18,7 @@
#include "wx/animdecod.h"
#include "wx/control.h"
#include "wx/timer.h"
#include "wx/bitmap.h"
#include "wx/bmpbndl.h"
class WXDLLIMPEXP_FWD_CORE wxAnimation;
class wxAnimationImpl;
@ -110,11 +110,11 @@ public:
virtual bool IsPlaying() const = 0;
virtual void SetInactiveBitmap(const wxBitmap &bmp);
virtual void SetInactiveBitmap(const wxBitmapBundle &bmp);
// always return the original bitmap set in this control
wxBitmap GetInactiveBitmap() const
{ return m_bmpStatic; }
{ return m_bmpStatic.GetBitmapFor(this); }
wxAnimation CreateAnimation() const
{ return MakeAnimFromImpl(DoCreateAnimationImpl()); }
@ -134,7 +134,7 @@ protected:
wxAnimation m_animation;
// the inactive bitmap as it was set by the user
wxBitmap m_bmpStatic;
wxBitmapBundle m_bmpStatic;
// the inactive bitmap currently shown in the control
// (may differ in the size from m_bmpStatic)

View file

@ -11,7 +11,7 @@
#ifndef _WX_GENERIC_ANIMATEH__
#define _WX_GENERIC_ANIMATEH__
#include "wx/bitmap.h"
#include "wx/bmpbndl.h"
// ----------------------------------------------------------------------------
@ -59,7 +59,7 @@ public:
void SetAnimation(const wxAnimation &animation) wxOVERRIDE;
virtual void SetInactiveBitmap(const wxBitmap &bmp) wxOVERRIDE;
virtual void SetInactiveBitmap(const wxBitmapBundle &bmp) wxOVERRIDE;
// override base class method
virtual bool SetBackgroundColour(const wxColour& col) wxOVERRIDE;
@ -110,6 +110,15 @@ protected: // internal utilities
virtual void DisplayStaticImage() wxOVERRIDE;
virtual wxSize DoGetBestSize() const wxOVERRIDE;
// This function can be used as event handler for wxEVT_DPI_CHANGED event
// and simply calls UpdateStaticImage() to refresh the m_bmpStaticReal when it happens.
void WXHandleDPIChanged(wxDPIChangedEvent& event)
{
UpdateStaticImage();
event.Skip();
}
// Helpers to safely access methods in the wxAnimationGenericImpl that are
// specific to the generic implementation
wxPoint AnimationImplGetFramePosition(unsigned int frame) const;

View file

@ -192,7 +192,7 @@ public:
Note that the inactive bitmap, if smaller than the control's size, will be
centered in the control; if bigger, it will be stretched to fit it.
*/
virtual void SetInactiveBitmap(const wxBitmap& bmp);
virtual void SetInactiveBitmap(const wxBitmapBundle& bmp);
/**
Stops playing the animation.

View file

@ -138,16 +138,19 @@ void wxAnimationCtrlBase::UpdateStaticImage()
// if given bitmap is not of the right size, recreate m_bmpStaticReal accordingly
const wxSize &sz = GetClientSize();
if (sz.GetWidth() != m_bmpStaticReal.GetWidth() ||
sz.GetHeight() != m_bmpStaticReal.GetHeight())
if (sz.GetWidth() != m_bmpStaticReal.GetLogicalWidth() ||
sz.GetHeight() != m_bmpStaticReal.GetLogicalHeight())
{
wxBitmap bmpCurrent = m_bmpStatic.GetBitmapFor(this);
if (!m_bmpStaticReal.IsOk() ||
m_bmpStaticReal.GetWidth() != sz.GetWidth() ||
m_bmpStaticReal.GetHeight() != sz.GetHeight())
m_bmpStaticReal.GetLogicalWidth() != sz.GetWidth() ||
m_bmpStaticReal.GetLogicalHeight() != sz.GetHeight())
{
// need to (re)create m_bmpStaticReal
if (!m_bmpStaticReal.Create(sz.GetWidth(), sz.GetHeight(),
m_bmpStatic.GetDepth()))
if (!m_bmpStaticReal.CreateWithLogicalSize(sz,
bmpCurrent.GetScaleFactor(),
bmpCurrent.GetDepth()))
{
wxLogDebug(wxT("Cannot create the static bitmap"));
m_bmpStatic = wxNullBitmap;
@ -155,8 +158,8 @@ void wxAnimationCtrlBase::UpdateStaticImage()
}
}
if (m_bmpStatic.GetWidth() <= sz.GetWidth() &&
m_bmpStatic.GetHeight() <= sz.GetHeight())
if (bmpCurrent.GetLogicalWidth() <= sz.GetWidth() &&
bmpCurrent.GetLogicalHeight() <= sz.GetHeight())
{
// clear the background of m_bmpStaticReal
wxBrush brush(GetBackgroundColour());
@ -166,25 +169,25 @@ void wxAnimationCtrlBase::UpdateStaticImage()
dc.Clear();
// center the user-provided bitmap in m_bmpStaticReal
dc.DrawBitmap(m_bmpStatic,
(sz.GetWidth()-m_bmpStatic.GetWidth())/2,
(sz.GetHeight()-m_bmpStatic.GetHeight())/2,
dc.DrawBitmap(bmpCurrent,
(sz.GetWidth()-bmpCurrent.GetLogicalWidth())/2,
(sz.GetHeight()-bmpCurrent.GetLogicalHeight())/2,
true /* use mask */ );
}
else
{
// the user-provided bitmap is bigger than our control, strech it
wxImage temp(m_bmpStatic.ConvertToImage());
wxImage temp(bmpCurrent.ConvertToImage());
temp.Rescale(sz.GetWidth(), sz.GetHeight(), wxIMAGE_QUALITY_HIGH);
m_bmpStaticReal = wxBitmap(temp);
}
}
}
void wxAnimationCtrlBase::SetInactiveBitmap(const wxBitmap &bmp)
void wxAnimationCtrlBase::SetInactiveBitmap(const wxBitmapBundle &bmp)
{
m_bmpStatic = bmp;
m_bmpStaticReal = bmp;
m_bmpStaticReal = bmp.GetBitmapFor(this);
// if not playing, update the control now
// NOTE: DisplayStaticImage() will call UpdateStaticImage automatically

View file

@ -178,6 +178,8 @@ void wxGenericAnimationCtrl::Init()
// use the window background colour by default to be consistent
// with the GTK+ native version
m_useWinBackgroundColour = true;
Bind(wxEVT_DPI_CHANGED, &wxGenericAnimationCtrl::WXHandleDPIChanged, this);
}
bool wxGenericAnimationCtrl::Create(wxWindow *parent, wxWindowID id,
@ -263,14 +265,14 @@ void wxGenericAnimationCtrl::SetAnimation(const wxAnimation& animation)
DisplayStaticImage();
}
void wxGenericAnimationCtrl::SetInactiveBitmap(const wxBitmap &bmp)
void wxGenericAnimationCtrl::SetInactiveBitmap(const wxBitmapBundle &bmp)
{
// if the bitmap has an associated mask, we need to set our background to
// the colour of our parent otherwise when calling DrawCurrentFrame()
// (which uses the bitmap's mask), our background colour would be used for
// transparent areas - and that's not what we want (at least for
// consistency with the GTK version)
if ( bmp.IsOk() && bmp.GetMask() != NULL && GetParent() != NULL )
if ( bmp.IsOk() && bmp.GetBitmapFor(this).GetMask() != NULL && GetParent() != NULL )
SetBackgroundColour(GetParent()->GetBackgroundColour());
wxAnimationCtrlBase::SetInactiveBitmap(bmp);