//
//  globalUI.mm
//  Wablet
//
//  Created by Robert Tubb on 19/03/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include "globalUI.h"

GlobalUI::GlobalUI(){
    // array of buttons, 

    
    numButtons = 12;
    borderSize = 128;
    buttonSize = 100;
    
    buttons = new ControlButton *[numButtons];

    touchMode = GRAB;

}

void GlobalUI::passCallBack(UIFunctor specFuncA){

}
void GlobalUI::makeButtons(Mesh *atheMeshPtr){
    
    // set up button size depending on screen resolution
    
    int w = ofGetWidth();
    cout << "width = " << w << "\n";
    if (w == 768){ // ipad
        
    }else if (w == 480){ // iphone
        borderSize = 80;
        buttonSize = 70;
        
    }else if (w == 1234){ // retinass?
        
    }else{
        // default to what?
        
    }
    
    theMeshPtr = atheMeshPtr;

    ofColor red(250,0,0);
    ofColor blue(0,0,250);
    ofColor green(0,250,0);  
    
    buttons[0] = new ControlButton(2,buttonSize,buttonSize,(borderSize-buttonSize)*0.5,(borderSize-buttonSize)*0.5,"still",atheMeshPtr,green);
    buttons[1] = new ControlButton(0,buttonSize,buttonSize,(borderSize-buttonSize)*0.5,borderSize+(borderSize-buttonSize)*0.5,"smooth",atheMeshPtr,green);
    buttons[2] = new ControlButton(1,buttonSize,buttonSize,(borderSize-buttonSize)*0.5,borderSize*2+(borderSize-buttonSize)*0.5,"drift",atheMeshPtr,green);
    buttons[3] = new ControlButton(3,buttonSize,buttonSize,(borderSize-buttonSize)*0.5,borderSize*3+(borderSize-buttonSize)*0.5,"gravity",atheMeshPtr,green);
    buttons[4] = new ControlButton(4,buttonSize,buttonSize,(borderSize-buttonSize)*0.5,borderSize*4+(borderSize-buttonSize)*0.5,"free",atheMeshPtr,green);
    buttons[5] = new ControlButton(5,buttonSize,buttonSize,(borderSize-buttonSize)*0.5,borderSize*5+(borderSize-buttonSize)*0.5,"something",atheMeshPtr,green);
    
    // touch mode buttons
    buttons[6] = new ControlButton(6,buttonSize,buttonSize,borderSize+ofGetHeight()+(borderSize-buttonSize)*0.5,(borderSize-buttonSize)*0.5,"grab",atheMeshPtr, blue);
    buttons[7] = new ControlButton(7,buttonSize,buttonSize,borderSize+ofGetHeight()+(borderSize-buttonSize)*0.5,borderSize+(borderSize-buttonSize)*0.5,"force",atheMeshPtr, blue);
    buttons[8] = new ControlButton(8,buttonSize,buttonSize,borderSize+ofGetHeight()+(borderSize-buttonSize)*0.5,borderSize*2+(borderSize-buttonSize)*0.5,"stick",atheMeshPtr, blue);
    buttons[9] = new ControlButton(9,buttonSize,buttonSize,borderSize+ofGetHeight()+(borderSize-buttonSize)*0.5,borderSize*3+(borderSize-buttonSize)*0.5,"unstick",atheMeshPtr, blue);
    buttons[10] = new ControlButton(10,buttonSize,buttonSize,borderSize+ofGetHeight()+(borderSize-buttonSize)*0.5,borderSize*4+(borderSize-buttonSize)*0.5,"drawscan",atheMeshPtr, red);
    buttons[11] = new ControlButton(11,buttonSize,buttonSize,borderSize+ofGetHeight()+(borderSize-buttonSize)*0.5,borderSize*5+(borderSize-buttonSize)*0.5,"reset",atheMeshPtr, red);
}

//--------------------------------------------------------------
void GlobalUI::draw(){
    
    
    for(int i=0; i<numButtons;i++){
        buttons[i]->draw();
    }
    
    //ofSetColor(blue); 
    //ofDrawBitmapString("touch mode", 670, 25);
}
//--------------------------------------------------------------
bool GlobalUI::inUIZone(float x, float y){
    if (x < borderSize || x > borderSize + ofGetHeight()){
        return true;
        
    }else{
        return false;
    }
}

//--------------------------------------------------------------
bool GlobalUI::handleTouchDown(int ax, int ay){
    // returns true if touch is not to be left to the mesh
    if(inUIZone(ax,ay)){
        
        /* not needed now we are using ofxUI
         
        for(int i=0; i < numButtons;i++){
            if(buttons[i]->checkTouchArea(ax,ay)){
                buttons[i]->press();
            }
        }
         */
        return true;
    }else{
    
    return false;
    }   
}
//--------------------------------------------------------------
bool GlobalUI::handleTouchUp(int ax, int ay){
    // returns true if touch is not to be left to the mesh
    if(inUIZone(ax,ay)){
        /* not needed now we are using ofxUI
         
         for(int i=0; i < numButtons;i++){
         if(buttons[i]->checkTouchArea(ax,ay)){
         buttons[i]->press();
         }
         }
         */
        return true;
    }else{
        
        return false;
    }   
}
//--------------------------------------------------------------
bool GlobalUI::handleTouchMove(int ax, int ay){
    // returns true if touch is not to be left to the mesh
    if(inUIZone(ax,ay)){

        return true;
    }else{
        
        return false;
    }   
}


