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
drawlist.cpp
Go to the documentation of this file.
1 #include "gui/glut_support.h"
2 
3 #include "gfx/hud.h"
4 
5 #include "cmd/unit_generic.h"
6 
7 #include "gui/glut_support.h"
8 
9 #include "universe_util.h"
10 
11 #include "config_xml.h"
12 
13 #include "drawlist.h"
14 #include "gfx/masks.h"
15 
16 navdrawnode::navdrawnode() //new undefined node, check for these values if wondering if assignment didnt happen.
17 {
18  type = -1;
19  size = 0.0;
20  x = 0;
21  y = 0;
22  nextitem = NULL;
23  source = NULL;
24 }
25 
26 navdrawnode::navdrawnode( int type_, float size_, float x_, float y_, navdrawnode *nextitem_ ) //new node into list
27 {
28  type = type_;
29  size = size_;
30  x = x_;
31  y = y_;
32  nextitem = nextitem_;
33  source = NULL;
34 }
35 
36 navdrawnode::navdrawnode( int type_, float size_, float x_, float y_, Unit *source_, navdrawnode *nextitem_ ) //new node into list
37 {
38  type = type_;
39  size = size_;
40  x = x_;
41  y = y_;
42  nextitem = nextitem_;
43  source = source_;
44 }
45 
46 navdrawlist::navdrawlist( bool mouse, navscreenoccupied *screenoccupation_, GFXColor *factioncolours_ ) //start list with a 'mouselist' flag
47 {
48  inmouserange = mouse;
49  head = NULL;
50  tail = NULL;
51  unselectedalpha = 0.8;
52  n_contents = 0;
53  screenoccupation = screenoccupation_;
54  localcolours = 0;
55  factioncolours = factioncolours_;
56 }
57 
58 navdrawlist::~navdrawlist() //destroy list
59 {
60  inmouserange = 0;
61  wipe();
62  head = NULL;
63  tail = NULL;
64  n_contents = 0;
65 }
66 
67 int navdrawlist::get_n_contents() //return the amount of items in the list
68 {
69  return n_contents;
70 }
71 
72 void navdrawlist::insert( int type, float size, float x, float y ) //insert iteam at head of list
73 {
74  if (head == NULL) {
75  head = new navdrawnode( type, size, x, y, NULL );
76  tail = head;
77  } else {
78  head = new navdrawnode( type, size, x, y, head );
79  }
80  n_contents += 1;
81 }
82 
83 void navdrawlist::insert( int type, float size, float x, float y, Unit *source ) //insert iteam at head of list
84 {
85  if (head == NULL) {
86  head = new navdrawnode( type, size, x, y, source, NULL );
87  tail = head;
88  } else {
89  head = new navdrawnode( type, size, x, y, source, head );
90  }
91  n_contents += 1;
92 }
93 
94 void navdrawlist::wipe() //whipe the list clean
95 {
96  navdrawnode *temp = head;
97  navdrawnode *tempdelete = NULL;
98  while (temp != NULL) {
99  tempdelete = temp;
100  temp = temp->nextitem;
101  delete tempdelete;
102  }
103  head = NULL;
104  n_contents = 0;
105 }
106 
107 void navdrawlist::rotate() //take the head and stick it in the back
108 {
109  if (head != NULL) {
110  //|
111  //|
112  if (head->nextitem != NULL) {
113  //there is something there, and its not alone
114  tail->nextitem = head;
115  tail = head;
116  head = head->nextitem;
117  tail->nextitem = NULL;
118  }
119  }
120 }
121 
122 string getUnitNameAndFgNoBase( Unit *target );
123 void drawdescription( Unit *source,
124  float x_,
125  float y_,
126  float size_x,
127  float size_y,
128  bool ignore_occupied_areas,
129  navscreenoccupied *screenoccupation,
130  const GFXColor &col ) //take the head and stick it in the back
131 {
132  if (source == NULL)
133  return;
134  drawdescription( getUnitNameAndFgNoBase( source ), x_, y_, size_x, size_y, ignore_occupied_areas, screenoccupation, col );
135 }
136 
137 void drawdescription( string text,
138  float x_,
139  float y_,
140  float size_x,
141  float size_y,
142  bool ignore_occupied_areas,
143  navscreenoccupied *screenoccupation,
144  const GFXColor &col ) //take the head and stick it in the back
145 {
146  if (text.size() == 0)
147  return;
148  TextPlane displayname; //will be used to display shits names
149 
150  displayname.col = col;
151 
152  int length = text.size();
153  float offset = (float(length)*0.005);
154  if (ignore_occupied_areas) {
155  displayname.SetPos( (x_-offset), y_ );
156  displayname.SetText( text );
157  displayname.SetCharSize( size_x, size_y );
158  } else {
159  float new_y = screenoccupation->findfreesector( x_, y_ );
160  displayname.SetPos( (x_-offset), new_y );
161  displayname.SetText( text );
162  displayname.SetCharSize( size_x, size_y );
163  }
164  static float background_alpha =
165  XMLSupport::parse_float( vs_config->getVariable( "graphics", "hud", "text_background_alpha", "0.0625" ) );
166  GFXColor tpbg = displayname.bgcol;
167  bool automatte = (0 == tpbg.a);
168  if (automatte) displayname.bgcol = GFXColor( 0, 0, 0, background_alpha );
169  displayname.Draw( text, 0, true, false, automatte );
170  displayname.bgcol = tpbg;
171 }
172 
174 {
175  return tail->source;
176 }
177 
178 #define INIT_COL_ARRAY( col, r, g, b, a ) do{ col[0] = r; col[1] = g; col[2] = b; col[3] = a; }while(0)
179 
180 static GFXColor getUnitTypeColor( std::string name, bool text, float col[4], float unselectedalpha )
181 {
182  vs_config->getColor( "nav", (std::string( "unhighlighted_" )+name)+(text ? "_text" : ""), col, true );
183  if (col[3] == 0) {
184  if (name != "unit" && col[0] == 0 && col[1] == 0 && col[2] == 0) {
185  if (!text) {
186  INIT_COL_ARRAY( col, 1, 1, .7, 1 );
187  return getUnitTypeColor( "unit", text, col, unselectedalpha );
188  } else {
189  INIT_COL_ARRAY( col, .2, 1, .5, 0 );
190  GFXColor temp = getUnitTypeColor( "unit", text, col, unselectedalpha );
191  temp.a = unselectedalpha;
192  return temp;
193  }
194  }
195  col[3] = unselectedalpha;
196  }
197  return GFXColor( col[0], col[1], col[2], col[3] );
198 }
199 
200 void drawlistitem( int type,
201  float size,
202  float x,
203  float y,
204  Unit *source,
205  navscreenoccupied *screenoccupation,
206  bool inmouserange,
207  bool currentistail,
208  float unselectedalpha,
209  GFXColor *factioncolours )
210 {
211  float relation = 0.0;
212  static GFXColor unhighlighted_sun_col;
213  static GFXColor unhighlighted_sun_text;
214  static GFXColor unhighlighted_planet_text;
215  static GFXColor unhighlighted_c_player_col;
216  static GFXColor unhighlighted_c_player_text;
217  static GFXColor unhighlighted_player_col;
218  static GFXColor unhighlighted_player_text;
219  static GFXColor unhighlighted_asteroid_col;
220  static GFXColor unhighlighted_asteroid_text;
221  static GFXColor unhighlighted_nebula_col;
222  static GFXColor unhighlighted_nebula_text;
223  static GFXColor unhighlighted_jump_col;
224  static GFXColor unhighlighted_jump_text;
225  static GFXColor unhighlighted_station_text;
226  static GFXColor unhighlighted_fighter_text;
227  static GFXColor unhighlighted_unit_text;
228  static GFXColor highlighted_tail_col;
229  static GFXColor highlighted_tail_text;
230  static GFXColor highlighted_untail_col;
231  static GFXColor unhighlighted_capship_text;
232  static bool init = false;
233  if (!init) {
234  //Get a color from the config
235  float col[4];
236 
237  INIT_COL_ARRAY( col, 1, .3, .3, .8 );
238  vs_config->getColor( "nav", "highlighted_unit_on_tail", col, true );
239  highlighted_tail_col = GFXColor( col[0], col[1], col[2], col[3] );
240 
241  INIT_COL_ARRAY( col, 1, 1, .7, 1 );
242  vs_config->getColor( "nav", "highlighted_text_on_tail", col, true );
243  highlighted_tail_text = GFXColor( col[0], col[1], col[2], col[3] );
244 
245  INIT_COL_ARRAY( col, 1, 1, 1, .8 );
246  vs_config->getColor( "nav", "highlighted_unit_off_tail", col, true );
247  highlighted_untail_col = GFXColor( col[0], col[1], col[2], col[3] );
248 
249  INIT_COL_ARRAY( col, 0, 0, 0, 0 ); //If not found, use defaults
250  unhighlighted_sun_col = getUnitTypeColor( "sun", false, col, unselectedalpha );
251  INIT_COL_ARRAY( col, 0, 0, 0, 0 );
252  unhighlighted_sun_text = getUnitTypeColor( "sun", true, col, unselectedalpha );
253 
254  //Planet color is the relation color, so is not defined here.
255  INIT_COL_ARRAY( col, 0, 0, 0, 0 );
256  unhighlighted_planet_text = getUnitTypeColor( "planet", true, col, unselectedalpha );
257 
258  INIT_COL_ARRAY( col, .3, .3, 1, .8 ); //If not found, use defaults
259  unhighlighted_c_player_col = getUnitTypeColor( "curplayer", false, col, .8 );
260  INIT_COL_ARRAY( col, .3, .3, 1, 0 );
261  unhighlighted_c_player_text = getUnitTypeColor( "curplayer", true, col, unselectedalpha );
262 
263  INIT_COL_ARRAY( col, .3, .3, 1, .8 ); //If not found, use defaults
264  unhighlighted_player_col = getUnitTypeColor( "player", false, col, .8 );
265  INIT_COL_ARRAY( col, .3, .3, 1, 0 );
266  unhighlighted_player_text = getUnitTypeColor( "player", true, col, unselectedalpha );
267 
268  INIT_COL_ARRAY( col, .3, .3, 1, .8 ); //If not found, use defaults
269  unhighlighted_player_col = getUnitTypeColor( "player", false, col, .8 );
270  INIT_COL_ARRAY( col, .3, .3, 1, 0 );
271  unhighlighted_player_text = getUnitTypeColor( "player", true, col, unselectedalpha );
272 
273  INIT_COL_ARRAY( col, 1, .8, .8, .6 ); //If not found, use defaults
274  unhighlighted_asteroid_col = getUnitTypeColor( "asteroid", false, col, .6 );
275  INIT_COL_ARRAY( col, 0, 0, 0, 0 );
276  unhighlighted_asteroid_text = getUnitTypeColor( "asteroid", true, col, unselectedalpha );
277 
278  INIT_COL_ARRAY( col, 1, .5, 1, .6 ); //If not found, use defaults
279  unhighlighted_nebula_col = getUnitTypeColor( "nebula", false, col, .6 );
280  INIT_COL_ARRAY( col, 0, 0, 0, 0 );
281  unhighlighted_nebula_text = getUnitTypeColor( "nebula", true, col, unselectedalpha );
282 
283  INIT_COL_ARRAY( col, .5, .9, .9, .6 ); //If not found, use defaults
284  unhighlighted_jump_col = getUnitTypeColor( "jump", false, col, .6 );
285  INIT_COL_ARRAY( col, .3, 1, .8, 0 );
286  unhighlighted_jump_text = getUnitTypeColor( "jump", true, col, unselectedalpha );
287 
288  //Basic unit types:
289  INIT_COL_ARRAY( col, 0, 0, 0, 0 );
290  unhighlighted_station_text = getUnitTypeColor( "station", true, col, unselectedalpha );
291  INIT_COL_ARRAY( col, 0, 0, 0, 0 );
292  unhighlighted_fighter_text = getUnitTypeColor( "fighter", true, col, unselectedalpha );
293  INIT_COL_ARRAY( col, 0, 0, 0, 0 );
294  unhighlighted_capship_text = getUnitTypeColor( "capship", true, col, unselectedalpha );
295  INIT_COL_ARRAY( col, 0, 0, 0, 0 );
296  unhighlighted_unit_text = getUnitTypeColor( "unit", true, col, unselectedalpha );
297 
298  init = true;
299  }
300 //if(source != NULL)
301 //relation = FactionUtil::GetIntRelation( ( UniverseUtil::getPlayerX(UniverseUtil::getCurrentPlayer()) )->faction ,source->faction);
302 //else
303 //relation = 0;
304  //the realtime relationship
305  if (source != NULL)
307  else
308  relation = 0;
309  relation = relation*0.5;
310  relation = relation+0.5;
311 
312 //to avoid duplicate code
313  GFXColor relColor( (1.0-relation), relation, ( 1.0-( 2.0*Delta( relation, 0.5 ) ) ), .7 );
314 //GFXColor((1.0-relation),relation,(1.0-(2.0*Delta(relation, 0.5))),1)
315  if (type == navsun) {
316  if (!inmouserange) {
317  NavigationSystem::DrawCircle( x, y, size, unhighlighted_sun_col );
318  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_sun_text );
319  } else {
320  if (currentistail)
321  NavigationSystem::DrawCircle( x, y, size, highlighted_tail_col );
322  else
323  NavigationSystem::DrawCircle( x, y, size, highlighted_untail_col );
324  }
325  } else if (type == navplanet) {
326  if (!inmouserange) {
327  NavigationSystem::DrawPlanet( x, y, size, relColor );
328  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_planet_text );
329  } else {
330  if (currentistail)
331  NavigationSystem::DrawPlanet( x, y, size, highlighted_tail_col );
332  else
333  NavigationSystem::DrawPlanet( x, y, size, highlighted_untail_col );
334  }
335  } else if (type == navcurrentplayer) {
336  if (!inmouserange) {
337  NavigationSystem::DrawHalfCircleTop( x, y, size, unhighlighted_c_player_col );
338  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_c_player_text );
339  } else {
340  if (currentistail)
341  NavigationSystem::DrawHalfCircleTop( x, y, size, highlighted_tail_col );
342  else
343  NavigationSystem::DrawHalfCircleTop( x, y, size, highlighted_untail_col );
344  }
345  } else if (type == navplayer) {
346  if (!inmouserange) {
347  NavigationSystem::DrawHalfCircleTop( x, y, size, unhighlighted_player_col );
348  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_player_col );
349  } else {
350  if (currentistail)
351  NavigationSystem::DrawHalfCircleTop( x, y, size, highlighted_tail_col );
352  else
353  NavigationSystem::DrawHalfCircleTop( x, y, size, highlighted_untail_col );
354  }
355  } else if (type == navstation) {
356  if (!inmouserange) {
357  NavigationSystem::DrawStation( x, y, size, relColor );
358  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_station_text );
359  } else {
360  if (currentistail)
361  NavigationSystem::DrawStation( x, y, size, highlighted_tail_col );
362  else
363  NavigationSystem::DrawStation( x, y, size, highlighted_untail_col );
364  }
365  } else if (type == navfighter) {
366  if (!inmouserange) {
367  if (factioncolours == NULL) {
368  NavigationSystem::DrawHalfCircleTop( x, y, size, relColor );
369  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_fighter_text );
370  } else {
371  NavigationSystem::DrawHalfCircleTop( x, y, size, relColor );
372  GFXColor thecolor = factioncolours[source->faction];
373  thecolor.a = unselectedalpha;
374  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, thecolor );
375  }
376  } else {
377  if (currentistail)
378  NavigationSystem::DrawHalfCircleTop( x, y, size, highlighted_tail_col );
379  else
380  NavigationSystem::DrawHalfCircleTop( x, y, size, highlighted_untail_col );
381  }
382  } else if (type == navcapship) {
383  if (!inmouserange) {
384  if (factioncolours == NULL) {
385  NavigationSystem::DrawCircle( x, y, size, relColor );
386  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_capship_text );
387  } else {
388  NavigationSystem::DrawCircle( x, y, size, relColor );
389  GFXColor thecolor = factioncolours[source->faction];
390 
391  thecolor.a = unselectedalpha;
392 
393  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, thecolor );
394  }
395  } else {
396  if (currentistail)
397  NavigationSystem::DrawCircle( x, y, size, highlighted_tail_col );
398  else
399  NavigationSystem::DrawCircle( x, y, size, highlighted_untail_col );
400  }
401  } else if (type == navmissile) {
402  if (!inmouserange) {
403  NavigationSystem::DrawMissile( x, y, size, relColor );
404 //drawdescription(source, x, y, 1.0, 1.0, false, screenoccupation, GFXColor(.2, 1, .5, unselectedalpha));
405  }
406  //NOT DRAWING NAME OF MISSILE TO MAKE ROOM FOR IMPORTANT TEXT ON SCREEN
407  else {
408  if (currentistail)
409  NavigationSystem::DrawMissile( x, y, size, highlighted_tail_col );
410  else
411  NavigationSystem::DrawMissile( x, y, size, highlighted_untail_col );
412  }
413  } else if (type == navasteroid) {
414  if (!inmouserange) {
415  NavigationSystem::DrawCircle( x, y, size, unhighlighted_asteroid_col );
416  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_asteroid_text );
417  } else {
418  if (currentistail)
419  NavigationSystem::DrawCircle( x, y, size, highlighted_tail_col );
420  else
421  NavigationSystem::DrawCircle( x, y, size, highlighted_untail_col );
422  }
423  } else if (type == navnebula) {
424  if (!inmouserange) {
425  NavigationSystem::DrawCircle( x, y, size, unhighlighted_nebula_col );
426  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_nebula_text );
427  } else {
428  if (currentistail)
429  NavigationSystem::DrawCircle( x, y, size, highlighted_tail_col );
430  else
431  NavigationSystem::DrawCircle( x, y, size, highlighted_untail_col );
432  }
433  } else if (type == navjump) {
434  if (!inmouserange) {
435  NavigationSystem::DrawJump( x, y, size, unhighlighted_jump_col );
436  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_jump_text );
437  } else {
438  if (currentistail)
439  NavigationSystem::DrawJump( x, y, size, highlighted_tail_col );
440  else
441  NavigationSystem::DrawJump( x, y, size, highlighted_untail_col );
442  }
443  } else {
444  if (!inmouserange) {
445  NavigationSystem::DrawCircle( x, y, size,
446  GFXColor( (1.0-relation), relation, ( 1.0-( 2.0*Delta( relation, 0.5 ) ) ), .6 ) );
447  drawdescription( source, x, y, 1.0, 1.0, false, screenoccupation, unhighlighted_unit_text );
448  } else {
449  if (currentistail)
450  NavigationSystem::DrawCircle( x, y, size, highlighted_tail_col );
451  else
452  NavigationSystem::DrawCircle( x, y, size, highlighted_untail_col );
453  }
454  }
455  //SHOW THE NAME ALL BIG AND SHIT
456  if ( (currentistail) && (inmouserange == 1) )
457  //DISPLAY THE NAME
458  drawdescription( source, x, y, 2.0, 2.0, false, screenoccupation, GFXColor( 1, 1, .7, 1 ) );
459 }
460 
461 void navdrawlist::draw() //Draw the items in the list
462 {
463  if (head == NULL) {
464  return;
465  } else {
466  navdrawnode *current = head;
467  while (current != NULL) {
468  drawlistitem( current->type,
469  current->size,
470  current->x,
471  current->y,
472  current->source,
473  screenoccupation,
474  inmouserange,
475  current == tail,
477  factioncolours );
478 
479  current = current->nextitem;
480  }
481  }
482 }
483