Merge pull request #1455 from wxWidgets/gradient-pen

Add support for gradients in wxGraphicsPen
This commit is contained in:
Robin Dunn
2019-09-11 20:17:34 -07:00
committed by GitHub
12 changed files with 1337 additions and 805 deletions

View File

@@ -71,6 +71,14 @@ enum wxCompositionMode
wxCOMPOSITION_ADD /* R = S + D */
};
enum wxGradientType
{
wxGRADIENT_NONE,
wxGRADIENT_LINEAR,
wxGRADIENT_RADIAL
};
class WXDLLIMPEXP_FWD_CORE wxDC;
class WXDLLIMPEXP_FWD_CORE wxWindowDC;
class WXDLLIMPEXP_FWD_CORE wxMemoryDC;
@@ -92,6 +100,7 @@ class WXDLLIMPEXP_FWD_CORE wxGraphicsBrush;
class WXDLLIMPEXP_FWD_CORE wxGraphicsFont;
class WXDLLIMPEXP_FWD_CORE wxGraphicsBitmap;
/*
* notes about the graphics context apis
*
@@ -133,33 +142,7 @@ protected:
wxDECLARE_DYNAMIC_CLASS(wxGraphicsObject);
};
// ----------------------------------------------------------------------------
// wxGraphicsPenInfo describes a wxGraphicsPen
// ----------------------------------------------------------------------------
class wxGraphicsPenInfo : public wxPenInfoBase<wxGraphicsPenInfo>
{
public:
explicit wxGraphicsPenInfo(const wxColour& colour = wxColour(),
wxDouble width = 1.0,
wxPenStyle style = wxPENSTYLE_SOLID)
: wxPenInfoBase<wxGraphicsPenInfo>(colour, style)
{
m_width = width;
}
// Setters
wxGraphicsPenInfo& Width(wxDouble width)
{ m_width = width; return *this; }
// Accessors
wxDouble GetWidth() const { return m_width; }
private:
wxDouble m_width;
};
class WXDLLIMPEXP_CORE wxGraphicsPen : public wxGraphicsObject
{
@@ -286,6 +269,201 @@ private:
extern WXDLLIMPEXP_DATA_CORE(wxGraphicsMatrix) wxNullGraphicsMatrix;
// ----------------------------------------------------------------------------
// wxGradientStop and wxGradientStops: Specify what intermediate colors are used
// and how they are spread out in a gradient
// ----------------------------------------------------------------------------
// Describes a single gradient stop.
class wxGraphicsGradientStop
{
public:
wxGraphicsGradientStop(wxColour col = wxTransparentColour,
float pos = 0.)
: m_col(col),
m_pos(pos)
{
}
// default copy ctor, assignment operator and dtor are ok
const wxColour& GetColour() const { return m_col; }
void SetColour(const wxColour& col) { m_col = col; }
float GetPosition() const { return m_pos; }
void SetPosition(float pos)
{
wxASSERT_MSG( pos >= 0 && pos <= 1, "invalid gradient stop position" );
m_pos = pos;
}
private:
// The colour of this gradient band.
wxColour m_col;
// Its starting position: 0 is the beginning and 1 is the end.
float m_pos;
};
// A collection of gradient stops ordered by their positions (from lowest to
// highest). The first stop (index 0, position 0.0) is always the starting
// colour and the last one (index GetCount() - 1, position 1.0) is the end
// colour.
class WXDLLIMPEXP_CORE wxGraphicsGradientStops
{
public:
wxGraphicsGradientStops(wxColour startCol = wxTransparentColour,
wxColour endCol = wxTransparentColour)
{
// we can't use Add() here as it relies on having start/end stops as
// first/last array elements so do it manually
m_stops.push_back(wxGraphicsGradientStop(startCol, 0.f));
m_stops.push_back(wxGraphicsGradientStop(endCol, 1.f));
}
// default copy ctor, assignment operator and dtor are ok for this class
// Add a stop in correct order.
void Add(const wxGraphicsGradientStop& stop);
void Add(wxColour col, float pos) { Add(wxGraphicsGradientStop(col, pos)); }
// Get the number of stops.
size_t GetCount() const { return m_stops.size(); }
// Return the stop at the given index (which must be valid).
wxGraphicsGradientStop Item(unsigned n) const { return m_stops.at(n); }
// Get/set start and end colours.
void SetStartColour(wxColour col)
{ m_stops[0].SetColour(col); }
wxColour GetStartColour() const
{ return m_stops[0].GetColour(); }
void SetEndColour(wxColour col)
{ m_stops[m_stops.size() - 1].SetColour(col); }
wxColour GetEndColour() const
{ return m_stops[m_stops.size() - 1].GetColour(); }
private:
// All the stops stored in ascending order of positions.
wxVector<wxGraphicsGradientStop> m_stops;
};
// ----------------------------------------------------------------------------
// wxGraphicsPenInfo describes a wxGraphicsPen
// ----------------------------------------------------------------------------
class wxGraphicsPenInfo : public wxPenInfoBase<wxGraphicsPenInfo>
{
public:
explicit wxGraphicsPenInfo(const wxColour& colour = wxColour(),
wxDouble width = 1.0,
wxPenStyle style = wxPENSTYLE_SOLID)
: wxPenInfoBase<wxGraphicsPenInfo>(colour, style)
{
m_width = width;
m_gradientType = wxGRADIENT_NONE;
}
// Setters
wxGraphicsPenInfo& Width(wxDouble width)
{ m_width = width; return *this; }
wxGraphicsPenInfo&
LinearGradient(wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
const wxColour& c1, const wxColour& c2,
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix)
{
m_gradientType = wxGRADIENT_LINEAR;
m_x1 = x1;
m_y1 = y1;
m_x2 = x2;
m_y2 = y2;
m_stops.SetStartColour(c1);
m_stops.SetEndColour(c2);
m_matrix = matrix;
return *this;
}
wxGraphicsPenInfo&
LinearGradient(wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
const wxGraphicsGradientStops& stops,
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix)
{
m_gradientType = wxGRADIENT_LINEAR;
m_x1 = x1;
m_y1 = y1;
m_x2 = x2;
m_y2 = y2;
m_stops = stops;
m_matrix = matrix;
return *this;
}
wxGraphicsPenInfo&
RadialGradient(wxDouble startX, wxDouble startY,
wxDouble endX, wxDouble endY, wxDouble radius,
const wxColour& oColor, const wxColour& cColor,
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix)
{
m_gradientType = wxGRADIENT_RADIAL;
m_x1 = startX;
m_y1 = startY;
m_x2 = endX;
m_y2 = endY;
m_radius = radius;
m_stops.SetStartColour(oColor);
m_stops.SetEndColour(cColor);
m_matrix = matrix;
return *this;
}
wxGraphicsPenInfo&
RadialGradient(wxDouble startX, wxDouble startY,
wxDouble endX, wxDouble endY,
wxDouble radius, const wxGraphicsGradientStops& stops,
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix)
{
m_gradientType = wxGRADIENT_RADIAL;
m_x1 = startX;
m_y1 = startY;
m_x2 = endX;
m_y2 = endY;
m_radius = radius;
m_stops = stops;
m_matrix = matrix;
return *this;
}
// Accessors
wxDouble GetWidth() const { return m_width; }
wxGradientType GetGradientType() const { return m_gradientType; }
wxDouble GetX1() const { return m_x1; }
wxDouble GetY1() const { return m_y1; }
wxDouble GetX2() const { return m_x2; }
wxDouble GetY2() const { return m_y2; }
wxDouble GetStartX() const { return m_x1; }
wxDouble GetStartY() const { return m_y1; }
wxDouble GetEndX() const { return m_x2; }
wxDouble GetEndY() const { return m_y2; }
wxDouble GetRadius() const { return m_radius; }
const wxGraphicsGradientStops& GetStops() const { return m_stops; }
const wxGraphicsMatrix& GetMatrix() const { return m_matrix; }
private:
wxDouble m_width;
wxGradientType m_gradientType;
wxDouble m_x1, m_y1, m_x2, m_y2; // also used for m_xo, m_yo, m_xc, m_yc
wxDouble m_radius;
wxGraphicsGradientStops m_stops;
wxGraphicsMatrix m_matrix;
};
class WXDLLIMPEXP_CORE wxGraphicsPath : public wxGraphicsObject
{
public:
@@ -373,82 +551,6 @@ private:
extern WXDLLIMPEXP_DATA_CORE(wxGraphicsPath) wxNullGraphicsPath;
// Describes a single gradient stop.
class wxGraphicsGradientStop
{
public:
wxGraphicsGradientStop(wxColour col = wxTransparentColour,
float pos = 0.)
: m_col(col),
m_pos(pos)
{
}
// default copy ctor, assignment operator and dtor are ok
const wxColour& GetColour() const { return m_col; }
void SetColour(const wxColour& col) { m_col = col; }
float GetPosition() const { return m_pos; }
void SetPosition(float pos)
{
wxASSERT_MSG( pos >= 0 && pos <= 1, "invalid gradient stop position" );
m_pos = pos;
}
private:
// The colour of this gradient band.
wxColour m_col;
// Its starting position: 0 is the beginning and 1 is the end.
float m_pos;
};
// A collection of gradient stops ordered by their positions (from lowest to
// highest). The first stop (index 0, position 0.0) is always the starting
// colour and the last one (index GetCount() - 1, position 1.0) is the end
// colour.
class WXDLLIMPEXP_CORE wxGraphicsGradientStops
{
public:
wxGraphicsGradientStops(wxColour startCol = wxTransparentColour,
wxColour endCol = wxTransparentColour)
{
// we can't use Add() here as it relies on having start/end stops as
// first/last array elements so do it manually
m_stops.push_back(wxGraphicsGradientStop(startCol, 0.f));
m_stops.push_back(wxGraphicsGradientStop(endCol, 1.f));
}
// default copy ctor, assignment operator and dtor are ok for this class
// Add a stop in correct order.
void Add(const wxGraphicsGradientStop& stop);
void Add(wxColour col, float pos) { Add(wxGraphicsGradientStop(col, pos)); }
// Get the number of stops.
size_t GetCount() const { return m_stops.size(); }
// Return the stop at the given index (which must be valid).
wxGraphicsGradientStop Item(unsigned n) const { return m_stops.at(n); }
// Get/set start and end colours.
void SetStartColour(wxColour col)
{ m_stops[0].SetColour(col); }
wxColour GetStartColour() const
{ return m_stops[0].GetColour(); }
void SetEndColour(wxColour col)
{ m_stops[m_stops.size() - 1].SetColour(col); }
wxColour GetEndColour() const
{ return m_stops[m_stops.size() - 1].GetColour(); }
private:
// All the stops stored in ascending order of positions.
wxVector<wxGraphicsGradientStop> m_stops;
};
class WXDLLIMPEXP_CORE wxGraphicsContext : public wxGraphicsObject
{
public:
@@ -523,24 +625,28 @@ public:
wxGraphicsBrush
CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
wxDouble x2, wxDouble y2,
const wxColour& c1, const wxColour& c2) const;
const wxColour& c1, const wxColour& c2,
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) const;
wxGraphicsBrush
CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
wxDouble x2, wxDouble y2,
const wxGraphicsGradientStops& stops) const;
const wxGraphicsGradientStops& stops,
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) const;
// sets the brush to a radial gradient originating at (xo,yc) and ending
// on a circle around (xc,yc) with the given radius; the colours may be
// specified by just the two extremes or the full array of gradient stops
wxGraphicsBrush
CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
wxDouble xc, wxDouble yc, wxDouble radius,
const wxColour& oColor, const wxColour& cColor) const;
CreateRadialGradientBrush(wxDouble startX, wxDouble startY,
wxDouble endX, wxDouble endY, wxDouble radius,
const wxColour& oColor, const wxColour& cColor,
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) const;
wxGraphicsBrush
CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
wxDouble xc, wxDouble yc, wxDouble radius,
const wxGraphicsGradientStops& stops) const;
CreateRadialGradientBrush(wxDouble startX, wxDouble startY,
wxDouble endX, wxDouble endY, wxDouble radius,
const wxGraphicsGradientStops& stops,
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) const;
// creates a font
virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) const;
@@ -916,13 +1022,15 @@ public:
virtual wxGraphicsBrush
CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
wxDouble x2, wxDouble y2,
const wxGraphicsGradientStops& stops) = 0;
const wxGraphicsGradientStops& stops,
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) = 0;
virtual wxGraphicsBrush
CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
wxDouble xc, wxDouble yc,
CreateRadialGradientBrush(wxDouble startX, wxDouble startY,
wxDouble endX, wxDouble endY,
wxDouble radius,
const wxGraphicsGradientStops& stops) = 0;
const wxGraphicsGradientStops& stops,
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix) = 0;
// sets the font
virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) = 0;