- (void)decode { // Take string const char *parseString = [chunk UTF8String]; int len = [chunk length]; // Create an array for the $ separated commands NSMutableArray *commands = [[NSMutableArray alloc] init]; int i = 0; int previous = 0; NSRange range; while (i < len-1) { while (parseString[i] != '$') i++; range.location = previous; range.length = i - previous; [commands addObject: [chunk substringWithRange:range]]; i++; previous = i; } NSMutableArray *parsedCommands = [[NSMutableArray alloc] init]; // Now we have an array of strings, each containing a string of each command int numCommands = [commands count]; for (i = previous = 0; i < numCommands; i++) { NSMutableArray *command = [[NSMutableArray alloc] init]; // Take a command string NSString *currentCommand = [commands objectAtIndex:i]; parseString = [currentCommand UTF8String]; int j = 0; previous = 0; // Loop through, converting the numbers between semicolons to NSNumbers in a mutable array while (j < [currentCommand length]) { while (parseString[j] != ';' && j < [currentCommand length]) j++; range.location = previous; range.length = j - previous; [command addObject:[NSNumber numberWithInt:[[currentCommand substringWithRange:range] intValue]]]; j++; previous = j; } // Add the completed array to the overall parsed commands array [parsedCommands addObject:command]; } // Now that the commands are fully decoded, act on them for (i = 0; i < [parsedCommands count]; i++) { int shape; // What kind of shape it is, if any // Take a command NSMutableArray *currentCommand = [parsedCommands objectAtIndex:i]; int gridOffset; if ([[currentCommand objectAtIndex:0] intValue] == 1) gridOffset = 2; else gridOffset = 1; if ([currentCommand count] > 1) grid = [[gridTabControllers objectAtIndex:[[currentCommand objectAtIndex:[currentCommand count]-gridOffset] intValue]] gridView]; int orientation; switch ([[currentCommand objectAtIndex:0] intValue]) { case 0: // Maxi-clear while ([[tabView tabViewItems] count] > 0) { [tabView removeTabViewItem:[[tabView tabViewItems] objectAtIndex:0]]; [gridTabControllers removeObjectAtIndex:0]; } [self addTab]; grid = [[gridTabControllers objectAtIndex:0] gridView]; break; case 1: // Circle shape = ItemCircle; NSLog(@"Circle!"); break; case 2: // Clear token 1; GridItem *modified = [grid gridItemAtX:[[currentCommand objectAtIndex:2] intValue] atY:19-[[currentCommand objectAtIndex:1] intValue]]; [modified setShape:ItemNone]; /*if ([modified covered] == 1) { GridItem *bigToken = [grid gridItemAtX:[modified coveredX] atY:19-[modified coveredY]]; [bigToken setShape:ItemNone]; int j, k; for (j = 0; j < [bigToken size]; j++) { for (k = 0; k < [bigToken size]; k++) { [[grid gridItemAtX:j+[bigToken x] atY:[bigToken y]-k] setCovered:0]; } } }*/ break; case 3: // Square shape = ItemSquare; break; case 5: // Line if ([[currentCommand objectAtIndex:1] intValue] == 0) orientation = ItemHorizontal; else orientation = ItemVertical; GridItem *line = [grid gridItemAtX:[[currentCommand objectAtIndex:3] intValue] atY:(orientation==ItemHorizontal?20:19)-[[currentCommand objectAtIndex:2] intValue] withOrientation:orientation]; [line setShape:orientation]; [line setColorWithRed:[[currentCommand objectAtIndex:4] intValue] withGreen:[[currentCommand objectAtIndex:5] intValue] withBlue:[[currentCommand objectAtIndex:6] intValue]]; break; case 6: // Clear line if ([[currentCommand objectAtIndex:1] intValue] == 0) orientation = ItemHorizontal; else orientation = ItemVertical; GridItem *lineToClear = [grid gridItemAtX:[[currentCommand objectAtIndex:3] intValue] atY:(orientation==ItemHorizontal?20:19)-[[currentCommand objectAtIndex:2] intValue] withOrientation:orientation]; [lineToClear setShape:ItemNone]; break; case 7: // Background/foreground colour settings and grid clear [grid setBackgroundColorWithRed:[[currentCommand objectAtIndex:1] intValue] withGreen:[[currentCommand objectAtIndex:2] intValue] withBlue:[[currentCommand objectAtIndex:3] intValue]]; [grid setForegroundColorWithRed:[[currentCommand objectAtIndex:4] intValue] withGreen:[[currentCommand objectAtIndex:5] intValue] withBlue:[[currentCommand objectAtIndex:6] intValue]]; // It is also expected that the grid is cleared by this command [grid clearGrid]; break; case 9: // Triangle shape = ItemTriangle; break; case 11: // Cross shape = ItemCross; break; case 15: // New tab [self addTab]; [tabView selectLastTabViewItem:self]; break; case 16: // Remove tab [self removeTab:[[currentCommand objectAtIndex:1] intValue]]; default: // Anything that we don't understand break; } // If it's a shape if ([[currentCommand objectAtIndex:0] intValue] == 1 || [[currentCommand objectAtIndex:0] intValue] == 3 || [[currentCommand objectAtIndex:0] intValue] == 9 || [[currentCommand objectAtIndex:0] intValue] == 11) { // Set all the settings for the gridItem GridItem *modified = [grid gridItemAtX:[[currentCommand objectAtIndex:2] intValue] atY:19-[[currentCommand objectAtIndex:1] intValue]]; [modified setShape:shape]; [modified setX:[[currentCommand objectAtIndex:2] intValue]]; [modified setY:19-[[currentCommand objectAtIndex:1] intValue]]; [modified setColorWithRed:[[currentCommand objectAtIndex:3] intValue] withGreen:[[currentCommand objectAtIndex:4] intValue] withBlue:[[currentCommand objectAtIndex:5] intValue]]; /*if ([[currentCommand objectAtIndex:0] intValue] == 1) { [modified setSize:[[currentCommand objectAtIndex:7] intValue]]; NSLog(@"Made it here: %d", [modified y]); int j, k; for (j = 0; j < [modified size]; j++) { for (k = 0; k < [modified size]; k++) { if (!(j == 0 && k == 0)) { [[grid gridItemAtX:j+[modified x] atY:[modified y]-k] setShape:ItemNone]; [[grid gridItemAtX:j+[modified x] atY:[modified y]-k] setCovered:1]; [[grid gridItemAtX:j+[modified x] atY:[modified y]-k] setCoveredX:[modified x] Y:[modified y]]; } } } }*/ //NSLog(@"made it here too"); } // Something's been changed, so ensure the GridView is redrawn [grid setNeedsDisplay:YES]; } }