CCControlSwitch.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * http://www.cocos2d-x.org
  4. *
  5. * Copyright 2012 Yannick Loriot. All rights reserved.
  6. * http://yannickloriot.com
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef __CCCONTROLSWITCH_H__
  28. #define __CCCONTROLSWITCH_H__
  29. #include "CCControl.h"
  30. #include "extensions/ExtensionExport.h"
  31. namespace cocos2d { class Sprite; }
  32. namespace cocos2d { class Label; }
  33. NS_CC_EXT_BEGIN
  34. class ControlSwitchSprite;
  35. /**
  36. * @addtogroup GUI
  37. * @{
  38. * @addtogroup control_extension
  39. * @{
  40. */
  41. /** @class ControlSwitch Switch control for Cocos2D. */
  42. class CC_EX_DLL ControlSwitch : public Control
  43. {
  44. public:
  45. /** Creates a switch with a mask sprite, on/off sprites for on/off states, a thumb sprite and an on/off labels. */
  46. static ControlSwitch* create(Sprite *maskSprite, Sprite * onSprite, Sprite * offSprite, Sprite * thumbSprite, Label* onLabel, Label* offLabel);
  47. /** Creates a switch with a mask sprite, on/off sprites for on/off states and a thumb sprite. */
  48. static ControlSwitch* create(Sprite *maskSprite, Sprite * onSprite, Sprite * offSprite, Sprite * thumbSprite);
  49. /**
  50. * @js ctor
  51. * @lua new
  52. */
  53. ControlSwitch();
  54. /**
  55. * @js NA
  56. * @lua NA
  57. */
  58. virtual ~ControlSwitch();
  59. /** Initializes a switch with a mask sprite, on/off sprites for on/off states and a thumb sprite. */
  60. bool initWithMaskSprite(Sprite *maskSprite, Sprite * onSprite, Sprite * offSprite, Sprite * thumbSprite);
  61. /** Initializes a switch with a mask sprite, on/off sprites for on/off states, a thumb sprite and an on/off labels. */
  62. bool initWithMaskSprite(Sprite *maskSprite, Sprite * onSprite, Sprite * offSprite, Sprite * thumbSprite, Label* onLabel, Label* offLabel);
  63. /**
  64. * Set the state of the switch to On or Off, optionally animating the transition.
  65. *
  66. * @param isOn YES if the switch should be turned to the On position; NO if it
  67. * should be turned to the Off position. If the switch is already in the
  68. * designated position, nothing happens.
  69. * @param animated YES to animate the "flipping" of the switch; otherwise NO.
  70. */
  71. void setOn(bool isOn, bool animated);
  72. void setOn(bool isOn);
  73. bool isOn(void) const { return _on; }
  74. bool hasMoved() const { return _moved; }
  75. virtual void setEnabled(bool enabled) override;
  76. Vec2 locationFromTouch(Touch* touch);
  77. // Overrides
  78. virtual bool onTouchBegan(Touch *pTouch, Event *pEvent) override;
  79. virtual void onTouchMoved(Touch *pTouch, Event *pEvent) override;
  80. virtual void onTouchEnded(Touch *pTouch, Event *pEvent) override;
  81. virtual void onTouchCancelled(Touch *pTouch, Event *pEvent) override;
  82. protected:
  83. /** Sprite which represents the view. */
  84. ControlSwitchSprite* _switchSprite;
  85. float _initialTouchXPosition;
  86. bool _moved;
  87. /** A Boolean value that determines the off/on state of the switch. */
  88. bool _on;
  89. };
  90. // end of GUI group
  91. /// @}
  92. /// @}
  93. NS_CC_EXT_END
  94. #endif /* __CCCONTROLSWITCH_H__ */