Show a confirmation dialog with Yes/No buttons * Centers the dialog on the screen with darkened background * @param {string} [text] - The message to display * @param {Function} [yesCallback] - Called when Yes is clicked * @param {Function} [noCallback] - Called when No is cli
(text='Are you sure?', yesCallback, noCallback, size=vec2(500,250), exitKey='Escape')
| 611 | * @return {UIObject} The confirmation menu object |
| 612 | */ |
| 613 | showConfirmDialog(text='Are you sure?', yesCallback, noCallback, size=vec2(500,250), exitKey='Escape') |
| 614 | { |
| 615 | ASSERT(!uiSystem.confirmDialog); |
| 616 | |
| 617 | const savedNavigationDirection = uiSystem.navigationDirection; |
| 618 | |
| 619 | // allow both axes for navigation |
| 620 | uiSystem.navigationDirection = 2; |
| 621 | |
| 622 | // confirm menu |
| 623 | const confirmMenu = new UIObject(vec2(), size); |
| 624 | uiSystem.confirmDialog = confirmMenu; |
| 625 | confirmMenu.onRender = ()=> |
| 626 | { |
| 627 | const backgroundColor = hsl(0,0,0,.7); |
| 628 | uiSystem.drawRect(vec2(), vec2(1e9), backgroundColor); |
| 629 | } |
| 630 | confirmMenu.onUpdate = ()=> |
| 631 | { |
| 632 | if (keyWasPressed(exitKey)) |
| 633 | closeMenu(); |
| 634 | } |
| 635 | confirmMenu.isMouseOverlapping = ()=> true; // always hover |
| 636 | |
| 637 | // title text |
| 638 | const gap = 50; |
| 639 | const textTitle = new UIText(vec2(0,-50), vec2(size.x-gap,70), text); |
| 640 | confirmMenu.addChild(textTitle); |
| 641 | |
| 642 | // yes button |
| 643 | const buttonYes = new UIButton(vec2(-80,50), vec2(120,70), 'Yes'); |
| 644 | buttonYes.textHeight = 40; |
| 645 | buttonYes.navigationIndex = 1; |
| 646 | buttonYes.hoverColor = hsl(0,1,.5); |
| 647 | buttonYes.onClick = ()=> { closeMenu(); yesCallback && yesCallback(); }; |
| 648 | confirmMenu.addChild(buttonYes); |
| 649 | |
| 650 | // no button |
| 651 | const buttonNo = new UIButton(vec2(80,50), vec2(120,70), 'No'); |
| 652 | buttonNo.textHeight = 40; |
| 653 | buttonNo.navigationIndex = 2; |
| 654 | buttonNo.navigationAutoSelect = true; |
| 655 | buttonNo.onClick = ()=> { closeMenu(); noCallback && noCallback(); }; |
| 656 | confirmMenu.addChild(buttonNo); |
| 657 | |
| 658 | // close menu and return to normal navigation |
| 659 | function closeMenu() |
| 660 | { |
| 661 | ASSERT(uiSystem.confirmDialog === confirmMenu); |
| 662 | confirmMenu.destroy(); |
| 663 | uiSystem.confirmDialog = undefined; |
| 664 | uiSystem.navigationDirection = savedNavigationDirection; |
| 665 | inputClear(); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /////////////////////////////////////////////////////////////////////////////// |