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

#include <picker.h>

Inheritance diagram for Picker:
Control EventResponder SimplePicker

Classes

struct  DisplayCell
 

Public Member Functions

void saveOpenCategories (std::list< std::list< std::string > > &idList) const
 
int restoreOpenCategories (const std::list< std::list< std::string > > &idList)
 
virtual void draw (void)
 
virtual PickerCellscells (void)
 
virtual const PickerCellscells (void) const
 
virtual void selectCell (PickerCell *cell, bool scroll=false)
 
virtual PickerCellselectedCell (void)
 
virtual int selectedItem (void)
 
virtual void scrollToCell (const PickerCell *cell, bool middle=false)
 
virtual void setMustRecalc (void)
 
virtual float extraCellHeight (void)
 
virtual void setExtraCellHeight (float f)
 
virtual GFXColor selectionColor (void)
 
virtual void setSelectionColor (const GFXColor &c)
 
virtual GFXColor selectionTextColor (void)
 
virtual void setSelectionTextColor (const GFXColor &c)
 
virtual GFXColor highlightColor (void)
 
virtual void setHighlightColor (const GFXColor &c)
 
virtual GFXColor highlightTextColor (void)
 
virtual void setHighlightTextColor (const GFXColor &c)
 
virtual Size textMargins (void)
 
virtual void setTextMargins (const Size &s)
 
virtual void setScroller (Scroller *s)
 
virtual bool processMouseDown (const InputEvent &event)
 
virtual bool processMouseUp (const InputEvent &event)
 
virtual bool processMouseMove (const InputEvent &event)
 
virtual bool processCommand (const EventCommandId &command, Control *control)
 
 Picker (void)
 
virtual ~Picker (void)
 
- Public Member Functions inherited from Control
virtual Rect rect (void)
 
virtual void setRect (const Rect &r)
 
virtual bool hitTest (const Point &p)
 
virtual bool hidden (void)
 
virtual void setHidden (bool h=true)
 
virtual const std::string & id (void)
 
virtual void setId (const std::string &newId)
 
virtual GFXColor color (void)
 
virtual void setColor (const GFXColor &c)
 
virtual GFXColor outlineColor (void)
 
virtual void setOutlineColor (const GFXColor &c)
 
virtual GFXColor textColor (void)
 
virtual void setTextColor (const GFXColor &c)
 
virtual Font font (void)
 
virtual void setFont (const Font &f)
 
virtual bool hasGroupChildren (void)
 
 Control (void)
 
virtual ~Control (void)
 
- Public Member Functions inherited from EventResponder
virtual bool processKeyDown (const InputEvent &event)
 
virtual bool processKeyUp (const InputEvent &event)
 
virtual void processUnfocus (const InputEvent &event)
 
virtual bool processMouseDrag (const InputEvent &event)
 
virtual void sendCommand (const EventCommandId &command, Control *control)
 
virtual void setCommandTarget (EventResponder *responder)
 
virtual void setModal (bool modal)
 
 EventResponder (void)
 
virtual ~EventResponder (void)
 

Protected Member Functions

float totalCellHeight (void)
 
PickerCellcellForMouse (const Point &p)
 
virtual void recalcDisplay (void)
 
void addListToDisplay (PickerCells *list, int level)
 
- Protected Member Functions inherited from Control
virtual void drawBackground (void)
 

Protected Attributes

PickerCellsm_cells
 
GFXColor m_selectionColor
 
GFXColor m_selectionTextColor
 
GFXColor m_highlightColor
 
GFXColor m_highlightTextColor
 
float m_extraCellHeight
 
Size m_textMargins
 
PickerCellm_cellPressed
 
const PickerCellm_highlightedCell
 
PickerCellm_selectedCell
 
Scrollerm_scroller
 
size_t m_scrollPosition
 
bool m_needRecalcDisplay
 
std::vector< DisplayCellm_displayCells
 
- Protected Attributes inherited from Control
Rect m_rect
 
std::string m_id
 
GFXColor m_color
 
GFXColor m_outlineColor
 
GFXColor m_textColor
 
Font m_font
 
bool m_hidden
 
- Protected Attributes inherited from EventResponder
bool m_modal
 
EventResponderm_commandTarget
 

Detailed Description

