Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 242 lines (183 sloc) 8.556 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
//
// GameObjectSelectionViewController.m
// RPG Kit
//
// Created by Philip Regan on 4/18/12.
// Copyright (c) 2012 Jones & Bartlett Learning. All rights reserved.
//

#import "GameObjectSelectionViewController.h"
#import "TaskMetadataViewController.h"

@interface GameObjectSelectionViewController ()

@end

@implementation GameObjectSelectionViewController

@synthesize gameDataController = _gameDataController;
@synthesize components = _components;
@synthesize targetEntity = _targetEntity;
@synthesize targetRelationship = _targetRelationship;
@synthesize targetParent = _targetParent;
@synthesize tableView = _tableView;

#pragma mark - Object Lifecycle Stack

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark NavigationController Stack

-(void) viewWillAppear:(BOOL)animated {
    // manage the view
    self.title = [self.gameDataController.currentUniverse valueForKey:OCAttributeTitleStory];
    self.navigationController.delegate = self;
    
    /*
we have to assess the intent of this instance's use based on what the previous
view controller put in place.
*/
    
    if ( self.targetRelationship != nil ) {
        if ( [self.targetRelationship isEqualToString:OCRelationshipQuests] ) {
            self.components = [self.gameDataController retrieveQuests];
        }
        // add hook for accomplishments, which means this should be factored out to
        // managing a target entity, relationship, and parent
    } else {
        if ( [self.targetEntity isEqualToString:OCEntityTask] ) {
            self.components = [self.gameDataController retrieveTasks];
        } else if ( [self.targetEntity isEqualToString:OCEntityTask] ) {
            self.components = [self.gameDataController retrieveTasks];
        }
    }
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    NSString *localizedString = @"";
    
    // update the back button according to the type of object being selected
    
    if ( self.targetRelationship != nil ) {
        if ( [self.targetRelationship isEqualToString:OCRelationshipQuests] ) {
            
            localizedString = NSLocalizedString(@"OCGameSelectionBackButtonTasks", @"");
        }
    } else {
        if ( [self.targetEntity isEqualToString:OCEntityTask] ) {
            
            localizedString = NSLocalizedString(@"OCGameSelectionBackButtonTasks", @"");
            
        } else if ( [self.targetEntity isEqualToString:OCEntityTask] ) {
            //self.components = [self.gameDataController retrieveTasks];
        }
    }
    
    // TODO: Add hook for Equipment CANCELLED
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(localizedString, @"")
                                                                             style:UIBarButtonItemStyleBordered
                                                                            target:nil
                                                                            action:nil];
}

#pragma mark - Table View Stack

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.components count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ObjectSelectionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if ( cell == nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    // content population typical of a game object
    cell.textLabel.text = [[self.components objectAtIndex:indexPath.row] valueForKey:OCAttributeTitleStory];
    cell.detailTextLabel.text = [[self.components objectAtIndex:indexPath.row] valueForKey:OCAttributeIntroStory];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    
    // check to see if this is an in-game object that should not be deleted
    if ( [[[self.components objectAtIndex:indexPath.row] valueForKey:OCAttributeInGameObject] boolValue] ) {
        return NO;
    }
    
    return YES;
}
 


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    /*
the method for doing this is exactly the same as it is with deleting games in
MainViewController
*/
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // capture the universe to be deleted
        NSManagedObject *targetTask = [self.components objectAtIndex:indexPath.row];
        
        // delete the object
        if ( [self.gameDataController deleteTask:targetTask] ) {
            
            // update the data source for the table view
            [self.components removeObjectAtIndex:indexPath.row];
            
            // update the rows
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            
        } else {
            
            // something bad happened
            NSLog(@"ERROR: MainViewController:delete task: NO");
        }
    }
    
    [self.tableView setEditing:NO animated:YES];
    
}
 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ( self.targetRelationship != nil ) {
        if ( [self.targetRelationship isEqualToString:OCRelationshipQuests] ) {
            
            TaskMetadataViewController *taskMetadataViewController = [[TaskMetadataViewController alloc] initWithNibName:@"TaskMetadataViewController" bundle:nil];
            taskMetadataViewController.gameDataController = self.gameDataController;
            taskMetadataViewController.targetTask = [self.components objectAtIndex:indexPath.row];
            [self.navigationController pushViewController:taskMetadataViewController animated:YES];
            
        }
        // add hook for accomplishments, which means this should be factored out to
        // managing a target entity, relationship, and parent
    } else {
        if ( [self.targetEntity isEqualToString:OCEntityTask] ) {
            
            TaskMetadataViewController *taskMetadataViewController = [[TaskMetadataViewController alloc] initWithNibName:@"TaskMetadataViewController" bundle:nil];
            taskMetadataViewController.gameDataController = self.gameDataController;
            taskMetadataViewController.targetTask = [self.components objectAtIndex:indexPath.row];
            [self.navigationController pushViewController:taskMetadataViewController animated:YES];

        } else if ( [self.targetEntity isEqualToString:OCEntityTask] ) {
            //self.components = [self.gameDataController retrieveTasks];
        }
    }
    
}

#pragma mark - Action Stack

- (IBAction)newButton:(UIBarButtonItem *)sender {
    if ( [self.gameDataController createNewTask] ) {
        self.components = [self.gameDataController retrieveTasks];
        [self.tableView reloadData];
    } else {
        // something bad happened
        NSLog(@"ERROR: MainViewController:newTask: NO");
    }
}

- (IBAction)editButton:(UIBarButtonItem *)sender {
    if ( self.tableView.editing ) {
        [self.tableView setEditing:NO animated:YES];
    } else {
        [self.tableView setEditing:YES animated:YES];
    }
}
@end
Something went wrong with that request. Please try again.