soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SGradient.cpp
1#include <souistd.h>
2#include <core/SGradient.h>
3
4SNSBEGIN
8
9const GradientItem *SGradient::GetGradientData(CTHIS) SCONST
10{
11 return m_arrGradient.GetData();
12}
13
15{
16 return (int)m_arrGradient.GetCount();
17}
18
19static int GradientItemCmp(const void *_p1, const void *_p2)
20{
21 const GradientItem *p1 = (const GradientItem *)_p1;
22 const GradientItem *p2 = (const GradientItem *)_p2;
23 float diff = p1->pos - p2->pos;
24 if (diff > 0.0f)
25 return 1;
26 else if (diff < 0.0f)
27 return -1;
28 else
29 return 0;
30}
31
32static BOOL ParseGradientColors(const SStringW &value, SArray<GradientItem> &output)
33{
34 SStringWList lstInfo;
35 SplitString(value, ',', lstInfo);
36
37 for (UINT i = 0; i < lstInfo.GetCount(); i++)
38 {
39 SStringWList lstColorInfo;
40 SplitString(lstInfo[i], '|', lstColorInfo);
41 GradientItem gradient;
42 if (lstColorInfo.GetCount() == 2)
43 {
44 gradient.cr = GETCOLOR(lstColorInfo[0]);
45 gradient.pos = (float)_wtof(lstColorInfo[1]);
46 }
47 else
48 {
49 gradient.cr = GETCOLOR(lstColorInfo[0]);
50 gradient.pos = 1.0f * i / (lstInfo.GetCount() - 1);
51 }
52 output.Add(gradient);
53 }
54 qsort(output.GetData(), output.GetCount(), sizeof(GradientItem), GradientItemCmp);
55 if (output.GetCount() < 2)
56 return FALSE;
57 output[0].pos = 0.0f;
58 output[output.GetCount() - 1].pos = 1.0f;
59 return TRUE;
60}
61
62HRESULT SGradient::OnAttrColors(const SStringW &value, BOOL bLoading)
63{
64 m_arrGradient.RemoveAll();
65 return ParseGradientColors(value, m_arrGradient) ? S_FALSE : E_INVALIDARG;
66}
67
69{
70 LoadColorTable(pNode);
71}
72
74{
75 IXmlNode *xmlColor = xmlNode->Child(L"item", FALSE);
76 if (!xmlColor)
77 {
78 return 0;
79 }
80
81 m_arrGradient.RemoveAll();
82 SXmlNode color(xmlColor);
83 float pos = -1.f;
84 while (color)
85 {
86 GradientItem gradient;
87 gradient.cr = GETCOLOR(color.attribute(L"color").as_string());
88 gradient.pos = color.attribute(L"pos").as_float();
89 if (gradient.pos < pos || gradient.pos < 0.f || gradient.pos > 1.0f)
90 {
91 SSLOGW() << "invalid gradient item pos. pos must between 0 and 1";
92 }
93 else
94 {
95 m_arrGradient.Add(gradient);
96 pos = gradient.pos;
97 }
98 color = color.next_sibling();
99 }
100 if (m_arrGradient.GetCount() < 2)
101 {
102 m_arrGradient.RemoveAll();
103 // invalid
104 SSLOGW() << "gradient color table is empty";
105 }
106 return (int)m_arrGradient.GetCount();
107}
108SNSEND
const GradientItem * GetGradientData() SCONST OVERRIDE
Gets the gradient data.
Definition SGradient.cpp:9
int GetGradientLength() SCONST OVERRIDE
Gets the length of the gradient.
Definition SGradient.cpp:14
SArray< GradientItem > m_arrGradient
Definition SGradient.h:82
HRESULT OnAttrColors(const SStringW &value, BOOL bLoading)
Handles the "colors" attribute.
Definition SGradient.cpp:62
SGradient()
Constructor.
Definition SGradient.cpp:5
int LoadColorTable(IXmlNode *xmlNode)
Loads the color table from an XML node.
Definition SGradient.cpp:73
void OnInitFinished(IXmlNode *pNode) OVERRIDE
Initializes the object after loading from XML.
Definition SGradient.cpp:68
A class representing an ASCII string.
Definition sstringw.h:96
float as_float(float def=0) const
Converts the attribute value to a float.
Definition SXml.cpp:115
const wchar_t * as_string(const wchar_t *def=L"") const
Gets the attribute value as a string.
Definition SXml.cpp:95
Class representing an XML node.
Definition SXml.h:352
SXmlNode next_sibling() const
Gets the next sibling node in the children list of the parent node.
Definition SXml.cpp:393
SXmlAttr attribute(const wchar_t *name, bool bCaseSensitive=false) const
Gets the attribute with the specified name.
Definition SXml.cpp:428
Interface for XML nodes.
Definition sxml-i.h:128
IXmlNode * Child(const wchar_t *name, BOOL bCaseSensitive) SCONST PURE
Gets a child node by name.