JSUIAPI.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_DebugGetUIKeepAliveCount(duk_context* ctx)
  97. {
  98. duk_push_global_stash(ctx);
  99. duk_get_prop_string(ctx, -1, "__jsui_widgetkeepalive");
  100. duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY);
  101. double count = 0;
  102. while (duk_next(ctx, -1 , 0)) {
  103. duk_pop(ctx); /* pop_key */
  104. count++;
  105. }
  106. duk_pop_n(ctx, 3); /* pop enum object, keep alive object, and stash */
  107. duk_push_number(ctx, count);
  108. return 1;
  109. }
  110. int UI_DebugGetWrappedWidgetCount(duk_context* ctx)
  111. {
  112. JSVM* vm = JSVM::GetJSVM(ctx);
  113. UI* ui = vm->GetSubsystem<UI>();
  114. duk_push_number(ctx, (double) ui->DebugGetWrappedWidgetCount());
  115. return 1;
  116. }
  117. int UI_DebugShowSettingsWindow(duk_context* ctx)
  118. {
  119. UIWidget* widget = js_to_class_instance<UIWidget>(ctx, 0, 0);
  120. if (!widget)
  121. return 0;
  122. #ifdef TB_RUNTIME_DEBUG_INFO
  123. if (widget->GetInternalWidget())
  124. tb::ShowDebugInfoSettingsWindow(widget->GetInternalWidget());
  125. #endif
  126. return 0;
  127. }
  128. void jsapi_init_ui(JSVM* vm)
  129. {
  130. duk_context* ctx = vm->GetJSContext();
  131. // UI class object
  132. duk_get_global_string(ctx, "Atomic");
  133. duk_get_prop_string(ctx, -1, "UI");
  134. duk_push_c_function(ctx, UI_DebugGetWrappedWidgetCount, 0);
  135. duk_put_prop_string(ctx, -2, "debugGetWrappedWidgetCount");
  136. duk_push_c_function(ctx, UI_DebugGetUIKeepAliveCount, 0);
  137. duk_put_prop_string(ctx, -2, "debugGetUIKeepAliveCount");
  138. duk_push_c_function(ctx, UI_DebugShowSettingsWindow, 1);
  139. duk_put_prop_string(ctx, -2, "debugShowSettingsWindow");
  140. duk_pop_2(ctx);
  141. js_class_get_prototype(ctx, "Atomic", "UIButton");
  142. duk_push_c_function(ctx, UIButton_Popup, 2);
  143. duk_put_prop_string(ctx, -2, "popup");
  144. duk_pop(ctx);
  145. js_class_get_prototype(ctx, "Atomic", "UIWindow");
  146. duk_push_c_function(ctx, UIWindow_GetResizeToFitContentRect, 0);
  147. duk_put_prop_string(ctx, -2, "getResizeToFitContentRect");
  148. duk_pop(ctx);
  149. }
  150. }