RUSwitchBtn.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // MySwitchBtn.cpp
  3. // CandyCpp
  4. //
  5. // Created by 杜家兑macbook on 16/8/23.
  6. //
  7. //
  8. #include "RUSwitchBtn.h"
  9. NS_RU_BEGIN
  10. SwitchBtn::SwitchBtn()
  11. {
  12. }
  13. SwitchBtn::~SwitchBtn()
  14. {
  15. }
  16. void SwitchBtn::setQueryFunc(std::function<bool()> query) {
  17. _query = query;
  18. }
  19. void SwitchBtn::setCallbackWhileSwitched(std::function<void(bool)> cb) {
  20. _cbWhileSwitched = cb;
  21. }
  22. void SwitchBtn::onEnter() {
  23. if (_query()) {
  24. playAnim("in_on");
  25. _tapTL = "tap_off";
  26. _releaseTL = "release_off";
  27. } else {
  28. playAnim("in_off");
  29. _tapTL = "tap_on";
  30. _releaseTL = "release_on";
  31. }
  32. coreClickButton = CC_CALLBACK_2(SwitchBtn::onClicked, this);
  33. QCoreLayer::onEnter();
  34. }
  35. void SwitchBtn::onClicked(QCoreBtn* cl, int tag)
  36. {
  37. scheduleOnce([=](float) {
  38. if (_query()) {
  39. _cbWhileSwitched(false);
  40. _tapTL = "tap_on";
  41. _releaseTL = "release_on";
  42. } else {
  43. _cbWhileSwitched(true);
  44. _tapTL = "tap_off";
  45. _releaseTL = "release_off";
  46. }
  47. }, 0.05f, "SCH_Switched");
  48. ResetButton();
  49. }
  50. #pragma mark -
  51. NS_RU_END