Definition at line 99 of file picker.h.

Constructor & Destructor Documentation

Picker::Picker ( void  )

Definition at line 396 of file picker.cpp.

References DISPLAY_VECTOR_RESERVE, and m_displayCells.

396  :
397  m_cells( NULL )
402  , m_extraCellHeight( 0.0 )
403  , m_textMargins( Size( 0.0, 0.0 ) )
404  , m_cellPressed( NULL )
405  , m_highlightedCell( NULL )
406  , m_selectedCell( NULL )
407  , m_scroller( NULL )
408  , m_scrollPosition( 0 )
409  , m_needRecalcDisplay( true )
410 {
412 }
Picker::~Picker ( void  )
virtual

Definition at line 414 of file picker.cpp.

414 {}

Member Function Documentation

void Picker::addListToDisplay ( PickerCells list,
int  level 
)
protected

Definition at line 252 of file picker.cpp.

References PickerCells::cellAt(), PickerCell::children(), PickerCells::count(), PickerCell::hideChildren(), i, and m_displayCells.

Referenced by recalcDisplay().

253 {
254  //Go through all the cells in this list.
255  for (int i = 0; i < list->count(); i++) {
256  PickerCell *cell = list->cellAt( i );
257  DisplayCell displayCell( cell, level );
258  m_displayCells.push_back( displayCell ); //Add this cell to the list.
259  PickerCells *children = cell->children();
260  if (!cell->hideChildren() && children != NULL)
261  //We have children to show, so add them, too.
262  addListToDisplay( children, level+1 );
263  }
264 }
PickerCell * Picker::cellForMouse ( const Point p)
protected

Definition at line 196 of file picker.cpp.

References float_to_int(), index, Rect::inside(), m_displayCells, Control::m_rect, m_scrollPosition, Rect::top(), totalCellHeight(), and Point::y.

Referenced by processMouseDown(), processMouseMove(), and processMouseUp().

197 {
198  if ( m_rect.inside( point ) ) {
199  const vector< DisplayCell >::size_type index = float_to_int(
200  (m_rect.top()-point.y)/totalCellHeight()+m_scrollPosition );
201  if ( index < m_displayCells.size() )
202  //It's within the cells we are displaying.
203  return m_displayCells[index].cell;
204  }
205  //Didn't find anything.
206  return NULL;
207 }
virtual PickerCells* Picker::cells ( void  )
inlinevirtual
virtual const PickerCells* Picker::cells ( void  ) const
inlinevirtual

Definition at line 114 of file picker.h.

References m_cells.

115  {
116  return m_cells;
117  }
void Picker::draw ( void  )
virtual

Implements Control.

Definition at line 117 of file picker.cpp.

References Picker::DisplayCell::cell, CHILD_INDENT_FACTOR, PaintText::draw(), Control::drawBackground(), drawRect(), GUI_CLEAR(), Size::height, i, Rect::inset(), isClear(), m_displayCells, Control::m_font, m_highlightColor, m_highlightedCell, m_highlightTextColor, m_needRecalcDisplay, Control::m_rect, m_scrollPosition, m_selectedCell, m_selectionColor, m_selectionTextColor, Control::m_textColor, m_textMargins, Rect::origin, Picker::DisplayCell::paintText, recalcDisplay(), Control::rect(), PaintText::setColor(), PaintText::setFont(), PaintText::setRect(), PaintText::setText(), Rect::size, PickerCell::text(), PickerCell::textColor(), Control::textColor(), totalCellHeight(), Size::width, Point::x, and Point::y.

118 {
119  //If we need to change the displayed cells, do that first.
121  recalcDisplay();
122  //Draw the background.
123  drawBackground();
124 
125  const float cellHeight = totalCellHeight();
126 
127  //This is the current cell rect. Start with control rect and adjust y.
128  Rect rect( m_rect );
129  rect.origin.y += m_rect.size.height-cellHeight;
130  rect.size.height = cellHeight;
131  for (vector< DisplayCell >::size_type i = m_scrollPosition; i< m_displayCells.size() && rect.origin.y >m_rect.origin.y;
132  i++) {
133  DisplayCell &display = m_displayCells[i];
134  const PickerCell *cell = display.cell; //Get the next cell.
135 
136  //Figure background and text color.
137  GFXColor backgroundColor = GUI_CLEAR;
138  GFXColor textColor = cell->textColor();
139  if (cell == m_selectedCell) {
140  //Selected state more important than highlighted state.
141  backgroundColor = m_selectionColor;
142  if ( isClear( textColor ) ) textColor = m_selectionTextColor;
143  }
144  //Selection color might be clear, or might be highlighted cell.
145  if (isClear( backgroundColor ) && cell == m_highlightedCell) {
146  //Highlighted cell.
147  backgroundColor = m_highlightColor;
148  if ( isClear( textColor ) ) textColor = m_highlightTextColor;
149  }
150  if ( !isClear( backgroundColor ) )
151  drawRect( rect, backgroundColor );
152  //If we haven't got a text color yet, use the control's color.
153  if ( isClear( textColor ) ) textColor = m_textColor;
154  //Include indent in drawing rect.
155  //Indent is based on cell height.
156  const float indentPerLevel = m_displayCells[i].level*cellHeight*CHILD_INDENT_FACTOR;
157  Rect drawRect = rect;
158  drawRect.inset( m_textMargins );
159  drawRect.origin.x += indentPerLevel;
160  drawRect.size.width -= indentPerLevel;
161 
162  //Paint the text.
163  //There is a PaintText object in each DisplayCell so that we don't have to re-layout the text
164  //all the time. This code should be smarter about only setting the attributes of the text
165  //object when things change, but that means cell changes need to be communicated back to
166  //this object, and they aren't now.
167  display.paintText.setRect( drawRect );
168  display.paintText.setText( cell->text() );
169  display.paintText.setFont( m_font );
170  display.paintText.setColor( textColor );
171  display.paintText.draw();
172 
173  rect.origin.y -= cellHeight;
174  }
175 }
virtual float Picker::extraCellHeight ( void  )
inlinevirtual

Definition at line 152 of file picker.h.

References m_extraCellHeight.

153  {
154  return m_extraCellHeight;
155  }
virtual GFXColor Picker::highlightColor ( void  )
inlinevirtual

Definition at line 182 of file picker.h.

References m_highlightColor.

183  {
184  return m_highlightColor;
185  }
virtual GFXColor Picker::highlightTextColor ( void  )
inlinevirtual

Definition at line 192 of file picker.h.

References m_highlightTextColor.

193  {
194  return m_highlightTextColor;
195  }
bool Picker::processCommand ( const EventCommandId command,
Control control 
)
virtual

Reimplemented from EventResponder.

Definition at line 327 of file picker.cpp.

References m_scroller, m_scrollPosition, EventResponder::processCommand(), and Scroller::scrollPosition().

328 {
329  if (command == "Scroller::PositionChanged") {
330  assert( control == m_scroller );
332  return true;
333  }
334  return Control::processCommand( command, control );
335 }
bool Picker::processMouseDown ( const InputEvent event)
virtual

Reimplemented from EventResponder.

Definition at line 338 of file picker.cpp.

References cellForMouse(), InputEvent::code, VegaConfig::getVariable(), globalEventManager(), Control::hitTest(), LEFT_MOUSE_BUTTON, InputEvent::loc, m_cellPressed, m_scroller, XMLSupport::parse_int(), EventResponder::processMouseDown(), EventManager::pushResponder(), Scroller::scrollPosition(), EventResponder::setModal(), Scroller::setScrollPosition(), vs_config, WHEELDOWN_MOUSE_BUTTON, and WHEELUP_MOUSE_BUTTON.

339 {
340  static int zoominc = XMLSupport::parse_int( vs_config->getVariable( "general", "wheel_increment_lines", "3" ) );
341  if (event.code == LEFT_MOUSE_BUTTON) {
342  PickerCell *cell = cellForMouse( event.loc );
343  if (cell != NULL) {
344  //We found the cell that was clicked.
345  m_cellPressed = cell;
346  setModal( true ); //Make sure we don't miss anything.
347  //Make sure we see mouse events *first* until we get a mouse-up.
349  return true;
350  }
351  } else if (event.code == WHEELUP_MOUSE_BUTTON) {
352  if ( hitTest( event.loc ) )
354  } else if (event.code == WHEELDOWN_MOUSE_BUTTON) {
355  if ( hitTest( event.loc ) )
357  }
358  return Control::processMouseDown( event );
359 }
bool Picker::processMouseMove ( const InputEvent event)
virtual

