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
TextArea Class Reference

#include <text_area.h>

Public Member Functions

 TextArea (void)
 
 TextArea (float x, float y, float wid, float hei, int scrollbar)
 
 ~TextArea (void)
 
int GetSelectedItem ()
 
void SetSelectedItem (int newh)
 
void DoHighlight (int yes)
 
void DoMultiline (int yes)
 
void Refresh (void)
 
void RenderText (void)
 
void AddTextItem (const char *name, const char *description, const char *parent_name=NULL, const GFXColor col=GFXColor(1, 1, 1, 1))
 
void ChangeTextItem (const char *name, const char *description, bool wrap=false)
 
void ChangeTextItemColor (const char *name, const GFXColor &col)
 
void SetText (const char *text)
 
void ClearList (void)
 
char * GetSelectedItemName (void)
 
char * GetSelectedItemDesc (void)
 
void SortList (void)
 
int MouseClick (int button, int state, float x, float y)
 
int MouseMove (float x, float y)
 
int MouseMoveClick (float x, float y)
 
int DoMouse (int type, float x, float y, int button, int state)
 

Detailed Description

Definition at line 86 of file text_area.h.

Constructor & Destructor Documentation

TextArea::TextArea ( void  )

Definition at line 37 of file text_area.cpp.

38 {
39  TextArea( 0, 0, 1, 1, 1 );
40 }
TextArea::TextArea ( float  x,
float  y,
float  wid,
float  hei,
int  scrollbar 
)

Definition at line 43 of file text_area.cpp.

References Refresh(), x, and y.

44 {
45  #ifdef DEBUG
46  cout<<"Displaying border at "<<x<<","<<y<<"\n";
47  cout<<"with the dimensions of "<<wid<<","<<y<<"\n";
48  cout<<"Scrollbar: "<<scrollbar<<endl;
49  cout<<"------------\n";
50  #endif
51 
52  LoadTextures();
53 
54  //Initialize the variables
55  item_count = 0;
56  cur_selected = 0;
57  top_item_number = 0;
58  button_pressed = 0;
59  cur_highlighted = 0;
60  page_size = 5;
61  scroll_start = 0;
62  scroll_cur = 0;
63  do_highlight = 1;
64  do_multiline = 0;
65 
66  ratio[0] = 0.06;
67  if (scrollbar == 0) {
68  ratio[1] = ratio[0];
69  has_scrollbar = 0;
70  } else {
71  ratio[1] = 0.12;
72  has_scrollbar = 1;
73  }
74  //Top and bottom button ratios. Only change these if you change the ratio[1] and/or the image
75  //If you change these, uncomment the appropriate ShowColor() in the Refresh to verify the alignment
76  button_ratio[0] = 0.91096; //left x, both buttons
77  button_ratio[1] = 0.0345; //Top button, top left corner y
78  button_ratio[2] = 0.0585; //Height and width of the buttons
79 
80  scrollbar_ratio[0] = 0.91096; //x axis
81  scrollbar_ratio[1] = 0.1; //y axis
82  scrollbar_ratio[2] = 0.0585; //Width
83  //scrollbar_ratio[3] = 0.89; // Height
84  scrollbar_ratio[3] = 0.73; //Height (how much the bar doesn't take of the buttons)
85 
86  //Set the variables to control where and how text will be displayed
87  font_size = 4;
88  font_size_float = 4;
89  horizontal_per_level = 0.05;
90  horizontal_spacer = 0.01;
91  vertical_left_of_text = 0.02;
92  text_spacing = (font_size_float/100)+(horizontal_spacer*2);
93 
94  //The parent TextAreaItem. This is the only link where parent == NULL. It is not displayed and handled only internally
95  ItemList = new TextAreaItem( "", "", NULL );
96  if (wid < 0 || hei < 0) {
97  printf( "Cannot draw border with negative height or width\n" );
98  return;
99  }
100  if (x+wid > 1) wid = 1-x;
101  if (y-hei < -1) hei = -1+y;
102  //Set the class variables to remember the dimensions and locations of the text area
103  xcoord[0] = x;
104  ycoord[0] = y;
105  width[0] = wid;
106  height[0] = hei;
107 
108  //Displayable text area
109  //Some parts of y and height are based on the width, not the height, of the text area
110  //If you want to see the area created by this, there's a commented ShowColor() at the end of TextArea::Refresh
111  xcoord[5] = x+(wid*ratio[0])+(wid*vertical_left_of_text);
112  ycoord[5] = y-(wid*ratio[0])-(wid*horizontal_spacer);
113  width[5] = ( wid*(1-ratio[0]-ratio[1]) )-(wid*vertical_left_of_text*2);
114  height[5] = ( hei*(1-ratio[0]) )-(wid*horizontal_spacer*2);
115 
116  //Top scroll button
117  xcoord[1] = x+(wid*button_ratio[0]);
118  ycoord[1] = y-(wid*button_ratio[1]);
119  width[1] = wid*button_ratio[2];
120  height[1] = width[1];
121 
122  //Bottom scroll button
123  xcoord[2] = xcoord[1];
124  ycoord[2] = y-hei+( wid*(button_ratio[2]+button_ratio[1]) );
125  //The original non-working patched formula. I spent so much time fixing this, that this line is to remember it. God knows why.
126  //ycoord[2] = y - (hei * (1 - button_ratio[1] - button_ratio[3]));
127  width[2] = width[1];
128  height[2] = width[1];
129 
130  //Scrollbar Area
131  xcoord[3] = x+(wid*scrollbar_ratio[0]);
132  ycoord[3] = y-(wid*scrollbar_ratio[1]);
133  width[3] = wid*scrollbar_ratio[2];
134  //height[3] = hei * scrollbar_ratio[3];
135  height[3] = hei-(height[1]+height[2])-( (height[2]*scrollbar_ratio[3])*2 );
136 
137  //Static portion of the active scrollbar
138  xcoord[4] = xcoord[3];
139  width[4] = width[3];
140 
141  max_lines = (height[5]/text_spacing)-1;
142 
143  Refresh();
144 }
TextArea::~TextArea ( void  )

