soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SSkinObjBase.cpp
1#include <souistd.h>
2#include <core/SSkinObjBase.h>
3#include <core/SWnd.h>
4SNSBEGIN
5
6////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7// handle state map
9{
10 m_mapOfStates = src.m_mapOfStates;
11}
12
14{
15 SXmlNode xmlNode(pNode);
16 SXmlNode xmlStates = xmlNode.child(L"states");
17 if (!xmlStates)
18 return FALSE;
19 SXmlNode xmlState = xmlStates.child(L"state");
20
21 while (xmlState)
22 {
23 int iState = xmlState.attribute(L"index").as_int();
24 SStringW strValue = xmlState.attribute(L"value").as_string();
25 strValue.MakeLower();
26
27 SStringWList lstValues;
28 size_t nValues = SplitString(strValue, L'|', lstValues);
29 for (size_t i = 0; i < nValues; i++)
30 {
31 DWORD dwState = String2State(lstValues[i]);
32 m_mapOfStates[dwState] = iState;
33 }
34
35 xmlState = xmlState.next_sibling(L"state");
36 }
37 return TRUE;
38}
39
40int SState2Index::GetDefIndex(DWORD dwState, bool checkAsPushdown /*= false*/)
41{
42 int idx = 0;
43 if (dwState & WndState_Disable)
44 idx = 3;
45 else if (dwState & WndState_PushDown)
46 idx = 2;
47 else if (dwState & WndState_Hover)
48 idx = 1;
49 else // WndState_Normal
50 idx = 0;
51 if (dwState & WndState_Check)
52 {
53 if (checkAsPushdown)
54 idx = 2;
55 else
56 idx += 4;
57 }
58 return idx;
59}
60
61int SState2Index::GetIndex(DWORD dwState, bool checkAsPushdown) const
62{
63 if (m_mapOfStates.IsEmpty())
64 {
65 int iRet = GetDefIndex(dwState, checkAsPushdown);
66
67 return iRet;
68 }
69 else
70 {
71 int nRet = -1;
72 const SMap<DWORD, int>::CPair *p = m_mapOfStates.Lookup(dwState);
73 if (!p)
74 {
75 if ((dwState & WndState_Hover) && dwState != WndState_Hover)
76 dwState &= ~WndState_Hover;
77 p = m_mapOfStates.Lookup(dwState);
78 }
79 if (p)
80 nRet = p->m_value;
81 return nRet;
82 }
83}
84
86{
87 const static struct _StateMap
88 {
89 LPCWSTR pszName;
90 DWORD dwValue;
91 } kStateMap[] = {
92 { L"normal", WndState_Normal }, { L"hover", WndState_Hover }, { L"pushdown", WndState_PushDown }, { L"disable", WndState_Disable }, { L"checked", WndState_Check },
93 };
94 static SMap<SStringW, DWORD> stateMap;
95 static bool bInited = false;
96 if (!bInited)
97 { // init map
98 for (int j = 0; j < ARRAYSIZE(kStateMap); j++)
99 {
100 stateMap[kStateMap[j].pszName] = kStateMap[j].dwValue;
101 }
102 bInited = true;
103 }
104 SStringWList states;
105 int nStates = (int)SplitString(strState, L'&', states);
106 DWORD dwRet = WndState_Normal;
107 for (int i = 0; i < nStates; i++)
108 {
109 SMap<SStringW, DWORD>::CPair *p = stateMap.Lookup(states[i]);
110 if (p)
111 dwRet |= p->m_value;
112 }
113 return dwRet;
114}
115
116////////////////////////////////////////////////////////////////////////////
118 : m_byAlpha(0xFF)
119 , m_bEnableColorize(true)
120 , m_crColorize(0)
121 , m_bEnableScale(true)
122 , m_nScale(100)
123 , m_checkAsPushdown(true)
124{
125}
126
128{
129 m_state2Index.Init(pNode);
130}
131
132int SSkinObjBase::State2Index(DWORD dwState) const
133{
134 return m_state2Index.GetIndex(dwState, m_checkAsPushdown);
135}
136
138{
139 return m_byAlpha;
140}
141
142void SSkinObjBase::_Scale(ISkinObj *pObj, int nScale)
143{
144 SSkinObjBase *pSkinObj = sobj_cast<SSkinObjBase>(pObj);
145 pSkinObj->m_nScale = nScale;
146 pSkinObj->m_byAlpha = m_byAlpha;
147 pSkinObj->m_bEnableColorize = m_bEnableColorize;
148 pSkinObj->m_crColorize = m_crColorize;
149 pSkinObj->m_strName = m_strName;
150 pSkinObj->m_checkAsPushdown = m_checkAsPushdown;
151}
152
154{
155 if (!m_bEnableScale)
156 {
157 AddRef();
158 return this;
159 }
161 if (!skinObj)
162 return NULL;
163 _Scale(skinObj, nScale);
164 return skinObj;
165}
166
168{
169 return m_nScale;
170}
171
173{
174 m_nScale = scale;
175}
176
178{
179 return 1;
180}
181
183{
184 SIZE ret = { 0, 0 };
185
186 return ret;
187}
188
189void SSkinObjBase::DrawByIndex(IRenderTarget *pRT, LPCRECT rcDraw, int iState) const
190{
191 DrawByIndex2(pRT, rcDraw, iState, GetAlpha());
192}
193
194void SSkinObjBase::DrawByIndex2(IRenderTarget *pRT, LPCRECT rcDraw, int iState, BYTE byAlpha) const
195{
196 _DrawByIndex(pRT, rcDraw, iState, byAlpha);
197}
198
199void SSkinObjBase::DrawByState(IRenderTarget *pRT, LPCRECT rcDraw, DWORD dwState) const
200{
201 DrawByState2(pRT, rcDraw, dwState, GetAlpha());
202}
203
204void SSkinObjBase::DrawByState2(IRenderTarget *pRT, LPCRECT rcDraw, DWORD dwState, BYTE byAlpha) const
205{
206 _DrawByState(pRT, rcDraw, dwState, byAlpha);
207}
208
209void SSkinObjBase::_DrawByState(IRenderTarget *pRT, LPCRECT rcDraw, DWORD dwState, BYTE byAlpha) const
210{
211 int idx = State2Index(dwState);
212 DrawByIndex2(pRT, rcDraw, idx, byAlpha);
213}
214
215void SSkinObjBase::SetAlpha(BYTE byAlpha)
216{
217 m_byAlpha = byAlpha;
218}
219
221{
222 return m_strName;
223}
224
226{
227}
228
229SNSEND
SOUI基础DUI窗口模块
@ WndState_Hover
Definition SWnd.h:76
@ WndState_Check
Definition SWnd.h:78
@ WndState_Disable
Definition SWnd.h:80
@ WndState_Normal
Definition SWnd.h:75
@ WndState_PushDown
Definition SWnd.h:77
virtual ISkinObj * CreateSkinByName(LPCWSTR pszSkinClass) const
Create a skin by name.
Definition SApp.cpp:644
SStringW m_strName
Definition Sobject.hpp:293
LPCWSTR GetObjectClass() SCONST OVERRIDE
Definition Sobject.hpp:211
static SApplication & getSingleton(void)
Definition SSingleton.h:63
int State2Index(DWORD dwState) const
Converts a state to its corresponding index.
LPCWSTR GetName() SCONST OVERRIDE
Gets the name of the skin object.
virtual void _DrawByState(IRenderTarget *pRT, LPCRECT rcDraw, DWORD dwState, BYTE byAlpha) const
Draws the skin by state with alpha blending.
void DrawByIndex(IRenderTarget *pRT, LPCRECT rcDraw, int iState) SCONST OVERRIDE
Draws the skin by index without alpha blending.
int GetStates() SCONST OVERRIDE
Gets the number of states supported by the skin.
virtual void _DrawByIndex(IRenderTarget *pRT, LPCRECT rcDraw, int iState, BYTE byAlpha) const =0
Draws the skin by index with alpha blending.
int GetScale() SCONST OVERRIDE
Gets the scale factor of the skin.
BYTE GetAlpha() SCONST OVERRIDE
Gets the alpha value of the skin.
SIZE GetSkinSize() SCONST OVERRIDE
Gets the size of the skin.
void OnColorize(COLORREF cr) OVERRIDE
Applies colorization to the skin.
void OnInitFinished(IXmlNode *pNode) OVERRIDE
Called when initialization is finished.
void DrawByState2(IRenderTarget *pRT, LPCRECT rcDraw, DWORD dwState, BYTE byAlpha) SCONST OVERRIDE
Draws the skin by state with alpha blending.
void SetAlpha(BYTE byAlpha) OVERRIDE
Sets the alpha value of the skin.
virtual void _Scale(ISkinObj *pObj, int nScale)
Scales the skin object.
void SetScale(int scale) OVERRIDE
Sets the scale factor of the skin.
void DrawByIndex2(IRenderTarget *pRT, LPCRECT rcDraw, int iState, BYTE byAlpha) SCONST OVERRIDE
Draws the skin by index with alpha blending.
ISkinObj * Scale(int nScale) OVERRIDE
Scales the skin.
SSkinObjBase()
Constructor.
void DrawByState(IRenderTarget *pRT, LPCRECT rcDraw, DWORD dwState) SCONST OVERRIDE
Draws the skin by state without alpha blending.
static DWORD String2State(const SStringW &strState)
Converts a string representation of a state to a DWORD value.
SState2Index()
Default constructor.
BOOL Init(IXmlNode *pNode)
Initializes the state-to-index mapping from an XML node.
static int GetDefIndex(DWORD dwState, bool checkAsPushdown=false)
Gets the default index for a given state.
int GetIndex(DWORD dwState, bool checkAsPushdown) const
Gets the index for a given state.
A class representing an ASCII string.
Definition sstringw.h:96
SStringW & MakeLower()
Converts the string to lowercase.
Definition sstringw.cpp:869
int as_int(int def=0) const
Converts the attribute value to an integer.
Definition SXml.cpp:100
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
SXmlNode child(const wchar_t *name, bool bCaseSensitive=false) const
Gets the child node, attribute, or next/previous sibling with the specified name.
Definition SXml.cpp:423
Interface for rendering target objects.
Definition SRender-i.h:1440
Interface for Skin Objects.
Definition SSkinobj-i.h:29
Interface for XML nodes.
Definition sxml-i.h:128