Reimplemented from EventResponder.

Definition at line 383 of file picker.cpp.

References cellForMouse(), InputEvent::loc, and m_highlightedCell.

384 {
385  const PickerCell *cell = cellForMouse( event.loc );
386  if (cell != NULL)
387  //Change the highlighted cell.
388  m_highlightedCell = cell;
389  else
390  //Make sure it's clear.
391  m_highlightedCell = NULL;
392  return true;
393 }
bool Picker::processMouseUp ( const InputEvent event)
virtual

Reimplemented from EventResponder.

Definition at line 362 of file picker.cpp.

References cellForMouse(), InputEvent::code, globalEventManager(), LEFT_MOUSE_BUTTON, InputEvent::loc, m_cellPressed, EventResponder::processMouseUp(), EventManager::removeResponder(), selectCell(), and EventResponder::setModal().

363 {
364  if (m_cellPressed && event.code == LEFT_MOUSE_BUTTON) {
365  //Select if the mouse goes up inside the pressed cell.
366  //If not, consider the action cancelled.
367  const bool newSelection = (cellForMouse( event.loc ) == m_cellPressed);
368 
369  //Make sure we get off the event chain.
370  globalEventManager().removeResponder( this, true );
371  setModal( false );
372  //Select a new cell, after we've cleaned up the event handling.
373  if (newSelection)
375  m_cellPressed = NULL;
376 
377  return true;
378  }
379  return Control::processMouseUp( event );
380 }
void Picker::recalcDisplay ( void  )
protectedvirtual

Definition at line 271 of file picker.cpp.

References addListToDisplay(), float_to_int(), Size::height, m_cells, m_displayCells, m_needRecalcDisplay, Control::m_rect, m_scroller, m_scrollPosition, Scroller::setRangeValues(), Scroller::setScrollPosition(), Rect::size, and totalCellHeight().

Referenced by draw(), scrollToCell(), and selectCell().

272 {
273  //Clear out the old display list.
274  m_displayCells.clear();
275 
276  //Recursively refill the display list.
278  if (m_scroller) {
279  //Update the scroller's view of the number of lines, and try to preserve the scroll position.
280  int oldScrollPosition = m_scrollPosition;
281  const int visibleCells = float_to_int( m_rect.size.height/totalCellHeight() );
282  m_scroller->setRangeValues( m_displayCells.size()-1, visibleCells );
283  m_scroller->setScrollPosition( oldScrollPosition );
284  }
285  //Mark that we don't need to recalc anymore.
286  m_needRecalcDisplay = false;
287 }
int Picker::restoreOpenCategories ( const std::list< std::list< std::string > > &  idList)

Definition at line 88 of file picker.cpp.

References cells(), PickerCells::cellWithId(), PickerCell::children(), PickerCells::count(), selectCell(), selectedCell(), PickerCell::setHideChildren(), and setMustRecalc().

Referenced by BaseComputer::refresh().

89 {
90  int numRestored = 0;
91  PickerCell *selectedCell = NULL;
92  for (std::list< std::list< std::string > >::const_iterator catIt = idList.begin();
93  catIt != idList.end();
94  ++catIt) {
95  PickerCells *celllist = cells();
96  for (std::list< std::string >::const_iterator travIt = (*catIt).begin();
97  travIt != (*catIt).end();
98  ++travIt) {
99  PickerCell *cell = celllist->cellWithId( (*travIt) );
100  if (!cell)
101  break;
102  celllist = cell->children();
103  if (!celllist || celllist->count() == 0) {
104  selectedCell = cell;
105  break;
106  }
107  cell->setHideChildren( false );
108  }
109  }
110  setMustRecalc();
111  if (selectedCell)
112  selectCell( selectedCell, true );
113  return numRestored;
114 }
void Picker::saveOpenCategories ( std::list< std::list< std::string > > &  idList) const

Definition at line 82 of file picker.cpp.

References cells(), m_selectedCell, and PickerCells::saveOpenCategories().

Referenced by BaseComputer::refresh().

