<commit_before>//
//  UIController.cpp
//  Kepler
//
//  Created by <NAME> on 7/17/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#include "UIController.h"
#include "cinder/app/AppCocoaTouch.h"
#include "cinder/gl/gl.h"
#include "OrientationHelper.h"

using namespace ci;
using namespace ci::app;

UIController::UIController( AppCocoaTouch *app, OrientationHelper *orientationHelper ): mApp(app), mOrientationHelper(orientationHelper)
{
    mParent = UINodeRef(this); // FIXME: shared_from_this() but in a subclass
    mRoot = UIControllerRef(this); // FIXME: shared_from_this() but for a subclass
    // FIXME: should mRoot be static (essentially, should UIController be a singleton?) how/when to clean up? mRoot as weak_ref?
    cbTouchesBegan = mApp->registerTouchesBegan( this, &UIController::touchesBegan );
    cbTouchesMoved = mApp->registerTouchesMoved( this, &UIController::touchesMoved );
    cbTouchesEnded = mApp->registerTouchesEnded( this, &UIController::touchesEnded );
    if (mOrientationHelper) {
        cbOrientationChanged = mOrientationHelper->registerOrientationChanged( this, &UIController::orientationChanged );    
    }
    mOrientationAnimationDuration = 0.25f;
    setInterfaceOrientation( mOrientationHelper->getInterfaceOrientation(), false );
	mInterfaceAngle		= 0.0f;
	mPrevInterfaceAngle = 0.0f;
}

UIController::~UIController()
{
    mApp->unregisterTouchesBegan( cbTouchesBegan );
    mApp->unregisterTouchesMoved( cbTouchesMoved );
    mApp->unregisterTouchesEnded( cbTouchesEnded );
    if (mOrientationHelper) {
        mOrientationHelper->unregisterOrientationChanged( cbOrientationChanged );
    }
}

bool UIController::touchesBegan( TouchEvent event )
{
    bool consumed = true;
    for (std::vector<TouchEvent::Touch>::const_iterator i = event.getTouches().begin(); i != event.getTouches().end(); i++) {
        consumed = privateTouchBegan(*i) && consumed; // recurses to children
    }    
    return consumed; // only true if all touches were consumed
}

bool UIController::touchesMoved( TouchEvent event )
{
    bool consumed = true;
    for (std::vector<TouchEvent::Touch>::const_iterator i = event.getTouches().begin(); i != event.getTouches().end(); i++) {
        consumed = privateTouchMoved(*i) && consumed; // recurses to children
    }
    return consumed; // only true if all touches were consumed
}

bool UIController::touchesEnded( TouchEvent event )
{
    bool consumed = true;
    for (std::vector<TouchEvent::Touch>::const_iterator i = event.getTouches().begin(); i != event.getTouches().end(); i++) {
        consumed = privateTouchEnded(*i) && consumed; // recurses to children
    }    
    return consumed; // only true if all touches were consumed
}

bool UIController::orientationChanged( OrientationEvent event )
{
    setInterfaceOrientation( event.getInterfaceOrientation() );
    return false;
}

