handle.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { __assign, __extends, __read, __rest } from "tslib";
  2. import { GUI } from '../../core';
  3. import { classNames, ifShow, select, splitStyle, subStyleProps } from '../../util';
  4. import { HANDLE_DEFAULT_CFG, HANDLE_ICON_DEFAULT_CFG, HANDLE_LABEL_DEFAULT_CFG } from './constant';
  5. var CLASS_NAMES = classNames({
  6. labelGroup: 'label-group',
  7. label: 'label',
  8. iconGroup: 'icon-group',
  9. icon: 'icon',
  10. iconRect: 'icon-rect',
  11. iconLine: 'icon-line',
  12. }, 'handle');
  13. var HandleIcon = /** @class */ (function (_super) {
  14. __extends(HandleIcon, _super);
  15. function HandleIcon() {
  16. return _super !== null && _super.apply(this, arguments) || this;
  17. }
  18. HandleIcon.prototype.render = function (attributes, container) {
  19. var _a = attributes.size, size = _a === void 0 ? 10 : _a, _b = attributes.radius, radius = _b === void 0 ? size / 4 : _b, orientation = attributes.orientation, iconStyle = __rest(attributes, ["size", "radius", "orientation"]);
  20. // 默认手柄
  21. var width = size;
  22. var height = width * 2.4;
  23. var rect = select(container)
  24. .maybeAppendByClassName(CLASS_NAMES.iconRect, 'rect')
  25. .styles(__assign(__assign({}, iconStyle), { width: width, height: height, radius: radius, x: -width / 2, y: -height / 2 }));
  26. var x1 = (1 / 3) * width;
  27. var x2 = (2 / 3) * width;
  28. var y1 = (1 / 4) * height;
  29. var y2 = (3 / 4) * height;
  30. rect.maybeAppendByClassName("".concat(CLASS_NAMES.iconLine, "-1"), 'line').styles(__assign({ x1: x1, x2: x1, y1: y1, y2: y2 }, iconStyle));
  31. rect.maybeAppendByClassName("".concat(CLASS_NAMES.iconLine, "-2"), 'line').styles(__assign({ x1: x2, x2: x2, y1: y1, y2: y2 }, iconStyle));
  32. rect.node().setOrigin(width / 2, height / 2);
  33. if (orientation === 'vertical')
  34. container.setLocalEulerAngles(90);
  35. else
  36. container.setLocalEulerAngles(0);
  37. };
  38. return HandleIcon;
  39. }(GUI));
  40. var Handle = /** @class */ (function (_super) {
  41. __extends(Handle, _super);
  42. function Handle(options) {
  43. return _super.call(this, options, HANDLE_DEFAULT_CFG) || this;
  44. }
  45. Handle.prototype.renderLabel = function (container) {
  46. var _this = this;
  47. var showLabel = this.attributes.showLabel;
  48. var style = subStyleProps(this.attributes, 'label');
  49. var _a = __read(splitStyle(style, []), 2), labelStyle = _a[0], groupStyle = _a[1];
  50. var labelGroup = select(container).maybeAppendByClassName(CLASS_NAMES.labelGroup, 'g').styles(groupStyle);
  51. // @ts-ignore
  52. var _b = __assign(__assign({}, HANDLE_LABEL_DEFAULT_CFG), labelStyle), text = _b.text, rest = __rest(_b, ["text"]);
  53. ifShow(!!showLabel, labelGroup, function (group) {
  54. _this.label = group.maybeAppendByClassName(CLASS_NAMES.label, 'text').styles(__assign(__assign({}, rest), { text: "".concat(text) }));
  55. /** avoid trigger event on label */
  56. _this.label.on('mousedown', function (e) {
  57. e.stopPropagation();
  58. });
  59. _this.label.on('touchstart', function (e) {
  60. e.stopPropagation();
  61. });
  62. });
  63. };
  64. Handle.prototype.renderIcon = function (container) {
  65. var orientation = this.attributes.orientation;
  66. var iconStyle = __assign(__assign({ orientation: orientation }, HANDLE_ICON_DEFAULT_CFG), subStyleProps(this.attributes, 'icon'));
  67. var _a = this.attributes.iconShape, iconShape = _a === void 0 ? function () { return new HandleIcon({ style: iconStyle }); } : _a;
  68. var iconGroup = select(container).maybeAppendByClassName(CLASS_NAMES.iconGroup, 'g');
  69. iconGroup
  70. .selectAll(CLASS_NAMES.icon.class)
  71. .data([iconShape])
  72. .join(function (enter) { return enter.append(iconShape).attr('className', CLASS_NAMES.icon.name); }, function (update) { return update.update(iconStyle); }, function (exit) { return exit.remove(); });
  73. };
  74. Handle.prototype.render = function (attributes, container) {
  75. this.renderIcon(container);
  76. this.renderLabel(container);
  77. };
  78. return Handle;
  79. }(GUI));
  80. export { Handle };
  81. //# sourceMappingURL=handle.js.map