CCDirector / CCNode Category for Working with Points instead of Pixels


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #pragma mark CCDirector Category for Point Handling
  2.  
  3.  
  4. #import "cocos2d.h"
  5.  
  6.  
  7. @interface CCDirector (Points)
  8.  
  9. - (CGSize) ptWinSize;
  10.  
  11. - (CGPoint) convertToPt:(CGPoint)point;
  12. - (CGPoint) convertToPx:(CGPoint)point;
  13.  
  14. @end
  15.  
  16.  
  17. @implementation CCDirector (Points)
  18.  
  19. - (CGSize) ptWinSize {
  20. CGSize s = [self winSize];
  21. CGPoint p = [self convertToPt:ccp(s.width, s.height)];
  22. return CGSizeMake(p.x, p.y);
  23. }
  24.  
  25.  
  26. - (CGPoint) convertToPt:(CGPoint)point {
  27. return ccpMult(point, 1.0/self.contentScaleFactor);
  28. }
  29.  
  30. - (CGPoint) convertToPx:(CGPoint)point {
  31. return ccpMult(point, self.contentScaleFactor);
  32. }
  33.  
  34. @end
  35.  
  36.  
  37. #pragma mark CCNode Category for Point Handling
  38.  
  39.  
  40. @interface CCNode (Points)
  41.  
  42. @property (assign) CGPoint ptPosition;
  43. @property (assign) CGSize ptContentSize;
  44.  
  45. - (CGPoint) ptConvertToNodeSpace:(CGPoint)point;
  46. - (CGPoint) ptConvertToWorldSpace:(CGPoint)point;
  47.  
  48. @end
  49.  
  50.  
  51. @implementation CCNode (Points)
  52.  
  53. - (void) setPtPosition:(CGPoint)pos {
  54. self.position = [[CCDirector sharedDirector] convertToPx:pos];
  55. }
  56.  
  57. - (CGPoint) ptPosition {
  58. return [[CCDirector sharedDirector] convertToPt:self.position];
  59. }
  60.  
  61. - (void) setPtContentSize:(CGSize)size {
  62. CGPoint p = [[CCDirector sharedDirector] convertToPx:ccp(size.width, size.height)];
  63. self.contentSize = CGSizeMake(p.x, p.y);
  64. }
  65.  
  66. - (CGSize) ptContentSize {
  67. CGPoint p = [[CCDirector sharedDirector] convertToPt:ccp(self.contentSize.width, self.contentSize.height)];
  68. return CGSizeMake(p.x, p.y);
  69. }
  70.  
  71. - (CGPoint) ptConvertToNodeSpace:(CGPoint)point {
  72. point = [[CCDirector sharedDirector] convertToPx:point];
  73. point = [self convertToNodeSpace:point];
  74. point = [[CCDirector sharedDirector] convertToPt:point];
  75. return point;
  76. }
  77.  
  78. - (CGPoint) ptConvertToWorldSpace:(CGPoint)point {
  79. point = [[CCDirector sharedDirector] convertToPx:point];
  80. point = [self convertToWorldSpace:point];
  81. point = [[CCDirector sharedDirector] convertToPt:point];
  82. return point;
  83. }
  84.  
  85. @end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.