Definition at line 35 of file text_area.cpp.

35 {}

Member Function Documentation

void TextArea::AddTextItem ( const char *  name,
const char *  description,
const char *  parent_name = NULL,
const GFXColor  col = GFXColor(                         1,                         1,                         1,                         1 ) 
)

Definition at line 261 of file text_area.cpp.

References TextAreaItem::AddChild(), and TextAreaItem::FindChild().

262 {
263  TextAreaItem *master;
264  master = ItemList->FindChild( parent_name );
265  item_count++;
266  if (master == NULL) ItemList->AddChild( name, description, col );
267 
268  else master->AddChild( name, description, col );
269 }
void TextArea::ChangeTextItem ( const char *  name,
const char *  description,
bool  wrap = false 
)

Definition at line 271 of file text_area.cpp.

References TextAreaItem::description, and TextAreaItem::FindChild().

272 {
273  TextAreaItem *search;
274  search = ItemList->FindChild( name );
275  if (search == 0) return;
276  if (search->description != 0) free( search->description );
277  search->description = strdup( description );
278 }
void TextArea::ChangeTextItemColor ( const char *  name,
const GFXColor col 
)

Definition at line 280 of file text_area.cpp.

References TextAreaItem::col, and TextAreaItem::FindChild().

281 {
282  TextAreaItem *search;
283  search = ItemList->FindChild( name );
284  if (search == 0) return;
285  search->col = col;
286 }
void TextArea::ClearList ( void  )

Definition at line 288 of file text_area.cpp.

Referenced by SetText().

