Community Core Vision 1.4
Cross platform, user friendly computer vision.
C:/CCV/1.4/addons/ofxNCore/src/Controls/ofxGuiObject.cpp
Go to the documentation of this file.
00001 /*
00002  *  ofxGuiObject.cpp
00003  *  openFrameworks
00004  *
00005  *  Created by Stefan Kirch on 18.06.08.
00006  *  Copyright 2008 alphakanal. All rights reserved.
00007  *
00008  */
00009 
00010 //      ----------------------------------------------------------------------------------------------------
00011 
00012 #include "ofxGuiObject.h"
00013 
00014 //      ----------------------------------------------------------------------------------------------------
00015 
00016 ofxGuiObject::ofxGuiObject()
00017 {
00018         mParamId                = -1;
00019         mParamType              = kofxGui_Object_Base;
00020         mParamName              = "";
00021         
00022         mObjX                   = 0; 
00023         mObjY                   = 0;
00024         mObjWidth               = 0; 
00025         mObjHeight              = 0;
00026         
00027         mDisplay                = kofxGui_Display_Float2;
00028         mSteps                  = 0;
00029         
00030         mMouseIsDown    = false;
00031                 
00032         mGlobals                = ofxGuiGlobals::Instance();
00033         
00034         
00035         setControlRegion(0, 0, 0, 0);
00036 }
00037 
00038 //      ----------------------------------------------------------------------------------------------------
00039 
00040 void ofxGuiObject::drawHeadString(float x, float y, string text, bool center)
00041 {
00042         glColor4f(mGlobals->mTextColor.r, mGlobals->mTextColor.g, mGlobals->mTextColor.b, mGlobals->mTextColor.a);
00043 
00044         if(center)
00045                 x -= roundInt(mGlobals->mHeadFont.stringWidth(text) / 2.0f);
00046         else
00047                 x += mGlobals->mHeadFontXOffset;
00048         
00049         y += mGlobals->mHeadFontYOffset;
00050         mGlobals->mHeadFont.drawString(text, x, y);
00051         
00052         //      debug rect to position font
00053         /*
00054         ofRectangle rect = uiGlobals->headFont.getStringBoundingBox(text, x, y);
00055         ofNoFill();
00056         glColor4f(1.0, 0.0, 0.0, 1.0);
00057         ofRect(x, y, rect.width, OFXGUI_HEAD_HEIGHT);
00058         */
00059 }
00060 
00061 //      ----------------------------------------------------------------------------------------------------
00062 
00063 void ofxGuiObject::drawParamString(float x, float y, string text, bool center)
00064 {
00065         glColor4f(mGlobals->mTextColor.r, mGlobals->mTextColor.g, mGlobals->mTextColor.b, mGlobals->mTextColor.a);
00066 
00067         if(center)
00068                 x -= roundInt(mGlobals->mParamFont.stringWidth(text) / 2.0f);
00069         else
00070                 x += mGlobals->mParamFontXOffset;
00071                 
00072         y += mGlobals->mParamFontYOffset;
00073         mGlobals->mParamFont.drawString(text, x, y);
00074         
00075         //      debug rect to position font
00076         /*
00077         ofRectangle rect = mGlobals->mParamFont.getStringBoundingBox(text, x, y);
00078         ofNoFill();
00079         glColor4f(1.0, 0.0, 0.0, 1.0);
00080         ofRect(x, y, rect.width, mGlobals->mParamFontHeight);
00081         */
00082 }
00083 
00084 //      ----------------------------------------------------------------------------------------------------
00085 
00086 string ofxGuiObject::floatToString(float value, int display)
00087 {
00088         string stringValue = "";
00089         
00090         switch(display)
00091         {
00092                 case kofxGui_Display_Int:
00093                         stringValue = ofToString((int)value, 0);
00094                         break;
00095                         
00096                 case kofxGui_Display_Hex:
00097                         char hex[64];
00098                         sprintf(hex, "%X", (int)value);
00099                         stringValue = hex;
00100                         break;
00101                         
00102                 case kofxGui_Display_Float2:
00103                         stringValue = ofToString(value, 2);     
00104                         break;
00105                         
00106                 case kofxGui_Display_Float4:
00107                         stringValue = ofToString(value, 4);
00108                         break;
00109                         
00110                 default:
00111                         stringValue = ofToString(value);
00112                         break;
00113         }
00114 
00115         return stringValue;
00116 }
00117 
00118 //      ----------------------------------------------------------------------------------------------------
00119 
00120 string ofxGuiObject::pointToString(ofxPoint2f value, int display)
00121 {
00122         return floatToString(value.x, display) + " " + floatToString(value.y, display); 
00123 }
00124 
00125 //      ----------------------------------------------------------------------------------------------------
00126 
00127 bool ofxGuiObject::isPointInsideMe(int x, int y) 
00128 {       
00129         return (x >= mCtrX && x <= mCtrRight && y >= mCtrY && y <= mCtrBottom);
00130 }
00131 
00132 //      ----------------------------------------------------------------------------------------------------
00133 
00134 bool ofxGuiObject::isPointInsideMe(ofxPoint2f p) 
00135 {       
00136         return (p.x >= mCtrX && p.x <= mCtrRight && p.y >= mCtrY && p.y <= mCtrBottom);
00137 }
00138 
00139 //      ----------------------------------------------------------------------------------------------------
00140 
00141 ofxPoint2f ofxGuiObject::mouseToLocal(int x, int y)
00142 {
00143         return ofxPoint2f((float)(x - mObjX), (float)(y - mObjY));
00144 }
00145 
00146 //      ----------------------------------------------------------------------------------------------------
00147 
00148 ofxPoint2f ofxGuiObject::mouseToFraction(ofxPoint2f p)
00149 {
00150         p.x = CLAMP(p.x, mCtrX, mCtrRight);
00151         p.y = CLAMP(p.y, mCtrY, mCtrBottom);
00152         
00153         p.x = (p.x - mCtrX) / mCtrWidth;
00154         p.y = (p.y - mCtrY) / mCtrHeight;
00155         
00156         return p;
00157 }
00158 
00159 //      ----------------------------------------------------------------------------------------------------
00160 
00161 ofxPoint2f ofxGuiObject::fractionToLocal(ofxPoint2f p)
00162 {
00163         return ofxPoint2f(mCtrX + mCtrWidth * p.x, mCtrY + mCtrHeight * p.y);
00164 }
00165 
00166 //      ----------------------------------------------------------------------------------------------------
00167 
00168 void ofxGuiObject::setControlRegion(int x, int y, int width, int height)
00169 {
00170         mCtrX           = x;
00171         mCtrY           = y;
00172         mCtrWidth       = width;
00173         mCtrHeight      = height;
00174         mCtrRight       = mCtrX + mCtrWidth; 
00175         mCtrBottom      = mCtrY + mCtrHeight;
00176 }
00177 
00178 //      ----------------------------------------------------------------------------------------------------
00179 
00180 int ofxGuiObject::saveObjectData()
00181 {
00182         int id  = mGlobals->mXml.addTag("OBJECT");
00183         
00184         mGlobals->mXml.setValue("OBJECT:ID", mParamId, id);
00185         mGlobals->mXml.setValue("OBJECT:TYPE", getTagName(), id);
00186         mGlobals->mXml.setValue("OBJECT:NAME", mParamName, id);
00187 
00188         mGlobals->mXml.setValue("OBJECT:LEFT", mObjX, id);
00189         mGlobals->mXml.setValue("OBJECT:TOP", mObjY, id);
00190         //      mGlobals->mXml.setValue("OBJECT:WIDTH", mObjWidth, id);
00191         //      mGlobals->mXml.setValue("OBJECT:HEIGHT", mObjHeight, id);
00192         
00193         //      mGlobals->mXml.setValue("OBJECT:CTRX", mCtrX, id);
00194         //      mGlobals->mXml.setValue("OBJECT:CTRY", mCtrY, id);
00195         
00196         mGlobals->mXml.setValue("OBJECT:WIDTH", mCtrWidth, id);
00197         mGlobals->mXml.setValue("OBJECT:HEIGHT", mCtrHeight, id);
00198         
00199         mGlobals->mXml.setValue("OBJECT:DISPLAY", mDisplay, id);
00200         mGlobals->mXml.setValue("OBJECT:STEPS", mSteps, id);
00201         
00202         return id;
00203 }
00204 
00205 //      ----------------------------------------------------------------------------------------------------
00206 
00207 string ofxGuiObject::getTagName()
00208 {
00209         return kofxGui_Tags[mParamType];
00210 }
00211 
00212 //      ----------------------------------------------------------------------------------------------------
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines