JSUIAPI.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "JSUIAPI.h"
  5. #include "JSVM.h"
  6. #include <Atomic/UI/UISelectItem.h>
  7. #include <Atomic/UI/UIMenuWindow.h>
  8. #include <Atomic/UI/UIButton.h>
  9. namespace Atomic
  10. {
  11. static int UIButton_Popup(duk_context* ctx)
  12. {
  13. if (!duk_is_object(ctx, 0))
  14. {
  15. duk_push_string(ctx, "UIButton.popup first argument must be an object");
  16. duk_throw(ctx);
  17. }
  18. if (!duk_is_callable(ctx, 1))
  19. {
  20. duk_push_string(ctx, "UIButton.popup second argument must be callable");
  21. duk_throw(ctx);
  22. }
  23. JSVM* vm = JSVM::GetJSVM(ctx);
  24. duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY);
  25. UISelectItemSource* source = new UISelectItemSource(vm->GetContext());
  26. while (duk_next(ctx, -1, 0)) {
  27. String key = duk_get_string(ctx, -1);
  28. duk_get_prop(ctx, 0);
  29. if (duk_is_array(ctx, -1))
  30. {
  31. // need to support this, for skin image, etc
  32. assert(0);
  33. }
  34. else if (duk_is_string(ctx, -1))
  35. {
  36. // id
  37. String id = duk_get_string(ctx, -1);
  38. source->AddItem(new UISelectItem(vm->GetContext(), key, id));
  39. }
  40. else
  41. {
  42. duk_push_string(ctx, "UIButton.popup data object key is not an array or string");
  43. duk_throw(ctx);
  44. }
  45. duk_pop(ctx); // pop key value
  46. }
  47. duk_pop(ctx); // pop enum object
  48. duk_push_this(ctx);
  49. duk_dup(ctx, 1);
  50. duk_put_prop_string(ctx, -2, "__popup_menu_callback");
  51. UIButton* button = js_to_class_instance<UIButton>(ctx, -1, 0);
  52. UIMenuWindow* menuWindow = new UIMenuWindow(vm->GetContext(), button, "__popup-menu");
  53. menuWindow->Show(source);
  54. duk_pop(ctx);
  55. return 0;
  56. }
  57. void jsapi_init_ui(JSVM* vm)
  58. {
  59. duk_context* ctx = vm->GetJSContext();
  60. js_class_get_prototype(ctx, "UIButton");
  61. duk_push_c_function(ctx, UIButton_Popup, 2);
  62. duk_put_prop_string(ctx, -2, "popup");
  63. duk_pop(ctx);
  64. }
  65. }