83 {
84  std::list< std::string >base;
85  cells()->saveOpenCategories( idList, base, m_selectedCell );
86 }
void Picker::scrollToCell ( const PickerCell cell,
bool  middle = false 
)
virtual

Definition at line 292 of file picker.cpp.

References float_to_int(), Size::height, i, m_displayCells, m_needRecalcDisplay, Control::m_rect, m_scroller, m_scrollPosition, recalcDisplay(), SCROLL_EDGE_EXTRA, Scroller::setScrollPosition(), Rect::size, and totalCellHeight().

Referenced by BaseComputer::scrollToItem(), and selectCell().

293 {
294  if (!cell || !m_scroller) return;
295  //If we need to change the displayed cells, do that first.
297  recalcDisplay();
298  for (vector< DisplayCell >::size_type i = 0; i < m_displayCells.size(); i++)
299  if (cell == m_displayCells[i].cell) {
300  const int visibleCells = float_to_int( m_rect.size.height/totalCellHeight() );
301  if (middle) {
302  //Regardless of where cell is, try to put it in the middle of
303  //the visible area.
304  m_scroller->setScrollPosition( i-visibleCells/2 );
305  } else {
306  //Just make sure we can see it.
307  if (i < m_scrollPosition)
308  //Cell is too "high". Move it to the top line.
310  else if (i >= m_scrollPosition+visibleCells)
311  //Cell is too "low". Move it to the bottom line.
312  m_scroller->setScrollPosition( i-visibleCells+1+SCROLL_EDGE_EXTRA );
313  }
314  //Found the cell. Done with loop.
315  break;
316  }
317 }
void Picker::selectCell ( PickerCell cell,
bool  scroll = false 
)
virtual

Definition at line 210 of file picker.cpp.

References PickerCells::cellAt(), PickerCell::children(), PickerCells::count(), PickerCell::hideChildren(), m_selectedCell, recalcDisplay(), scrollToCell(), EventResponder::sendCommand(), PickerCell::setHideChildren(), and setMustRecalc().

Referenced by BaseComputer::buyUpgrade(), processMouseUp(), UpgradeOperationMountDialog::processWindowCommand(), restoreOpenCategories(), BaseComputer::scrollToItem(), and BaseComputer::updateTransactionControlsForSelection().

211 {
212  PickerCell *oldCell = m_selectedCell;
213  m_selectedCell = cell;
214  //If the cell has children, flip whether the children are displayed.
215  if (cell != NULL) {
216  PickerCells *list = cell->children();
217  if (list != NULL && list->count() > 0) {
218  const bool hideChildren = !cell->hideChildren();
219  cell->setHideChildren( hideChildren );
220  if (!hideChildren) {
221  recalcDisplay();
222  //Make sure the children are visible.
223  PickerCells *loopList = list;
224  PickerCell *lastChild = NULL;
225  while (true) {
226  lastChild = loopList->cellAt( loopList->count()-1 );
227  if ( lastChild->hideChildren() )
228  //Can't see children below this. Done.
229  break;
230  loopList = lastChild->children();
231  if (loopList == NULL || loopList->count() == 0)
232  //lastChild has no children.
233  break;
234  }
235  scrollToCell( lastChild );
236  //Now make sure the original parent is still visible.
237  scrollToCell( cell );
238  } else {
239  setMustRecalc();
240  }
241  }
242  if (scroll)
243  //Make sure the cell is visible.
244  scrollToCell( cell );
245  }
246  if (oldCell != m_selectedCell)
247  sendCommand( "Picker::NewSelection", this );
248 }
int Picker::selectedItem ( void  )
virtual

Definition at line 180 of file picker.cpp.

References PickerCells::cellAt(), PickerCells::count(), i, m_cells, and m_selectedCell.

Referenced by ListQuestionDialog::processWindowCommand().

181 {
182  if (m_cells != NULL && m_selectedCell != NULL) {
183  //If we have a selection, find it in the list. Won't find it if it's a child.
184  for (int i = 0; i < m_cells->count(); i++) {
185  PickerCell *cell = m_cells->cellAt( i );
186  if (cell == m_selectedCell)
187  //Found it.
188  return i;
189  }
190  }
191  //Didn't find it.
192  return -1;
193 }
virtual GFXColor Picker::selectionColor ( void  )
inlinevirtual

