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) { |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 41 | index += (*fMenus)[i]->getCount(); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 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) { |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 60 | first += (*fMenus)[i]->getCount(); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 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 { |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 69 | const SkOSMenu::Item* menuitems[menu->getCount()]; |
| 70 | menu->getItems(menuitems); |
| 71 | for (int i = 0; i < menu->getCount(); ++i) { |
| 72 | const SkOSMenu::Item* item = menuitems[i]; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 73 | NSString* title = [NSString stringWithUTF8String:item->getLabel()]; |
| 74 | |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 75 | if (SkOSMenu::kList_Type == item->getType()) { |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 76 | int value = 0; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 77 | SkOptionListItem* List = [[SkOptionListItem alloc] init]; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 78 | |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 79 | List.fItem = item; |
| 80 | List.fOptions = [[SkOptionListController alloc] initWithStyle:UITableViewStyleGrouped]; |
| 81 | |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 82 | int count = 0; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 83 | SkOSMenu::FindListItemCount(*item->getEvent(), &count); |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 84 | SkString options[count]; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 85 | SkOSMenu::FindListItems(*item->getEvent(), options); |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 86 | for (int i = 0; i < count; ++i) |
| 87 | [List.fOptions addOption:[NSString stringWithUTF8String:options[i].c_str()]]; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 88 | SkOSMenu::FindListIndex(*item->getEvent(), item->getSlotName(), &value); |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 89 | |
| 90 | List.fOptions.fSelectedIndex = value; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 91 | List.fCell = [self createList:title |
| 92 | default:[List.fOptions getSelectedOption]]; |
| 93 | List.fOptions.fParentCell = List.fCell; |
| 94 | [fItems addObject:List]; |
| 95 | [List release]; |
| 96 | } |
| 97 | else { |
| 98 | SkOptionItem* option = [[SkOptionItem alloc] init]; |
| 99 | option.fItem = item; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 100 | |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 101 | bool state = false; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 102 | SkString str; |
| 103 | SkOSMenu::TriState tristate; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 104 | switch (item->getType()) { |
| 105 | case SkOSMenu::kAction_Type: |
| 106 | option.fCell = [self createAction:title]; |
| 107 | break; |
| 108 | case SkOSMenu::kSwitch_Type: |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 109 | SkOSMenu::FindSwitchState(*item->getEvent(), item->getSlotName(), &state); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 110 | option.fCell = [self createSwitch:title default:(BOOL)state]; |
| 111 | break; |
| 112 | case SkOSMenu::kSlider_Type: |
| 113 | SkScalar min, max, value; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 114 | SkOSMenu::FindSliderValue(*item->getEvent(), item->getSlotName(), &value); |
| 115 | SkOSMenu::FindSliderMin(*item->getEvent(), &min); |
| 116 | SkOSMenu::FindSliderMax(*item->getEvent(), &max); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 117 | option.fCell = [self createSlider:title |
| 118 | min:min |
| 119 | max:max |
| 120 | default:value]; |
| 121 | break; |
| 122 | case SkOSMenu::kTriState_Type: |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 123 | SkOSMenu::FindTriState(*item->getEvent(), item->getSlotName(), &tristate); |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 124 | option.fCell = [self createTriState:title default:(int)tristate]; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 125 | break; |
| 126 | case SkOSMenu::kTextField_Type: |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 127 | SkOSMenu::FindText(*item->getEvent(), item->getSlotName(), &str); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 128 | option.fCell = [self createTextField:title |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 129 | default:[NSString stringWithUTF8String:str.c_str()]]; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 130 | break; |
| 131 | default: |
| 132 | break; |
| 133 | } |
| 134 | [fItems addObject:option]; |
| 135 | [option release]; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | - (void)valueChanged:(id)sender { |
| 141 | UITableViewCell* cell = (UITableViewCell*)(((UIView*)sender).superview); |
| 142 | NSUInteger index = [self convertPathToIndex:[self.tableView indexPathForCell:cell]]; |
| 143 | SkOptionItem* item = (SkOptionItem*)[fItems objectAtIndex:index]; |
| 144 | if ([sender isKindOfClass:[UISlider class]]) {//Slider |
| 145 | UISlider* slider = (UISlider *)sender; |
| 146 | cell.detailTextLabel.text = [NSString stringWithFormat:@"%1.1f", slider.value]; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 147 | item.fItem->setScalar(slider.value); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 148 | } |
| 149 | else if ([sender isKindOfClass:[UISwitch class]]) {//Switch |
| 150 | UISwitch* switch_ = (UISwitch *)sender; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 151 | item.fItem->setBool(switch_.on); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 152 | } |
| 153 | else if ([sender isKindOfClass:[UITextField class]]) { //TextField |
| 154 | UITextField* textField = (UITextField *)sender; |
| 155 | [textField resignFirstResponder]; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 156 | item.fItem->setString([textField.text UTF8String]); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 157 | } |
| 158 | else if ([sender isKindOfClass:[UISegmentedControl class]]) { //Action |
| 159 | UISegmentedControl* segmented = (UISegmentedControl *)sender; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 160 | SkOSMenu::TriState state; |
| 161 | if (2 == segmented.selectedSegmentIndex) { |
| 162 | state = SkOSMenu::kMixedState; |
| 163 | } else { |
| 164 | state = (SkOSMenu::TriState)segmented.selectedSegmentIndex; |
| 165 | } |
| 166 | item.fItem->setTriState(state); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 167 | } |
| 168 | else{ |
| 169 | NSLog(@"unknown"); |
| 170 | } |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 171 | item.fItem->postEvent(); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | - (UITableViewCell*)createAction:(NSString*)title { |
| 175 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 176 | initWithStyle:UITableViewCellStyleValue1 |
| 177 | reuseIdentifier:nil] autorelease]; |
| 178 | cell.textLabel.text = title; |
| 179 | return cell; |
| 180 | } |
| 181 | |
| 182 | - (UITableViewCell*)createSwitch:(NSString*)title default:(BOOL)state { |
| 183 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 184 | initWithStyle:UITableViewCellStyleValue1 |
| 185 | reuseIdentifier:nil] autorelease]; |
| 186 | cell.textLabel.text = title; |
| 187 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 188 | UISwitch* switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; |
| 189 | [switchView setOn:state animated:NO]; |
| 190 | [switchView addTarget:self |
| 191 | action:@selector(valueChanged:) |
| 192 | forControlEvents:UIControlEventValueChanged]; |
| 193 | cell.accessoryView = switchView; |
| 194 | [switchView release]; |
| 195 | return cell; |
| 196 | } |
| 197 | |
| 198 | - (UITableViewCell*)createSlider:(NSString*)title |
| 199 | min:(float)min |
| 200 | max:(float)max |
| 201 | default:(float)value { |
| 202 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 203 | initWithStyle:UITableViewCellStyleValue1 |
| 204 | reuseIdentifier:nil] autorelease]; |
| 205 | cell.textLabel.text = title; |
| 206 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 207 | UISlider* sliderView = [[UISlider alloc] init]; |
| 208 | sliderView.value = value; |
| 209 | sliderView.minimumValue = min; |
| 210 | sliderView.maximumValue = max; |
| 211 | [sliderView addTarget:self |
| 212 | action:@selector(valueChanged:) |
| 213 | forControlEvents:UIControlEventValueChanged]; |
| 214 | cell.detailTextLabel.text = [NSString stringWithFormat:@"%1.1f", value]; |
| 215 | cell.accessoryView = sliderView; |
| 216 | [sliderView release]; |
| 217 | return cell; |
| 218 | } |
| 219 | |
| 220 | - (UITableViewCell*)createTriState:(NSString*)title default:(int)index { |
| 221 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 222 | initWithStyle:UITableViewCellStyleValue1 |
| 223 | reuseIdentifier:nil] autorelease]; |
| 224 | cell.textLabel.text = title; |
| 225 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 226 | NSArray* items = [NSArray arrayWithObjects:@"Off", @"On", @"Mixed", nil]; |
| 227 | UISegmentedControl* segmented = [[UISegmentedControl alloc] initWithItems:items]; |
| 228 | segmented.selectedSegmentIndex = (index == -1) ? 2 : index; |
| 229 | segmented.segmentedControlStyle = UISegmentedControlStyleBar; |
| 230 | [segmented addTarget:self |
| 231 | action:@selector(valueChanged:) |
| 232 | forControlEvents:UIControlEventValueChanged]; |
| 233 | cell.accessoryView = segmented; |
| 234 | [segmented release]; |
| 235 | return cell; |
| 236 | } |
| 237 | |
| 238 | - (UITableViewCell*)createTextField:(NSString*)title |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 239 | default:(NSString*)value { |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 240 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 241 | initWithStyle:UITableViewCellStyleValue1 |
| 242 | reuseIdentifier:nil] autorelease]; |
| 243 | cell.textLabel.text = title; |
| 244 | cell.selectionStyle = UITableViewCellSelectionStyleNone; |
| 245 | UITextField* textField = [[UITextField alloc] |
| 246 | initWithFrame:CGRectMake(0, 10, 150, 25)]; |
| 247 | textField.adjustsFontSizeToFitWidth = YES; |
| 248 | textField.textAlignment = UITextAlignmentRight; |
| 249 | textField.textColor = cell.detailTextLabel.textColor; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 250 | textField.placeholder = value; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 251 | textField.returnKeyType = UIReturnKeyDone; |
| 252 | [textField addTarget:self |
| 253 | action:@selector(valueChanged:) |
| 254 | forControlEvents:UIControlEventEditingDidEndOnExit]; |
| 255 | cell.accessoryView = textField; |
| 256 | [textField release]; |
| 257 | return cell; |
| 258 | } |
| 259 | |
| 260 | - (UITableViewCell*)createList:(NSString*)title default:(NSString*)value{ |
| 261 | UITableViewCell* cell = [[[UITableViewCell alloc] |
| 262 | initWithStyle:UITableViewCellStyleValue1 |
| 263 | reuseIdentifier:nil] autorelease]; |
| 264 | cell.textLabel.text = title; |
| 265 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 266 | cell.detailTextLabel.text = value; |
| 267 | return cell; |
| 268 | } |
| 269 | |
| 270 | #pragma mark - |
| 271 | #pragma mark Table view data source |
| 272 | |
| 273 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 274 | return fMenus->count(); |
| 275 | } |
| 276 | |
| 277 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 278 | return [NSString stringWithUTF8String:(*fMenus)[section]->getTitle()]; |
| 279 | } |
| 280 | |
| 281 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 282 | return (*fMenus)[section]->getCount(); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 286 | return ((SkOptionItem*)[fItems objectAtIndex:[self convertPathToIndex:indexPath]]).fCell; |
| 287 | } |
| 288 | |
| 289 | #pragma mark - |
| 290 | #pragma mark Table view delegate |
| 291 | |
| 292 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 293 | UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 294 | id item = [fItems objectAtIndex:[self convertPathToIndex:indexPath]]; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 295 | |
| 296 | if ([item isKindOfClass:[SkOptionListItem class]]) { |
| 297 | SkOptionListItem* list = (SkOptionListItem*)item; |
| 298 | self.fCurrentList = list; |
| 299 | self.navigationController.delegate = self; |
| 300 | [self.navigationController pushViewController:list.fOptions animated:YES]; |
| 301 | } |
| 302 | else if ([item isKindOfClass:[SkOptionItem class]]) { |
| 303 | if (UITableViewCellSelectionStyleNone != cell.selectionStyle) { //Actions |
| 304 | SkOptionItem* action = (SkOptionItem*)item; |
| 305 | action.fItem->postEvent(); |
| 306 | } |
| 307 | } |
| 308 | else{ |
| 309 | NSLog(@"unknown"); |
| 310 | } |
| 311 | |
| 312 | [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 313 | } |
| 314 | |
| 315 | #pragma mark - |
| 316 | #pragma mark Navigation controller delegate |
| 317 | |
| 318 | - (void)navigationController:(UINavigationController *)navigationController |
| 319 | willShowViewController:(UIViewController *)viewController |
| 320 | animated:(BOOL)animated { |
| 321 | if (self == viewController) { //when a List option is popped, trigger event |
| 322 | NSString* selectedOption = [fCurrentList.fOptions getSelectedOption]; |
| 323 | fCurrentList.fCell.detailTextLabel.text = selectedOption; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame^] | 324 | fCurrentList.fItem->setInt(fCurrentList.fOptions.fSelectedIndex); |
| 325 | fCurrentList.fItem->postEvent(); |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | #pragma mark - |
| 330 | #pragma mark Memory management |
| 331 | |
| 332 | - (void)dealloc { |
| 333 | self.fItems = nil; |
| 334 | [super dealloc]; |
| 335 | } |
| 336 | |
| 337 | @end |