JSUIAPI.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_window.h>
  23. #include "JSUIAPI.h"
  24. #include "JSVM.h"
  25. #include <Atomic/UI/UI.h>
  26. #include <Atomic/UI/UISelectItem.h>
  27. #include <Atomic/UI/UIMenuWindow.h>
  28. #include <Atomic/UI/UIButton.h>
  29. #include <Atomic/UI/UIWindow.h>
  30. namespace Atomic
  31. {
  32. static int UIButton_Popup(duk_context* ctx)
  33. {
  34. if (!duk_is_object(ctx, 0))
  35. {
  36. duk_push_string(ctx, "UIButton.popup first argument must be an object");
  37. duk_throw(ctx);
  38. }
  39. if (!duk_is_callable(ctx, 1))
  40. {
  41. duk_push_string(ctx, "UIButton.popup second argument must be callable");
  42. duk_throw(ctx);
  43. }
  44. JSVM* vm = JSVM::GetJSVM(ctx);
  45. duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY);
  46. UISelectItemSource* source = new UISelectItemSource(vm->GetContext());
  47. while (duk_next(ctx, -1, 0)) {
  48. String key = duk_get_string(ctx, -1);
  49. duk_get_prop(ctx, 0);
  50. if (duk_is_array(ctx, -1))
  51. {
  52. // need to support this, for skin image, etc
  53. assert(0);
  54. }
  55. else if (duk_is_string(ctx, -1))
  56. {
  57. // id
  58. String id = duk_get_string(ctx, -1);
  59. source->AddItem(new UISelectItem(vm->GetContext(), key, id));
  60. }
  61. else
  62. {
  63. duk_push_string(ctx, "UIButton.popup data object key is not an array or string");
  64. duk_throw(ctx);
  65. }
  66. duk_pop(ctx); // pop key value
  67. }
  68. duk_pop(ctx); // pop enum object
  69. duk_push_this(ctx);
  70. duk_dup(ctx, 1);
  71. duk_put_prop_string(ctx, -2, "__popup_menu_callback");
  72. UIButton* button = js_to_class_instance<UIButton>(ctx, -1, 0);
  73. UIMenuWindow* menuWindow = new UIMenuWindow(vm->GetContext(), button, "__popup-menu");
  74. menuWindow->Show(source);
  75. duk_pop(ctx);
  76. return 0;
  77. }
  78. int UIWindow_GetResizeToFitContentRect(duk_context* ctx)
  79. {
  80. duk_push_this(ctx);
  81. UIWindow* window = js_to_class_instance<UIWindow>(ctx, -1, 0);
  82. duk_pop(ctx);
  83. tb::TBWindow* tbwindow = (tb::TBWindow*) window->GetInternalWidget();
  84. tb::TBRect rect = tbwindow->GetResizeToFitContentRect();
  85. duk_push_object(ctx);
  86. duk_push_number(ctx, rect.x);
  87. duk_put_prop_string(ctx, -2, "x");
  88. duk_push_number(ctx, rect.y);
  89. duk_put_prop_string(ctx, -2, "y");
  90. duk_push_number(ctx, rect.w);
  91. duk_put_prop_string(ctx, -2, "width");
  92. duk_push_number(ctx, rect.h);
  93. duk_put_prop_string(ctx, -2, "height");
  94. return 1;
  95. }
  96. int UI_DebugGetWrappedWidgetCount(duk_context* ctx)
  97. {
  98. JSVM* vm = JSVM::GetJSVM(ctx);
  99. UI* ui = vm->GetSubsystem<UI>();
  100. duk_push_number(ctx, (double) ui->DebugGetWrappedWidgetCount());
  101. return 1;
  102. }
  103. void jsapi_init_ui(JSVM* vm)
  104. {
  105. duk_context* ctx = vm->GetJSContext();
  106. // UI class object
  107. duk_get_global_string(ctx, "Atomic");
  108. duk_get_prop_string(ctx, -1, "UI");
  109. duk_push_c_function(ctx, UI_DebugGetWrappedWidgetCount, 0);
  110. duk_put_prop_string(ctx, -2, "debugGetWrappedWidgetCount");
  111. duk_pop_2(ctx);
  112. js_class_get_prototype(ctx, "Atomic", "UIButton");
  113. duk_push_c_function(ctx, UIButton_Popup, 2);
  114. duk_put_prop_string(ctx, -2, "popup");
  115. duk_pop(ctx);
  116. js_class_get_prototype(ctx, "Atomic", "UIWindow");
  117. duk_push_c_function(ctx, UIWindow_GetResizeToFitContentRect, 0);
  118. duk_put_prop_string(ctx, -2, "getResizeToFitContentRect");
  119. duk_pop(ctx);
  120. }
  121. }