Definition at line 162 of file picker.h.

References m_selectionColor.

163  {
164  return m_selectionColor;
165  }
virtual GFXColor Picker::selectionTextColor ( void  )
inlinevirtual

Definition at line 172 of file picker.h.

References m_selectionTextColor.

173  {
174  return m_selectionTextColor;
175  }
virtual void Picker::setExtraCellHeight ( float  f)
inlinevirtual

Definition at line 156 of file picker.h.

References m_extraCellHeight.

157  {
159  }
virtual void Picker::setHighlightColor ( const GFXColor c)
inlinevirtual
virtual void Picker::setHighlightTextColor ( const GFXColor c)
inlinevirtual
virtual void Picker::setMustRecalc ( void  )
inlinevirtual

Definition at line 142 of file picker.h.

References m_needRecalcDisplay.

Referenced by SimplePicker::clear(), restoreOpenCategories(), BaseComputer::scrollToItem(), and selectCell().

143  {
144  m_needRecalcDisplay = true;
145  }
void Picker::setScroller ( Scroller s)
virtual
virtual void Picker::setSelectionColor ( const GFXColor c)
inlinevirtual
virtual void Picker::setSelectionTextColor ( const GFXColor c)
inlinevirtual

Definition at line 176 of file picker.h.

References c, and m_selectionTextColor.

177  {
179  }
virtual void Picker::setTextMargins ( const Size s)
inlinevirtual
virtual Size Picker::textMargins ( void  )
inlinevirtual

Definition at line 202 of file picker.h.

References m_textMargins.

203  {
204  return m_textMargins;
205  }
float Picker::totalCellHeight ( void  )
inlineprotected

Definition at line 229 of file picker.h.

References m_extraCellHeight, Control::m_font, and Font::size().

Referenced by cellForMouse(), draw(), recalcDisplay(), and scrollToCell().

230  {
231  return m_font.size()+m_extraCellHeight;
232  }

Member Data Documentation

PickerCell* Picker::m_cellPressed
protected

Definition at line 269 of file picker.h.

Referenced by SimplePicker::clear(), processMouseDown(), and processMouseUp().

PickerCells* Picker::m_cells
protected
std::vector< DisplayCell > Picker::m_displayCells
protected

Definition at line 275 of file picker.h.

Referenced by addListToDisplay(), cellForMouse(), draw(), Picker(), recalcDisplay(), and scrollToCell().

float Picker::m_extraCellHeight
protected

Definition at line 267 of file picker.h.

Referenced by extraCellHeight(), setExtraCellHeight(), and totalCellHeight().

GFXColor Picker::m_highlightColor
protected

Definition at line 265 of file picker.h.

Referenced by draw(), highlightColor(), and setHighlightColor().

const PickerCell* Picker::m_highlightedCell
protected

Definition at line 270 of file picker.h.

Referenced by SimplePicker::clear(), draw(), and processMouseMove().

GFXColor Picker::m_highlightTextColor
protected

Definition at line 266 of file picker.h.

Referenced by draw(), highlightTextColor(), and setHighlightTextColor().

bool Picker::m_needRecalcDisplay
protected

Definition at line 274 of file picker.h.

Referenced by draw(), recalcDisplay(), scrollToCell(), and setMustRecalc().

Scroller* Picker::m_scroller
protected

Definition at line 272 of file picker.h.

Referenced by processCommand(), processMouseDown(), recalcDisplay(), scrollToCell(), and setScroller().

size_t Picker::m_scrollPosition
protected

Definition at line 273 of file picker.h.

Referenced by cellForMouse(), draw(), processCommand(), recalcDisplay(), and scrollToCell().

PickerCell* Picker::m_selectedCell
protected
GFXColor Picker::m_selectionColor
protected

Definition at line 263 of file picker.h.

Referenced by draw(), selectionColor(), and setSelectionColor().

GFXColor Picker::m_selectionTextColor
protected

Definition at line 264 of file picker.h.

Referenced by draw(), selectionTextColor(), and setSelectionTextColor().

Size Picker::m_textMargins
protected

Definition at line 268 of file picker.h.

Referenced by draw(), setTextMargins(), and textMargins().


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