00001
00002 #include "TFControlPoint.h"
00003
00004 #include "math.h"
00005 #include <iostream>
00006
00007 #include "TransferFunctionGUI.h"
00008
00009 using namespace std;
00010 using namespace Colours;
00011
00012
00013 bool TFControlPoint::g_bMouseInsideBottomWindow = false;
00014
00015 int TFControlPoint::MAXIMUM_CONTROLPOINT_OUTSIDE_DRAG_DIST_SQUARED = 289;
00016
00017
00018 TFControlPoint::TFControlPoint(float x, float y, const Lab_Colour& colour)
00019 :
00020 m_colour(colour)
00021 {
00022 m_pos.x = x;
00023 m_pos.y = y;
00024
00025 beingDragged = false;
00026 selected = false;
00027 markedForDeletion = false;
00028
00029 dragDifferenceX = 0;
00030 dragDifferenceY = 0;
00031 }
00032
00033
00034 bool TFControlPoint::operator == (const TFControlPoint& other)
00035 {
00036 return (this->m_pos.x == other.m_pos.x);
00037 }
00038
00039 bool TFControlPoint::operator < (const TFControlPoint& other)
00040 {
00041 return (this->m_pos.x < other.m_pos.x);
00042 }
00043
00044
00045
00046 void TFControlPoint::setPosition(float x, float y)
00047 {
00048 m_pos.x = x;
00049 m_pos.y = y;
00050 }
00051
00052 void TFControlPoint::setColour(float l, float a, float b)
00053 {
00054 m_colour.L = l;
00055 m_colour.a = a;
00056 m_colour.b = b;
00057 }
00058
00059 Lab_Colour& TFControlPoint::getColourLab()
00060 {
00061 return m_colour;
00062 }
00063
00064
00065 void TFControlPoint::drawControlPoint()
00066 {
00067 float radius = 4.f;
00068 float square = 4*sqrt(radius + 2.f);
00069
00070 RGBA_Colour colourRGBA(toRGB(m_colour), GLubyte(255));
00071
00072 float x = m_pos.x * TransferFunctionGUI::g_iTransferFuncPickerWidth;
00073 float y = -m_pos.y * TransferFunctionGUI::g_iTransferFuncPickerHeight;
00074
00075
00076
00077
00078 glPushMatrix();
00079 glTranslatef( x, y, 0.f );
00080
00081 drawControlPointNormal(square, colourRGBA, radius);
00082
00083 glPopMatrix();
00084
00085
00086 if(beingDragged)
00087 {
00088
00089 float movePosX = x - dragDifferenceX;
00090 float movePosY = y - dragDifferenceY;
00091 clamp(movePosX, 0.f, TransferFunctionGUI::g_iTransferFuncPickerWidth);
00092 clamp(movePosY, -TransferFunctionGUI::g_iTransferFuncPickerHeight, 0);
00093
00094 glPushMatrix();
00095 glTranslatef((float)movePosX, (float)movePosY, 0.f );
00096
00097 drawControlPointDragged(square, colourRGBA, radius);
00098
00099 glPopMatrix();
00100 }
00101 }
00102
00103
00104
00105 void TFControlPoint::drawControlPointCenterPoint( RGBA_Colour &colourRGBA, float radius, bool drawVersionDragged )
00106 {
00107 glEnable(GL_LINE_SMOOTH);
00108 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
00109
00110 glBegin(GL_TRIANGLE_FAN);
00111
00112
00113 if(beingDragged && !drawVersionDragged)
00114 glColor4ub(colourRGBA.r, colourRGBA.g, colourRGBA.b, (GLubyte)(85));
00115 else
00116 glColor4ub(colourRGBA.r, colourRGBA.g, colourRGBA.b, (GLubyte)(255));
00117
00118 glVertex2f(0, 0);
00119 for(float angle = 0.; angle < 360.f; angle += 5.f)
00120 {
00121 glVertex2f(sin(angle) * radius, cos(angle) * radius);
00122 }
00123 glEnd();
00124
00125 glDisable(GL_LINE_SMOOTH);
00126 }
00127
00128 void TFControlPoint::drawControlPointFrame(float square, bool drawDraggedVersion, bool drawVersionDeleting)
00129 {
00130 RGBA_Colour frameColour;
00131
00132 if(beingDragged && !drawDraggedVersion)
00133 frameColour.setColour(0, 0, 0, 85);
00134 else
00135 frameColour.setColour(0, 0, 0, 255);
00136
00137 if(selected)
00138 {
00139 frameColour.r = (GLubyte)255;
00140 frameColour.g = (GLubyte)255;
00141 frameColour.b = (GLubyte)0;
00142 }
00143
00144 if(drawVersionDeleting)
00145 {
00146 frameColour.r = (GLubyte)255;
00147 frameColour.g = (GLubyte)0;
00148 frameColour.b = (GLubyte)0;
00149 }
00150
00151 glLineWidth(1.5f);
00152 glBegin(GL_LINE_LOOP);
00153 glColor4ub(frameColour.r, frameColour.g, frameColour.b, frameColour.a);
00154 glVertex2f(-square, 0.f);
00155 glVertex2f(0.f, +square);
00156 glVertex2f(square, 0.f);
00157 glVertex2f(0.f, -square);
00158 glColor3i(0,0,0);
00159 glEnd();
00160 }
00161
00162 void TFControlPoint::drawControlPointBackGround( float square, bool drawDraggedVersion)
00163 {
00164 GLubyte transparency;
00165
00166 if(beingDragged && !drawDraggedVersion)
00167 transparency = (GLubyte)127;
00168 else
00169 transparency = (GLubyte)42;
00170
00171 glBegin(GL_TRIANGLES);
00172 glColor4ub(255, 255, 255, 127);
00173 glVertex2f(0.f, 0.f);
00174 glVertex2f(0.f, square);
00175 glVertex2f(square, 0.f);
00176 glVertex2f(0.f, 0.f);
00177 glVertex2f(0.f, -square);
00178 glVertex2f(-square, 0.f);
00179 glColor4ub(0, 0, 0, 127);
00180 glVertex2f(0.f, 0.f);
00181 glVertex2f(0.f, -square);
00182 glVertex2f(square, 0.f);
00183 glVertex2f(0.f, 0.f);
00184 glVertex2f(0.f, square);
00185 glVertex2f(-square, 0.f);
00186 glEnd();
00187 }
00188
00189 void TFControlPoint::setBeingDragged(bool beingDragged)
00190 {
00191 this->beingDragged = beingDragged;
00192 }
00193
00194 bool TFControlPoint::isBeingDragged()
00195 {
00196 return beingDragged;
00197 }
00198
00199
00200 void TFControlPoint::setSelected(bool selected)
00201 {
00202 this->selected = selected;
00203 }
00204
00205 bool TFControlPoint::isSelected()
00206 {
00207 return selected;
00208 }
00209
00210 void TFControlPoint::dragMovement(int mouseX, int mouseY)
00211 {
00212 updateDragDifference(mouseX, mouseY);
00213
00214 updateBeingDraggedIntoDeletion(mouseX, mouseY);
00215 }
00216
00217 void TFControlPoint::draggingEnded()
00218 {
00219 this->beingDragged = false;
00220 this->markedForDeletion = false;
00221 m_pos.x -= dragDifferenceX / TransferFunctionGUI::g_iTransferFuncPickerWidth;
00222 m_pos.y += dragDifferenceY / TransferFunctionGUI::g_iTransferFuncPickerHeight;
00223
00224 clamp(m_pos.x, 0.f, 1.f);
00225 clamp(m_pos.y, 0.f, 1.f);
00226 }
00227
00228 void TFControlPoint::updateDragDifference( int mouseX, int mouseY )
00229 {
00230 dragDifferenceX = m_pos.x * TransferFunctionGUI::g_iTransferFuncPickerWidth - mouseX;
00231 dragDifferenceY = TransferFunctionGUI::g_iTransferFuncPickerHeight - m_pos.y * TransferFunctionGUI::g_iTransferFuncPickerHeight - mouseY;
00232 if(dragDifferenceX * dragDifferenceX + dragDifferenceY * dragDifferenceY < 33)
00233 {
00234 dragDifferenceX = 0;
00235 dragDifferenceY = 0;
00236 }
00237 }
00238
00239 void TFControlPoint::updateBeingDraggedIntoDeletion( int mouseX, int mouseY )
00240 {
00241 int movePosX = m_pos.x * TransferFunctionGUI::g_iTransferFuncPickerWidth - dragDifferenceX;
00242 int movePosY = - m_pos.y * TransferFunctionGUI::g_iTransferFuncPickerHeight - dragDifferenceY;
00243 int movePosXCopy = movePosX;
00244 int movePosYCopy = movePosY;
00245 clamp(movePosX, 0, TransferFunctionGUI::g_iTransferFuncPickerWidth);
00246 clamp(movePosY, -TransferFunctionGUI::g_iTransferFuncPickerHeight, 0);
00247
00248 markedForDeletion =
00249 (pow(movePosXCopy - (float)movePosX, 2.f) + pow(movePosYCopy - (float)movePosY, 2.f)) > MAXIMUM_CONTROLPOINT_OUTSIDE_DRAG_DIST_SQUARED
00250 || !g_bMouseInsideBottomWindow;
00251 }
00252
00253 bool TFControlPoint::isMarkedForDeletion()
00254 {
00255 return markedForDeletion;
00256 }
00257
00258 void TFControlPoint::setMarkedForDeletion(bool deletePoint)
00259 {
00260 markedForDeletion = deletePoint;
00261 }
00262
00263 void TFControlPoint::drawControlPointNormal( float square, RGBA_Colour colourRGBA, float radius )
00264 {
00265 drawControlPointBackGround(square);
00266
00267 drawControlPointCenterPoint(colourRGBA, radius);
00268
00269 drawControlPointFrame(square, false, markedForDeletion);
00270 }
00271
00272 void TFControlPoint::drawControlPointDragged( float square, RGBA_Colour colourRGBA, float radius )
00273 {
00274 drawControlPointBackGround(square, true);
00275
00276 drawControlPointCenterPoint(colourRGBA, radius, true);
00277
00278 drawControlPointFrame(square, true, markedForDeletion);
00279 }