yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame^] | 1 | #import "SkOptionsTableViewController.h" |
| 2 | #include "SkEvent.h" |
| 3 | |
| 4 | @implementation SkOptionItem |
| 5 | @synthesize fCell, fItem; |
| 6 | - (void)dealloc { |
| 7 | [fCell release]; |
| 8 | [super dealloc]; |
| 9 | } |
| 10 | @end |
| 11 | |
| 12 | @implementation SkOptionListItem |
| 13 | @synthesize fOptions; |
| 14 | - (void)dealloc { |
| 15 | [fOptions release]; |
| 16 | [super dealloc]; |
| 17 | } |
| 18 | @end |
| 19 | |
| 20 | @implementation SkOptionsTableViewController |
| 21 | |
| 22 | @synthesize fItems, fCurrentList; |
| 23 | |
| 24 | - (id)initWithStyle:(UITableViewStyle)style { |
| 25 | self = [super initWithStyle:style]; |
| 26 | if (self) { |
| 27 | self.fItems = [NSMutableArray array]; |
| 28 | } |
| 29 | return self; |
| 30 | } |
| 31 | |
| 32 | //SkUIViewOptionsDelegate |
| 33 | - (void) view:(SkUIView*)view didAddMenu:(const SkOSMenu*)menu {} |
| 34 | - (void) view:(SkUIView*)view didUpdateMenu:(const SkOSMenu*)menu { |
| 35 | [self updateMenu:menu]; |
| 36 | } |
| 37 | |
| 38 | - (NSUInteger)convertPathToIndex:(NSIndexPath*)path { |
| 39 | NSUInteger index = 0; |
| 40 | for (NSInteger i = 0; i < path.section; ++i) { |
| 41 | index += (*fMenus)[i]->countItems(); |
| 42 | } |
| 43 | return index + path.row; |
| 44 | } |
| 45 | |
| 46 | - (void)registerMenus:(const SkTDArray<SkOSMenu*>*)menus { |
| 47 | fMenus = menus; |
| 48 | for (NSUInteger i = 0; i < fMenus->count(); ++i) { |
| 49 | [self loadMenu:(*fMenus)[i]]; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | - (void)updateMenu:(SkOSMenu*)menu { |
| 54 | // the first menu is always assumed to be the static, the second is |
| 55 | // repopulated every time over and over again |
| 56 | int menuIndex = fMenus->find(menu); |
| 57 | if (menuIndex >= 0 && menuIndex < fMenus->count()) { |
| 58 | NSUInteger first = 0; |
| 59 | for (NSInteger i = 0; i < menuIndex; ++i) { |
| 60 | first += (*fMenus)[i]->countItems(); |
| 61 | } |
| 62 | [fItems removeObjectsInRange:NSMakeRange(first, [fItems count] - first)]; |
| 63 | [self loadMenu:menu]; |
| 64 | } |
| 65 | [self.tableView reloadData]; |
| 66 | } |
| 67 | |
| 68 | - (void)loadMenu:(const SkOSMenu*)menu { |
| 69 | for (int i = 0; i < menu->countItems(); ++i) { |
| 70 | const SkOSMenu::Item* item = menu->getItem(i); |
| 71 | NSString* title = [NSString stringWithUTF8String:item->getLabel()]; |
| 72 | |
| 73 | int index = 0; |
| 74 | NSArray* optionstrs = nil; |
| 75 | if (SkOSMenu::kList_Type == item->getType()) { |
| 76 | SkOptionListItem* List = [[SkOptionListItem alloc] init]; |
| 77 | //List.fCmdID = item->fOSCmd; |
| 78 | //List.getEvent() = item->getEvent(); |
| 79 | List.fItem = item; |
| 80 | List.fOptions = [[SkOptionListController alloc] initWithStyle:UITableViewStyleGrouped]; |
| 81 | |
| 82 | NSArray* optionstrs = [[NSString stringWithUTF8String:item->getEvent()->findString(SkOSMenu::List_Items_Str)] |
| 83 | componentsSeparatedByString:[NSString stringWithUTF8String:SkOSMenu::Delimiter]]; |
| 84 | for (NSString* optionstr in optionstrs) { |
| 85 | [List.fOptions addOption:optionstr]; |
| 86 | } |
| 87 | item->getEvent()->findS32(item->getSlotName(), &index); |
| 88 | List.fOptions.fSelectedIndex = index; |
| 89 | List.fCell = [self createList:title |
| 90 | default:[List.fOptions getSelectedOption]]; |
| 91 | List.fOptions.fParentCell = List.fCell; |
| 92 | [fItems addObject:List]; |
| 93 | [List release]; |
| 94 | } |
| 95 | else { |
| 96 | SkOptionItem* option = [[SkOptionItem alloc] init]; |
| 97 | option.fItem = item; |
| 98 | bool state = false; |
| 99 | switch (item->getType()) { |
| 100 | case SkOSMenu::kAction_Type: |
| 101 | option.fCell = [self createAction:title]; |
| 102 | break; |
| 103 | case SkOSMenu::kSwitch_Type: |
| 104 | item->getEvent()->findBool(item->getSlotName(), &state); |
| 105 | option.fCell = [self createSwitch:title default:(BOOL)state]; |
| 106 | break; |
| 107 | case SkOSMenu::kSlider_Type: |
| 108 | SkScalar min, max, value; |
| 109 | item->getEvent()->findScalar(SkOSMenu::Slider_Min_Scalar, &min); |
| 110 | item->getEvent()->findScalar(SkOSMenu::Slider_Max_Scalar, &max); |
| 111 | item->getEvent()->findScalar(item->getSlotName(), &value); |
| 112 | option.fCell = [self createSlider:title |
| 113 | min:min |
| 114 | max:max |
| 115 | default:value]; |
| 116 | break; |
| 117 | case SkOSMenu::kTriState_Type: |
| 118 | item->getEvent()->findS32(item->getSlotName(), &index); |
| 119 | option.fCell = [self createTriState:title default:index]; |
| 120 | break; |
| 121 | case SkOSMenu::kTextField_Type: |
| 122 | option.fCell = [self createTextField:title |
| 123 | default:item->getEvent()->findString(item->getSlotName())]; |
| 124 | break; |
| 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | [fItems addObject:option]; |
| 129 | [option release]; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | - (void)valueChanged:(id)sender { |
| 135 | UITableViewCell* cell = (UITableViewCell*)(((UIView*)sender).superview); |
| 136 | NSUInteger index = [self convertPathToIndex:[self.tableView indexPathForCell:cell]]; |
| 137 | SkOptionItem* item = (SkOptionItem*)[fItems objectAtIndex:index]; |
| 138 | if ([sender isKindOfClass:[UISlider class]]) {//Slider |
| 139 | UISlider* slider = (UISlider *)sender; |
| 140 | cell.detailTextLabel.text = [NSString stringWithFormat:@"%1.1f", slider.value]; |
| 141 | item.fItem->postEventWithScalar(slider.value); |
| 142 | } |
| 143 | else if ([sender isKindOfClass:[UISwitch class]]) {//Switch |
| 144 | UISwitch* switch_ = (UISwitch *)sender; |
| 145 | item.fItem->postEventWithBool(switch_.on); |
| 146 | } |
| 147 | else if ([sender isKindOfClass:[UITextField class]]) { //TextField |
| 148 | UITextField* textField = (UITextField *)sender; |
| 149 | [textField resignFirstResponder]; |
| 150 | item.fItem->postEventWithString([textField.text UTF8String]); |
| 151 | } |
| 152 | else if ([sender isKindOfClass:[UISegmentedControl class]]) { //Action |
| 153 | UISegmentedControl* segmented = (UISegmentedControl *)sender; |
| 154 | item.fItem->postEventWithInt((2 == segmented.selectedSegmentIndex) ? |
| 155 | SkOSMenu::kMixedState : |
| 156 | segmented.selectedSegmentIndex); |
| 157 | } |
| 158 | else{ |
| 159 | NSLog(@"unknown"); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | - (UITableViewCell*)createAction:(NSString*)title { |
| 164 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 165 | initWithStyle:UITableViewCellStyleValue1 |
| 166 | reuseIdentifier:nil] autorelease]; |
| 167 | cell.textLabel.text = title; |
| 168 | return cell; |
| 169 | } |
| 170 | |
| 171 | - (UITableViewCell*)createSwitch:(NSString*)title default:(BOOL)state { |
| 172 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 173 | initWithStyle:UITableViewCellStyleValue1 |
| 174 | reuseIdentifier:nil] autorelease]; |
| 175 | cell.textLabel.text = title; |
| 176 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 177 | UISwitch* switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; |
| 178 | [switchView setOn:state animated:NO]; |
| 179 | [switchView addTarget:self |
| 180 | action:@selector(valueChanged:) |
| 181 | forControlEvents:UIControlEventValueChanged]; |
| 182 | cell.accessoryView = switchView; |
| 183 | [switchView release]; |
| 184 | return cell; |
| 185 | } |
| 186 | |
| 187 | - (UITableViewCell*)createSlider:(NSString*)title |
| 188 | min:(float)min |
| 189 | max:(float)max |
| 190 | default:(float)value { |
| 191 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 192 | initWithStyle:UITableViewCellStyleValue1 |
| 193 | reuseIdentifier:nil] autorelease]; |
| 194 | cell.textLabel.text = title; |
| 195 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 196 | UISlider* sliderView = [[UISlider alloc] init]; |
| 197 | sliderView.value = value; |
| 198 | sliderView.minimumValue = min; |
| 199 | sliderView.maximumValue = max; |
| 200 | [sliderView addTarget:self |
| 201 | action:@selector(valueChanged:) |
| 202 | forControlEvents:UIControlEventValueChanged]; |
| 203 | cell.detailTextLabel.text = [NSString stringWithFormat:@"%1.1f", value]; |
| 204 | cell.accessoryView = sliderView; |
| 205 | [sliderView release]; |
| 206 | return cell; |
| 207 | } |
| 208 | |
| 209 | - (UITableViewCell*)createTriState:(NSString*)title default:(int)index { |
| 210 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 211 | initWithStyle:UITableViewCellStyleValue1 |
| 212 | reuseIdentifier:nil] autorelease]; |
| 213 | cell.textLabel.text = title; |
| 214 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 215 | NSArray* items = [NSArray arrayWithObjects:@"Off", @"On", @"Mixed", nil]; |
| 216 | UISegmentedControl* segmented = [[UISegmentedControl alloc] initWithItems:items]; |
| 217 | segmented.selectedSegmentIndex = (index == -1) ? 2 : index; |
| 218 | segmented.segmentedControlStyle = UISegmentedControlStyleBar; |
| 219 | [segmented addTarget:self |
| 220 | action:@selector(valueChanged:) |
| 221 | forControlEvents:UIControlEventValueChanged]; |
| 222 | cell.accessoryView = segmented; |
| 223 | [segmented release]; |
| 224 | return cell; |
| 225 | } |
| 226 | |
| 227 | - (UITableViewCell*)createTextField:(NSString*)title |
| 228 | default:(const char*)value { |
| 229 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 230 | initWithStyle:UITableViewCellStyleValue1 |
| 231 | reuseIdentifier:nil] autorelease]; |
| 232 | cell.textLabel.text = title; |
| 233 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 234 | UITextField* textField = [[UITextField alloc] |
| 235 | initWithFrame:CGRectMake(0, 10, 150, 25)]; |
| 236 | textField.adjustsFontSizeToFitWidth = YES; |
| 237 | textField.textAlignment = UITextAlignmentRight; |
| 238 | textField.textColor = cell.detailTextLabel.textColor; |
| 239 | textField.placeholder = [NSString stringWithUTF8String:value]; |
| 240 | textField.returnKeyType = UIReturnKeyDone; |
| 241 | [textField addTarget:self |
| 242 | action:@selector(valueChanged:) |
| 243 | forControlEvents:UIControlEventEditingDidEndOnExit]; |
| 244 | cell.accessoryView = textField; |
| 245 | [textField release]; |
| 246 | return cell; |
| 247 | } |
| 248 | |
| 249 | - (UITableViewCell*)createList:(NSString*)title default:(NSString*)value{ |
| 250 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 251 | initWithStyle:UITableViewCellStyleValue1 |
| 252 | reuseIdentifier:nil] autorelease]; |
| 253 | cell.textLabel.text = title; |
| 254 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 255 | cell.detailTextLabel.text = value; |
| 256 | return cell; |
| 257 | } |
| 258 | |
| 259 | #pragma mark - |
| 260 | #pragma mark Table view data source |
| 261 | |
| 262 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 263 | return fMenus->count(); |
| 264 | } |
| 265 | |
| 266 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 267 | return [NSString stringWithUTF8String:(*fMenus)[section]->getTitle()]; |
| 268 | } |
| 269 | |
| 270 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 271 | return (*fMenus)[section]->countItems(); |
| 272 | } |
| 273 | |
| 274 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 275 | return ((SkOptionItem*)[fItems objectAtIndex:[self convertPathToIndex:indexPath]]).fCell; |
| 276 | } |
| 277 | |
| 278 | #pragma mark - |
| 279 | #pragma mark Table view delegate |
| 280 | |
| 281 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 282 | UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; |
| 283 | id item = [fItems objectAtIndex:indexPath.row]; |
| 284 | |
| 285 | if ([item isKindOfClass:[SkOptionListItem class]]) { |
| 286 | SkOptionListItem* list = (SkOptionListItem*)item; |
| 287 | self.fCurrentList = list; |
| 288 | self.navigationController.delegate = self; |
| 289 | [self.navigationController pushViewController:list.fOptions animated:YES]; |
| 290 | } |
| 291 | else if ([item isKindOfClass:[SkOptionItem class]]) { |
| 292 | if (UITableViewCellSelectionStyleNone != cell.selectionStyle) { //Actions |
| 293 | SkOptionItem* action = (SkOptionItem*)item; |
| 294 | action.fItem->postEvent(); |
| 295 | } |
| 296 | } |
| 297 | else{ |
| 298 | NSLog(@"unknown"); |
| 299 | } |
| 300 | |
| 301 | [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 302 | } |
| 303 | |
| 304 | #pragma mark - |
| 305 | #pragma mark Navigation controller delegate |
| 306 | |
| 307 | - (void)navigationController:(UINavigationController *)navigationController |
| 308 | willShowViewController:(UIViewController *)viewController |
| 309 | animated:(BOOL)animated { |
| 310 | if (self == viewController) { //when a List option is popped, trigger event |
| 311 | NSString* selectedOption = [fCurrentList.fOptions getSelectedOption]; |
| 312 | fCurrentList.fCell.detailTextLabel.text = selectedOption; |
| 313 | fCurrentList.fItem->postEventWithInt(fCurrentList.fOptions.fSelectedIndex); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | #pragma mark - |
| 318 | #pragma mark Memory management |
| 319 | |
| 320 | - (void)dealloc { |
| 321 | self.fItems = nil; |
| 322 | [super dealloc]; |
| 323 | } |
| 324 | |
| 325 | @end |