void UIController::setInterfaceOrientation( const Orientation &orientation, bool animate )
{
    mInterfaceOrientation = orientation;

    // get the facts
    Vec2f deviceSize = mApp->getWindowSize();
    float orientationAngle = getOrientationAngle(mInterfaceOrientation);
    
	if( mInterfaceAngle < ( -2.0f * M_PI ) ){
		std::cout << "InterfaceAngle is way too low. Being adjusted" << std::endl;
		mInterfaceAngle = -2.0f * M_PI;
	} else if( mInterfaceAngle > ( 2.0f * M_PI ) ){
		std::cout << "InterfaceAngle is way too high. Being adjusted" << std::endl;
		mInterfaceAngle = 2.0f * M_PI;
	}
	
    // normalize interface angle (could be many turns)
    while (mInterfaceAngle < 0.0){
		mInterfaceAngle += 2.0f * M_PI;
		std::cout << "mInterfaceAngle = " << mInterfaceAngle << std::endl;
	}
    while (mInterfaceAngle > 2.0f * M_PI){
		mInterfaceAngle -= 2.0f * M_PI;
		std::cout << "mInterfaceAngle = " << mInterfaceAngle << std::endl;
	}
    
    // assign new targets
    mTargetInterfaceSize.x = fabs(deviceSize.x * cos(orientationAngle) + deviceSize.y * sin(orientationAngle));
    mTargetInterfaceSize.y = fabs(deviceSize.y * cos(orientationAngle) + deviceSize.x * sin(orientationAngle));
    mTargetInterfaceAngle = 2.0f*M_PI-orientationAngle;
    
    // make sure we're turning the right way
    if (abs(mTargetInterfaceAngle-mInterfaceAngle) > M_PI) {
        if (mTargetInterfaceAngle < mInterfaceAngle) {
            mTargetInterfaceAngle += 2.0f * M_PI;
        }
        else {
            mTargetInterfaceAngle -= 2.0f * M_PI;
        }
    }
    <commit_msg>initialize all UIController attributes properly, skip mInterfaceAngle constraint<commit_after>//
//  UIController.cpp
//  Kepler
//
//  Created by <NAME> on 7/17/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#include "UIController.h"
#include "cinder/app/AppCocoaTouch.h"
#include "cinder/gl/gl.h"
#include "OrientationHelper.h"

using namespace ci;
using namespace ci::app;

UIController::UIController( AppCocoaTouch *app, OrientationHelper *orientationHelper ): 
    mApp(app), 
    mOrientationHelper(orientationHelper),
    mInterfaceSize(0.0f,0.0f),
    mInterfaceAngle(0.0f),
    mTargetInterfaceSize(0.0f,0.0f),
    mTargetInterfaceAngle(0.0f),
    mLastOrientationChangeTime(-1.0f),
    mOrientationAnimationDuration(0.25f),
    mPrevInterfaceAngle(0.0f),
    mPrevInterfaceSize(0.0f,0.0f)
{
    mParent = UINodeRef(this); // FIXME: shared_from_this() but in a subclass
    mRoot = UIControllerRef(this); // FIXME: shared_from_this() but for a subclass
    
    // FIXME: should mRoot be static (essentially, should UIController be a singleton?) how/when to clean up? mRoot as weak_ref?
    
    cbTouchesBegan = mApp->registerTouchesBegan( this, &UIController::touchesBegan );
    cbTouchesMoved = mApp->registerTouchesMoved( this, &UIController::touchesMoved );
    cbTouchesEnded = mApp->registerTouchesEnded( this, &UIController::touchesEnded );
    
    if (mOrientationHelper) {
        cbOrientationChanged = mOrientationHelper->registerOrientationChanged( this, &UIController::orientationChanged );    
    }
    
    // initialize orientation without animating
    setInterfaceOrientation( mOrientationHelper->getInterfaceOrientation(), false );
}

UIController::~UIController()
{
    mApp->unregisterTouchesBegan( cbTouchesBegan );
    mApp->unregisterTouchesMoved( cbTouchesMoved );
    mApp->unregisterTouchesEnded( cbTouchesEnded );
    if (mOrientationHelper) {
        mOrientationHelper->unregisterOrientationChanged( cbOrientationChanged );
    }
}

bool UIController::touchesBegan( TouchEvent event )
{
    bool consumed = true;
    for (std::vector<TouchEvent::Touch>::const_iterator i = event.getTouches().begin(); i != event.getTouches().end(); i++) {
        consumed = privateTouchBegan(*i) && consumed; // recurses to children
    }    
    return consumed; // only true if all touches were consumed
}

bool UIController::touchesMoved( TouchEvent event )
{
    bool consumed = true;
    for (std::vector<TouchEvent::Touch>::const_iterator i = event.getTouches().begin(); i != event.getTouches().end(); i++) {
        consumed = privateTouchMoved(*i) && consumed; // recurses to children
    }
    return consumed; // only true if all touches were consumed
}

bool UIController::touchesEnded( TouchEvent event )
{
    bool consumed = true;
    for (std::vector<TouchEvent::Touch>::const_iterator i = event.getTouches().begin(); i != event.getTouches().end(); i++) {
        consumed = privateTouchEnded(*i) && consumed; // recurses to children
    }    
    return consumed; // only true if all touches were consumed
}

bool UIController::orientationChanged( OrientationEvent event )
{
    setInterfaceOrientation( event.getInterfaceOrientation() );
    return false;
}

void UIController::setInterfaceOrientation( const Orientation &orientation, bool animate )
{
    mInterfaceOrientation = orientation;

    // get the facts
    Vec2f deviceSize = mApp->getWindowSize();
    float orientationAngle = getOrientationAngle(mInterfaceOrientation);
    
//	if( mInterfaceAngle < ( -2.0f * M_PI ) ){
//		std::cout << "InterfaceAngle is way too low. Being adjusted" << std::endl;
//		mInterfaceAngle = -2.0f * M_PI;
//	} else if( mInterfaceAngle > ( 2.0f * M_PI ) ){
//		std::cout << "InterfaceAngle is way too high. Being adjusted" << std::endl;
//		mInterfaceAngle = 2.0f * M_PI;
//	}
	
    // normalize interface angle (could be many turns)
    while (mInterfaceAngle < 0.0){
		mInterfaceAngle += 2.0f * M_PI;
		std::cout << "mInterfaceAngle = " << mInterfaceAngle << std::endl;
	}
    while (mInterfaceAngle > 2.0f * M_PI){
		mInterfaceAngle -= 2.0f * M_PI;
		std::cout << "mInterfaceAngle = " << mInterfaceAngle << std::endl;
	}
    
    // assign new targets
    mTargetInterfaceSize.x = fabs(deviceSize.x * cos(orientationAngle) + deviceSize.y * sin(orientationAngle));
    mTargetInterfaceSize.y = fabs(deviceSize.y * cos(orientationAngle) + deviceSize.x * sin(orientationAngle));
    mTargetInterfaceAngle = 2.0f*M_PI-orientationAngle;
    
    // make sure we're turning the right way
    if (abs(mTargetInterfaceAngle-mInterfaceAngle) > M_PI) {
        if (mTargetInterfaceAngle < mInterfaceAngle) {
            mTargetInterfaceAngle += 2.0f * M_PI;
        }
        else {
            mTargetInterfaceAngle -= 2.0f * M_PI;
        }
    }
    
{'fraction_non_alphanumeric': 0.12179072276159655, 'fraction_numerical': 0.010140237324703344, 'mean_word_length': 3.859014675052411, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5860441', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2897, 'n_words': 645}