Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
click_list.cpp
Go to the documentation of this file.
1 #include "gfxlib.h"
2 #include "click_list.h"
3 #include "unit_generic.h"
4 #include "vegastrike.h"
5 #include "vs_globals.h"
6 #include "gfx/camera.h"
7 extern Vector mouseline;
8 extern vector< Vector >perplines;
9 
10 Vector MouseCoordinate( int mouseX, int mouseY )
11 {
12  return GFXDeviceToEye( mouseX, mouseY );
13 }
14 
15 bool ClickList::queryShip( int mouseX, int mouseY, Unit *ship )
16 {
17  Vector mousePoint = MouseCoordinate( mouseX, mouseY );
18 
19  //mousePoint.k= -mousePoint.k;
20  Vector CamP, CamQ, CamR;
21  QVector CamPos;
22  _Universe->AccessCamera()->GetPQR( CamP, CamQ, CamR );
23  mousePoint = Transform( CamP, CamQ, CamR, mousePoint );
24  _Universe->AccessCamera()->GetPosition( CamPos );
25 
26  mousePoint.Normalize();
27  mouseline = mousePoint+CamPos.Cast();
28  /* int tmp = ship->querySphere(CamP,mousePoint,0); //FIXME bounding spheres seem to be broken
29  if (tmp) {
30  if ( ship->querySphereClickList( CamPos, mousePoint.Cast(), 0 ) ) {
31  //camera position is not actually the center of the camera
32  //VSFileSystem::vs_fprintf (stderr, "bounding sphere hit\n");
33  return true;
34  }
35  } POSSIBLE DELETE */
36  return false;
37 }
38 
40 {
41  lastSelected = NULL;
42  lastCollection = NULL;
43  parentSystem = parSystem;
44  parentIter = parIter;
45 }
46 
47 UnitCollection* ClickList::requestIterator( int minX, int minY, int maxX, int maxY )
48 {
49  UnitCollection *uc = new UnitCollection();
50  if (minX == maxX || minY == maxY)
51  return uc; //nothing in it
52 
53  Matrix view;
54  float frustmat[16];
55  float l, r, b, t, n, f;
56  float drivel[16];
57  GFXGetFrustumVars( true, &l, &r, &b, &t, &n, &f );
58  GFXFrustum( frustmat,
59  drivel,
60  l
61  *(-2.*minX/g_game.x_resolution+1) /* *g_game.MouseSensitivityX*/,
62  r*(2.*maxX/g_game.x_resolution-1) /* *g_game.MouseSensitivityX*/,
63  t*(-2.*minY/g_game.y_resolution+1) /* *g_game.MouseSensitivityY*/,
64  b*(2.*maxY/g_game.y_resolution-1) /* *g_game.MouseSensitivityY*/,
65  n,
66  f );
67  _Universe->AccessCamera()->GetView( view );
68  double frustum[6][4];
69  GFXCalculateFrustum( frustum, view, frustmat );
70  Unit *un;
71  for (un_iter myParent = parentIter->createIterator(); (un=*myParent)!=NULL; ++myParent)
72  if ( (un)->queryFrustum( frustum ) )
73  uc->prepend( un );
74  return uc;
75 }
76 
77 UnitCollection* ClickList::requestIterator( int mouseX, int mouseY )
78 {
79  perplines = vector< Vector > ();
80  UnitCollection *uc = new UnitCollection();
81  Unit *un;
82  for (un_iter myParent=parentIter->createIterator(),UAye=uc->createIterator(); (un=*myParent)!=NULL; ++myParent)
83  if ( queryShip( mouseX, mouseY, un ) )
84  UAye.preinsert( un );
85  return uc;
86 }
87 
88 Unit* ClickList::requestShip( int mouseX, int mouseY )
89 {
90  bool equalCheck = false;
91  UnitCollection *uc = requestIterator( mouseX, mouseY );
92  if (lastCollection != NULL) {
93  equalCheck = true;
94  Unit *lastun;
95  Unit *un;
96  for (un_iter lastiter = lastCollection->createIterator(), UAye = uc->createIterator();
97  (lastun = *lastiter) && (un = *UAye) && equalCheck;
98  ++lastiter, ++UAye)
99  if (un != lastun)
100  equalCheck = false;
101  delete lastCollection;
102  }
103  float minDistance = 1e+10;
104  float tmpdis;
105  Unit *targetUnit = NULL;
106  if (equalCheck && lastSelected) {
107  //the person clicked the same place and wishes to cycle through units from front to back
108  float morethan = lastSelected->getMinDis( _Universe->AccessCamera()->GetPosition() ); //parent system for access cam
109  Unit *un;
110  for (un_iter UAye = uc->createIterator(); (un=*UAye)!=NULL; ++UAye) {
111  tmpdis = un->getMinDis( _Universe->AccessCamera()->GetPosition() ); //parent_system? FIXME (for access cam
112  if (tmpdis > morethan && tmpdis < minDistance) {
113  minDistance = tmpdis;
114  targetUnit = un;
115  }
116  }
117  }
118  if (targetUnit == NULL) {
119  //ok the click location is either different, or
120  //he clicked on the back of the list and wishes to start over
121  Unit *un;
122  for (un_iter UAye = uc->createIterator(); (un=*UAye)!=NULL; ++UAye) {
123  tmpdis = un->getMinDis( _Universe->AccessCamera()->GetPosition() ); //parent_system FIXME
124  if (tmpdis < minDistance) {
125  minDistance = tmpdis;
126  targetUnit = un;
127  }
128  }
129  }
130  lastCollection = uc;
131  lastSelected = targetUnit;
132  return targetUnit;
133 }
134