KrpanoActionProxy.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { __awaiter } from "tslib";
  2. import { TagActionProxy } from ".";
  3. import { ROTATE_DIRECTION, ZOOM_ACTION, } from "../types";
  4. import { buildKrpanoAction, buildKrpanoTagSetterActions } from "../utils";
  5. export class KrpanoActionProxy {
  6. constructor(krpanoRenderer, name = "ReactKrpanoActionProxy") {
  7. this.eventHandlers = [];
  8. /**
  9. * 小行星视角
  10. */
  11. this.littlePlanetIntro = false;
  12. this.krpanoRenderer = krpanoRenderer;
  13. this.name = name;
  14. this.tagAction = new TagActionProxy(krpanoRenderer);
  15. }
  16. /**
  17. * 执行 Javascript 函数
  18. * @param action 动作
  19. * @param nexttick 是否在下一个渲染帧后执行
  20. */
  21. call(action, nexttick = false) {
  22. var _a;
  23. const actionStr = nexttick ? `nexttick(${action})` : action;
  24. (_a = this.krpanoRenderer) === null || _a === void 0 ? void 0 : _a.call(actionStr);
  25. }
  26. set(name, ...params) {
  27. this.call(buildKrpanoAction("set", name, ...params));
  28. }
  29. /**
  30. * 动态添加标签
  31. * @param tag 标签
  32. * @param name 名称
  33. * @param attrs 属性
  34. * @param nextTick 等待下一帧执行
  35. */
  36. setTag(tag, name, attrs, nextTick = false) {
  37. return __awaiter(this, void 0, void 0, function* () {
  38. let nexttick = nextTick;
  39. yield this.tagAction.waitIncludeLoaded();
  40. this.call(buildKrpanoTagSetterActions(name ? `${tag}[${name}]` : tag, attrs), nexttick);
  41. });
  42. }
  43. get(name) {
  44. var _a;
  45. return (_a = this.krpanoRenderer) === null || _a === void 0 ? void 0 : _a.get(name);
  46. }
  47. /**
  48. * 删除场景
  49. * @param name 场景名称
  50. */
  51. removeScene(name) {
  52. if (this.get("scene") &&
  53. typeof this.get("scene").removeItem === "function") {
  54. this.get("scene").removeItem(name);
  55. }
  56. }
  57. /**
  58. * 加载场景
  59. * @param name 场景 name
  60. */
  61. loadScene(name) {
  62. this.call(buildKrpanoAction("loadscene", name, "null", "MERGE", "OPENBLEND(0.5, 0.0, 0.75, 0.05, linear)"));
  63. }
  64. /**
  65. * 旋转视图
  66. * @param direction 方位
  67. * @param degrees 旋转度数,默认为 10
  68. */
  69. rotateView(direction, degrees = 10) {
  70. let str = "";
  71. const view = this.get("view");
  72. switch (direction) {
  73. case ROTATE_DIRECTION.LEFT:
  74. str = `view.hlookat, ${(view.hlookat || 0) - degrees}`;
  75. break;
  76. case ROTATE_DIRECTION.RIGHT:
  77. str = `view.hlookat, ${(view.hlookat || 0) + degrees}`;
  78. break;
  79. case ROTATE_DIRECTION.UP:
  80. str = `view.vlookat, ${(view.vlookat || 0) - degrees}`;
  81. break;
  82. case ROTATE_DIRECTION.DOWN:
  83. str = `view.vlookat, ${(view.vlookat || 0) + degrees}`;
  84. break;
  85. }
  86. this.call(buildKrpanoAction("tween", str, 0.5));
  87. }
  88. /**
  89. * 缩放视图
  90. * @param action 动作
  91. * @param num 缩放大小
  92. */
  93. zoomView(action, num = 10) {
  94. const view = this.get("view");
  95. const targetFov = action === ZOOM_ACTION.IN ? -num : num;
  96. this.call(buildKrpanoAction("tween", "view.fov", (view.fov || 0) + targetFov, 1));
  97. }
  98. on(eventName, selector, handler) {
  99. this.eventHandlers.push({
  100. eventName: eventName.toLowerCase(),
  101. selector,
  102. handler,
  103. });
  104. return this;
  105. }
  106. off(eventName, selector, handler) {
  107. this.eventHandlers = this.eventHandlers.filter((e) => !(e.eventName === eventName.toLowerCase() &&
  108. e.selector === selector &&
  109. e.handler === handler));
  110. }
  111. fire(eventName, selector) {
  112. this.eventHandlers
  113. .filter((e) => e.eventName === eventName.toLowerCase() && e.selector === selector)
  114. .map(({ handler }) => handler(this));
  115. }
  116. bindEvents(selector, mapEventsToHandler) {
  117. Object.keys(mapEventsToHandler).map((eventName) => {
  118. const func = mapEventsToHandler[eventName];
  119. if (func) {
  120. this.on(eventName, selector, func);
  121. }
  122. });
  123. }
  124. unbindEvents(selector, mapEventsToHandler) {
  125. Object.keys(mapEventsToHandler).map((eventName) => {
  126. const func = mapEventsToHandler[eventName];
  127. if (func) {
  128. this.off(eventName, selector, func);
  129. }
  130. });
  131. }
  132. addHotspot(name, attrs) {
  133. return __awaiter(this, void 0, void 0, function* () {
  134. yield this.tagAction.waitIncludeLoaded();
  135. this.call(buildKrpanoAction("addhotspot", name), true);
  136. this.setTag("hotspot", name, attrs);
  137. });
  138. }
  139. removeHotspot(name) {
  140. this.call(buildKrpanoAction("removehotspot", name), true);
  141. }
  142. addLayer(name, attrs) {
  143. return __awaiter(this, void 0, void 0, function* () {
  144. yield this.tagAction.waitIncludeLoaded();
  145. this.call(buildKrpanoAction("addlayer", name), true);
  146. this.setTag("layer", name, attrs);
  147. });
  148. }
  149. removeLayer(name) {
  150. this.call(buildKrpanoAction("removelayer", name), true);
  151. }
  152. }