Advertisement
Guest User

CCLabelTTF

a guest
Feb 3rd, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void) drawChar:(ccTime) dt
  2. {
  3.     //Se mi trovo in pausa esco dal metodo
  4.     if(self.paused)
  5.         return;
  6.     //Altrimenti controllo cosa e dove stampare
  7.    
  8.     //Get della parola attuale da stampare
  9.     NSString *parola = [self.messToArray objectAtIndex:0];
  10.     //Se precedentemente ho inserito uno spazio
  11.     if(self.isLastSpace){
  12.         //rimuovo la parola precedente allo spazio e carico la nuova parola
  13.         self.isLastSpace = false;
  14.         [self.messToArray removeObjectAtIndex:0];
  15.         //Se il count delle parole da scrivere è 0 elimino lo scheduler ed esco dal metodo
  16.         if([self.messToArray count] == 0){
  17.             [[CCScheduler sharedScheduler] unscheduleSelector:@selector(drawChar:) forTarget:self];
  18.             self.isFinished = true;
  19.             return;
  20.         }
  21.         self.currWord = 0;
  22.         parola = [self.messToArray objectAtIndex:0];
  23.     }else if(self.currWord >= [parola length]){
  24.         //se la lunghezza del contatore alla parola da stampare è maggiore della lunghezza della parola allora devo mettere uno spazio
  25.         self.currWord = 0;
  26.         self.isLastSpace = true;
  27.         parola = [NSString stringWithFormat:@" "];
  28.     }
  29.     //La lattera da stampare
  30.     NSString *lettera = [NSString stringWithFormat:@"%C", [parola characterAtIndex:self.currWord]];
  31.     //La label che contiene la parola completa per controllare se entra tutta nello schermo del telefono
  32.     CCLabelTTF *labelToCheck = [CCLabelTTF labelWithString:[parola substringFromIndex:self.currWord]
  33.                                                   fontName:[GeneralManager sharedManager].fontName fontSize:16];
  34.     //La label che contiene la lettera da scrivere a video
  35.     CCLabelTTF *label = [CCLabelTTF labelWithString:lettera
  36.                                            fontName:[GeneralManager sharedManager].fontName fontSize:16];
  37.     float labelWidth = [labelToCheck contentSize].width;
  38.     //se la width attualmente scritta a schermo più quella della parola da scrivere sorpassa la finestra allora vado a capo
  39.     if(self.lineWidth + labelWidth > [[CCDirector sharedDirector] winSize].width - 20*2){
  40.         //Nuova linea
  41.         //Se vado a capo e la linea nuova da aggiungere sorpassa la finestra del telefono allo metto in pausa e aspetto il touch dell'utente per continuare .. aggiorno anche la height attualmente scritta
  42.         if(self.lineHeight + [labelToCheck contentSize].height > [[CCDirector sharedDirector] winSize].height){
  43.             self.paused = true;
  44.             return;
  45.         }
  46.         self.lineWidth = 0;
  47.         self.lineHeight += [labelToCheck contentSize].height;
  48.         self.lineX = 14;
  49.         self.lineY -= [label contentSize].height;
  50.     }
  51.     //Setto l'anchor point e la position della label per poi stamparla sul ccLayer e aumentare il contatore alla prossima parola da scrivere
  52.     //e aggionrare anche la width attualmente scritta sul ccLayer
  53.     label.anchorPoint = ccp(0,0);
  54.     label.position = ccp(self.lineX, self.lineY);
  55.     self.lineX += [label contentSize].width;
  56.     [self.actualLayer addChild:label];
  57.     self.currWord++;
  58.     self.lineWidth += [label contentSize].width;
  59. }
  60.  
  61. -(void) drawAllText
  62. {
  63.     //Array temporaneo
  64.     NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:self.messToArray];
  65.    
  66.     //Get della parola attuale da stampare
  67.     for (NSString *parola in self.messToArray) {
  68.         if(self.currWord == 0){
  69.             //se la lunghezza del contatore alla parola da stampare è maggiore della lunghezza della parola allora devo mettere uno spazio
  70.             self.currWord = 0;
  71.             CCLabelTTF *spazio = [CCLabelTTF labelWithString:[NSString stringWithFormat:@" "] fontName:[GeneralManager sharedManager].fontName fontSize:16];
  72.             spazio.anchorPoint = ccp(0,0);
  73.             spazio.position = ccp(self.lineX, self.lineY);
  74.             self.lineX += [spazio contentSize].width;
  75.             [self.actualLayer addChild:spazio];
  76.             self.lineWidth += [spazio contentSize].width;
  77.         }
  78.  
  79.         //La label che contiene la parola completa per controllare se entra tutta nello schermo del telefono
  80.         CCLabelTTF *labelToCheck = [CCLabelTTF labelWithString:[parola substringFromIndex:self.currWord]
  81.                                                       fontName:[GeneralManager sharedManager].fontName fontSize:16];
  82.         float labelWidth = [labelToCheck contentSize].width;
  83.         //se la width attualmente scritta a schermo più quella della parola da scrivere sorpassa la finestra allora vado a capo
  84.         if(self.lineWidth + labelWidth > [[CCDirector sharedDirector] winSize].width - 20*2){
  85.             //Nuova linea
  86.             //Se vado a capo e la linea nuova da aggiungere sorpassa la finestra del telefono allo metto in pausa e aspetto il touch dell'utente per continuare .. aggiorno anche la height attualmente scritta
  87.             if(self.lineHeight + [labelToCheck contentSize].height > [[CCDirector sharedDirector] winSize].height){
  88.                 self.paused = true;
  89.                 break;
  90.             }
  91.             self.lineWidth = 0;
  92.             self.lineHeight += [labelToCheck contentSize].height;
  93.             self.lineX = 14;
  94.             self.lineY -= [labelToCheck contentSize].height;
  95.         }
  96.         //Setto l'anchor point e la position della label per poi stamparla sul ccLayer e aumentare il contatore alla prossima parola da scrivere
  97.         //e aggionrare anche la width attualmente scritta sul ccLayer
  98.         labelToCheck.anchorPoint = ccp(0,0);
  99.         labelToCheck.position = ccp(self.lineX, self.lineY);
  100.         self.lineX += [labelToCheck contentSize].width;
  101.         [self.actualLayer addChild:labelToCheck];
  102.         self.currWord = 0;
  103.         self.lineWidth += [labelToCheck contentSize].width;
  104.         //NSLog(@"temp: %d, mess: %d, parola: %@", [temp count], [self.messToArray count], parola);
  105.         [temp removeObject:parola];
  106.     }
  107.     //[self.messToArray release];
  108.     //self.messToArray = nil;
  109.     self.messToArray = [[NSMutableArray alloc] initWithArray:temp];
  110.     //NSLog(@"ce ne sono ancora %d", [self.messToArray count]);
  111.     if([self.messToArray count] == 0){
  112.         self.isFinished = true;
  113.     }else
  114.         self.paused = true;
  115.     return;
  116.  
  117. }
  118.  
  119. -(void) changeVelocityTo:(float)vel
  120. {
  121.     [[CCScheduler sharedScheduler] unscheduleSelector:@selector(drawChar:) forTarget:self];
  122.     [[CCScheduler sharedScheduler] scheduleSelector:@selector(drawChar:) forTarget:self interval:vel paused:NO];
  123. }
  124.  
  125. - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
  126. {
  127.     CGPoint touchLocation = [touch locationInView: [touch view]];
  128.     CGPoint location = [[CCDirector sharedDirector] convertToGL:touchLocation];
  129.    
  130.     /*NSLog(@"Location x: %f, y: %f", location.x, location.y);
  131.     NSLog(@"Rect dimensione x:%f y:%f, w:%f, h:%f", self.dimension.origin.x, self.dimension.origin.y, self.dimension.size.width, self.dimension.size.height);*/
  132.    
  133.     if(CGRectContainsPoint(self.dimension, location)){
  134.         if(self.isFinished){
  135.             self.isRun = false;
  136.             [self.actualLayer removeAllChildrenWithCleanup:YES];
  137.             [self.boxLayer removeAllChildrenWithCleanup:YES];
  138.             [self.nameLayer removeAllChildrenWithCleanup:YES];
  139.             [self.winLayer removeChild:self.actualLayer cleanup:YES];
  140.             [self.winLayer removeChild:self.boxLayer cleanup:YES];
  141.             [self.winLayer removeChild:self.nameLayer cleanup:YES];
  142.             [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
  143.             [self.winLayer runAction:[[CCCallFuncO alloc] initWithTarget:self.winLayer selector:@selector(goToNextAction:) object:self]];
  144.             return true;
  145.         }else if(self.paused){
  146.             //Elimino quello scritto fino ad ora e setto la pausa a false e le altre variabili al loro stato iniziale
  147.             [self.actualLayer removeAllChildrenWithCleanup:YES];
  148.             [self initVariables];
  149.             [self changeVelocityTo:self.writeVel];
  150.         }else{
  151.             //Scrivo tutto il messaggio in una volta sola ed elimino il selector su drawChar
  152.             [[CCScheduler sharedScheduler] unscheduleSelector:@selector(drawChar:) forTarget:self];
  153.             NSLog(@"Curr Word: %d", self.currWord);
  154.             [self drawAllText];
  155.         }
  156.     }
  157.    
  158.     return true;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement