text: //
  //  PRPGlobalHelper.h
  //  primaryp
  //
  //  Created by Stefan Schmitt on 5/19/14.
  //
  //
  
  #ifndef primaryp_PRPGlobalHelper_h
  #define primaryp_PRPGlobalHelper_h
  
  /** Returns true of the floats are considered equal */
  inline bool floatEqualsFloat(const float a, const float b)
  {
      return fabsf(a - b) < FLT_EPSILON;
  }
  
  /** Rounds a float to the nearest int */
  inline int convertFloatToNearestInt(const float a)
  {
      return a > 0 ? (int)(a + 0.5f) : (int)(a - 0.5f);
  }
  
  /** Returns the smaller side of a given Size */
  inline float getSmallerSideOfSize(const cocos2d::Size& size)
  {
      return size.width <= size.height ? size.width : size.height;
  }
  
  /** Checks if the given size is either portrait or landscape */
  inline bool isOrientationPortrait(const cocos2d::Size& size)
  {
      return (size.height >= size.width);
  }
  
  /** Converts a string to lowercase characters */
  inline void stringToLowerCase(std::string& string)
  {
      std::transform(string.begin(), string.end(), string.begin(), ::tolower);
  }
  
  /** Compares two strings and returns true if equal */
  inline bool stringCompare(const std::string& a, const std::string& b)
  {
      return (a.compare(b) == 0);
  }
  
  /** Returns true of a given string is empty */
  inline bool stringIsEmpty(const std::string& s)
  {
      return s.empty();
  }
  
  /** Replaces part of a string */
  inline bool StringReplace(std::string& str, const std::string& from, const std::string& to)
  {
      size_t start_pos = str.find(from);
      if(start_pos == std::string::npos)
          return false;
      str.replace(start_pos, from.length(), to);
      return true;
  }
  
  inline std::string getOSString()
  {
  #if (CC_TARGET_PLATFORM == CC_PLATFORM_UNKNOWN)
      return "unknown";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
      return "ios";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
      return "android";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
      return "win32";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
      return "marmalade";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
      return "linux";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_BADA)
      return "bada";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
      return "blackberry";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
      return "osx";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_NACL)
      return "nacl";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_EMSCRIPTEN)
      return "emscripten";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN)
      return "tizen";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_QT5)
      return "qt5";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
      return "wp8";
  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
      return "winrt";
  #else
      return "unknown";
  #endif
  }
  
  #pragma mark - Time
  /** Returns the current time in milliseconds */
  inline long getCurrentMilliSecond()
  {
      long lLastTime = 0;
      struct timeval stCurrentTime;
      
      gettimeofday(&stCurrentTime,NULL);
      lLastTime = stCurrentTime.tv_sec*1000+stCurrentTime.tv_usec*0.001; //millseconds
      return lLastTime;
  }
  
  #pragma mark - Node
  /** 
   * Recursively searches for a child node
   * @param typename T (optional): the type of the node searched for.
   * @param nodeTag: the tag of the node searched for.
   * @param parent: the initial parent node where the search should begin.
   */
  template <typename T = cocos2d::Node*>
  inline T getChildByTypeAndTagRecursively(const int nodeTag, cocos2d::Node* parent) {
      auto aNode = parent->getChildByTag(nodeTag);
      T nodeFound = dynamic_cast<T>(aNode);
      
      if (parent->getTag() == nodeTag) { // for cases where several children have the same tag
          auto castParent = dynamic_cast<T>(parent);
          if (castParent)
              nodeFound = castParent;
      }
      
      if (!nodeFound) {
          auto children = parent->getChildren();
          for (auto child : children)
          {
              nodeFound = getChildByTypeAndTagRecursively<T>(nodeTag, child);
              if (nodeFound) break;
          }
      }
      return nodeFound;
  }
  
  /**
   * Recursively searches for a child node
   * @param typename T (optional): the type of the node searched for.
   * @param nodeTag: the tag of the node searched for.
   * @param parent: the initial parent node where the search should begin.
   */
  template <typename T = cocos2d::Node*>
  inline T getChildByTypeAndNameRecursively(const std::string& name, cocos2d::Node* parent) {
      auto aNode = parent->getChildByName(name);
      T nodeFound = dynamic_cast<T>(aNode);
      
      if (stringCompare(parent->getName(), name)) { // for cases where several children have the same tag
          auto castParent = dynamic_cast<T>(parent);
          if (castParent)
              nodeFound = castParent;
      }
      
      if (!nodeFound) {
          auto children = parent->getChildren();
          for (auto child : children)
          {
              nodeFound = getChildByTypeAndNameRecursively<T>(name, child);
              if (nodeFound) break;
          }
      }
      return nodeFound;
  }
  
  /** Returns the child node with the highest z value */
  inline cocos2d::Node* getChildWithHighestZ(cocos2d::Node* parent)
  {
      auto children = parent->getChildren();
      cocos2d::Node* retNode = nullptr;
      for (auto child : children)
      {
          if (!retNode
              || child->getLocalZOrder() > retNode->getLocalZOrder())
          {
              retNode = child;
          }
      }
      return retNode;
  }
  
  
  #pragma mark - Label
  /** Crops a label to a given width using ... */
  inline void cropLabelToWidth(cocos2d::Label& label, const float width)
  {
      std::string labelString = label.getString();
      const float labelStringSize = labelString.size();
      const cocos2d::Size labelSize = label.getContentSize();
      
      const float difference = labelSize.width - width;
      const float percent = difference / labelSize.width;
      const unsigned int lettersThatOverlap = ceil( labelStringSize * percent + 0.5f);
      const unsigned int lettersToReplace = MAX(lettersThatOverlap, 3);
      std::string str2 = labelString.substr(labelString.size()-lettersToReplace,lettersToReplace);
      std::string newStr = labelString.substr(0, labelString.size()-lettersToReplace);
      newStr += "...";
      label.setString(newStr);
  }
  
  #endif
  