CCNode+REDRelativePositioning.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "CCNode+REDRelativePositioning.h"
  2. #include "REDReader.h"
  3. using namespace cocos2d;
  4. namespace redream {
  5. CC_DLL Vec2 getAbsolutePosition(const Vec2 &pt, REDReader::PositionType type, const Size &containerSize, const std::string& /*propName*/)
  6. {
  7. Vec2 absPt;
  8. if (type == REDReader::PositionType::RELATIVE_BOTTOM_LEFT)
  9. {
  10. absPt = pt;
  11. }
  12. else if (type == REDReader::PositionType::RELATIVE_TOP_LEFT)
  13. {
  14. absPt.x = pt.x;
  15. absPt.y = containerSize.height - pt.y;
  16. }
  17. else if (type == REDReader::PositionType::RELATIVE_TOP_RIGHT)
  18. {
  19. absPt.x = containerSize.width - pt.x;
  20. absPt.y = containerSize.height - pt.y;
  21. }
  22. else if (type == REDReader::PositionType::RELATIVE_BOTTOM_RIGHT)
  23. {
  24. absPt.x = containerSize.width - pt.x;
  25. absPt.y = pt.y;
  26. }
  27. else if (type == REDReader::PositionType::PERCENT)
  28. {
  29. absPt.x = (int)(containerSize.width * pt.x / 100.0f);
  30. absPt.y = (int)(containerSize.height * pt.y / 100.0f);
  31. }
  32. else if (type == REDReader::PositionType::MULTIPLY_RESOLUTION)
  33. {
  34. float resolutionScale = REDReader::getResolutionScale();
  35. absPt.x = pt.x * resolutionScale;
  36. absPt.y = pt.y * resolutionScale;
  37. }
  38. return absPt;
  39. }
  40. CC_DLL void setRelativeScale(Node *pNode, float scaleX, float scaleY, REDReader::ScaleType type, const std::string& /*propName*/)
  41. {
  42. CCASSERT(pNode, "pNode should not be null");
  43. // if (type == REDReader::ScaleType::MULTIPLY_RESOLUTION)
  44. // {
  45. // float resolutionScale = REDReader::getResolutionScale();
  46. //
  47. // scaleX *= resolutionScale;
  48. // scaleY *= resolutionScale;
  49. // }
  50. pNode->setScaleX(scaleX);
  51. pNode->setScaleY(scaleY);
  52. }
  53. }