project_files/HedgewarsMobile/Classes/ObjcExports.m
author koda
Wed, 02 Mar 2011 00:27:20 +0100
changeset 4976 088d40d8aba2
parent 4948 c3dc41ae68fa
child 5002 a9c44a8ffec8
permissions -rw-r--r--
Happy 2011 :)

/*
 * Hedgewars-iOS, a Hedgewars port for iOS devices
 * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * File created on 30/10/2010.
 */


#import "ObjcExports.h"
#import "AmmoMenuViewController.h"
#import "AudioToolbox/AudioToolbox.h"

#pragma mark -
#pragma mark internal variables
// actual game started (controls should be enabled)
BOOL gameRunning;
// black screen present
BOOL savedGame;
// cache the grenade time
NSInteger grenadeTime;
// the reference to the newMenu instance
AmmoMenuViewController *amvc_instance;
// the audiosession must be initialized before using properties
BOOL gAudioSessionInited = NO;

#pragma mark -
#pragma mark functions called like oop
void objcExportsInit() {
    gameRunning = NO;
    savedGame = NO;
    grenadeTime = 2;
}

BOOL inline isGameRunning() {
    return gameRunning;
}

void inline setGameRunning(BOOL value) {
    gameRunning = value;
}

NSInteger cachedGrenadeTime() {
    return grenadeTime;
}

void inline setGrenadeTime(NSInteger value) {
    grenadeTime = value;
}

void inline setAmmoMenuInstance(AmmoMenuViewController *instance) {
    amvc_instance = instance;
}

#pragma mark -
#pragma mark functions called by pascal code
void startSpinning() {
    gameRunning = NO;
    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicator.tag = ACTIVITYINDICATOR_TAG;
    int offset;
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
        offset = -120;
    else
        offset = 120;
    if (IS_DUALHEAD())
        indicator.center = CGPointMake(theWindow.frame.size.width/2, theWindow.frame.size.height/2 + offset);
    else
        indicator.center = CGPointMake(theWindow.frame.size.width/2 + offset, theWindow.frame.size.height/2);
    indicator.hidesWhenStopped = YES;
    [indicator startAnimating];
    [theWindow addSubview:indicator];
    [indicator release];
}

void stopSpinning() {
    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
    UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[theWindow viewWithTag:ACTIVITYINDICATOR_TAG];
    [indicator stopAnimating];
    HW_zoomSet(1.7);
    if (savedGame == NO)
        gameRunning = YES;
}

void clearView() {
    // don't use any engine calls here as this function is called every time the ammomenu is opened
    UIWindow *theWindow = (IS_DUALHEAD()) ? [SDLUIKitDelegate sharedAppDelegate].uiwindow : [[UIApplication sharedApplication] keyWindow];
    UIButton *theButton = (UIButton *)[theWindow viewWithTag:CONFIRMATION_TAG];
    UISegmentedControl *theSegment = (UISegmentedControl *)[theWindow viewWithTag:GRENADE_TAG];

    [UIView beginAnimations:@"remove button" context:NULL];
    [UIView setAnimationDuration:ANIMATION_DURATION];
    theButton.alpha = 0;
    theSegment.alpha = 0;
    [UIView commitAnimations];

    if (theButton)
        [theWindow performSelector:@selector(removeFromSuperview) withObject:theButton afterDelay:ANIMATION_DURATION];
    if (theSegment)
        [theWindow performSelector:@selector(removeFromSuperview) withObject:theSegment afterDelay:ANIMATION_DURATION];

    grenadeTime = 2;
}

void replayBegan() {
    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *blackView = [[UIView alloc] initWithFrame:theWindow.frame];
    blackView.backgroundColor = [UIColor blackColor];
    blackView.alpha = 0.6;
    blackView.tag = REPLAYBLACKVIEW_TAG;
    blackView.exclusiveTouch = NO;
    blackView.multipleTouchEnabled = NO;
    blackView.userInteractionEnabled = NO;

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicator.center = theWindow.center;
    [indicator startAnimating];
    [blackView addSubview:indicator];
    [indicator release];
    [theWindow addSubview:blackView];
    [blackView release];

    savedGame = YES;
    stopSpinning();
}

void replayFinished() {
    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *blackView = (UIView *)[theWindow viewWithTag:REPLAYBLACKVIEW_TAG];

    [UIView beginAnimations:@"removing black" context:NULL];
    [UIView setAnimationDuration:1];
    blackView.alpha = 0;
    [UIView commitAnimations];
    [theWindow performSelector:@selector(removeFromSuperview) withObject:blackView afterDelay:1];

    gameRunning = YES;
    savedGame = NO;
}

void updateVisualsNewTurn(void) {
    [amvc_instance updateAmmoVisuals];
}