Debian Codegraph @

src/GUI/WaypointList.cxx

raw symbols SCIP
1
2
3 #ifdef HAVE_CONFIG_H
4 # include "config.h"
5 #endif
6
7 #include "WaypointList.hxx"
8
9 #include <algorithm>
10 #include <utility>
11
12 #include <plib/puAux.h>
13
14 #include <simgear/structure/callback.hxx>
15 #include <simgear/sg_inlines.h>
16
17 #include <Main/globals.hxx>
18 #include <Main/fg_props.hxx>
19
20 #include <Navaids/positioned.hxx>
21 #include <Navaids/routePath.hxx>
22 #include <Autopilot/route_mgr.hxx>
23
24 // select if the widget grabs keys necessary to fly aircraft from the keyboard,
25 // or not. See http://code.google.com/p/flightgear-bugs/issues/detail?id=338
26 // for discussion about why / what is going on.
27 #define AVOID_FLIGHT_KEYS 1
28
29 using namespace flightgear;
30
31 enum {
32 SCROLL_NO = 0,
33 SCROLL_UP,
34 SCROLL_DOWN
35 };
36
37 static const double BLINK_TIME = 0.3;
38 static const int DRAG_START_DISTANCE_PX = 5;
39
40 class FlightPlanWaypointModel :
41 public WaypointList::Model,
42 public SGPropertyChangeListener
43 {
44 public:
45 FlightPlanWaypointModel(flightgear::FlightPlan* fp) :
46 _fp(fp)
47 {
48 SGPropertyNode* routeEdited = fgGetNode("/autopilot/route-manager/signals/edited", true);
49 SGPropertyNode* flightplanChanged = fgGetNode("/autopilot/route-manager/signals/flightplan-changed", true);
50 routeEdited->addChangeListener(this);
51 flightplanChanged->addChangeListener(this);
52 }
53
54 ~FlightPlanWaypointModel()
55 {
56 SGPropertyNode* routeEdited = fgGetNode("/autopilot/route-manager/signals/edited", true);
57 SGPropertyNode* flightplanChanged = fgGetNode("/autopilot/route-manager/signals/flightplan-changed", true);
58 routeEdited->removeChangeListener(this);
59 flightplanChanged->removeChangeListener(this);
60 }
61
62 // implement WaypointList::Model
63 virtual unsigned int numWaypoints() const
64 {
65 return _fp->numLegs();
66 }
67
68 virtual int currentWaypoint() const
69 {
70 return _fp->currentIndex();
71 }
72
73 virtual flightgear::Waypt* waypointAt(unsigned int index) const
74 {
75 if (index >= numWaypoints()) {
76 return NULL;
77 }
78
79 return _fp->legAtIndex(index)->waypoint();
80 }
81
82 virtual flightgear::FlightPlan* flightplan() const
83 {
84 return _fp;
85 }
86
87 virtual void deleteAt(unsigned int index)
88 {
89 _fp->deleteIndex(index);
90 }
91
92 virtual void moveWaypointToIndex(unsigned int srcIndex, unsigned int destIndex)
93 {
94 SG_LOG(SG_GENERAL, SG_INFO, "moveWaypoint: from " << srcIndex << " to " << destIndex);
95 if (destIndex > srcIndex) {
96 --destIndex;
97 }
98
99 int currentWpIndex = currentWaypoint();
100
101 WayptRef w = _fp->legAtIndex(srcIndex)->waypoint();
102 _fp->deleteIndex(srcIndex);
103 _fp->insertWayptAtIndex(w, destIndex);
104
105 if ((signed int) srcIndex == currentWpIndex) {
106 // current waypoint was moved
107 _fp->setCurrentIndex(destIndex);
108 }
109 }
110
111 virtual void setUpdateCallback(simgear::Callback cb)
112 {
113 _cb = cb;
114 }
115
116 // implement SGPropertyChangeListener
117 void valueChanged(SGPropertyNode *prop)
118 {
119 if (prop->getNameString() == "edited") {
120 if (_cb) {
121 _cb();
122 }
123 }
124
125 if (prop->getNameString() == "flightplan-changed") {
126 _fp =
127 globals->get_subsystem<FGRouteMgr>()->flightPlan();
128 }
129 }
130 private:
131 flightgear::FlightPlan* _fp;
132 simgear::Callback _cb;
133 };
134
135 //////////////////////////////////////////////////////////////////////////////
136
137 static void drawClippedString(puFont& font, const char* s, int x, int y, int maxWidth)
138 {
139 int fullWidth = font.getStringWidth(s);
140 if (fullWidth <= maxWidth) { // common case, easy and efficient
141 font.drawString(s, x, y);
142 return;
143 }
144
145 std::string buf(s);
146 int len = buf.size();
147 do {
148 buf.resize(--len);
149 fullWidth = font.getStringWidth(buf.c_str());
150 } while (fullWidth > maxWidth);
151
152 font.drawString(buf.c_str(), x, y);
153 }
154
155 //////////////////////////////////////////////////////////////////////////////
156
157 WaypointList::WaypointList(int x, int y, int width, int height) :
158 puFrame(x, y, width, height),
159 GUI_ID(FGCLASS_WAYPOINTLIST),
160 _scrollPx(0),
161 _dragging(false),
162 _dragScroll(SCROLL_NO),
163 _showLatLon(false),
164 _model(NULL),
165 _updateCallback(NULL),
166 _scrollCallback(NULL),
167 _blink(false)
168 {
169 // pretend to be a list, so fgPopup doesn't mess with our mouse events
170 type |= PUCLASS_LIST;
171 flightgear::FlightPlan* fp =
172 globals->get_subsystem<FGRouteMgr>()->flightPlan();
173 setModel(new FlightPlanWaypointModel(fp));
174 setSize(width, height);
175 setValue(-1);
176
177 _blinkTimer.stamp();
178 }
179
180 WaypointList::~WaypointList()
181 {
182 delete _model;
183 }
184
185 void WaypointList::setUpdateCallback(simgear::Callback cb)
186 {
187 _updateCallback = std::move(cb);
188 }
189
190 void WaypointList::setScrollCallback(simgear::Callback cb)
191 {
192 _scrollCallback = std::move(cb);
193 }
194
195 void WaypointList::setSize(int width, int height)
196 {
197 double scrollP = getVScrollPercent();
198 _heightPx = height;
199 puFrame::setSize(width, height);
200
201 if (wantsVScroll()) {
202 setVScrollPercent(scrollP);
203 } else {
204 _scrollPx = 0;
205 }
206 }
207
208 int WaypointList::checkHit ( int button, int updown, int x, int y )
209 {
210 if ( isHit( x, y ) || ( puActiveWidget () == this ) )
211 {
212 doHit ( button, updown, x, y ) ;
213 return TRUE ;
214 }
215
216 return FALSE ;
217 }
218
219
220 void WaypointList::doHit( int button, int updown, int x, int y )
221 {
222 puFrame::doHit(button, updown, x, y);
223 if (updown == PU_DRAG) {
224 handleDrag(x, y);
225 return;
226 }
227
228 if (button != active_mouse_button) {
229 return;
230 }
231
232 if (updown == PU_UP) {
233 puDeactivateWidget();
234 if (_dragging) {
235 doDrop(x, y);
236 return;
237 }
238 } else if (updown == PU_DOWN) {
239 puSetActiveWidget(this, x, y);
240 _mouseDownX = x;
241 _mouseDownY = y;
242 return;
243 }
244
245 // update selection
246 int row = rowForY(y - abox.min[1]);
247 if (row >= (int) _model->numWaypoints()) {
248 row = -1; // 'no selection'
249 }
250
251 if (row == getSelected()) {
252 _showLatLon = !_showLatLon;
253 puPostRefresh();
254 return;
255 }
256
257 setSelected(row);
258 }
259
260 void WaypointList::handleDrag(int x, int y)
261 {
262 if (!_dragging) {
263 // don't start drags immediately, require a certain mouse movement first
264 int manhattanLength = abs(x - _mouseDownX) + abs(y - _mouseDownY);
265 if (manhattanLength < DRAG_START_DISTANCE_PX) {
266 return;
267 }
268
269 _dragSourceRow = rowForY(y - abox.min[1]);
270 Waypt* wp = _model->waypointAt(_dragSourceRow);
271 if (!wp || wp->flag(WPT_GENERATED) || (wp->type() == "discontinuity")) {
272 return; // don't allow generated points to be dragged
273 }
274
275 _dragging = true;
276 _dragScroll = SCROLL_NO;
277 }
278
279 if (y < abox.min[1]) {
280 if (_dragScroll != SCROLL_DOWN) {
281 _dragScroll = SCROLL_DOWN;
282 _dragScrollTime.stamp();
283 }
284 } else if (y > abox.max[1]) {
285 if (_dragScroll != SCROLL_UP) {
286 _dragScroll = SCROLL_UP;
287 _dragScrollTime.stamp();
288 }
289 } else {
290 _dragScroll = SCROLL_NO;
291 _dragTargetRow = rowForY(y - abox.min[1] - (rowHeightPx() / 2));
292 }
293 }
294
295 void WaypointList::doDrop(int x, int y)
296 {
297 _dragging = false;
298 puDeactivateWidget();
299
300 SG_LOG(SG_GENERAL, SG_INFO, "doDrop");
301
302 if ((y < abox.min[1]) || (y >= abox.max[1])) {
303 SG_LOG(SG_GENERAL, SG_INFO, "y out of bounds:" << y);
304 return;
305 }
306
307 if (_dragSourceRow == _dragTargetRow) {
308 SG_LOG(SG_GENERAL, SG_INFO, "source and target row match");
309 return;
310 }
311
312 _model->moveWaypointToIndex(_dragSourceRow, _dragTargetRow);
313
314 // keep row indexes linged up when moving an item down the list
315 if (_dragSourceRow < _dragTargetRow) {
316 --_dragTargetRow;
317 }
318
319 setSelected(_dragTargetRow);
320 }
321
322 void WaypointList::invokeDownCallback(void)
323 {
324 _dragging = false;
325 _dragScroll = SCROLL_NO;
326 SG_LOG(SG_GENERAL, SG_INFO, "cancel drag");
327 }
328
329 int WaypointList::rowForY(int y) const
330 {
331 if (!_model) {
332 return -1;
333 }
334
335 // flip y to increase down, not up (as rows do)
336 int flipY = _heightPx - y;
337 int row = (flipY + _scrollPx) / rowHeightPx();
338 return row;
339 }
340
341 void WaypointList::draw( int dx, int dy )
342 {
343 puFrame::draw(dx, dy);
344
345 if (!_model) {
346 return;
347 }
348
349 if (_dragScroll != SCROLL_NO) {
350 doDragScroll();
351 }
352
353 double dt = (SGTimeStamp::now() - _blinkTimer).toSecs();
354 if (dt > BLINK_TIME) {
355 _blinkTimer.stamp();
356 _blink = !_blink;
357 }
358
359 glEnable(GL_BLEND);
360 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
361 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
362
363 glEnable(GL_SCISSOR_TEST);
364 GLint sx = (int) abox.min[0],
365 sy = abox.min[1];
366 GLsizei w = (GLsizei) abox.max[0] - abox.min[0],
367 h = _heightPx;
368
369 sx += border_thickness;
370 sy += border_thickness;
371 w -= 2 * border_thickness;
372 h -= 2 * border_thickness;
373
374 glScissor(sx + dx, sy + dy, w, h);
375 int row = firstVisibleRow(),
376 finalRow = lastVisibleRow(),
377 rowHeight = rowHeightPx(),
378 y = rowHeight;
379
380 y -= (_scrollPx % rowHeight); // partially draw the first row
381
382 _arrowWidth = legendFont.getStringWidth(">");
383 _latLonFormat = static_cast<simgear::strutils::LatLonFormat>(fgGetInt("/sim/lon-lat-format"));
384
385 RoutePath path(_model->flightplan());
386
387 for ( ; row <= finalRow; ++row, y += rowHeight) {
388 drawRow(dx, dy, row, y, path);
389 } // of row drawing iteration
390
391 glDisable(GL_SCISSOR_TEST);
392
393 if (_dragging) {
394 // draw the insert marker after the rows
395 int insertY = (_dragTargetRow * rowHeight) - _scrollPx;
396 SG_CLAMP_RANGE(insertY, 0, std::min(_heightPx, totalHeightPx()));
397
398 glColor4f(1.0f, 0.5f, 0.0f, 0.8f);
399 glLineWidth(3.0f);
400 glBegin(GL_LINES);
401 glVertex2f(dx + abox.min[0], dy + abox.max[1] - insertY);
402 glVertex2f(dx + abox.max[0], dy + abox.max[1] - insertY);
403 glEnd();
404 }
405
406 glDisable(GL_BLEND);
407 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
408 }
409
410 void WaypointList::drawRow(int dx, int dy, int rowIndex, int y,
411 const RoutePath& path)
412 {
413 flightgear::Waypt* wp(_model->waypointAt(rowIndex));
414
415 bool isSelected = (rowIndex == getSelected());
416 bool isCurrent = (rowIndex == _model->currentWaypoint());
417 bool isDragSource = (_dragging && (rowIndex == _dragSourceRow));
418
419 puBox bkgBox = abox;
420 bkgBox.min[1] = abox.max[1] - y;
421 bkgBox.max[1] = bkgBox.min[1] + rowHeightPx();
422
423 puColour col;
424 puFont* f = &legendFont;
425 bool drawBox = false;
426
427 if (wp->flag(WPT_MISS)) {
428 drawBox = true;
429 puSetColor(col, 1.0, 0.0, 0.0, 0.3); // red
430 } else if (wp->flag(WPT_ARRIVAL) || wp->flag(WPT_DEPARTURE)) {
431 drawBox = true;
432 puSetColor(col, 0.0, 0.0, 0.0, 0.3);
433 } else if (wp->flag(WPT_APPROACH)) {
434 drawBox = true;
435 puSetColor(col, 0.0, 0.0, 0.1, 0.3);
436 }
437
438 if (isDragSource) {
439 // draw later, on *top* of text string
440 } else if (isSelected) { // -PLAIN means selected, apparently
441 bkgBox.draw(dx, dy, -PUSTYLE_PLAIN, colour, false, 0);
442 } else if (drawBox) {
443 bkgBox.draw(dx, dy, PUSTYLE_PLAIN, &col, false, 0);
444 }
445
446 if (isCurrent) {
447 glColor4f (1.0, 0.5, 0.0, 1.0) ;
448 } else {
449 glColor4fv ( colour [ PUCOL_LEGEND ] ) ;
450 }
451
452 int xx = dx + abox.min[0] + PUSTR_LGAP;
453 int yy = dy + abox.max[1] - y ;
454 yy += 4; // center text in row height
455
456 if (isCurrent) {
457 f->drawString(">", xx, yy);
458 }
459
460 int x = xx;
461 x += _arrowWidth + PUSTR_LGAP;
462 drawRowText(x, yy, rowIndex, path);
463
464
465 if (isDragSource) {
466 puSetColor(col, 1.0, 0.5, 0.0, 0.5);
467 bkgBox.draw(dx, dy, PUSTYLE_PLAIN, &col, false, 0);
468 }
469 }
470
471 void WaypointList::drawRowText(int x, int baseline, int rowIndex, const RoutePath& path)
472 {
473 flightgear::Waypt* wp(_model->waypointAt(rowIndex));
474 const bool isDiscontinuity = (wp->type() == "discontinuity");
475 const bool isVia = (wp->type() == "via");
476
477 char buffer[128];
478 int count;
479
480 if (isVia) {
481 // VIA has long ident but no name
482 count = ::snprintf(buffer, 128, "%03d %s", rowIndex, wp->ident().c_str());
483 drawClippedString(legendFont, buffer, x, baseline, 300);
484 x += 300 + PUSTR_LGAP;
485 } else {
486 count = ::snprintf(buffer, 128, "%03d %-5s", rowIndex, wp->ident().c_str());
487
488 FGPositioned* src = wp->source();
489 if (src && !src->name().empty() && (src->name() != wp->ident())) {
490 // append name if present, and different to id
491 ::snprintf(buffer + count, 128 - count, " (%s)", src->name().c_str());
492 }
493
494 drawClippedString(legendFont, buffer, x, baseline, 300);
495 x += 300 + PUSTR_LGAP;
496
497 if (isDiscontinuity) {
498 return;
499 }
500
501 if (_showLatLon) {
502 // only show for non-dynamic waypoints
503 if (!wp->flag(WPT_DYNAMIC)) {
504 const auto s = simgear::strutils::formatGeodAsString(wp->position(),
505 _latLonFormat);
506 const auto bytesToCopy = std::min((size_t) 128 - count, s.size() + 1);
507 ::memcpy(buffer, s.c_str(), bytesToCopy);
508 } else {
509 buffer[0] = 0;
510 }
511 } else if (rowIndex > 0) {
512 double courseDeg = path.trackForIndex(rowIndex);
513 double distanceM = path.distanceForIndex(rowIndex);
514 ::snprintf(buffer, 128 - count, "%03.0f %5.1fnm",
515 courseDeg, distanceM * SG_METER_TO_NM);
516 }
517 } // of is not a VIA waypoint
518
519 puFont* f = &legendFont;
520 f->drawString(buffer, x, baseline);
521 x += 100 + PUSTR_LGAP;
522
523 if (wp->altitudeRestriction() != RESTRICT_NONE) {
524 char aboveAtBelow = ' ';
525 if (wp->altitudeRestriction() == RESTRICT_ABOVE) {
526 aboveAtBelow = 'A';
527 } else if (wp->altitudeRestriction() == RESTRICT_BELOW) {
528 aboveAtBelow = 'B';
529 }
530
531 int altHundredFt = (wp->altitudeFt() + 50) / 100; // round to nearest 100ft
532 if (altHundredFt < 100) {
533 count = ::snprintf(buffer, 128, "%d'%c", altHundredFt * 100, aboveAtBelow);
534 } else { // display as a flight-level
535 count = ::snprintf(buffer, 128, "FL%d%c", altHundredFt, aboveAtBelow);
536 }
537
538 f->drawString(buffer, x, baseline);
539 } // of valid wp altitude
540 x += 60 + PUSTR_LGAP;
541
542 if (wp->speedRestriction() == SPEED_RESTRICT_MACH) {
543 count = ::snprintf(buffer, 126, "%03.2fM", wp->speedMach());
544 f->drawString(buffer, x, baseline);
545 } else if (wp->speedRestriction() != RESTRICT_NONE) {
546 count = ::snprintf(buffer, 126, "%dKts", (int) wp->speedKts());
547 f->drawString(buffer, x, baseline);
548 }
549 }
550
551 const double SCROLL_PX_SEC = 200.0;
552
553 void WaypointList::doDragScroll()
554 {
555 double dt = (SGTimeStamp::now() - _dragScrollTime).toSecs();
556 _dragScrollTime.stamp();
557 int deltaPx = (int)(dt * SCROLL_PX_SEC);
558
559 if (_dragScroll == SCROLL_UP) {
560 _scrollPx = _scrollPx - deltaPx;
561 SG_CLAMP_RANGE(_scrollPx, 0, scrollRangePx());
562 _dragTargetRow = firstVisibleRow();
563 } else {
564 _scrollPx = _scrollPx + deltaPx;
565 SG_CLAMP_RANGE(_scrollPx, 0, scrollRangePx());
566 _dragTargetRow = lastFullyVisibleRow() + 1;
567 }
568
569 if (_scrollCallback) {
570 _scrollCallback();
571 }
572 }
573
574 int WaypointList::getSelected()
575 {
576 return getIntegerValue();
577 }
578
579 void WaypointList::setSelected(int rowIndex)
580 {
581 if (rowIndex == getSelected()) {
582 return;
583 }
584
585 setValue(rowIndex);
586 invokeCallback();
587 if (rowIndex == -1) {
588 return;
589 }
590
591 ensureRowVisible(rowIndex);
592 }
593
594 void WaypointList::ensureRowVisible(int rowIndex)
595 {
596 if ((rowIndex >= firstFullyVisibleRow()) && (rowIndex <= lastFullyVisibleRow())) {
597 return; // already visible, fine
598 }
599
600 // ideal position would place the desired row in the middle of the
601 // visible section - hence subtract half the visible height.
602 int targetScrollPx = (rowIndex * rowHeightPx()) - (_heightPx / 2);
603
604 // clamp the scroll value to something valid
605 SG_CLAMP_RANGE(targetScrollPx, 0, scrollRangePx());
606 _scrollPx = targetScrollPx;
607
608 puPostRefresh();
609 if (_scrollCallback) { // keep scroll observers in sync
610 _scrollCallback();
611 }
612 }
613
614 unsigned int WaypointList::numWaypoints() const
615 {
616 if (!_model) {
617 return 0;
618 }
619
620 return _model->numWaypoints();
621 }
622
623 bool WaypointList::wantsVScroll() const
624 {
625 return totalHeightPx() > _heightPx;
626 }
627
628 float WaypointList::getVScrollPercent() const
629 {
630 float scrollRange = scrollRangePx();
631 if (scrollRange < 1.0f) {
632 return 0.0;
633 }
634
635 return _scrollPx / scrollRange;
636 }
637
638 float WaypointList::getVScrollThumbPercent() const
639 {
640 return _heightPx / (float) totalHeightPx();
641 }
642
643 void WaypointList::setVScrollPercent(float perc)
644 {
645 float scrollRange = scrollRangePx();
646 _scrollPx = (int)(scrollRange * perc);
647 }
648
649 int WaypointList::firstVisibleRow() const
650 {
651 return _scrollPx / rowHeightPx();
652 }
653
654 int WaypointList::firstFullyVisibleRow() const
655 {
656 int rh = rowHeightPx();
657 return (_scrollPx + rh - 1) / rh;
658 }
659
660 int WaypointList::numVisibleRows() const
661 {
662 int rh = rowHeightPx();
663 int topOffset = _scrollPx % rh; // pixels of first visible row
664 return (_heightPx - topOffset + rh - 1) / rh;
665
666 }
667
668 int WaypointList::numFullyVisibleRows() const
669 {
670 int rh = rowHeightPx();
671 int topOffset = _scrollPx % rh; // pixels of first visible row
672 return (_heightPx - topOffset) / rh;
673 }
674
675 int WaypointList::rowHeightPx() const
676 {
677 return legendFont.getStringHeight() + PUSTR_BGAP;
678 }
679
680 int WaypointList::scrollRangePx() const
681 {
682 return std::max(0, totalHeightPx() - _heightPx);
683 }
684
685 int WaypointList::totalHeightPx() const
686 {
687 if (!_model) {
688 return 0;
689 }
690
691 return (int) _model->numWaypoints() * rowHeightPx();
692 }
693
694 int WaypointList::lastFullyVisibleRow() const
695 {
696 int row = firstFullyVisibleRow() + numFullyVisibleRows();
697 return std::min(row, (int) _model->numWaypoints() - 1);
698 }
699
700 int WaypointList::lastVisibleRow() const
701 {
702 int row = firstVisibleRow() + numVisibleRows();
703 return std::min(row, (int) _model->numWaypoints() - 1);
704 }
705
706 void WaypointList::setModel(Model* model)
707 {
708 if (_model) {
709 delete _model;
710 }
711
712 _model = model;
713 _model->setUpdateCallback([this](){ this->modelUpdateCallback(); });
714
715 puPostRefresh();
716 }
717
718 int WaypointList::checkKey (int key, int updown )
719 {
720 if ((updown == PU_UP) || !isVisible () || !isActive () || (window != puGetWindow())) {
721 return FALSE ;
722 }
723
724 #ifdef AVOID_FLIGHT_KEYS
725 return FALSE;
726 #endif
727
728 switch (key)
729 {
730 case PU_KEY_HOME:
731 setSelected(0);
732 break;
733
734 case PU_KEY_END:
735 setSelected(_model->numWaypoints() - 1);
736 break ;
737
738 case PU_KEY_UP :
739 case PU_KEY_PAGE_UP :
740 if (getSelected() >= 0) {
741 setSelected(getSelected() - 1);
742 }
743 break ;
744
745 case PU_KEY_DOWN :
746 case PU_KEY_PAGE_DOWN : {
747 int newSel = getSelected() + 1;
748 if (newSel >= (int) _model->numWaypoints()) {
749 setSelected(-1);
750 } else {
751 setSelected(newSel);
752 }
753 break ;
754 }
755
756 case '-':
757 if (getSelected() >= 0) {
758 Waypt* wp = _model->waypointAt(getSelected());
759 if (wp->flag(WPT_GENERATED)) {
760 break;
761 }
762
763 if (wp->altitudeRestriction() != RESTRICT_NONE) {
764 int curAlt = (static_cast<int>(wp->altitudeFt()) + 50) / 100;
765 if (curAlt <= 0) {
766 wp->setAltitude(0, RESTRICT_NONE);
767 } else {
768 wp->setAltitude((curAlt - 10) * 100, wp->altitudeRestriction());
769 }
770 }
771 }
772 break;
773
774 case '=':
775 if (getSelected() >= 0) {
776 flightgear::Waypt* wp = _model->waypointAt(getSelected());
777 if (wp->flag(WPT_GENERATED)) {
778 break;
779 }
780
781 if (wp->altitudeRestriction() == RESTRICT_NONE) {
782 wp->setAltitude(1000, RESTRICT_AT);
783 } else {
784 int curAlt = (static_cast<int>(wp->altitudeFt()) + 50) / 100;
785 wp->setAltitude((curAlt + 10) * 100, wp->altitudeRestriction());
786 }
787 }
788 break;
789
790 case 0x7f: // delete
791 if (getSelected() >= 0) {
792 int index = getSelected();
793 Waypt* wp = _model->waypointAt(getSelected());
794 if (wp->flag(WPT_GENERATED)) {
795 break;
796 }
797
798 _model->deleteAt(index);
799 setSelected(index - 1);
800 }
801 break;
802
803 default :
804 return FALSE;
805 }
806
807 return TRUE ;
808 }
809
810 void WaypointList::modelUpdateCallback()
811 {
812 // local stuff
813
814 if (_updateCallback) {
815 _updateCallback();
816 }
817 }
818
819 //////////////////////////////////////////////////////////////////////////////
820
821
822 static void handle_scrollbar(puObject* scrollbar)
823 {
824 ScrolledWaypointList* self = (ScrolledWaypointList*)scrollbar->getUserData();
825 self->setScrollPercent(scrollbar->getFloatValue());
826 }
827
828 static void waypointListCb(puObject* wpl)
829 {
830 ScrolledWaypointList* self = (ScrolledWaypointList*)wpl->getUserData();
831 self->setValue(wpl->getIntegerValue());
832 self->invokeCallback();
833 }
834
835 ScrolledWaypointList::ScrolledWaypointList(int x, int y, int width, int height) :
836 puGroup(x,y),
837 _scrollWidth(16)
838 {
839 // ensure our type is compound, so fgPopup::applySize doesn't descend into
840 // us, and try to cast our children's user-data to GUIInfo*.
841 type |= PUCLASS_LIST;
842
843 init(width, height);
844 }
845
846 void ScrolledWaypointList::setValue(float v)
847 {
848 puGroup::setValue(v);
849 _list->setValue(v);
850 }
851
852 void ScrolledWaypointList::setValue(int v)
853 {
854 puGroup::setValue(v);
855 _list->setValue(v);
856 }
857
858 void ScrolledWaypointList::init(int w, int h)
859 {
860 _list = new WaypointList(0, 0, w, h);
861 _list->setUpdateCallback([this](){ this->modelUpdated(); });
862 _hasVScroll = _list->wantsVScroll();
863 _list->setUserData(this);
864 _list->setCallback(waypointListCb);
865
866 _list->setScrollCallback([this](){ this->updateScroll(); });
867
868 _scrollbar = new puaScrollBar(w - _scrollWidth, 0, h,
869 1 /*arrow*/, 1 /* vertical */, _scrollWidth);
870 _scrollbar->setMinValue(0.0);
871 _scrollbar->setMaxValue(1.0);
872 _scrollbar->setUserData(this);
873 _scrollbar->setCallback(handle_scrollbar);
874 close(); // close the group
875
876 setSize(w, h);
877 }
878
879 void ScrolledWaypointList::modelUpdated()
880 {
881 int w, h;
882 getSize(&w, &h);
883 updateWantsScroll(w, h);
884 }
885
886 void ScrolledWaypointList::setScrollPercent(float v)
887 {
888 // slider's min is the bottom, so invert the value
889 _list->setVScrollPercent(1.0f - v);
890 }
891
892 void ScrolledWaypointList::setSize(int w, int h)
893 {
894 updateWantsScroll(w, h);
895 puGroup::setSize(w, h);
896 }
897
898 void ScrolledWaypointList::updateWantsScroll(int w, int h)
899 {
900 _hasVScroll = _list->wantsVScroll();
901
902 if (_hasVScroll) {
903 _scrollbar->reveal();
904 _scrollbar->setPosition(w - _scrollWidth, 0);
905 _scrollbar->setSize(_scrollWidth, h);
906 _list->setSize(w - _scrollWidth, h);
907 updateScroll();
908 } else {
909 _scrollbar->hide();
910 _list->setSize(w, h);
911 }
912 }
913
914 void ScrolledWaypointList::updateScroll()
915 {
916 // _scrollbar->setMaxValue(_list->numWaypoints());
917 _scrollbar->setValue(1.0f - _list->getVScrollPercent());
918 _scrollbar->setSliderFraction(_list->getVScrollThumbPercent());
919 }
920