Blame view

ios/Runner/Wowgame/Classes/game_animal/AniProgressSliderNode.cpp 5.59 KB
5daad4bc   xiaoyu   游戏源码添加编译(现存问题:游戏内...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  //
  //  TimeSliderNode.cpp
  //  WattsenglishToyApp-mobile
  //
  //  Created by Katarzyna Kalinowska-Górska on 27/12/2019.
  //
  
  #include "AniProgressSliderNode.h"
  #include "AniScalingUtils.h"
  
  
  AniProgressSliderNode* AniProgressSliderNode::create(const std::string& sliderImageFilePath, const std::string& thumbImageFilePath, float minVal, float maxVal, AniProgressSliderNodeOrientation p_orientation)
  {
      AniProgressSliderNode * node = new (std::nothrow) AniProgressSliderNode();
      if(node && node->init(sliderImageFilePath, thumbImageFilePath, minVal, maxVal, p_orientation))
      {
          node->autorelease();
          return node;
      }
      CC_SAFE_DELETE(node);
      return nullptr;
  }
  
  bool AniProgressSliderNode::init(const std::string& sliderImageFilePath, const std::string& thumbImageFilePath, float minVal, float maxVal, AniProgressSliderNodeOrientation p_orientation){
      if(!cocos2d::Node::init()){
          return false;
      }
      
      m_orientation = p_orientation;
      
      if(sliderImageFilePath != ""){
          _sliderSprite = cocos2d::Sprite::create(sliderImageFilePath);
          addChild(_sliderSprite);
          _sliderSprite->setPosition(cocos2d::Vec2(_sliderSprite->getContentSize().width/2, _sliderSprite->getContentSize().height/2));
          setContentSize(_sliderSprite->getBoundingBox().size);
      }  
      _thumbSprite = cocos2d::Sprite::create(thumbImageFilePath);
      addChild(_thumbSprite);
      adjustThumbSpritePosition();
      
      setCascadeOpacityEnabled(true);
      
      _minValue = minVal;
      _maxValue = maxVal;
      _currentValue = minVal;
      
      return true;
  }
  
  void AniProgressSliderNode::loadPropertiesFromJSON(const rapidjson::Value& jsonValue, AniLayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder)
  {
      this->loadCommonPropertiesFromJSON(jsonValue);
      if(AniJSONParseUtils::hasMemberColor3B(jsonValue, "colour")){
          setColor(AniJSONParseUtils::getColor3B(jsonValue["colour"]));
      }
      if(AniJSONParseUtils::hasMemberFloat(jsonValue, "indicatorInset")){
          _indicatorInset = jsonValue["indicatorInset"].GetFloat();
      }
  }
  
  void AniProgressSliderNode::setContentSize(const cocos2d::Size& contentSize){
      cocos2d::Node::setContentSize(contentSize);
      adjustThumbSpritePosition();
  }
  
  void AniProgressSliderNode::prepareSize(const rapidjson::Value& jsonValue, float& width, float& height)
  {
      if(_sliderSprite != nullptr){
          width = _sliderSprite->getBoundingBox().size.width;
          height = _sliderSprite->getBoundingBox().size.height;
      } else {
          width = this->getContentSize().width;
          height = this->getContentSize().height;
      }
  }
  
  void AniProgressSliderNode::adjustThumbSpritePosition(){
      if(m_orientation == AniProgressSliderNodeOrientationVertical){
          _thumbSprite->setPosition(cocos2d::Vec2(getContentSize().width/2, _thumbSprite->getContentSize().height/2));
      } else {
          _thumbSprite->setPosition(cocos2d::Vec2(_thumbSprite->getContentSize().width/2, getContentSize().height/2));
      }
      updateProgressUI(_currentValue);
  }
  
  void AniProgressSliderNode::setProperty(std::string propertyName, const rapidjson::Value& newValue, ActionParseDelegate* parseDelegate)
  {
      if(propertyName == "opacity"){
          auto value = newValue.GetInt();
          this->setOpacity(value);
      }
  }
  
  void AniProgressSliderNode::callFunctionByName(std::string methodName, const rapidjson::Value* arguments, ActionParseDelegate* parseDelegate, std::function<void()> callback)
  {
      AniLayoutObject::callFunctionByName(methodName, arguments, parseDelegate);
  }
  
  void AniProgressSliderNode::setProgressFrac(float progressFrac) {
      float progress = progressFrac*_maxValue;
      setProgress(progress);
  }
  
  void AniProgressSliderNode::setProgress(float progress) {
      float newValue = MIN((MAX(progress, _minValue)),_maxValue);
      _currentValue = newValue;
      updateProgressUI(newValue);
  }
  
  void AniProgressSliderNode::updateProgressUI(float value, bool animated){
  //    0 - _minValue
  //    contentSize().width - thumbWidht/2 - _maxValue
      
      float newThumbPosition;
      if(m_orientation == AniProgressSliderNodeOrientationVertical){
          auto inset = _indicatorInset * AniScalingUtils::scaleAspectFitToDesignIpadProSize();
          newThumbPosition = (getContentSize().height - _thumbSprite->getContentSize().height)*value/(_maxValue-_minValue) + inset;
          _thumbSprite->setPositionY(newThumbPosition);
      } else {
          newThumbPosition = (getContentSize().width - _thumbSprite->getContentSize().width/2)*value/(_maxValue-_minValue) + _thumbSprite->getContentSize().width/2;
          _thumbSprite->setPositionX(newThumbPosition);
      }
      
      
  }
  
  void AniProgressSliderNode::startTimeAnimation(float timeDuration){
      cocos2d::Point finalPoint;
      if(m_orientation == AniProgressSliderNodeOrientationVertical){
          finalPoint = cocos2d::Point(_thumbSprite->getPositionX(), getContentSize().height - _thumbSprite->getContentSize().height/2);
      } else {
          finalPoint = cocos2d::Point(getContentSize().width - _thumbSprite->getContentSize().width/2, _thumbSprite->getPositionY());
      }
      
      _thumbSprite->runAction(cocos2d::MoveTo::create(timeDuration, finalPoint));
  //    _thumbSprite->runAction(cocos2d::RepeatForever::create(cocos2d::RotateBy::create(timeDuration/(getContentSize().width-_thumbSprite->getContentSize().width - _thumbSprite->getPositionX())*(_thumbSprite->getContentSize().width*3.14), 360)));
  }
  
  void AniProgressSliderNode::pauseTimeAnimation(){
      _thumbSprite->pause();
  }
  
  void AniProgressSliderNode::resumeTimeAnimation(){
      _thumbSprite->resume();
  }
  
  void AniProgressSliderNode::stopTimeAnimation(){
      _thumbSprite->stopAllActions();
  }