289 {
290  //Wipe the list clean
291  if (ItemList != NULL) delete ItemList;
292  item_count = 0;
293  cur_selected = 0;
294  top_item_number = 0;
295  ItemList = new TextAreaItem( "", "", NULL );
296 }
void TextArea::DoHighlight ( int  yes)
int TextArea::DoMouse ( int  type,
float  x,
float  y,
int  button,
int  state 
)

Definition at line 422 of file text_area.cpp.

References MouseClick(), MouseMove(), and MouseMoveClick().

423 {
424  if (type == 1) return MouseClick( button, state, x, y );
425  if (type == 2) return MouseMoveClick( x, y );
426  if (type == 3) return MouseMove( x, y );
427  return 0;
428 }
void TextArea::DoMultiline ( int  yes)
inline

Definition at line 100 of file text_area.h.

101  {
102  do_multiline = yes;
103  } //DoMultiline(1) to enable multi-line entries
int TextArea::GetSelectedItem ( )
inline

Definition at line 91 of file text_area.h.

Referenced by GetSelectedItemDesc(), and GetSelectedItemName().

92  {
93  return cur_selected;
94  }
char * TextArea::GetSelectedItemDesc ( void  )

Definition at line 310 of file text_area.cpp.

References GetSelectedItem().

311 {
312  return GetSelectedItem( 2 );
313 }
char * TextArea::GetSelectedItemName ( void  )

Definition at line 306 of file text_area.cpp.

References GetSelectedItem().

307 {
308  return GetSelectedItem( 1 );
309 }
int TextArea::MouseClick ( int  button,
int  state,
float  x,
float  y 
)

Definition at line 332 of file text_area.cpp.

References WS_LEFT_BUTTON, WS_MOUSE_UP, and y.

Referenced by DoMouse().

333 {
334  if (state == WS_MOUSE_UP && scroll_start != 0) {
335  scroll_cur = 0;
336  scroll_start = 0;
337  return 1;
338  }
339  if (Inside( x, y, 0 ) == 0) return 0;
340  if (button != WS_LEFT_BUTTON) return 1; //Don't have anything to do with the middle and right button
341  //Check to see if the cursor is in the same x axis as the buttons and scrollbar
342  if ( x > xcoord[1] && x < (xcoord[1]+width[1]) ) {
343  //Find out if the click is on a button, the scrollbar, or nowhere
344  if ( y < ycoord[1] && y > (ycoord[1]-height[1]) ) {
345  if (state == WS_MOUSE_UP) {
346  //ShowImage(xcoord[1], ycoord[1], width[1], height[1], Images[IMG_BUTTON_UP], 0, 0);
347  button_pressed = 0;
348  } else {
349  //ShowImage(xcoord[1], ycoord[1], width[1], height[1], Images[IMG_HIGHLIGHT_BUTTON_UP], 0, 0);
350  button_pressed = 1;
351 
352  top_item_number--;
353  if (top_item_number < 0) top_item_number = 0;
354  if (cur_selected < 0) cur_selected = 0;
355  }
356  }
357  if ( y < ycoord[2] && y > (ycoord[2]-height[2]) ) {
358  if (state == WS_MOUSE_UP) {
359  //ShowImage(xcoord[2], ycoord[2], width[2], height[2], Images[IMG_BUTTON_DOWN], 0, 0);
360  button_pressed = 0;
361  } else {
362  //ShowImage(xcoord[2], ycoord[2], width[2], height[2], Images[IMG_HIGHLIGHT_BUTTON_DOWN], 0, 0);
363  button_pressed = 2;
364 
365  top_item_number++;
366  if ( top_item_number >= (item_count-max_lines) ) {
367  //This is to avoid a compile time warning.
368  #ifdef NO_WARNINGS
369  float tmp = item_count-max_lines;
370  char LINE[10];
371  sprintf( LINE, "%.0f", tmp );
372  top_item_number = atoi( LINE );
373  #else
374  top_item_number = item_count-max_lines;
375  #endif //NO_WARNINGS
376  }
377  }
378  }
379  }
380  if (Inside( x, y, 5 ) != 0 && do_highlight > 0)
381  cur_selected = LocateCount( y );
382  if (Inside( x, y, 3 ) != 0 && Inside( x, y, 4 ) == 0) {
383  if (state != WS_MOUSE_UP) return 1; //We're outside the active scroll bar, but in the passive area. The active scrollbar is only for MouseMoveClick
384  if (y > ycoord[4]) {
385  //Top area
386  top_item_number -= page_size;
387  if (top_item_number < 0) top_item_number = 0;
388  } else {
389  //Bottom area
390  top_item_number += page_size;
391  if ( top_item_number >= (item_count-max_lines) ) {
392  #ifdef NO_WARNINGS
393  float tmp = item_count-max_lines;
394  char LINE[10];
395  sprintf( LINE, "%.0f", tmp );
396  top_item_number = atoi( LINE );
397  #else
398  top_item_number = item_count-max_lines;
399  #endif //NO_WARNINGS
400  }
401  }
402  }
403  if ( Inside( x, y, 4 ) )
404  scroll_start = y;
405  return 1;
406 }
int TextArea::MouseMove ( float  x,
float  y 
)

