Newer
Older
//
// DetailViewController.m
// Onelab
//
// Created by Maxime Graulich on 08/04/13.
// Copyright (c) 2013 Maxime Graulich. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "iosGModel.h"
#import "AppDelegate.h"
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end
#pragma mark - Managing the detail item
@synthesize glView;
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
-(void)viewDidAppear:(BOOL)animated
{
if(self.initialModel != nil) [self.glView load:self.initialModel];
//[self.initialModel release];
self.initialModel = nil;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
scaleFactor = 1.;
setObjCBridge((__bridge void*) self);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestRender) name:@"requestRender" object:nil];
if([[UIDevice currentDevice].model isEqualToString:@"iPad"] || [[UIDevice currentDevice].model isEqualToString:@"iPad Simulator"]){
UIBarButtonItem *model = [[UIBarButtonItem alloc] initWithTitle:@"Models list" style:UIBarButtonItemStyleBordered target:self action:@selector(showModelsList)];
[self.navigationItem setRightBarButtonItem:model];
else
{
UIBarButtonItem *settings = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered target:self action:@selector(showSettings)];
[self.navigationItem setRightBarButtonItem:settings];
}
}
- (IBAction)pinch:(UIPinchGestureRecognizer *)sender
{
float mScale = scaleFactor;
if (sender.state == UIGestureRecognizerStateBegan)
mScale = scaleFactor;
else if(sender.state == UIGestureRecognizerStateChanged)
mScale = scaleFactor * [sender scale];
else if(sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled){
scaleFactor *= [sender scale];
mScale = scaleFactor;
}
mScale = MAX(0.1, mScale);
glView->mContext->eventHandler(2,mScale);
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
glView->mContext->eventHandler(0, touchPoint.x, touchPoint.y);
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
glView->mContext->eventHandler(4, touchPoint.x, touchPoint.y);
}
- (IBAction)tap:(UITapGestureRecognizer *)sender
{
sender.numberOfTapsRequired = 2;
sender.numberOfTouchesRequired = 1;
if(sender.state == UIGestureRecognizerStateEnded){
scaleFactor = 1;
[glView drawView];
}
}
- (void) showModelsList
{
if(((AppDelegate *)[UIApplication sharedApplication].delegate)->compute) {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@"Can't show the models list" message:@"The compute have to be finished before you can select an other model." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
return;
}
if([[UIDevice currentDevice].model isEqualToString:@"iPad"] || [[UIDevice currentDevice].model isEqualToString:@"iPad Simulator"]){
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[UIView transitionWithView:appDelegate.window
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{ appDelegate.window.rootViewController = appDelegate.modelListController; }
completion:nil];
}
[self.navigationController popToRootViewControllerAnimated:YES];
[self performSegueWithIdentifier:@"showSettingsSegue" sender:self];
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
-(void) showAlert:(std::string)msg title:(std::string) title
{
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithCString:title.c_str() encoding:[NSString defaultCStringEncoding]] message:[NSString stringWithCString:msg.c_str() encoding:[NSString defaultCStringEncoding]] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
- (void)viewDidUnload
{
[self setGlView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)requestRender
{
[glView drawView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = NSLocalizedString(@"Settings", @"Settings");
[self.navigationItem setLeftBarButtonItem:barButtonItem];
self.masterPopoverController = popoverController;
}
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
#pragma mark - actionsheet
-(void)showMore: (UIBarButtonItem*)sender
{
UIActionSheet *popupMore = [[UIActionSheet alloc] initWithTitle:@"Other settings" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:
(glView->mContext->isShowedMesh())?@"Hide mesh":@"Show mesh",
(glView->mContext->isShowedGeom())?@"Hide geometry" :@"Show geometry",
@"Set X view",
@"Set Y view",
@"Set Z view",
nil];
[popupMore showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *text = [actionSheet buttonTitleAtIndex:buttonIndex];
if([text isEqualToString:@"Hide mesh"])
else if([text isEqualToString:@"Show mesh"])
else if([text isEqualToString:@"Hide geometry"])
else if([text isEqualToString:@"Show geometry"])
else if([text isEqualToString:@"Set X view"]){
glView->mContext->eventHandler(5);
}
else if([text isEqualToString:@"Set Y view"]){
glView->mContext->eventHandler(6);
}
else if([text isEqualToString:@"Set Z view"]){
glView->mContext->eventHandler(7);
}
[glView drawView];
}
void messageFromCpp (void *self, std::string level, std::string msg)
{
if(level == "RequestRender"){
[(__bridge id)self requestRender];
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshParameters" object:nil];
}
else if(level == "Error")
void getBitmap(void *self, const char *text, int textsize, unsigned char **map, int *height, int *width, int *realWidth)
[(__bridge id)self getBitmapFromStringObjC:text withTextSize:textsize inMap:map inHeight:height inWidth:width inRealWidth:realWidth];
-(void) getBitmapFromStringObjC:(const char *)text withTextSize:(int)textsize inMap:(unsigned char **)map inHeight:(int *)height inWidth:(int *)width inRealWidth:(int *) realWidth
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1024, textsize)];
[lbl setText:[NSString stringWithCString:text encoding:[NSString defaultCStringEncoding]]];
[lbl setBackgroundColor:[UIColor clearColor]];
CGSize lblSize = [[lbl text] sizeWithFont:[lbl font]];
*realWidth = lblSize.width;
int i;
for(i=2;i<*realWidth;i*=2); *width = i;
*height = lblSize.height;
for(i=2;i<*height;i*=2); *height = i;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(*width, *height), NO, 0.0);
[lbl.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
CGImageRef bitmap = [img CGImage];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(*height * *width * 4, sizeof(unsigned char));
*map = (unsigned char*) calloc(*height * *width, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * *width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, *width, *height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, *width, *height), bitmap);
CGContextRelease(context);
// rawData contains the image data in the RGBA8888 pixel format.
for (int byteIndex = 0 ; byteIndex < *width * *height * 4 ; byteIndex+=4)
free(rawData);