diff UI code/UIElement.mm @ 0:a223551fdc1f

First commit - copy from tweakathlon.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 10 Oct 2014 11:46:42 +0100
parents
children af71bf84660f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UI code/UIElement.mm	Fri Oct 10 11:46:42 2014 +0100
@@ -0,0 +1,147 @@
+//
+//  UIElement.cpp
+//  emptyExample
+//
+//  Created by Robert Tubb on 22/05/2013.
+//
+//
+
+#include "UIElement.h"
+//----------------------------------------------------------------------
+UIElement::UIElement(){
+    //
+    //cout << " UIElement default constructur BAD !!!\n";
+    init();
+}
+//----------------------------------------------------------------------
+UIElement::UIElement(float ax,
+                     float ay,
+                     float awidth,
+                     float aheight,
+                     const UIProps& props) :
+x(ax),
+y(ay),
+width(awidth),
+height(aheight),
+background(props.generalBackground)
+{
+    //cout << " UIElement constructur with defs \n";
+    init();
+    verdana16 = props.verdana16;
+    bigFont = props.bigFont;
+    
+}
+//----------------------------------------------------------------------
+UIElement::UIElement(float ax,
+                     float ay,
+                     float awidth,
+                     float aheight,
+                     ofColor bg) :
+x(ax),
+y(ay),
+width(awidth),
+height(aheight),
+background(bg)
+{
+    init();
+
+}
+//----------------------------------------------------------------------
+void UIElement::init(){
+
+    
+    hidden = false;
+    inactive = false;
+}
+//----------------------------------------------------------------------
+void UIElement::draw(){
+    if(hidden) return;
+    //cout<<"element draw\n";
+    ofSetColor(background);
+    ofRect(x,y,width,height);
+    
+    
+};
+//----------------------------------------------------------------------
+bool UIElement::touch(int tx, int ty, touchType ttype, int touchID){
+    if(isMyTouch(tx,ty,ttype,touchID)){
+        handleMyTouch(tx, ty, ttype,touchID);
+        return true;
+    }else{
+        return false;
+    }
+}
+//----------------------------------------------------------------------
+// called first from all subclasses
+bool UIElement::isMyTouch(int tx, int ty, touchType ttype, int touchID){
+    if(hidden || inactive) return false;
+
+    if(ttype == TOUCH_DOWN){
+        if (touchIsInMyArea(tx, ty)){
+            if (!isExistingTouchID(touchID)){
+                //cout << "Touchdown in area, grabbing focus " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
+                addTouchID(touchID);
+                return true;
+
+            }else{
+                //shouldn't happen?
+                return true;
+            }
+
+        }else{
+            //cout << "Touchdown outside area, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
+            return false;
+
+        }
+    }
+    if(ttype == TOUCH_UP){
+        if (isExistingTouchID(touchID)){
+            //cout << "Touch Up for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
+            //myTouchID = -1;
+            removeTouchID(touchID);
+            return true;
+        }else{
+            //cout << "Touch Up NOT my  ID, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
+            return false;
+        }
+    }
+    
+    if(ttype == TOUCH_MOVED){
+        if(isExistingTouchID(touchID)){
+            //cout << "Touch moved for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
+            return true;
+        }else{
+            //cout << "Touch moved NOT my ID, ignore " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
+            return false;
+        }
+    }
+    
+    cout << "UNHANDLED SITUATION!" << labelName << endl;
+    return false;
+
+}
+
+//----------------------------------------------------------------------
+bool UIElement::touchIsInMyArea(int tx, int ty){
+    
+    // work out relative coords
+    double relx = tx - x;
+    double rely = ty - y;
+    return !(relx < 0 || relx > width || rely < 0 || rely > height);
+}
+//----------------------------------------------------------------------
+bool UIElement::isCoordInMyRegion(double ax, double ay){
+    if(hidden) return false;
+    
+    if( (ax > x && ax < x+width) && (ay > y && ay < y+height)){
+        return true;
+    }else{
+        return false;
+    }
+  
+}
+//----------------------------------------------------------------------
+//----------------------------------------------------------------------
+//----------------------------------------------------------------------
+//----------------------------------------------------------------------
+//----------------------------------------------------------------------
\ No newline at end of file