MCPcopy Index your code
hub / github.com/KilledByAPixel/LittleJS / showConfirmDialog

Method showConfirmDialog

plugins/uiSystem.js:613–667  ·  view source on GitHub ↗

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')

Source from the content-addressed store, hash-verified

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///////////////////////////////////////////////////////////////////////////////

Callers 2

gameInitFunction · 0.80
createUIFunction · 0.80

Calls 6

addChildMethod · 0.95
vec2Function · 0.85
hslFunction · 0.85
keyWasPressedFunction · 0.85
ASSERTFunction · 0.50
drawRectMethod · 0.45

Tested by

no test coverage detected