This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

RoundedRectNode.m

By: a guest on Jun 2nd, 2011  |  syntax: Objective C  |  size: 5.70 KB  |  views: 1,399  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2. *  RoundedRectNode.m
  3. *
  4. *  Created by John Swensen on 6/2/11.
  5. *  Copyright 2011 swenGames.com.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. */
  26.  
  27.  
  28.  
  29. #import "RoundedRectNode.h"
  30.  
  31.  
  32. @implementation RoundedRectNode
  33.  
  34. @synthesize size, radius, borderWidth, cornerSegments, borderColor, fillColor;
  35.  
  36. #define kappa 0.552228474
  37.  
  38. -(id) initWithRectSize:(CGSize)sz  {
  39.    
  40.     if((self == [super init]))
  41.     {
  42.         size = sz;
  43.         radius = 1;
  44.         borderWidth = 1;
  45.         cornerSegments = 4;
  46.         borderColor = ccc4(255,255,255,255);
  47.         fillColor = ccc4(0,0,0,255);
  48.        
  49.     }
  50.     return self;
  51. }
  52.  
  53. void appendCubicBezier(int startPoint, CGPoint* vertices, CGPoint origin, CGPoint control1, CGPoint control2, CGPoint destination, NSUInteger segments)
  54. {
  55.         //ccVertex2F vertices[segments + 1];
  56.        
  57.         float t = 0;
  58.         for(NSUInteger i = 0; i < segments; i++)
  59.         {
  60.                 GLfloat x = powf(1 - t, 3) * origin.x + 3.0f * powf(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x;
  61.                 GLfloat y = powf(1 - t, 3) * origin.y + 3.0f * powf(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * destination.y;
  62.         vertices[startPoint+i] = CGPointMake(x * CC_CONTENT_SCALE_FACTOR(), y * CC_CONTENT_SCALE_FACTOR() );
  63.                 //vertices[startPoint+i] = (ccVertex2F) {x * CC_CONTENT_SCALE_FACTOR(), y * CC_CONTENT_SCALE_FACTOR() };
  64.                 t += 1.0f / segments;
  65.         }
  66.         //vertices[segments] = (ccVertex2F) {destination.x * CC_CONTENT_SCALE_FACTOR(), destination.y * CC_CONTENT_SCALE_FACTOR() };
  67. }
  68.  
  69. void ccFillPoly( CGPoint *poli, int points, BOOL closePolygon )
  70. {
  71.         // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
  72.         // Needed states: GL_VERTEX_ARRAY,
  73.         // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
  74.         glDisable(GL_TEXTURE_2D);
  75.         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  76.         glDisableClientState(GL_COLOR_ARRAY);
  77.        
  78.         glVertexPointer(2, GL_FLOAT, 0, poli);
  79.         if( closePolygon )
  80.                 //       glDrawArrays(GL_LINE_LOOP, 0, points);
  81.                 glDrawArrays(GL_TRIANGLE_FAN, 0, points);
  82.         else
  83.                 glDrawArrays(GL_LINE_STRIP, 0, points);
  84.        
  85.         // restore default state
  86.         glEnableClientState(GL_COLOR_ARRAY);
  87.         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  88.         glEnable(GL_TEXTURE_2D);
  89. }
  90.  
  91. -(void) draw {
  92.         CGPoint vertices[16];
  93.    
  94.     vertices[0] = ccp(0,-radius);
  95.     vertices[1] = ccp(0,-radius*(1-kappa));
  96.     vertices[2] = ccp(radius*(1-kappa),0);
  97.     vertices[3] = ccp(radius,0);
  98.    
  99.     vertices[4] = ccp(size.width-radius,0);
  100.     vertices[5] = ccp(size.width-radius*(1-kappa),0);
  101.     vertices[6] = ccp(size.width,-radius*(1-kappa));
  102.     vertices[7] = ccp(size.width,-radius);
  103.    
  104.     vertices[8] = ccp(size.width,-size.height + radius);
  105.     vertices[9] = ccp(size.width,-size.height + radius*(1-kappa));
  106.     vertices[10] = ccp(size.width-radius*(1-kappa),-size.height);
  107.     vertices[11] = ccp(size.width-radius,-size.height);
  108.    
  109.     vertices[12] = ccp(radius,-size.height);
  110.     vertices[13] = ccp(radius*(1-kappa),-size.height);                  
  111.     vertices[14] = ccp(0,-size.height+radius*(1-kappa));                  
  112.     vertices[15] = ccp(0,-size.height+radius);    
  113.    
  114.    
  115.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  116.    
  117.    
  118.    
  119.     CGPoint polyVertices[4*cornerSegments+1];
  120.     appendCubicBezier(0*cornerSegments,polyVertices,vertices[0], vertices[1], vertices[2], vertices[3], cornerSegments);
  121.     appendCubicBezier(1*cornerSegments,polyVertices,vertices[4], vertices[5], vertices[6], vertices[7], cornerSegments);
  122.     appendCubicBezier(2*cornerSegments,polyVertices,vertices[8], vertices[9], vertices[10], vertices[11], cornerSegments);
  123.     appendCubicBezier(3*cornerSegments,polyVertices,vertices[12], vertices[13], vertices[14], vertices[15], cornerSegments);
  124.     polyVertices[4*cornerSegments] = vertices[0];
  125.    
  126.     glColor4ub(fillColor.r, fillColor.g, fillColor.b, fillColor.a);
  127.     ccFillPoly(polyVertices, 4*cornerSegments+1, YES);
  128.  
  129.    
  130.     glColor4ub(borderColor.r, borderColor.g, borderColor.b, borderColor.a);
  131.     glLineWidth(borderWidth);
  132.     glEnable(GL_LINE_SMOOTH);
  133.     ccDrawCubicBezier(vertices[0], vertices[1], vertices[2], vertices[3], cornerSegments);
  134.     ccDrawLine(vertices[3], vertices[4]);
  135.     ccDrawCubicBezier(vertices[4], vertices[5], vertices[6], vertices[7], cornerSegments);
  136.     ccDrawLine(vertices[7], vertices[8]);
  137.     ccDrawCubicBezier(vertices[8], vertices[9], vertices[10], vertices[11], cornerSegments);
  138.     ccDrawLine(vertices[11], vertices[12]);
  139.     ccDrawCubicBezier(vertices[12], vertices[13], vertices[14], vertices[15], cornerSegments);
  140.     ccDrawLine(vertices[15], vertices[0]);
  141.     glDisable(GL_LINE_SMOOTH);
  142.  
  143.    
  144. }
  145.  
  146. @end
clone this paste RAW Paste Data