TestButtons.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var deltaTime = 0;
  2. var buttonClicked = null;
  3. var widgetClicked = false;
  4. function start() {
  5. // will go out of scope, however will be held while the
  6. // button is in a UI heirarchy
  7. var widget = new Atomic.UIWidget();
  8. widget.setSize(256, 256);
  9. TheView.addChild(widget);
  10. var button = new Atomic.UIButton();
  11. button.text = "Click Me To Destroy";
  12. button.setSize(256, 256);
  13. widget.addChild(button);
  14. widget.onClick = function() {
  15. widgetClicked = true;
  16. print ("Error: Widget on Click!");
  17. }
  18. button.onClick = function() {
  19. print ("Button on Click!");
  20. // can't destroy the button in it's own callback
  21. buttonClicked = button;
  22. // returning true signals the event was handled and stops event bubble
  23. // so parent widget should not get event
  24. return true;
  25. }
  26. }
  27. function update(timeStep) {
  28. if (buttonClicked) {
  29. buttonClicked.parent.destroy();
  30. try {
  31. // widget onClicked handler should not have been called
  32. MyAssert(!widgetClicked);
  33. // just the view should be alive
  34. MyAssert(Atomic.UI.debugGetWrappedWidgetCount() == 1);
  35. MyAssert(Atomic.UI.debugGetUIKeepAliveCount() == Atomic.UI.debugGetWrappedWidgetCount());
  36. } catch (e) {
  37. print (e);
  38. }
  39. buttonClicked = null;
  40. }
  41. deltaTime += timeStep;
  42. if (deltaTime > .5) {
  43. deltaTime = 0;
  44. }
  45. }