7 #include "WaypointList.hxx"
10 #include <boost/tuple/tuple.hpp>
12 #include <plib/puAux.h>
14 #include <simgear/route/waypoint.hxx>
15 #include <simgear/structure/callback.hxx>
16 #include <simgear/sg_inlines.h>
18 #include <Main/globals.hxx>
19 #include <Main/fg_props.hxx>
21 #include <Navaids/positioned.hxx>
22 #include <Autopilot/route_mgr.hxx>
24 using namespace flightgear;
32 static const double BLINK_TIME = 0.3;
33 static const int DRAG_START_DISTANCE_PX = 5;
35 class RouteManagerWaypointModel :
36 public WaypointList::Model,
37 public SGPropertyChangeListener
40 RouteManagerWaypointModel()
42 _rm = static_cast<FGRouteMgr*>(globals->get_subsystem("route-manager"));
44 SGPropertyNode* routeEdited = fgGetNode("/autopilot/route-manager/signals/edited", true);
45 routeEdited->addChangeListener(this);
48 virtual ~RouteManagerWaypointModel()
50 SGPropertyNode* routeEdited = fgGetNode("/autopilot/route-manager/signals/edited", true);
51 routeEdited->removeChangeListener(this);
54 // implement WaypointList::Model
55 virtual unsigned int numWaypoints() const
57 return _rm->numWaypts();
60 virtual int currentWaypoint() const
62 return _rm->currentIndex();
65 virtual flightgear::Waypt* waypointAt(unsigned int index) const
67 if (index >= numWaypoints()) {
71 return _rm->wayptAtIndex(index);
74 virtual void deleteAt(unsigned int index)
76 _rm->removeWayptAtIndex(index);
79 virtual void moveWaypointToIndex(unsigned int srcIndex, unsigned int destIndex)
81 SG_LOG(SG_GENERAL, SG_INFO, "moveWaypoint: from " << srcIndex << " to " << destIndex);
82 if (destIndex > srcIndex) {
86 WayptRef w(_rm->removeWayptAtIndex(srcIndex));
87 SG_LOG(SG_GENERAL, SG_INFO, "wpt:" << w->ident());
88 _rm->insertWayptAtIndex(w, destIndex);
91 virtual void setUpdateCallback(SGCallback* cb)
96 // implement SGPropertyChangeListener
97 void valueChanged(SGPropertyNode *prop)
108 //////////////////////////////////////////////////////////////////////////////
110 static void drawClippedString(puFont& font, const char* s, int x, int y, int maxWidth)
112 int fullWidth = font.getStringWidth(s);
113 if (fullWidth <= maxWidth) { // common case, easy and efficent
114 font.drawString(s, x, y);
119 int len = buf.size();
122 fullWidth = font.getStringWidth(buf.c_str());
123 } while (fullWidth > maxWidth);
125 font.drawString(buf.c_str(), x, y);
128 //////////////////////////////////////////////////////////////////////////////
130 WaypointList::WaypointList(int x, int y, int width, int height) :
131 puFrame(x, y, width, height),
132 GUI_ID(FGCLASS_WAYPOINTLIST),
135 _dragScroll(SCROLL_NO),
138 _updateCallback(NULL),
139 _scrollCallback(NULL),
142 // pretend to be a list, so fgPopup doesn't mess with our mouse events
143 type |= PUCLASS_LIST;
144 setModel(new RouteManagerWaypointModel());
145 setSize(width, height);
151 WaypointList::~WaypointList()
154 delete _updateCallback;
155 delete _scrollCallback;
158 void WaypointList::setUpdateCallback(SGCallback* cb)
160 _updateCallback = cb;
163 void WaypointList::setScrollCallback(SGCallback* cb)
165 _scrollCallback = cb;
168 void WaypointList::setSize(int width, int height)
170 double scrollP = getVScrollPercent();
172 puFrame::setSize(width, height);
174 if (wantsVScroll()) {
175 setVScrollPercent(scrollP);
181 int WaypointList::checkHit ( int button, int updown, int x, int y )
183 if ( isHit( x, y ) || ( puActiveWidget () == this ) )
185 doHit ( button, updown, x, y ) ;
193 void WaypointList::doHit( int button, int updown, int x, int y )
195 puFrame::doHit(button, updown, x, y);
196 if (updown == PU_DRAG) {
201 if (button != active_mouse_button) {
205 if (updown == PU_UP) {
206 puDeactivateWidget();
211 } else if (updown == PU_DOWN) {
212 puSetActiveWidget(this, x, y);
219 int row = rowForY(y - abox.min[1]);
220 if (row >= (int) _model->numWaypoints()) {
221 row = -1; // 'no selection'
224 if (row == getSelected()) {
225 _showLatLon = !_showLatLon;
233 void WaypointList::handleDrag(int x, int y)
236 // don't start drags immediately, require a certain mouse movement first
237 int manhattanLength = abs(x - _mouseDownX) + abs(y - _mouseDownY);
238 if (manhattanLength < DRAG_START_DISTANCE_PX) {
242 _dragSourceRow = rowForY(y - abox.min[1]);
243 Waypt* wp = _model->waypointAt(_dragSourceRow);
244 if (!wp || wp->flag(WPT_GENERATED)) {
245 return; // don't allow generated points to be dragged
249 _dragScroll = SCROLL_NO;
252 if (y < abox.min[1]) {
253 if (_dragScroll != SCROLL_DOWN) {
254 _dragScroll = SCROLL_DOWN;
255 _dragScrollTime.stamp();
257 } else if (y > abox.max[1]) {
258 if (_dragScroll != SCROLL_UP) {
259 _dragScroll = SCROLL_UP;
260 _dragScrollTime.stamp();
263 _dragScroll = SCROLL_NO;
264 _dragTargetRow = rowForY(y - abox.min[1] - (rowHeightPx() / 2));
268 void WaypointList::doDrop(int x, int y)
271 puDeactivateWidget();
273 SG_LOG(SG_GENERAL, SG_INFO, "doDrop");
275 if ((y < abox.min[1]) || (y >= abox.max[1])) {
276 SG_LOG(SG_GENERAL, SG_INFO, "y out of bounds:" << y);
280 if (_dragSourceRow == _dragTargetRow) {
281 SG_LOG(SG_GENERAL, SG_INFO, "source and target row match");
285 _model->moveWaypointToIndex(_dragSourceRow, _dragTargetRow);
287 // keep row indexes linged up when moving an item down the list
288 if (_dragSourceRow < _dragTargetRow) {
292 setSelected(_dragTargetRow);
295 void WaypointList::invokeDownCallback(void)
298 _dragScroll = SCROLL_NO;
299 SG_LOG(SG_GENERAL, SG_INFO, "cancel drag");
302 int WaypointList::rowForY(int y) const
308 // flip y to increase down, not up (as rows do)
309 int flipY = _heightPx - y;
310 int row = (flipY + _scrollPx) / rowHeightPx();
314 void WaypointList::draw( int dx, int dy )
316 puFrame::draw(dx, dy);
322 if (_dragScroll != SCROLL_NO) {
326 double dt = (SGTimeStamp::now() - _blinkTimer).toSecs();
327 if (dt > BLINK_TIME) {
332 glEnable(GL_SCISSOR_TEST);
333 GLint sx = (int) abox.min[0],
335 GLsizei w = (GLsizei) abox.max[0] - abox.min[0],
338 sx += border_thickness;
339 sy += border_thickness;
340 w -= 2 * border_thickness;
341 h -= 2 * border_thickness;
343 glScissor(sx + dx, sy + dy, w, h);
344 int row = firstVisibleRow(),
345 final = lastVisibleRow(),
346 rowHeight = rowHeightPx(),
349 y -= (_scrollPx % rowHeight); // partially draw the first row
351 _arrowWidth = legendFont.getStringWidth(">");
352 for ( ; row <= final; ++row, y += rowHeight) {
353 drawRow(dx, dy, row, y);
354 } // of row drawing iteration
356 glDisable(GL_SCISSOR_TEST);
359 // draw the insert marker after the rows
360 int insertY = (_dragTargetRow * rowHeight) - _scrollPx;
361 SG_CLAMP_RANGE(insertY, 0, std::min(_heightPx, totalHeightPx()));
363 glColor4f(1.0f, 0.5f, 0.0f, 0.8f);
366 glVertex2f(dx + abox.min[0], dy + abox.max[1] - insertY);
367 glVertex2f(dx + abox.max[0], dy + abox.max[1] - insertY);
372 void WaypointList::drawRow(int dx, int dy, int rowIndex, int y)
374 flightgear::Waypt* wp(_model->waypointAt(rowIndex));
376 bool isSelected = (rowIndex == getSelected());
377 bool isCurrent = (rowIndex == _model->currentWaypoint());
378 bool isDragSource = (_dragging && (rowIndex == _dragSourceRow));
381 bkgBox.min[1] = abox.max[1] - y;
382 bkgBox.max[1] = bkgBox.min[1] + rowHeightPx();
385 puFont* f = &legendFont;
386 bool drawBox = false;
388 if (wp->flag(WPT_MISS)) {
390 puSetColor(col, 1.0, 0.0, 0.0, 0.3); // red
391 } else if (wp->flag(WPT_ARRIVAL)) {
393 puSetColor(col, 0.0, 0.0, 0.0, 0.3);
394 } else if (wp->flag(WPT_DEPARTURE)) {
396 puSetColor(col, 0.0, 0.0, 0.0, 0.3);
400 // draw later, on *top* of text string
401 } else if (isSelected) { // -PLAIN means selected, apparently
402 bkgBox.draw(dx, dy, -PUSTYLE_PLAIN, colour, false, 0);
403 } else if (drawBox) {
404 bkgBox.draw(dx, dy, PUSTYLE_PLAIN, &col, false, 0);
408 glColor4f (1.0, 0.5, 0.0, 1.0) ;
410 glColor4fv ( colour [ PUCOL_LEGEND ] ) ;
413 int xx = dx + abox.min[0] + PUSTR_LGAP;
414 int yy = dy + abox.max[1] - y ;
415 yy += 4; // center text in row height
418 f->drawString(">", xx, yy);
422 x += _arrowWidth + PUSTR_LGAP;
427 int count = ::snprintf(buffer, 128, "%03d %-5s", rowIndex, wp->ident().c_str());
429 FGPositioned* src = wp->source();
430 if (src && !src->name().empty() && (src->name() != wp->ident())) {
431 // append name if present, and different to id
432 ::snprintf(buffer + count, 128 - count, " (%s)", src->name().c_str());
435 drawClippedString(legendFont, buffer, x, yy, 300);
436 x += 300 + PUSTR_LGAP;
439 SGGeod p(wp->position());
440 char ns = (p.getLatitudeDeg() > 0.0) ? 'N' : 'S';
441 char ew = (p.getLongitudeDeg() > 0.0) ? 'E' : 'W';
443 ::snprintf(buffer, 128 - count, "%4.2f%c %4.2f%c",
444 fabs(p.getLongitudeDeg()), ew, fabs(p.getLatitudeDeg()), ns);
445 } else if (rowIndex > 0) {
448 Waypt* prev = _model->waypointAt(rowIndex - 1);
449 boost::tie(courseDeg, distanceM) = wp->courseAndDistanceFrom(prev->position());
451 ::snprintf(buffer, 128 - count, "%03.0f %5.1fnm",
452 courseDeg, distanceM * SG_METER_TO_NM);
455 f->drawString(buffer, x, yy);
456 x += 100 + PUSTR_LGAP;
458 if (wp->altitudeRestriction() != RESTRICT_NONE) {
459 int altHundredFt = (wp->altitudeFt() + 50) / 100; // round to nearest 100ft
460 if (altHundredFt < 100) {
461 count = ::snprintf(buffer, 128, "%d'", altHundredFt * 100);
462 } else { // display as a flight-level
463 count = ::snprintf(buffer, 128, "FL%d", altHundredFt);
466 f->drawString(buffer, x, yy);
467 } // of valid wp altitude
468 x += 60 + PUSTR_LGAP;
470 if (wp->speedRestriction() != RESTRICT_NONE) {
471 count = ::snprintf(buffer, 126, "%dKts", (int) wp->speedKts());
472 f->drawString(buffer, x, yy);
476 puSetColor(col, 1.0, 0.5, 0.0, 0.5);
477 bkgBox.draw(dx, dy, PUSTYLE_PLAIN, &col, false, 0);
481 const double SCROLL_PX_SEC = 200.0;
483 void WaypointList::doDragScroll()
485 double dt = (SGTimeStamp::now() - _dragScrollTime).toSecs();
486 _dragScrollTime.stamp();
487 int deltaPx = (int)(dt * SCROLL_PX_SEC);
489 if (_dragScroll == SCROLL_UP) {
490 _scrollPx = _scrollPx - deltaPx;
491 SG_CLAMP_RANGE(_scrollPx, 0, scrollRangePx());
492 _dragTargetRow = firstVisibleRow();
494 _scrollPx = _scrollPx + deltaPx;
495 SG_CLAMP_RANGE(_scrollPx, 0, scrollRangePx());
496 _dragTargetRow = lastFullyVisibleRow() + 1;
499 if (_scrollCallback) {
500 (*_scrollCallback)();
504 int WaypointList::getSelected()
506 return getIntegerValue();
509 void WaypointList::setSelected(int rowIndex)
511 if (rowIndex == getSelected()) {
517 if (rowIndex == -1) {
521 ensureRowVisible(rowIndex);
524 void WaypointList::ensureRowVisible(int rowIndex)
526 if ((rowIndex >= firstFullyVisibleRow()) && (rowIndex <= lastFullyVisibleRow())) {
527 return; // already visible, fine
530 // ideal position would place the desired row in the middle of the
531 // visible section - hence subtract half the visible height.
532 int targetScrollPx = (rowIndex * rowHeightPx()) - (_heightPx / 2);
534 // clamp the scroll value to something valid
535 SG_CLAMP_RANGE(targetScrollPx, 0, scrollRangePx());
536 _scrollPx = targetScrollPx;
539 if (_scrollCallback) { // keep scroll observers in sync
540 (*_scrollCallback)();
544 unsigned int WaypointList::numWaypoints() const
550 return _model->numWaypoints();
553 bool WaypointList::wantsVScroll() const
555 return totalHeightPx() > _heightPx;
558 float WaypointList::getVScrollPercent() const
560 float scrollRange = scrollRangePx();
561 if (scrollRange < 1.0f) {
565 return _scrollPx / scrollRange;
568 float WaypointList::getVScrollThumbPercent() const
570 return _heightPx / (float) totalHeightPx();
573 void WaypointList::setVScrollPercent(float perc)
575 float scrollRange = scrollRangePx();
576 _scrollPx = (int)(scrollRange * perc);
579 int WaypointList::firstVisibleRow() const
581 return _scrollPx / rowHeightPx();
584 int WaypointList::firstFullyVisibleRow() const
586 int rh = rowHeightPx();
587 return (_scrollPx + rh - 1) / rh;
590 int WaypointList::numVisibleRows() const
592 int rh = rowHeightPx();
593 int topOffset = _scrollPx % rh; // pixels of first visible row
594 return (_heightPx - topOffset + rh - 1) / rh;
598 int WaypointList::numFullyVisibleRows() const
600 int rh = rowHeightPx();
601 int topOffset = _scrollPx % rh; // pixels of first visible row
602 return (_heightPx - topOffset) / rh;
605 int WaypointList::rowHeightPx() const
607 return legendFont.getStringHeight() + PUSTR_BGAP;
610 int WaypointList::scrollRangePx() const
612 return std::max(0, totalHeightPx() - _heightPx);
615 int WaypointList::totalHeightPx() const
621 return (int) _model->numWaypoints() * rowHeightPx();
624 int WaypointList::lastFullyVisibleRow() const
626 int row = firstFullyVisibleRow() + numFullyVisibleRows();
627 return std::min(row, (int) _model->numWaypoints() - 1);
630 int WaypointList::lastVisibleRow() const
632 int row = firstVisibleRow() + numVisibleRows();
633 return std::min(row, (int) _model->numWaypoints() - 1);
636 void WaypointList::setModel(Model* model)
643 _model->setUpdateCallback(make_callback(this, &WaypointList::modelUpdateCallback));
648 int WaypointList::checkKey (int key, int updown )
650 if ((updown == PU_UP) || !isVisible () || !isActive () || (window != puGetWindow())) {
661 setSelected(_model->numWaypoints() - 1);
665 case PU_KEY_PAGE_UP :
666 if (getSelected() >= 0) {
667 setSelected(getSelected() - 1);
672 case PU_KEY_PAGE_DOWN : {
673 int newSel = getSelected() + 1;
674 if (newSel >= (int) _model->numWaypoints()) {
683 if (getSelected() >= 0) {
684 Waypt* wp = _model->waypointAt(getSelected());
685 if (wp->flag(WPT_GENERATED)) {
689 if (wp->altitudeRestriction() != RESTRICT_NONE) {
690 int curAlt = (static_cast<int>(wp->altitudeFt()) + 50) / 100;
692 wp->setAltitude(0, RESTRICT_NONE);
694 wp->setAltitude((curAlt - 10) * 100, wp->altitudeRestriction());
701 if (getSelected() >= 0) {
702 flightgear::Waypt* wp = _model->waypointAt(getSelected());
703 if (wp->flag(WPT_GENERATED)) {
707 if (wp->altitudeRestriction() == RESTRICT_NONE) {
708 wp->setAltitude(1000, RESTRICT_AT);
710 int curAlt = (static_cast<int>(wp->altitudeFt()) + 50) / 100;
711 wp->setAltitude((curAlt + 10) * 100, wp->altitudeRestriction());
717 if (getSelected() >= 0) {
718 int index = getSelected();
719 Waypt* wp = _model->waypointAt(getSelected());
720 if (wp->flag(WPT_GENERATED)) {
724 _model->deleteAt(index);
725 setSelected(index - 1);
736 void WaypointList::modelUpdateCallback()
740 if (_updateCallback) {
741 (*_updateCallback)();
745 //////////////////////////////////////////////////////////////////////////////
748 static void handle_scrollbar(puObject* scrollbar)
750 ScrolledWaypointList* self = (ScrolledWaypointList*)scrollbar->getUserData();
751 self->setScrollPercent(scrollbar->getFloatValue());
754 static void waypointListCb(puObject* wpl)
756 ScrolledWaypointList* self = (ScrolledWaypointList*)wpl->getUserData();
757 self->setValue(wpl->getIntegerValue());
758 self->invokeCallback();
761 ScrolledWaypointList::ScrolledWaypointList(int x, int y, int width, int height) :
765 // ensure our type is compound, so fgPopup::applySize doesn't descend into
766 // us, and try to cast our children's user-data to GUIInfo*.
767 type |= PUCLASS_LIST;
772 void ScrolledWaypointList::setValue(float v)
774 puGroup::setValue(v);
778 void ScrolledWaypointList::setValue(int v)
780 puGroup::setValue(v);
784 void ScrolledWaypointList::init(int w, int h)
786 _list = new WaypointList(0, 0, w, h);
787 _list->setUpdateCallback(make_callback(this, &ScrolledWaypointList::modelUpdated));
788 _hasVScroll = _list->wantsVScroll();
789 _list->setUserData(this);
790 _list->setCallback(waypointListCb);
792 _list->setScrollCallback(make_callback(this, &ScrolledWaypointList::updateScroll));
794 _scrollbar = new puaScrollBar(w - _scrollWidth, 0, h,
795 1 /*arrow*/, 1 /* vertical */, _scrollWidth);
796 _scrollbar->setMinValue(0.0);
797 _scrollbar->setMaxValue(1.0);
798 _scrollbar->setUserData(this);
799 _scrollbar->setCallback(handle_scrollbar);
800 close(); // close the group
805 void ScrolledWaypointList::modelUpdated()
809 updateWantsScroll(w, h);
812 void ScrolledWaypointList::setScrollPercent(float v)
814 // slider's min is the bottom, so invert the value
815 _list->setVScrollPercent(1.0f - v);
818 void ScrolledWaypointList::setSize(int w, int h)
820 updateWantsScroll(w, h);
821 puGroup::setSize(w, h);
824 void ScrolledWaypointList::updateWantsScroll(int w, int h)
826 _hasVScroll = _list->wantsVScroll();
829 _scrollbar->reveal();
830 _scrollbar->setPosition(w - _scrollWidth, 0);
831 _scrollbar->setSize(_scrollWidth, h);
832 _list->setSize(w - _scrollWidth, h);
836 _list->setSize(w, h);
840 void ScrolledWaypointList::updateScroll()
842 // _scrollbar->setMaxValue(_list->numWaypoints());
843 _scrollbar->setValue(1.0f - _list->getVScrollPercent());
844 _scrollbar->setSliderFraction(_list->getVScrollThumbPercent());