Definition at line 407 of file text_area.cpp.

Referenced by DoMouse().

408 {
409  if (do_highlight == 0) return 0;
410  if (Inside( x, y, 5 ) == 0) return 0;
411  cur_highlighted = LocateCount( y );
412  return 1;
413 }
int TextArea::MouseMoveClick ( float  x,
float  y 
)

Definition at line 414 of file text_area.cpp.

References y.

Referenced by DoMouse().

415 {
416  if (scroll_start == 0 && Inside( x, y, 4 ) == 0) return 0;
417  if (scroll_start == 0) scroll_start = y;
418  scroll_cur = y;
419  return 1;
420 }
void TextArea::Refresh ( void  )

Definition at line 146 of file text_area.cpp.

References RenderText(), x, and y.

Referenced by TextArea().

147 {
148  float x = xcoord[0], y = ycoord[0], wid = width[0], hei = height[0];
149  //Local x,y,wid,hei for images to render
150  float lx = 0;
151  float ly = 0;
152  float lwid = 0;
153  float lhei = 0;
154 
155  //Ratio border and scrollbar
156  //float ratio[] = { 0.06, 0.12 };
157 
158  lwid = ratio[0]*wid;
159  lhei = ratio[1]*wid;
160  //Draw the bars to run across the length
161 /*
162  * // Top Horizontal
163  * lx = x;
164  * ly = y;
165  * ShowImage(lx, ly, wid, lwid, Images[IMG_TOP], 1, 0);
166  *
167  * // Left Horizontal
168  * ShowImage(lx, ly, lwid, hei, Images[IMG_LEFT_SIDE], 0, 1);
169  *
170  * // Bottom Horizontal
171  * ly = y - hei + lwid;
172  * ShowImage(lx, ly, wid, lwid, Images[IMG_BOTTOM], 1, 0);
173  *
174  * // Right Horizontal
175  * ly = y;
176  * lx = x + wid - lhei;
177  * if (has_scrollbar != 0) { ShowImage(lx, ly, lhei, hei, Images[IMG_RIGHT_SIDE], 0, 1); }
178  *
179  *
180  * // Draw the corners
181  *
182  * // Top right corner
183  * if (has_scrollbar != 0) { ShowImage(lx, ly, lhei, lhei, Images[IMG_CORNER_TOP_RIGHT], 0, 0); }
184  * else { ShowImage(lx, ly, lhei, lhei, Images[IMG_END], 0, 0); }
185  *
186  * // Bottom right corner
187  * ly = y - hei + lhei;
188  * if (has_scrollbar != 0) { ShowImage(lx, ly, lhei, lhei, Images[IMG_CORNER_BOTTOM_RIGHT], 0, 0); }
189  * else { ShowImage(lx, ly, lhei, lhei, Images[IMG_END], 0, 0); }
190  *
191  * // Bottom left corner
192  * lx = x;
193  * ly = y - hei + lwid;
194  * ShowImage(lx, ly, lwid, lwid, Images[IMG_CORNER_BOTTOM_LEFT], 0, 0);
195  *
196  * // Top left corner
197  * ly = y;
198  * ShowImage(lx, ly, lwid, lwid, Images[IMG_CORNER_TOP_LEFT], 0, 0);
199  *
200  * if (button_pressed == 1) {
201  * ShowImage(xcoord[1], ycoord[1], width[1], height[1], Images[IMG_HIGHLIGHT_BUTTON_UP], 0, 0);
202  * }
203  * if (button_pressed == 2) {
204  * ShowImage(xcoord[2], ycoord[2], width[2], height[2], Images[IMG_HIGHLIGHT_BUTTON_DOWN], 0, 0);
205  * }
206  */
207  if (has_scrollbar != 0) DisplayScrollbar();
208  RenderText();
209 //if (cur_highlighted > 0 && cur_highlighted != (cur_selected - top_item_number)) { HighlightCount(cur_highlighted, 1); }
210  if (cur_highlighted > 0) HighlightCount( cur_highlighted, 1 ); //if (cur_selected > 0) { HighlightCount(cur_selected - top_item_number, 2); }
211  if (cur_selected > 0) HighlightCount( cur_selected, 2 );
212  //Displays a transparent red box in the area where text can be displayed
213  //ShowColor(xcoord[5], ycoord[5], width[5], height[5], 1, 0, 0, 0.5);
214 
215  //Displays a transparent green box in the area where the buttons should be
216  //ShowColor(xcoord[1], ycoord[1], width[1], height[1], 0, 1, 0, 0.5);
217  //ShowColor(xcoord[2], ycoord[2], width[2], height[2], 0, 1, 0, 0.5);
218 
219  //Displays a transparent yellow box in the area where the scrollbar should be
220  //ShowColor(xcoord[3], ycoord[3], width[3], height[3], 1, 1, 0, 0.5);
221 
222  //ShowColor(xcoord[3], ycoord[3], width[3], height[3], 0.51, 0.47, 0.79, 1);
223  //ShowColor(xcoord[3], ycoord[3], width[3], height[3]/2, 0.66, 0.6, 1, 1);
224  //ShowColor(xcoord[3], ycoord[3], width[3], height[3]/2, 0, 1,1, 0.05);
225 
226  //ShowColor(xcoord[3], ycoord[3], width[3], height[3]/2, 1, 0, 0, 0.05);
227 
228  //ShowImage(xcoord[3], ycoord[3], width[3], height[3], Images[IMG_SCROLLBAR], 0, 1);
229 }
void TextArea::RenderText ( void  )

Definition at line 231 of file text_area.cpp.

Referenced by Refresh().

232 {
233  if (item_count == 0) return;
234  //There's a bug in glut_support. Can't show a color then text. Have to render an image between colors and text
235  //ShowImage(0,0,0,0, Images[0], 0, 0);
236  RenderTextItem( ItemList, 0 );
237 }
void TextArea::SetSelectedItem ( int  newh)
inline

Definition at line 95 of file text_area.h.

96  {
97  cur_selected = newh;
98  }
void TextArea::SetText ( const char *  text)

Definition at line 298 of file text_area.cpp.

References ClearList().

299 {
300  do_highlight = 0;
301  do_multiline = 1;
302  ClearList();
303  ChompIntoItems( text, NULL );
304 }
void TextArea::SortList ( void  )

Definition at line 325 of file text_area.cpp.

References TextAreaItem::Sort().

326 {
327  ItemList->Sort();
328 }

The documentation for this class was generated from the following files: