00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00142 if((*i)->GetParent() != parent) {
00143 continue;
00144 }
00145
00146
00147 if( ((*i) == from) || ((*i)->IsVisible() == false) ) {
00148 continue;
00149 }
00150
00151
00152 i_x = (*i)->x + (*i)->w / 2;
00153 i_y = (*i)->y + (*i)->h / 2;
00154
00155
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 }