soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SSwitch.cpp
1#include <souistd.h>
2#include "control/SSwitch.h"
3
4SNSBEGIN
5
7 : m_pSkin(GETBUILTINSKIN(SKIN_SYS_SWITCH_BG))
8 , m_pSkinForce(GETBUILTINSKIN(SKIN_SYS_SWITCH))
9 , m_animator(NULL)
10{
11 m_animator.Attach(SApplication::getSingletonPtr()->CreateValueAnimatorByName(SFloatAnimator::GetClassName()));
12 m_animator->setDuration(200);
13 m_animator->addListener(this);
14 m_animator->addUpdateListener(this);
15}
16
20
21void SSwitch::onAnimationEnd(IValueAnimator *pAnimator)
22{
23 SLOGI2("switch") << "fraction:" << pAnimator->getAnimatedFraction();
24 SetCheck(!pAnimator->GetID());
25}
26
27void SSwitch::onAnimationUpdate(IValueAnimator *pAnimator)
28{
29 Invalidate();
30}
31
33{
34 CRect rcWnd = GetWindowRect();
35 if (m_pSkin)
36 m_pSkin->DrawByState(pRT, rcWnd, 0);
37 if (!m_pSkinForce)
38 return;
39 SIZE skSize = m_pSkinForce->GetSkinSize();
40 int dwSpace = rcWnd.Width() - skSize.cx;
41 CRect rcDst;
42 rcDst.top = 0;
43 rcDst.bottom = skSize.cy;
44 if (m_animator->isRunning())
45 {
46 float fac = m_animator->getAnimatedFraction();
47 if (m_animator->GetID() == 0) // uncheck to check
48 {
49 rcDst.left = 0;
50 rcDst.right = skSize.cx;
51 rcDst.OffsetRect((int)(fac * (rcWnd.Width() - skSize.cx)), 0);
52 }
53 else // check to uncheck
54 {
55 rcDst.right = rcWnd.Width();
56 rcDst.left = rcDst.right - skSize.cx;
57 rcDst.OffsetRect(-(int)(fac * (rcWnd.Width() - skSize.cx)), 0);
58 }
59 rcDst.OffsetRect(rcWnd.TopLeft());
60
61 BYTE alpha2 = (BYTE)(fac * 255);
62 BYTE alpha1 = 255 - alpha2;
63
64 DWORD dwState = GetState();
65 m_pSkinForce->DrawByState2(pRT, rcDst, dwState, alpha1);
66 dwState ^= WndState_Check;
67 m_pSkinForce->DrawByState2(pRT, rcDst, dwState, alpha2);
68 }
69 else
70 {
71 rcDst.left = 0;
72 rcDst.right = skSize.cx;
73 if (IsChecked())
74 {
75 rcDst.OffsetRect(rcWnd.Width() - skSize.cx, 0);
76 }
77 rcDst.OffsetRect(rcWnd.TopLeft());
78 m_pSkinForce->DrawByState(pRT, rcDst, GetState());
79 }
80}
81
82void SSwitch::GetDesiredSize(SIZE *ret, int wid, int hei)
83{
84 if (m_pSkin)
85 *ret = m_pSkin->GetSkinSize();
86 else
87 __baseCls::GetDesiredSize(ret, wid, hei);
88}
89
90void SSwitch::OnLButtonUp(UINT nFlags, CPoint point)
91{
92 if (!(GetState() & WndState_PushDown))
93 return;
96
97 m_animator->SetID(IsChecked());
98 m_animator->start(GetContainer());
99}
100
101void SSwitch::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
102{
103 if (nChar == VK_SPACE)
104 {
105 m_animator->SetID(IsChecked());
106 m_animator->start(GetContainer());
107 }
108}
109
110HRESULT SSwitch::OnAttrAnimator(const SStringW &value, BOOL bLoading)
111{
112 m_animator.Attach(SApplication::getSingletonPtr()->LoadValueAnimator(S_CW2T(value)));
113 m_animator->addListener(this);
114 m_animator->addUpdateListener(this);
115 return S_FALSE;
116}
117
118HRESULT SSwitch::OnAttrCheck(const SStringW &strValue, BOOL bLoading)
119{
120 SetCheck(STRINGASBOOL(strValue));
121 return bLoading ? S_OK : S_FALSE;
122}
123
124SNSEND
Definition of the SSwitch class.
@ WndState_Check
Definition SWnd.h:78
@ WndState_PushDown
Definition SWnd.h:77
static LPCWSTR GetClassName()
Definition Sobject.hpp:41
static SApplication * getSingletonPtr(void)
Definition SSingleton.h:73
A class representing an ASCII string.
Definition sstringw.h:96
void onAnimationEnd(IValueAnimator *pAnimator) override
Handles the end of an animation.
Definition SSwitch.cpp:21
void onAnimationUpdate(IValueAnimator *pAnimator) override
Handles the update of an animation.
Definition SSwitch.cpp:27
~SSwitch()
Destructor for SSwitch.
Definition SSwitch.cpp:17
HRESULT OnAttrAnimator(const SStringW &value, BOOL bLoading)
Handles the 'animator' attribute.
Definition SSwitch.cpp:110
void OnPaint(IRenderTarget *pRT)
Handles the paint event.
Definition SSwitch.cpp:32
SAutoRefPtr< ISkinObj > m_pSkin
Definition SSwitch.h:118
void OnLButtonUp(UINT nFlags, CPoint point)
Handles the left mouse button up event.
Definition SSwitch.cpp:90
void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
Handles the key down event.
Definition SSwitch.cpp:101
HRESULT OnAttrCheck(const SStringW &strValue, BOOL bLoading)
Handles the 'checked' attribute.
Definition SSwitch.cpp:118
void GetDesiredSize(SIZE *psz, int nParentWid, int nParentHei) OVERRIDE
Calculates the desired size of the control.
Definition SSwitch.cpp:82
SAutoRefPtr< ISkinObj > m_pSkinForce
Definition SSwitch.h:119
SSwitch()
Constructor for SSwitch.
Definition SSwitch.cpp:6
DWORD GetState() SCONST OVERRIDE
Retrieves the current state of the window.
Definition Swnd.cpp:437
CRect GetWindowRect() const
Retrieves the bounding rectangle of the window.
Definition Swnd.cpp:230
ISwndContainer * GetContainer() OVERRIDE
Retrieves the container associated with this window.
Definition Swnd.cpp:679
void SetCheck(BOOL bCheck) OVERRIDE
Sets the check state of the window.
Definition Swnd.cpp:671
BOOL IsChecked() SCONST OVERRIDE
Checks if the window is checked.
Definition Swnd.cpp:633
void Invalidate() OVERRIDE
Invalidates the entire window.
Definition Swnd.cpp:1437
DWORD ModifyState(DWORD dwStateAdd, DWORD dwStateRemove, BOOL bUpdate=FALSE) OVERRIDE
Modifies the state of the window.
Definition Swnd.cpp:443
BOOL ReleaseCapture() OVERRIDE
Releases the mouse capture from the window.
Definition Swnd.cpp:2491
Interface for rendering target objects.
Definition SRender-i.h:1440