JSUIAPI.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <TurboBadger/tb_window.h>
  5. #include "JSUIAPI.h"
  6. #include "JSVM.h"
  7. #include <Atomic/UI/UISelectItem.h>
  8. #include <Atomic/UI/UIMenuWindow.h>
  9. #include <Atomic/UI/UIButton.h>
  10. #include <Atomic/UI/UIWindow.h>
  11. namespace Atomic
  12. {
  13. static int UIButton_Popup(duk_context* ctx)
  14. {
  15. if (!duk_is_object(ctx, 0))
  16. {
  17. duk_push_string(ctx, "UIButton.popup first argument must be an object");
  18. duk_throw(ctx);
  19. }
  20. if (!duk_is_callable(ctx, 1))
  21. {
  22. duk_push_string(ctx, "UIButton.popup second argument must be callable");
  23. duk_throw(ctx);
  24. }
  25. JSVM* vm = JSVM::GetJSVM(ctx);
  26. duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY);
  27. UISelectItemSource* source = new UISelectItemSource(vm->GetContext());
  28. while (duk_next(ctx, -1, 0)) {
  29. String key = duk_get_string(ctx, -1);
  30. duk_get_prop(ctx, 0);
  31. if (duk_is_array(ctx, -1))
  32. {
  33. // need to support this, for skin image, etc
  34. assert(0);
  35. }
  36. else if (duk_is_string(ctx, -1))
  37. {
  38. // id
  39. String id = duk_get_string(ctx, -1);
  40. source->AddItem(new UISelectItem(vm->GetContext(), key, id));
  41. }
  42. else
  43. {
  44. duk_push_string(ctx, "UIButton.popup data object key is not an array or string");
  45. duk_throw(ctx);
  46. }
  47. duk_pop(ctx); // pop key value
  48. }
  49. duk_pop(ctx); // pop enum object
  50. duk_push_this(ctx);
  51. duk_dup(ctx, 1);
  52. duk_put_prop_string(ctx, -2, "__popup_menu_callback");
  53. UIButton* button = js_to_class_instance<UIButton>(ctx, -1, 0);
  54. UIMenuWindow* menuWindow = new UIMenuWindow(vm->GetContext(), button, "__popup-menu");
  55. menuWindow->Show(source);
  56. duk_pop(ctx);
  57. return 0;
  58. }
  59. int UIWindow_GetResizeToFitContentRect(duk_context* ctx)
  60. {
  61. duk_push_this(ctx);
  62. UIWindow* window = js_to_class_instance<UIWindow>(ctx, -1, 0);
  63. duk_pop(ctx);
  64. tb::TBWindow* tbwindow = (tb::TBWindow*) window->GetInternalWidget();
  65. tb::TBRect rect = tbwindow->GetResizeToFitContentRect();
  66. duk_push_object(ctx);
  67. duk_push_number(ctx, rect.x);
  68. duk_put_prop_string(ctx, -2, "x");
  69. duk_push_number(ctx, rect.y);
  70. duk_put_prop_string(ctx, -2, "y");
  71. duk_push_number(ctx, rect.w);
  72. duk_put_prop_string(ctx, -2, "width");
  73. duk_push_number(ctx, rect.h);
  74. duk_put_prop_string(ctx, -2, "height");
  75. return 1;
  76. }
  77. void jsapi_init_ui(JSVM* vm)
  78. {
  79. duk_context* ctx = vm->GetJSContext();
  80. js_class_get_prototype(ctx, "UIButton");
  81. duk_push_c_function(ctx, UIButton_Popup, 2);
  82. duk_put_prop_string(ctx, -2, "popup");
  83. duk_pop(ctx);
  84. js_class_get_prototype(ctx, "UIWindow");
  85. duk_push_c_function(ctx, UIWindow_GetResizeToFitContentRect, 0);
  86. duk_put_prop_string(ctx, -2, "getResizeToFitContentRect");
  87. duk_pop(ctx);
  88. }
  89. }