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

pgnavigator.cpp

Go to the documentation of this file.
00001 /*
00002     ParaGUI - crossplatform widgetset
00003     Copyright (C) 2000,2001,2002  Alexander Pipelka
00004  
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009  
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014  
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  
00019     Alexander Pipelka
00020     pipelka@teleweb.at
00021  
00022     Last Update:      $Author: mbickel $
00023     Update Date:      $Date: 2007-04-13 16:16:00 $
00024     Source File:      $Source: /home/cvspsrv/cvsroot/games/asc/source/libs/paragui/src/core/pgnavigator.cpp,v $
00025     CVS/RCS Revision: $Revision: 1.2 $
00026     Status:           $State: Exp $
00027 */
00028 
00029 #include "pgnavigator.h"
00030 
00031 #include <algorithm>
00032 #include <cmath>
00033 
00034 PG_Widget* PG_Navigator::my_currentWidget = NULL;
00035 
00036 void PG_Navigator::Add(PG_Widget* widget) {
00037         iterator i = find(begin(), end(), widget);
00038 
00039         if(i != end()) {
00040                 return;
00041         }
00042 
00043         push_back(widget);
00044 }
00045 
00046 void PG_Navigator::Remove(PG_Widget* widget) {
00047         iterator i = find(begin(), end(), widget);
00048 
00049         if(i == end()) {
00050                 return;
00051         }
00052 
00053         erase(i);
00054 }
00055 
00056 bool PG_Navigator::Action(PG_Widget::KeyAction action) {
00057         if(my_currentWidget == NULL) {
00058                 return false;
00059         }
00060 
00061         return my_currentWidget->Action(action);
00062 }
00063 
00064 PG_Widget* PG_Navigator::Goto(PG_Widget* widget) {
00065         iterator i = find(begin(), end(), widget);
00066 
00067         if(i == end()) {
00068                 return NULL;
00069         }
00070 
00071         Action(PG_Widget::ACT_DEACTIVATE);
00072         my_currentWidget = widget;
00073         Action(PG_Widget::ACT_ACTIVATE);
00074 
00075         return my_currentWidget;
00076 }
00077 
00078 PG_Widget* PG_Navigator::GotoFirst() {
00079         iterator i = begin();
00080 
00081         if(i == end()) {
00082                 return NULL;
00083         }
00084 
00085         return Goto(*i);
00086 }
00087 
00088 PG_Widget* PG_Navigator::GotoLast() {
00089         if(size() == 0) {
00090                 return NULL;
00091         }
00092 
00093         if(size() == 1) {
00094                 return GotoFirst();
00095         }
00096 
00097         iterator i = end();
00098         i--;
00099 
00100         return Goto(*i);
00101 }
00102 
00103 PG_Widget* PG_Navigator::GotoNext() {
00104         iterator i = find(begin(), end(), my_currentWidget);
00105 
00106         if(i == end()) {
00107                 return NULL;
00108         }
00109 
00110         i++;
00111         return Goto(*i);
00112 }
00113 
00114 PG_Widget* PG_Navigator::GotoPrev() {
00115         iterator i = find(begin(), end(), my_currentWidget);
00116 
00117         if(i == begin()) {
00118                 return NULL;
00119         }
00120 
00121         i--;
00122         return Goto(*i);
00123 }
00124 
00125 PG_Widget* PG_Navigator::FindWidget(PG_Widget* from, PG_Point ref, bool absx, bool absy, int xmode, int ymode) {
00126         if (!from)
00127                 return NULL;
00128 
00129         int i_x = 0;
00130         int i_y = 0;
00131         int my_x = ref.x;
00132         int my_y = ref.y;
00133         PG_Widget* result = NULL;
00134         PG_Widget* parent = from->GetParent();
00135         int dy = 0;
00136         int dx = 0;
00137         double min_l = 100000;
00138 
00139         for(iterator i = begin(); i != end(); i++) {
00140 
00141                 // check if we have the same parent-widget
00142                 if((*i)->GetParent() != parent) {
00143                         continue;
00144                 }
00145 
00146                 // discard myself and invisible objects
00147                 if( ((*i) == from) || ((*i)->IsVisible() == false) ) {
00148                         continue;
00149                 }
00150 
00151                 // get middle of the widget
00152                 i_x = (*i)->x + (*i)->w / 2;
00153                 i_y = (*i)->y + (*i)->h / 2;
00154 
00155                 // delta x & y
00156                 dy = absy ? abs(i_y - my_y) : (i_y - my_y);
00157                 dx = absx ? abs(i_x - my_x) : (i_x - my_x);
00158 
00159                 if((xmode != 0) && (dx*xmode < 0)) {
00160                         continue;
00161                 }
00162 
00163                 if((ymode != 0) && (dy*ymode < 0)) {
00164                         continue;
00165                 }
00166 
00167                 double l = sqrt((double)(dx*dx + dy*dy));
00168                 if( ((xmode != 0) && (dx != 0)) || ((ymode != 0) && (dy != 0)) ) {
00169                         if(l < min_l) {
00170                                 min_l = l;
00171                                 result = (*i);
00172                         }
00173                 }
00174         }
00175 
00176         return result;
00177 }
00178 
00179 PG_Widget* PG_Navigator::FindLeft(PG_Widget* widget) {
00180         if(widget == NULL) {
00181                 widget = my_currentWidget;
00182         }
00183 
00184         PG_Point p;
00185         p.x = widget->x + widget->w / 2;
00186         p.y = widget->y + widget->h / 2;
00187 
00188         return FindWidget(widget, p, false, true, -1, 0);
00189 }
00190 
00191 PG_Widget* PG_Navigator::FindRight(PG_Widget* widget) {
00192         if(widget == NULL) {
00193                 widget = my_currentWidget;
00194         }
00195 
00196         PG_Point p;
00197         p.x = widget->x + widget->w / 2;
00198         p.y = widget->y + widget->h / 2;
00199 
00200         return FindWidget(widget, p, false, true, +1, 0);
00201 }
00202 
00203 PG_Widget* PG_Navigator::FindUp(PG_Widget* widget) {
00204         if(widget == NULL) {
00205                 widget = my_currentWidget;
00206         }
00207 
00208         PG_Point p;
00209         p.x = widget->x + widget->w / 2;
00210         p.y = widget->y + widget->h / 2;
00211 
00212         return FindWidget(widget, p, true, false, 0, -1);
00213 }
00214 
00215 PG_Widget* PG_Navigator::FindDown(PG_Widget* widget) {
00216         if(widget == NULL) {
00217                 widget = my_currentWidget;
00218         }
00219 
00220         PG_Point p;
00221         p.x = widget->x + widget->w / 2;
00222         p.y = widget->y + widget->h / 2;
00223 
00224         return FindWidget(widget, p, true, false, 0, +1);
00225 }

Generated on Tue Jun 24 01:27:48 2008 for Advanced Strategic Command by  doxygen 1.4.2