Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

control.cpp

Go to the documentation of this file.
00001 /* *************************************************************************************
00002         Writer:         Sebastien Bloc
00003         Copyright:      2003-2004
00004         eMail:          sebastien.bloc@free.fr
00005         URL:            http://mignonsoft.free.fr
00006 
00007         This program is free software; you can redistribute it and/or
00008         modify it under the terms of the GNU General Public License
00009         as published by the Free Software Foundation; either version 2
00010         of the License, or (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016         http://www.gnu.org/copyleft/gpl.html
00017 ************************************************************************************* */
00018 
00019 #include "3d.h"
00020 #include "input.h"
00021 #include "controlEngine.h"
00022 #include "container.h"
00023 #include "mylist.h"
00024 #include "control.h"
00025 
00026 extern ControlEngine ce;
00027 
00028 Control::Control()
00029 {
00030         parent=NULL;
00031         parentFace=-1;
00032         faceNode=NULL;
00033         sleepedDelay=-1;
00034         id=nextId++;
00035         name="Control";
00036         isEnable=TRUE;
00037         isView=TRUE;
00038         leftDown = FALSE;
00039         midDown = FALSE;
00040         rightDown = FALSE;
00041         controls.i+=this; // placement dans les instansiations de control
00042 }
00043 
00044 Control::~Control()
00045 {
00046         Delete();
00047 }
00048 
00050 void Control::SetParent()
00051 {
00052         parent = ce.Peek();
00053         FaceNode *faceNode = ce.primitive.tree.GetCurrent();// ce.primitive.Peek();
00054         if (faceNode) parentFace = faceNode->elem->idFace;
00055         else parentFace = -1;
00056 }
00057 
00058 // ********************************************************************************
00059 
00060 void Control::operator= (Control &src)
00061 {
00062         parent = src.parent;
00063         parentFace = src.parentFace;
00064         faceNode = src.faceNode;
00065         name = src.name;
00066         isEnable=src.isEnable;
00067         isView=src.isView;
00068         onControl = src.onControl;
00069         sleepedDelay = src.sleepedDelay;
00070 }
00071 
00072 // ********************************************************************************
00073 
00074 BOOL Control::Delete()
00075 {
00076         DeleteFaces();
00077 
00078         // supression de l'element dans la liste static
00079         if (controls.i.GoTo((Control *)this)) 
00080         {
00081                 controls.i.Supr();
00082                 return TRUE;
00083         }
00084         return FALSE;
00085 }
00086 
00087 void Control::DeleteFaces()
00088 {
00089         DeleteFaces(faceNode);
00090         faceNode=NULL;
00091 }
00092 
00093 void Control::DeleteFaces(FaceNode *curFaceNode)
00094 {
00095         if (!curFaceNode || curFaceNode->elem->idControl!=id) return;
00096         
00097         FaceNode *curChild;
00098         MyListItem<FaceNode *> *item;
00099         item = curFaceNode->childs.first;
00100         while (item)
00101         {
00102                 curChild = *item->elem;
00103                 item = item->after;
00104                 DeleteFaces(curChild);
00105         }
00106         ce.primitive.tree.SuprNode(curFaceNode,FALSE);
00107 }
00108 
00111 void Control::DeleteSub()
00112 {
00113         DeleteSub(faceNode);
00114 }
00115 
00116 
00117 void Control::DeleteSub(FaceNode *curFaceNode)
00118 {
00119         if (!curFaceNode) return ;
00120         if (curFaceNode->elem->idControl!=id) 
00121         {
00122                 // retrouve le control lié a une face particuliere
00123                 Control *control = Control::Find(curFaceNode->elem->idControl);
00124                 if (control) 
00125                 {
00126                         control->DeleteSub();
00127                         delete control;
00128                         return;
00129                 }
00130         }
00131 
00132         FaceNode *curChild;
00133         MyListItem<FaceNode *> *item;
00134         item = curFaceNode->childs.first;
00135         while (item)
00136         {
00137                 curChild = *item->elem;
00138                 item = item->after;
00139                 DeleteSub(curChild);
00140         }
00141 }
00142 
00144 void Control::SetBlending(BOOL enable) 
00145 {
00146         if (!faceNode) return;
00147         faceNode->elem->blending=enable;
00148         for (faceNode->childs.i=0;faceNode->childs.i.More();faceNode->childs.i++)
00149         {
00150                 faceNode->childs.i.GetElem()->elem->blending=enable;
00151         }
00152 }
00153 
00155 void Control::SetAlphaBlending(float alpha) 
00156 {
00157         if (!faceNode) return;
00158         Face * face = faceNode->elem;
00159         face->material.SetAlpha(alpha);
00160         face->blending=TRUE;
00161         for (faceNode->childs.i=0;faceNode->childs.i.More();faceNode->childs.i++)
00162         {
00163                 face = faceNode->childs.i.GetElem()->elem;
00164                 face->material.SetAlpha(alpha);
00165                 face->blending=TRUE;
00166         }
00167 }
00168 
00169 void Control::SetLines(Color color,float size)                                          
00170 {
00171         if (!faceNode) return;
00172         Face * face = faceNode->elem;
00173         face->lineMaterial.SetDS(color);
00174         face->line=size;
00175         for (faceNode->childs.i=0;faceNode->childs.i.More();faceNode->childs.i++)
00176         {
00177                 face = faceNode->childs.i.GetElem()->elem;
00178                 face->lineMaterial.SetDS(color);
00179                 face->line=size;
00180         }
00181 }
00182 
00184 BOOL Control::ScreenMove(double x,double y,double z)
00185 {
00186         double zDistOfSelected;
00187         if (ce.usingInertia) zDistOfSelected = ce.pickingInertia.zDist;
00188         else zDistOfSelected = ce.pickingCursor.zDist;
00189         double realZ =ce.ComputeRealZ(zDistOfSelected);
00190         return Move(x*realZ,y*realZ,z);
00191 
00192 }
00193 
00195 BOOL Control::ScreenMoveTo(double x,double y,double z)
00196 {
00197         double realZ =ce.ComputeRealZ(ce.pickingCursor.zDist);
00198         return MoveTo(x*realZ,y*realZ,z);
00199 }
00200 
00202 BOOL Control::MoveTo(double x,double y,double z)
00203 {
00204         if (!faceNode) return FALSE;
00205         faceNode->elem->MoveTo(x,y,z);
00206         return TRUE;
00207 }
00208 
00210 BOOL Control::Move(double x,double y,double z)
00211 {
00212         if (!faceNode) return FALSE;
00213         faceNode->elem->Move(x,y,z);
00214         return TRUE;
00215 }
00216 
00218 BOOL Control::ReSize(double dx,double dy)
00219 {
00220         if (!faceNode) return FALSE;
00221         faceNode->elem->size.x.constValue+=dx;
00222         faceNode->elem->size.y.constValue+=dy;
00223         return TRUE;
00224 }
00225 
00227 BOOL Control::Size(double x,double y)
00228 {
00229         if (!faceNode) return FALSE;
00230         faceNode->elem->size.x=x;
00231         faceNode->elem->size.y=y;
00232         return TRUE;
00233 }
00234 
00236 BOOL Control::RotateQuaternion(double x,double y,double z)
00237 {
00238         if (!faceNode) return FALSE;
00239         Point3D<double> angle(-y,-x,z);
00240         faceNode->elem->angle+=angle.QuaternionRotate(angle);
00241         return TRUE;
00242 }
00243 
00245 BOOL Control::Rotate(double x,double y,double z)
00246 {
00247         if (!faceNode) return FALSE;
00248         Point3D<double> pt(x,y,z);
00249         faceNode->elem->angle+=pt;
00250         return TRUE;
00251 }
00252 
00254 BOOL Control::RotateTo(double x,double y,double z)
00255 {
00256         if (!faceNode) return FALSE;
00257         Point3D<double> pt(x,y,z);
00258         faceNode->elem->angle=pt;
00259         return TRUE;
00260 }
00261 
00269 #define DOCK_INIT \
00270         if (!faceNode) return FALSE; \
00271         Face *father,*child; \
00272         child = faceNode->elem; \
00273         father = faceNode->parent->elem; \
00274         if (!child||!father) return FALSE; \
00275 
00276 BOOL Control::YDockTop(double margeY,double sizeY,BOOL isCenter)
00277 {
00278         DOCK_INIT;
00279         child->center.y=isCenter?0:-1;
00280         child->position.y.Set(-margeY,0.5,&father->computeSize.y);
00281         child->size.y.Set(sizeY);
00282         return TRUE;
00283 }
00284 
00285 BOOL Control::YDockBottom(double margeY,double sizeY,BOOL isCenter)
00286 {
00287         DOCK_INIT;
00288         child->center.y=isCenter?0:1;
00289         child->position.y.Set(margeY,-0.5,&father->computeSize.y);
00290         child->size.y=sizeY;
00291         return TRUE;
00292 }
00293 
00294 BOOL Control::XDockLeft(double margeX,double sizeX,BOOL isCenter)
00295 {
00296         DOCK_INIT;
00297         child->center.x=isCenter?0:1;
00298         child->position.x.Set(margeX,-0.5,&father->computeSize.x);
00299         child->size.x=sizeX;
00300         return TRUE;
00301 }
00302 
00303 BOOL Control::XDockRight(double margeX,double sizeX,BOOL isCenter)
00304 {
00305         DOCK_INIT;
00306         child->center.x=isCenter?0:-1;
00307         child->position.x.Set(-margeX,0.5,&father->computeSize.x);
00308         child->size.x=sizeX;
00309         return TRUE;
00310 }
00311 
00312 BOOL Control::XDockCenter(double sizeX)
00313 {
00314         DOCK_INIT;
00315         child->center.x=0;
00316         child->position.x.Set(0);
00317         child->size.x.Set(sizeX);
00318         return TRUE;
00319 }
00320 
00321 BOOL Control::XDockCenter(double margeLeft,double margeRight)
00322 {
00323         DOCK_INIT;
00324         child->center.x=0;
00325         child->position.x.Set((margeLeft-margeRight)/2.0);
00326         child->size.x.Set(-margeLeft-margeRight,1,&father->computeSize.x);
00327         return TRUE;
00328 }
00329 
00330 BOOL Control::YDockCenter(double sizeY)
00331 {
00332         DOCK_INIT;
00333         child->center.y=0;
00334         child->position.y.Set(0);
00335         child->size.y.Set(sizeY);
00336         return TRUE;
00337 }
00338 
00339 BOOL Control::ChangeDirection()
00340 {
00341         DOCK_INIT;
00342         Rotate(0,0,90);
00343         Face old = *child;
00344         //child->center.x=old.center.y;
00345         //child->center.y=old.center.x;
00346         child->position.x=old.position.y;
00347         child->position.y=old.position.x;
00348         child->size.x=old.size.y;
00349         child->size.y=old.size.x;
00350         return TRUE;
00351 }
00352 
00353 BOOL Control::YDockCenter(double margeTop,double margeBottom)
00354 {
00355         DOCK_INIT;
00356         child->center.y=0;
00357         child->position.y.Set((margeTop-margeBottom)/2.0);
00358         child->size.y.Set(-margeTop-margeBottom,1,&father->computeSize.y);
00359         return TRUE;
00360 }
00361 
00365 void Control::ToString(MyString &str)
00366 {
00367         str.AddFormat("name:%s isEnable:%d isView:%d",name,isEnable,isView);
00368 }
00369 
00373 Control * Control::Find(int idControl)
00374 {
00375         Control *controlFind;
00376         MyListIterator<Control *> i(&Control::controls);
00377         for (i=0;i.More();i++)
00378         {
00379                 controlFind = i.GetElem();
00380                 if (controlFind->id==idControl) return controlFind;
00381         }
00382         return NULL;
00383 }
00384 
00390 int Control::OnAutoMouseWheel(Timer *timer)
00391 {
00392         Debug0Param("Control::AutoMouseWheel");
00393         onControl.mouse.wheel(&ce.pickingCursor.mouse,ce.pickingCursor.faceHover);
00394         return 0;
00395 }
00396 
00397 int Control::OnMouseWheel(StateMouse *mouse,int face)
00398 {
00399         DebugMouse2("Control::OnMouseWheel",mouse,face);
00400         if (mouse->midBtn) 
00401         {
00402                 BindTimer(timerAutoWheel,OnAutoMouseWheel,40);
00403         }
00404         return onControl.mouse.wheel(mouse,face);
00405 }
00406 
00410 int Control::OnSleeped(Timer *timer)
00411 {
00412         Debug0Param("Control::Sleeped");
00413         timerSleeped.Stop();
00414         onControl.sleeped();
00415         return 0;
00416 }
00420 int Control::OnMouseMove(StateMouse *mouse,int face)
00421 {
00422         if (!this) return 0;
00423         //DebugMouse2("Control::OnMouseMove",mouse,face);
00424         // gestion du Sleeped
00425         if (sleepedDelay!=-1) 
00426         {
00427                 timerSleeped.onTimer.disconnect_all();
00428                 BindTimer(timerSleeped,OnSleeped,sleepedDelay);
00429         }
00430 
00431         // calcul du delta par rapport au dernier appel
00432         StateMouse * delta = ce.pickingCursor.mouse.GetDelta(&ce.pickingCursor.oldMouse);
00433         if (ce.enableInertia) inertia.Add(delta->pos); // gestion d'inertia
00434         return onControl.mouse.move(mouse,delta->pos.x,delta->pos.y,face);
00435 }
00436 
00440 void Control::OverIn(Control *oldControl,int face)
00441 {
00442         Debug2Param("Control::OverIn(oldCtrl:%s,face:%d)",(char*)oldControl->name,face);
00443         leftDown = FALSE;
00444         rightDown = FALSE;
00445         midDown = FALSE;
00446         onControl.over.in(oldControl,face);
00447 } 
00448 
00452 void Control::OverOut(Control *newControl,int face)
00453 {
00454         Debug2Param("Control::OverOut(newCtrl:%s,face:%d)",(char*)newControl->name,face);
00455         leftDown = FALSE;
00456         rightDown = FALSE;
00457         midDown = FALSE;
00458         onControl.over.out(newControl,face);
00459 } 
00460 
00464 void Control::FaceOverIn(int oldFace)
00465 {
00466         Debug1Param("Control::FaceOverIn(oldFace:%d)",oldFace);
00467         onControl.faceOver.in(oldFace);
00468 } 
00469 
00475 void Control::FaceOverOut(int newFace)
00476 {
00477         Debug1Param("Control::FaceOverOut(newFace:%d)",newFace);
00478         onControl.faceOver.out(newFace);
00479 } 
00480 
00484 int Control::OnLButtonDown(StateMouse *mouse,int face)
00485 {
00486         DebugMouse2("Control::OnLButtonDown",mouse,face);
00487         leftDown = TRUE;
00488         return onControl.mouse.left.down(mouse,face);
00489 }
00490 
00491 int Control::OnMButtonDown(StateMouse *mouse,int face)
00492 {
00493         DebugMouse2("Control::OnMButtonDown",mouse,face);
00494         midDown = TRUE;
00495         return onControl.mouse.mid.down(mouse,face);
00496 }
00497 
00498 int Control::OnRButtonDown(StateMouse *mouse,int face)
00499 {
00500         DebugMouse2("Control::OnRButtonDown",mouse,face);
00501         rightDown = TRUE;
00502         return onControl.mouse.right.down(mouse,face);
00503 }
00504 
00505 int Control::OnClick(StateMouse *mouse,int face)
00506 {
00507         DebugMouse2("Control::OnClick",mouse,face);
00508         return onControl.mouse.left.click(mouse,face);
00509 }
00510 
00511 BOOL Control::UseInertia(int numButton,StateMouse *mouse,int face)
00512 {
00513         //MyDebug::SendLine("UseInertia","use");
00514         if (!ce.enableInertia) return FALSE;
00515         if (ce.eventInertia[numButton].enable)
00516         {
00517                 ce.eventInertia[numButton].Stop();
00518         }
00519         Point2D<double> moyInertia = inertia.GetMoyValue();
00520         if (moyInertia.Power()>SEUIL_MIN_INERTIA) // seuil avant traitement
00521         {
00522                 ce.eventInertia[numButton].enable=TRUE;
00523                 ce.usingInertia++;
00524                 ce.eventInertia[numButton].Start(face,moyInertia);
00525                 return TRUE;
00526         }
00527         return FALSE;
00528 }
00529 
00530 int Control::OnLButtonUp(StateMouse *mouse,int face)
00531 {
00532         DebugMouse2("Control::OnLButtonUp",mouse,face);
00533         int res = 0;
00534         if (!UseInertia(0,mouse,face)) 
00535         {
00536                 res = onControl.mouse.left.up(mouse,face);
00537                 if (leftDown) OnClick(mouse,face);
00538         }
00539         return res;
00540 }
00541 
00542 int Control::OnMButtonUp(StateMouse *mouse,int face)
00543 {
00544         DebugMouse2("Control::OnMButtonUp",mouse,face);
00545         int res = 0;
00546         if (timerAutoWheel.isRuning) timerAutoWheel.StopAndUnBind();
00547         if (!UseInertia(1,mouse,face)) 
00548         {
00549                 res = onControl.mouse.mid.up(mouse,face);
00550                 if (midDown) onControl.mouse.mid.click(mouse,face);
00551         }
00552         return res;
00553 }
00554 
00555 int Control::OnRButtonUp(StateMouse *mouse,int face)
00556 {
00557         DebugMouse2("Control::OnRButtonUp",mouse,face);
00558         int res = 0;
00559         if (!UseInertia(2,mouse,face)) 
00560         {
00561                 res = onControl.mouse.right.up(mouse,face);
00562                 if (rightDown) onControl.mouse.right.click(mouse,face);
00563         }
00564         return res;
00565 }
00566 
00567 int Control::OnLButtonDouble(StateMouse *mouse,int face)
00568 {
00569         DebugMouse2("Control::OnLButtonDblClk",mouse,face);
00570         return onControl.mouse.left.doubleClick(mouse,face);
00571 }
00572 
00573 int Control::OnMButtonDouble(StateMouse *mouse,int face)
00574 {
00575         DebugMouse2("Control::OnMButtonDblClk",mouse,face);
00576         return onControl.mouse.mid.doubleClick.emit(mouse,face);
00577 }
00578 
00579 int Control::OnRButtonDouble(StateMouse *mouse,int face)
00580 {
00581         DebugMouse2("Control::OnRButtonDblClk",mouse,face);
00582         return onControl.mouse.right.doubleClick(mouse,face);
00583 }
00584 
00585 // ********************************************************************************
00586 
00587 int Control::OnKeyUp(StateKey *key,int face)
00588 {
00589         DebugKey2("Control::OnKeyUp",key,face);
00590         return onControl.key.up(key,face);
00591 } 
00592 
00593 int Control::OnKeyDown(StateKey *key,int face)
00594 {
00595         DebugKey2("Control::OnKeyDown",key,face);
00596         return onControl.key.down(key,face);
00597 } 
00598 
00599 int Control::OnKeyDouble(StateKey *key,int face)
00600 {
00601         DebugKey2("Control::OnKeyDblClk",key,face);
00602         return onControl.key.doubleClick(key,face);
00603 } 
00604 
00605 // ********************************************************************************
00606 
00607 void Control::GetCorners(int face,Point3D<double> *points)
00608 {
00609         ce.primitive.GetCorners(faceNode->childs.i[face],points);
00610 }
00611 
00612 Point2D<double> Control::GetLocalPos(int face,Point2D<double> absolutePos)
00613 {
00614         return ce.primitive.GetLocalPos(faceNode->childs.i[face],absolutePos);
00615 }

Generated on Fri Aug 20 19:19:43 2004 for 3d Controls by doxygen 1.3.6