manga-renderer 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/LICENSE +17 -0
  2. package/README.md +118 -0
  3. package/dist/index.d.ts +28 -0
  4. package/dist/index.js +184 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/lib/book/book.d.ts +133 -0
  7. package/dist/lib/book/book.js +329 -0
  8. package/dist/lib/book/book.js.map +1 -0
  9. package/dist/lib/book/envelope.d.ts +26 -0
  10. package/dist/lib/book/envelope.js +158 -0
  11. package/dist/lib/book/envelope.js.map +1 -0
  12. package/dist/lib/book/imagePacking.d.ts +10 -0
  13. package/dist/lib/book/imagePacking.js +213 -0
  14. package/dist/lib/book/imagePacking.js.map +1 -0
  15. package/dist/lib/book/layout.d.ts +41 -0
  16. package/dist/lib/book/layout.js +14 -0
  17. package/dist/lib/book/layout.js.map +1 -0
  18. package/dist/lib/book/notebook.d.ts +72 -0
  19. package/dist/lib/book/notebook.js +30 -0
  20. package/dist/lib/book/notebook.js.map +1 -0
  21. package/dist/lib/book/richChat.d.ts +36 -0
  22. package/dist/lib/book/richChat.js +87 -0
  23. package/dist/lib/book/richChat.js.map +1 -0
  24. package/dist/lib/book/storyboard.d.ts +125 -0
  25. package/dist/lib/book/storyboard.js +44 -0
  26. package/dist/lib/book/storyboard.js.map +1 -0
  27. package/dist/lib/layeredCanvas/dataModels/bubble.d.ts +96 -0
  28. package/dist/lib/layeredCanvas/dataModels/bubble.js +630 -0
  29. package/dist/lib/layeredCanvas/dataModels/bubble.js.map +1 -0
  30. package/dist/lib/layeredCanvas/dataModels/effect.d.ts +25 -0
  31. package/dist/lib/layeredCanvas/dataModels/effect.js +104 -0
  32. package/dist/lib/layeredCanvas/dataModels/effect.js.map +1 -0
  33. package/dist/lib/layeredCanvas/dataModels/film.d.ts +44 -0
  34. package/dist/lib/layeredCanvas/dataModels/film.js +172 -0
  35. package/dist/lib/layeredCanvas/dataModels/film.js.map +1 -0
  36. package/dist/lib/layeredCanvas/dataModels/frameTree.d.ts +99 -0
  37. package/dist/lib/layeredCanvas/dataModels/frameTree.js +861 -0
  38. package/dist/lib/layeredCanvas/dataModels/frameTree.js.map +1 -0
  39. package/dist/lib/layeredCanvas/dataModels/media.d.ts +39 -0
  40. package/dist/lib/layeredCanvas/dataModels/media.js +69 -0
  41. package/dist/lib/layeredCanvas/dataModels/media.js.map +1 -0
  42. package/dist/lib/layeredCanvas/layers/SampleLayer.d.ts +6 -0
  43. package/dist/lib/layeredCanvas/layers/SampleLayer.js +22 -0
  44. package/dist/lib/layeredCanvas/layers/SampleLayer.js.map +1 -0
  45. package/dist/lib/layeredCanvas/layers/arrayLayer.d.ts +49 -0
  46. package/dist/lib/layeredCanvas/layers/arrayLayer.js +364 -0
  47. package/dist/lib/layeredCanvas/layers/arrayLayer.js.map +1 -0
  48. package/dist/lib/layeredCanvas/layers/bubbleLayer.d.ts +97 -0
  49. package/dist/lib/layeredCanvas/layers/bubbleLayer.js +1418 -0
  50. package/dist/lib/layeredCanvas/layers/bubbleLayer.js.map +1 -0
  51. package/dist/lib/layeredCanvas/layers/floorLayer.d.ts +13 -0
  52. package/dist/lib/layeredCanvas/layers/floorLayer.js +64 -0
  53. package/dist/lib/layeredCanvas/layers/floorLayer.js.map +1 -0
  54. package/dist/lib/layeredCanvas/layers/frameLayer.d.ts +106 -0
  55. package/dist/lib/layeredCanvas/layers/frameLayer.js +1283 -0
  56. package/dist/lib/layeredCanvas/layers/frameLayer.js.map +1 -0
  57. package/dist/lib/layeredCanvas/layers/inlinePainterLayer.d.ts +33 -0
  58. package/dist/lib/layeredCanvas/layers/inlinePainterLayer.js +222 -0
  59. package/dist/lib/layeredCanvas/layers/inlinePainterLayer.js.map +1 -0
  60. package/dist/lib/layeredCanvas/layers/paperRendererLayer.d.ts +64 -0
  61. package/dist/lib/layeredCanvas/layers/paperRendererLayer.js +443 -0
  62. package/dist/lib/layeredCanvas/layers/paperRendererLayer.js.map +1 -0
  63. package/dist/lib/layeredCanvas/layers/undoLayer.d.ts +8 -0
  64. package/dist/lib/layeredCanvas/layers/undoLayer.js +24 -0
  65. package/dist/lib/layeredCanvas/layers/undoLayer.js.map +1 -0
  66. package/dist/lib/layeredCanvas/system/keyCache.d.ts +4 -0
  67. package/dist/lib/layeredCanvas/system/keyCache.js +61 -0
  68. package/dist/lib/layeredCanvas/system/keyCache.js.map +1 -0
  69. package/dist/lib/layeredCanvas/system/layeredCanvas.d.ts +172 -0
  70. package/dist/lib/layeredCanvas/system/layeredCanvas.js +597 -0
  71. package/dist/lib/layeredCanvas/system/layeredCanvas.js.map +1 -0
  72. package/dist/lib/layeredCanvas/system/paperArray.d.ts +29 -0
  73. package/dist/lib/layeredCanvas/system/paperArray.js +108 -0
  74. package/dist/lib/layeredCanvas/system/paperArray.js.map +1 -0
  75. package/dist/lib/layeredCanvas/tools/draw/bubbleGraphic.d.ts +7 -0
  76. package/dist/lib/layeredCanvas/tools/draw/bubbleGraphic.js +728 -0
  77. package/dist/lib/layeredCanvas/tools/draw/bubbleGraphic.js.map +1 -0
  78. package/dist/lib/layeredCanvas/tools/draw/clickableIcon.d.ts +42 -0
  79. package/dist/lib/layeredCanvas/tools/draw/clickableIcon.js +129 -0
  80. package/dist/lib/layeredCanvas/tools/draw/clickableIcon.js.map +1 -0
  81. package/dist/lib/layeredCanvas/tools/draw/drawFilmStack.d.ts +4 -0
  82. package/dist/lib/layeredCanvas/tools/draw/drawFilmStack.js +53 -0
  83. package/dist/lib/layeredCanvas/tools/draw/drawFilmStack.js.map +1 -0
  84. package/dist/lib/layeredCanvas/tools/draw/drawText.d.ts +8 -0
  85. package/dist/lib/layeredCanvas/tools/draw/drawText.js +45 -0
  86. package/dist/lib/layeredCanvas/tools/draw/drawText.js.map +1 -0
  87. package/dist/lib/layeredCanvas/tools/draw/richText.d.ts +15 -0
  88. package/dist/lib/layeredCanvas/tools/draw/richText.js +172 -0
  89. package/dist/lib/layeredCanvas/tools/draw/richText.js.map +1 -0
  90. package/dist/lib/layeredCanvas/tools/draw/selectionFrame.d.ts +5 -0
  91. package/dist/lib/layeredCanvas/tools/draw/selectionFrame.js +63 -0
  92. package/dist/lib/layeredCanvas/tools/draw/selectionFrame.js.map +1 -0
  93. package/dist/lib/layeredCanvas/tools/draw/typeSetting.d.ts +16 -0
  94. package/dist/lib/layeredCanvas/tools/draw/typeSetting.js +2 -0
  95. package/dist/lib/layeredCanvas/tools/draw/typeSetting.js.map +1 -0
  96. package/dist/lib/layeredCanvas/tools/draw/verticalText.d.ts +3 -0
  97. package/dist/lib/layeredCanvas/tools/draw/verticalText.js +230 -0
  98. package/dist/lib/layeredCanvas/tools/draw/verticalText.js.map +1 -0
  99. package/dist/lib/layeredCanvas/tools/focusKeeper.d.ts +8 -0
  100. package/dist/lib/layeredCanvas/tools/focusKeeper.js +15 -0
  101. package/dist/lib/layeredCanvas/tools/focusKeeper.js.map +1 -0
  102. package/dist/lib/layeredCanvas/tools/frameExamples.d.ts +382 -0
  103. package/dist/lib/layeredCanvas/tools/frameExamples.js +758 -0
  104. package/dist/lib/layeredCanvas/tools/frameExamples.js.map +1 -0
  105. package/dist/lib/layeredCanvas/tools/geometry/bubbleGeometry.d.ts +12 -0
  106. package/dist/lib/layeredCanvas/tools/geometry/bubbleGeometry.js +117 -0
  107. package/dist/lib/layeredCanvas/tools/geometry/bubbleGeometry.js.map +1 -0
  108. package/dist/lib/layeredCanvas/tools/geometry/convertPoint.d.ts +2 -0
  109. package/dist/lib/layeredCanvas/tools/geometry/convertPoint.js +25 -0
  110. package/dist/lib/layeredCanvas/tools/geometry/convertPoint.js.map +1 -0
  111. package/dist/lib/layeredCanvas/tools/geometry/geometry.d.ts +69 -0
  112. package/dist/lib/layeredCanvas/tools/geometry/geometry.js +408 -0
  113. package/dist/lib/layeredCanvas/tools/geometry/geometry.js.map +1 -0
  114. package/dist/lib/layeredCanvas/tools/geometry/geometry.vitest.d.ts +1 -0
  115. package/dist/lib/layeredCanvas/tools/geometry/geometry.vitest.js +58 -0
  116. package/dist/lib/layeredCanvas/tools/geometry/geometry.vitest.js.map +1 -0
  117. package/dist/lib/layeredCanvas/tools/geometry/quickHull.d.ts +10 -0
  118. package/dist/lib/layeredCanvas/tools/geometry/quickHull.js +91 -0
  119. package/dist/lib/layeredCanvas/tools/geometry/quickHull.js.map +1 -0
  120. package/dist/lib/layeredCanvas/tools/geometry/trapezoid.d.ts +18 -0
  121. package/dist/lib/layeredCanvas/tools/geometry/trapezoid.js +105 -0
  122. package/dist/lib/layeredCanvas/tools/geometry/trapezoid.js.map +1 -0
  123. package/dist/lib/layeredCanvas/tools/grid.d.ts +7 -0
  124. package/dist/lib/layeredCanvas/tools/grid.js +16 -0
  125. package/dist/lib/layeredCanvas/tools/grid.js.map +1 -0
  126. package/dist/lib/layeredCanvas/tools/haiku.d.ts +1 -0
  127. package/dist/lib/layeredCanvas/tools/haiku.js +17 -0
  128. package/dist/lib/layeredCanvas/tools/haiku.js.map +1 -0
  129. package/dist/lib/layeredCanvas/tools/imageUtil.d.ts +8 -0
  130. package/dist/lib/layeredCanvas/tools/imageUtil.js +58 -0
  131. package/dist/lib/layeredCanvas/tools/imageUtil.js.map +1 -0
  132. package/dist/lib/layeredCanvas/tools/kinsoku.d.ts +26 -0
  133. package/dist/lib/layeredCanvas/tools/kinsoku.js +124 -0
  134. package/dist/lib/layeredCanvas/tools/kinsoku.js.map +1 -0
  135. package/dist/lib/layeredCanvas/tools/misc.d.ts +25 -0
  136. package/dist/lib/layeredCanvas/tools/misc.js +46 -0
  137. package/dist/lib/layeredCanvas/tools/misc.js.map +1 -0
  138. package/dist/lib/layeredCanvas/tools/perfectFreehandUtil.d.ts +2 -0
  139. package/dist/lib/layeredCanvas/tools/perfectFreehandUtil.js +21 -0
  140. package/dist/lib/layeredCanvas/tools/perfectFreehandUtil.js.map +1 -0
  141. package/dist/lib/layeredCanvas/tools/pictureControl.d.ts +4 -0
  142. package/dist/lib/layeredCanvas/tools/pictureControl.js +24 -0
  143. package/dist/lib/layeredCanvas/tools/pictureControl.js.map +1 -0
  144. package/dist/lib/layeredCanvas/tools/rectHandle.d.ts +6 -0
  145. package/dist/lib/layeredCanvas/tools/rectHandle.js +17 -0
  146. package/dist/lib/layeredCanvas/tools/rectHandle.js.map +1 -0
  147. package/dist/tsconfig.tsbuildinfo +1 -0
  148. package/package.json +64 -0
@@ -0,0 +1,1283 @@
1
+ import { LayerBase, sequentializePointer } from "../system/layeredCanvas";
2
+ import { FrameElement, calculatePhysicalLayout, findLayoutAt, findLayoutOf, findBorderAt, findPaddingOn, findPaddingOf, makeBorderCorners, makeBorderFormalCorners, calculateOffsettedCorners, listLayoutsAt } from "../dataModels/frameTree";
3
+ import { Film, FilmStackTransformer } from "../dataModels/film";
4
+ import { ImageMedia, VideoMedia } from "../dataModels/media";
5
+ import { constraintRecursive, constraintLeaf } from "../dataModels/frameTree";
6
+ import { translate, scale, rotate } from "../tools/pictureControl";
7
+ import { keyDownFlags } from "../system/keyCache";
8
+ import { ClickableIcon, ClickableSelfRenderer } from "../tools/draw/clickableIcon";
9
+ import { pointToQuadrilateralDistance, isPointInTrapezoid, trapezoidCorners, trapezoidPath, trapezoidBoundingRect, trapezoidCenter } from "../tools/geometry/trapezoid";
10
+ import { box2Rect, add2D, scale2D, lerp2D, distance2D, segmentIntersection, isTriangleClockwise, vectorEquals, getRectCenter, rectContains, denormalizePositionInRect } from '../tools/geometry/geometry';
11
+ import { rectCornerHandles } from "../tools/rectHandle";
12
+ import { drawSelectionFrame, calculateSheetRect, drawSheet } from "../tools/draw/selectionFrame";
13
+ import { drawFilmStackBorders } from "../tools/draw/drawFilmStack";
14
+ import { Grid } from "../tools/grid";
15
+ import paper from 'paper';
16
+ import { PaperOffset } from "paperjs-offset";
17
+ const SHEET_Y_MARGIN = 48;
18
+ const iconUnit = [32, 32];
19
+ const BORDER_MARGIN = 10;
20
+ const PADDING_HANDLE_INNER_WIDTH = 20;
21
+ const PADDING_HANDLE_OUTER_WIDTH = 20;
22
+ export class FrameLayer extends LayerBase {
23
+ renderLayer;
24
+ focusKeeper;
25
+ frameTree;
26
+ onFocus;
27
+ onCommit;
28
+ onRevert;
29
+ onShift;
30
+ onUnshift;
31
+ onSwap;
32
+ onInsert;
33
+ cursorPosition;
34
+ splitHorizontalIcon;
35
+ splitVerticalIcon;
36
+ deleteIcon;
37
+ duplicateIcon;
38
+ shiftIcon;
39
+ unshiftIcon;
40
+ resetPaddingIcon;
41
+ zplusIcon;
42
+ zminusIcon;
43
+ visibilityIcon;
44
+ scaleIcon;
45
+ rotateIcon;
46
+ flipHorizontalIcon;
47
+ flipVerticalIcon;
48
+ fitIcon;
49
+ expandHorizontalIcon;
50
+ slantHorizontalIcon;
51
+ insertHorizontalIcon;
52
+ expandVerticalIcon;
53
+ slantVerticalIcon;
54
+ insertVerticalIcon;
55
+ swapIcon;
56
+ zvalue;
57
+ frameIcons;
58
+ borderIcons;
59
+ litIcons;
60
+ litLayout = null;
61
+ litBorder = null;
62
+ selectedLayout = null;
63
+ selectedBorder = null;
64
+ focusedPadding = null;
65
+ pointerHandler = null;
66
+ canvasPattern;
67
+ constructor(renderLayer, focusKeeper, frameTree, onFocus, onCommit, onRevert, onShift, onUnshift, onSwap, onInsert) {
68
+ super();
69
+ this.renderLayer = renderLayer;
70
+ this.focusKeeper = focusKeeper;
71
+ this.frameTree = frameTree;
72
+ this.onFocus = onFocus;
73
+ this.onCommit = onCommit;
74
+ this.onRevert = onRevert;
75
+ this.onShift = onShift;
76
+ this.onUnshift = onUnshift;
77
+ this.onSwap = onSwap;
78
+ this.onInsert = onInsert;
79
+ this.cursorPosition = [-1, -1];
80
+ const unit = iconUnit;
81
+ const spinUnit = [unit[0], unit[1] * 1 / 6];
82
+ const mp = () => this.paper.matrix;
83
+ const isFrameActiveAndVisible = () => this.interactable && 0 < (this.selectedLayout?.element.visibility ?? 0);
84
+ this.splitHorizontalIcon = new ClickableIcon(["frameLayer/split-horizontal.png"], unit, [0, 1], "横に分割", isFrameActiveAndVisible, mp);
85
+ this.splitVerticalIcon = new ClickableIcon(["frameLayer/split-vertical.png"], unit, [0, 1], "縦に分割", isFrameActiveAndVisible, mp);
86
+ this.deleteIcon = new ClickableIcon(["frameLayer/delete.png"], unit, [1, 0], "削除", isFrameActiveAndVisible, mp);
87
+ this.duplicateIcon = new ClickableIcon(["frameLayer/duplicate.png"], unit, [1, 0], "複製", isFrameActiveAndVisible, mp);
88
+ this.shiftIcon = new ClickableIcon(["frameLayer/shift.png"], unit, [1, 0], "画像のシフト", isFrameActiveAndVisible, mp);
89
+ this.unshiftIcon = new ClickableIcon(["frameLayer/unshift.png"], unit, [1, 0], "画像のアンシフト", isFrameActiveAndVisible, mp);
90
+ this.resetPaddingIcon = new ClickableIcon(["frameLayer/reset-padding.png"], unit, [1, 0], "パディングのリセット", isFrameActiveAndVisible, mp);
91
+ this.zplusIcon = new ClickableIcon(["frameLayer/increment.png"], spinUnit, [0, 0], "手前に", isFrameActiveAndVisible, mp);
92
+ this.zminusIcon = new ClickableIcon(["frameLayer/decrement.png"], spinUnit, [0, 0], "奥に", isFrameActiveAndVisible, mp);
93
+ this.visibilityIcon = new ClickableIcon(["frameLayer/visibility1.png", "frameLayer/visibility2.png", "frameLayer/visibility3.png"], unit, [0, 0], "不可視/背景と絵/枠線も", isFrameActiveAndVisible, mp);
94
+ this.visibilityIcon.index = 2;
95
+ this.zvalue = new ClickableSelfRenderer((ctx, csr) => {
96
+ const rscale = 1 / this.paper.matrix.a;
97
+ const b = csr.boundingRect;
98
+ const l = this.selectedLayout;
99
+ // const c = getRectCenter(b);
100
+ const c = denormalizePositionInRect([0.45, 0.6], b);
101
+ const fontSize = Math.floor(b[3] * 0.8);
102
+ // ctx.fillStyle = "#222222";
103
+ // ctx.fillRect(b[0], b[1], b[2], b[3]);
104
+ ctx.font = `${fontSize}px serif`;
105
+ ctx.textAlign = "center";
106
+ ctx.textBaseline = "middle";
107
+ ctx.fillStyle = "#86C8FF";
108
+ ctx.fillText((l.element.z + 3).toString(), ...c);
109
+ }, unit, [0, 0], "z値", isFrameActiveAndVisible, mp);
110
+ const isImageActiveDraggable = () => this.interactable && 0 < (this.selectedLayout?.element.filmStack.films.length ?? 0);
111
+ const isImageActive = () => this.interactable && 0 < (this.selectedLayout?.element.filmStack.films.length ?? 0) && !this.pointerHandler;
112
+ this.scaleIcon = new ClickableIcon(["frameLayer/scale.png"], unit, [1, 1], "スケール", isImageActiveDraggable, mp);
113
+ this.rotateIcon = new ClickableIcon(["frameLayer/rotate.png"], unit, [1, 1], "回転", isImageActiveDraggable, mp);
114
+ this.flipHorizontalIcon = new ClickableIcon(["frameLayer/flip-horizontal.png"], unit, [1, 1], "左右反転", isImageActive, mp);
115
+ this.flipVerticalIcon = new ClickableIcon(["frameLayer/flip-vertical.png"], unit, [1, 1], "上下反転", isImageActive, mp);
116
+ this.fitIcon = new ClickableIcon(["frameLayer/fit.png"], unit, [1, 1], "フィット", isImageActive, mp);
117
+ const isBorderActive = (dir) => this.interactable && this.selectedBorder?.layout.dir === dir;
118
+ this.expandHorizontalIcon = new ClickableIcon(["frameLayer/expand-horizontal.png"], unit, [0.5, 1], "幅を変更", () => isBorderActive('h'), mp);
119
+ this.slantHorizontalIcon = new ClickableIcon(["frameLayer/slant-horizontal.png"], unit, [0.5, 0], "傾き", () => isBorderActive('h'), mp);
120
+ this.insertHorizontalIcon = new ClickableIcon(["frameLayer/insert-horizontal.png"], unit, [0.5, 0], "コマ挿入", () => isBorderActive('h'), mp);
121
+ this.expandVerticalIcon = new ClickableIcon(["frameLayer/expand-vertical.png"], unit, [1, 0.5], "幅を変更", () => isBorderActive('v'), mp);
122
+ this.slantVerticalIcon = new ClickableIcon(["frameLayer/slant-vertical.png"], unit, [0, 0.5], "傾き", () => isBorderActive('v'), mp);
123
+ this.insertVerticalIcon = new ClickableIcon(["frameLayer/insert-vertical.png"], unit, [0, 0.5], "コマ挿入", () => isBorderActive('v'), mp);
124
+ const isSwapVisible = () => this.interactable && this.litLayout != null && this.selectedLayout != null && this.litLayout.element !== this.selectedLayout.element && !this.pointerHandler && 0 < this.litLayout.element.visibility;
125
+ this.swapIcon = new ClickableIcon(["frameLayer/swap.png"], unit, [0.5, 0], "選択コマと\n中身を入れ替え", isSwapVisible, mp);
126
+ this.frameIcons = [this.splitHorizontalIcon, this.splitVerticalIcon, this.deleteIcon, this.duplicateIcon, this.shiftIcon, this.unshiftIcon, this.resetPaddingIcon, this.zplusIcon, this.zminusIcon, this.visibilityIcon, this.scaleIcon, this.rotateIcon, this.flipHorizontalIcon, this.flipVerticalIcon, this.fitIcon, this.zvalue];
127
+ this.borderIcons = [this.slantVerticalIcon, this.expandVerticalIcon, this.slantHorizontalIcon, this.expandHorizontalIcon, this.insertHorizontalIcon, this.insertVerticalIcon];
128
+ this.litIcons = [this.swapIcon];
129
+ for (let icon of this.frameIcons) {
130
+ if (icon instanceof ClickableIcon) {
131
+ icon.shadowColor = "#448";
132
+ }
133
+ }
134
+ this.zplusIcon.marginBottom = 16;
135
+ this.zminusIcon.marginTop = 16;
136
+ this.makeCanvasPattern();
137
+ focusKeeper.subscribe(this.changeFocus.bind(this));
138
+ }
139
+ calculateRootLayout() {
140
+ return calculatePhysicalLayout(this.frameTree, this.getPaperSize(), [0, 0]);
141
+ }
142
+ rebuildPageLayouts(matrix) {
143
+ this.relayoutIcons();
144
+ }
145
+ makeCanvasPattern() {
146
+ const pcanvas = document.createElement("canvas");
147
+ pcanvas.width = 16;
148
+ pcanvas.height = 16;
149
+ const pctx = pcanvas.getContext("2d");
150
+ pctx.strokeStyle = "rgba(0, 200, 200, 0.4)";
151
+ pctx.lineWidth = 6;
152
+ pctx.beginPath();
153
+ const slope = 1;
154
+ for (let y = -16; y < 32; y += 16) {
155
+ pctx.moveTo(-8, y);
156
+ pctx.lineTo(72, y + 80 * slope);
157
+ }
158
+ pctx.stroke();
159
+ this.canvasPattern = pctx.createPattern(pcanvas, 'repeat');
160
+ }
161
+ prerender() {
162
+ this.renderLayer.setFrameTree(this.frameTree);
163
+ }
164
+ render(ctx, depth) {
165
+ if (!this.interactable) {
166
+ return;
167
+ }
168
+ if (depth !== 0) {
169
+ return;
170
+ }
171
+ function fillTrapezoid(corners, color) {
172
+ ctx.fillStyle = color;
173
+ ctx.beginPath();
174
+ trapezoidPath(ctx, corners);
175
+ ctx.fill();
176
+ }
177
+ function strokeTrapezoid(corners, color, width) {
178
+ ctx.strokeStyle = color;
179
+ ctx.lineWidth = width;
180
+ ctx.setLineDash([10, 10]);
181
+ ctx.beginPath();
182
+ trapezoidPath(ctx, corners);
183
+ ctx.stroke();
184
+ }
185
+ function strokeConnectors(corners0, corners1, color, width) {
186
+ ctx.strokeStyle = color;
187
+ ctx.lineWidth = width;
188
+ for (let position of trapezoidCorners) {
189
+ ctx.beginPath();
190
+ ctx.moveTo(...corners0[position]);
191
+ ctx.lineTo(...corners1[position]);
192
+ ctx.stroke();
193
+ }
194
+ }
195
+ if (this.focusedPadding) {
196
+ fillTrapezoid(this.focusedPadding.corners, "rgba(200,200,0, 0.7)");
197
+ }
198
+ if (this.litBorder) {
199
+ const corners = this.litBorder.corners;
200
+ const canvasPath = getTrapezoidPath(corners, BORDER_MARGIN, true);
201
+ // 線のスタイルを設定して描画
202
+ ctx.fillStyle = "rgba(0, 200, 200, 0.2)";
203
+ ctx.fill(canvasPath);
204
+ }
205
+ else if (this.litLayout) {
206
+ if (this.litLayout.element != this.selectedLayout?.element) {
207
+ fillTrapezoid(this.litLayout.corners, "rgba(0, 0, 255, 0.2)");
208
+ strokeTrapezoid(this.litLayout.formalCorners, "rgba(0, 0, 255, 0.4)", 1);
209
+ strokeTrapezoid(this.litLayout.corners, "rgba(192, 192, 255, 1)", 3);
210
+ strokeConnectors(this.litLayout.corners, this.litLayout.formalCorners, "rgba(0, 0, 255, 0.4)", 1);
211
+ }
212
+ }
213
+ if (this.selectedBorder) {
214
+ const corners = this.selectedBorder.corners;
215
+ const canvasPath = getTrapezoidPath(corners, BORDER_MARGIN, true);
216
+ // 線のスタイルを設定して描画
217
+ ctx.fillStyle = this.canvasPattern;
218
+ ctx.fill(canvasPath);
219
+ }
220
+ else if (this.selectedLayout) {
221
+ const paperSize = this.getPaperSize();
222
+ const [x0, y0, w, h] = trapezoidBoundingRect(this.selectedLayout.corners);
223
+ ctx.save();
224
+ ctx.translate(x0 + w * 0.5, y0 + h * 0.5);
225
+ drawFilmStackBorders(ctx, this.selectedLayout.element.filmStack, paperSize);
226
+ ctx.restore();
227
+ // シート角丸
228
+ if (0 < this.selectedLayout.element.visibility) {
229
+ this.drawSheet(ctx, this.selectedLayout.corners);
230
+ }
231
+ // 選択枠
232
+ if (this.selectedLayout.element.visibility === 0) {
233
+ drawSelectionFrame(ctx, "rgba(0, 128, 255, 1)", this.selectedLayout.corners, 1, 2, false);
234
+ }
235
+ else {
236
+ drawSelectionFrame(ctx, "rgba(0, 128, 255, 1)", this.selectedLayout.corners);
237
+ }
238
+ }
239
+ this.frameIcons.forEach(icon => icon.render(ctx));
240
+ this.borderIcons.forEach(icon => icon.render(ctx));
241
+ this.litIcons.forEach(icon => icon.render(ctx));
242
+ }
243
+ calculateSheetRect(corners) {
244
+ return calculateSheetRect(trapezoidBoundingRect(corners), [320, 320], SHEET_Y_MARGIN, 1 / this.paper.matrix.a);
245
+ }
246
+ drawSheet(ctx, corners) {
247
+ drawSheet(ctx, corners, this.calculateSheetRect(corners), "rgba(64, 64, 128, 0.7)");
248
+ }
249
+ dropped(position, media) {
250
+ if (!this.interactable) {
251
+ return false;
252
+ }
253
+ let layoutlet = null;
254
+ if (this.selectedLayout) {
255
+ const r = this.calculateSheetRect(this.selectedLayout.corners);
256
+ if (rectContains(r, position)) {
257
+ layoutlet = this.selectedLayout;
258
+ }
259
+ }
260
+ if (!layoutlet) {
261
+ const layout = this.calculateRootLayout();
262
+ layoutlet = findLayoutAt(layout, position, PADDING_HANDLE_OUTER_WIDTH);
263
+ if (!layoutlet) {
264
+ return false;
265
+ }
266
+ }
267
+ if (media instanceof HTMLCanvasElement) {
268
+ this.importMedia(layoutlet, new ImageMedia(media));
269
+ layoutlet.element.gallery.push(media);
270
+ }
271
+ if (media instanceof HTMLVideoElement) {
272
+ this.importMedia(layoutlet, new VideoMedia(media));
273
+ }
274
+ this.onCommit();
275
+ return true;
276
+ }
277
+ updateLit(point) {
278
+ const layout = this.calculateRootLayout();
279
+ this.focusedPadding = null;
280
+ this.litBorder = null;
281
+ this.litLayout = null;
282
+ if (this.selectedLayout) {
283
+ for (let icon of this.frameIcons) {
284
+ if (icon.contains(point)) {
285
+ this.litLayout = this.selectedLayout;
286
+ return;
287
+ }
288
+ }
289
+ if (pointToQuadrilateralDistance(point, this.selectedLayout.corners, false) < PADDING_HANDLE_OUTER_WIDTH) {
290
+ const padding = findPaddingOn(this.selectedLayout, point, PADDING_HANDLE_INNER_WIDTH, PADDING_HANDLE_OUTER_WIDTH);
291
+ this.focusedPadding = padding;
292
+ this.litLayout = this.selectedLayout;
293
+ return;
294
+ }
295
+ const r = this.calculateSheetRect(this.selectedLayout.corners);
296
+ if (rectContains(r, point)) {
297
+ return;
298
+ }
299
+ }
300
+ if (this.selectedBorder) {
301
+ if (isPointInTrapezoid(point, this.selectedBorder.corners)) {
302
+ this.litBorder = this.selectedBorder;
303
+ return;
304
+ }
305
+ }
306
+ this.litBorder = findBorderAt(layout, point, BORDER_MARGIN);
307
+ if (this.litBorder) {
308
+ return;
309
+ }
310
+ this.litLayout = findLayoutAt(layout, point, PADDING_HANDLE_OUTER_WIDTH);
311
+ if (this.litLayout) {
312
+ this.relayoutLitIcons(this.litLayout);
313
+ }
314
+ }
315
+ pointerHover(position) {
316
+ this.cursorPosition = position;
317
+ if (!position) {
318
+ this.litLayout = null;
319
+ this.litBorder = null;
320
+ return false;
321
+ }
322
+ if (keyDownFlags["Space"]) {
323
+ return false;
324
+ }
325
+ this.updateLit(position);
326
+ this.redraw();
327
+ return this.decideHint(position);
328
+ }
329
+ decideHint(position) {
330
+ const hintIfContains = (a) => {
331
+ for (let e of a) {
332
+ if (e.hintIfContains(position, this.hint)) {
333
+ return true;
334
+ }
335
+ }
336
+ return false;
337
+ };
338
+ if (this.litLayout && this.litLayout.element != this.selectedLayout?.element) {
339
+ if (this.swapIcon.hintIfContains(position, this.hint)) {
340
+ return true;
341
+ }
342
+ if (0 < this.litLayout.element.visibility) {
343
+ const r = trapezoidBoundingRect(this.litLayout.corners);
344
+ this.hint(r, "画像をドロップ");
345
+ return true;
346
+ }
347
+ }
348
+ else if (this.litBorder && this.litBorder.layout.element != this.selectedBorder?.layout.element) {
349
+ if (isPointInTrapezoid(position, this.litBorder.corners)) {
350
+ this.hint([...trapezoidCenter(this.litBorder.corners), 0, 0], "クリックで選択");
351
+ return true;
352
+ }
353
+ }
354
+ else if (this.selectedBorder) {
355
+ if (hintIfContains(this.borderIcons)) {
356
+ return true;
357
+ }
358
+ if (isPointInTrapezoid(position, this.selectedBorder.corners)) {
359
+ this.hint([...trapezoidCenter(this.selectedBorder.corners), 0, 0], "ドラッグで移動");
360
+ return true;
361
+ }
362
+ return true;
363
+ }
364
+ else if (this.selectedLayout) {
365
+ const r = this.calculateSheetRect(this.selectedLayout.corners);
366
+ if (hintIfContains(this.frameIcons)) {
367
+ return true;
368
+ }
369
+ else if (this.focusedPadding) {
370
+ this.hint(r, "ドラッグでパディング変更");
371
+ return true;
372
+ }
373
+ else if (rectContains(r, position)) {
374
+ return true;
375
+ }
376
+ }
377
+ return false;
378
+ }
379
+ async keyDown(position, event) {
380
+ if (event.code === "KeyV" && event.ctrlKey) {
381
+ try {
382
+ console.log('クリップボードから画像を貼り付け');
383
+ const items = await navigator.clipboard.read();
384
+ console.log(items);
385
+ for (let item of items) {
386
+ for (let type of item.types) {
387
+ console.log(type);
388
+ if (type.startsWith("image/")) {
389
+ const blob = await item.getType(type);
390
+ const url = URL.createObjectURL(blob);
391
+ const image = new Image();
392
+ image.src = url;
393
+ await image.decode();
394
+ const canvas = document.createElement("canvas");
395
+ canvas.width = image.width;
396
+ canvas.height = image.height;
397
+ const ctx = canvas.getContext("2d");
398
+ ctx.drawImage(image, 0, 0);
399
+ this.dropped(position, canvas);
400
+ return true;
401
+ }
402
+ }
403
+ }
404
+ }
405
+ catch (err) {
406
+ console.error('ユーザが拒否、もしくはなんらかの理由で失敗', err);
407
+ }
408
+ }
409
+ return false;
410
+ }
411
+ pick(point) {
412
+ const layout = this.calculateRootLayout();
413
+ return listLayoutsAt(layout, point, PADDING_HANDLE_OUTER_WIDTH).map(layout => {
414
+ return {
415
+ selected: layout.element === this.selectedLayout?.element,
416
+ action: () => this.selectLayout(layout),
417
+ };
418
+ });
419
+ }
420
+ acceptDepths() {
421
+ return [0, 1];
422
+ }
423
+ accepts(point, button, depth) {
424
+ console.log("frame accepts", depth);
425
+ if (!this.interactable) {
426
+ return null;
427
+ }
428
+ if (keyDownFlags["Space"]) {
429
+ return null;
430
+ }
431
+ if (depth == 1) {
432
+ const q = this.acceptsForeground(point, button);
433
+ console.log("q1", q);
434
+ return q;
435
+ }
436
+ else {
437
+ const q = this.acceptsBackground(point, button);
438
+ console.log("q2", q);
439
+ return q;
440
+ }
441
+ }
442
+ acceptsForeground(point, _button) {
443
+ if (keyDownFlags["KeyT"]) {
444
+ if (this.litBorder) {
445
+ return { action: "transpose-border", border: this.litBorder };
446
+ }
447
+ return null;
448
+ }
449
+ if (this.selectedLayout) {
450
+ const q = this.acceptsOnSelectedFrameIcons(point);
451
+ if (q) {
452
+ return q;
453
+ }
454
+ if (this.selectedLayout.element.visibility === 0) {
455
+ if (pointToQuadrilateralDistance(point, this.selectedLayout.corners, false) < PADDING_HANDLE_OUTER_WIDTH) {
456
+ return { action: "pierce" };
457
+ }
458
+ }
459
+ else {
460
+ const r = this.calculateSheetRect(this.selectedLayout.corners);
461
+ if (rectContains(r, point)) {
462
+ if (this.focusedPadding) {
463
+ return { action: "move-padding", padding: this.focusedPadding };
464
+ }
465
+ if (this.selectedLayout.element.filmStack.films.length !== 0) {
466
+ if (keyDownFlags["ControlLeft"] || keyDownFlags["ControlRight"]) {
467
+ return { action: "scale", layout: this.selectedLayout };
468
+ }
469
+ else if (keyDownFlags["AltLeft"] || keyDownFlags["AltRight"]) {
470
+ return { action: "rotate", layout: this.selectedLayout };
471
+ }
472
+ else {
473
+ return { action: "translate", layout: this.selectedLayout };
474
+ }
475
+ }
476
+ else {
477
+ return { action: "pierce" };
478
+ }
479
+ }
480
+ if (this.litLayout && this.litLayout.element != this.selectedLayout.element) {
481
+ if (this.swapIcon.contains(point)) {
482
+ return { action: "swap", element0: this.selectedLayout.element, element1: this.litLayout.element };
483
+ }
484
+ }
485
+ }
486
+ return null;
487
+ }
488
+ // 選択ボーダー操作
489
+ if (this.selectedBorder) {
490
+ const r = this.acceptsOnSelectedBorder(point);
491
+ if (r) {
492
+ return r;
493
+ }
494
+ }
495
+ return null;
496
+ }
497
+ acceptsOnSelectedBorder(p) {
498
+ if (keyDownFlags["ControlLeft"] || keyDownFlags["ControlRight"] ||
499
+ this.expandHorizontalIcon.contains(p) || this.expandVerticalIcon.contains(p)) {
500
+ return { action: "expand-border", border: this.selectedBorder };
501
+ }
502
+ else if (keyDownFlags["ShiftLeft"] || keyDownFlags["ShiftRight"] ||
503
+ this.slantHorizontalIcon.contains(p) || this.slantVerticalIcon.contains(p)) {
504
+ return { action: "slant-border", border: this.selectedBorder };
505
+ }
506
+ else if (keyDownFlags["KeyT"]) {
507
+ return { action: "transpose-border", border: this.selectedBorder };
508
+ }
509
+ else if (this.insertHorizontalIcon.contains(p) || this.insertVerticalIcon.contains(p)) {
510
+ return { action: "insert", border: this.selectedBorder };
511
+ }
512
+ else if (pointToQuadrilateralDistance(p, this.selectedBorder.corners, false) < BORDER_MARGIN) {
513
+ return { action: "move-border", border: this.selectedBorder };
514
+ }
515
+ return null;
516
+ }
517
+ acceptsOnSelectedFrameIcons(point) {
518
+ const layout = this.selectedLayout;
519
+ if (this.splitHorizontalIcon.contains(point)) {
520
+ return { action: "split-horizontal", layout: layout };
521
+ }
522
+ if (this.splitVerticalIcon.contains(point)) {
523
+ return { action: "split-vertical", layout: layout };
524
+ }
525
+ if (this.deleteIcon.contains(point)) {
526
+ return { action: "erase", layout: layout };
527
+ }
528
+ if (this.duplicateIcon.contains(point)) {
529
+ return { action: "duplicate", layout: layout };
530
+ }
531
+ if (this.shiftIcon.contains(point)) {
532
+ return { action: "shift", layout: layout };
533
+ }
534
+ if (this.unshiftIcon.contains(point)) {
535
+ return { action: "unshift", layout: layout };
536
+ }
537
+ if (this.resetPaddingIcon.contains(point)) {
538
+ return { action: "reset-padding", layout: layout };
539
+ }
540
+ if (this.zplusIcon.contains(point)) {
541
+ return { action: "zplus", layout: layout };
542
+ }
543
+ if (this.zminusIcon.contains(point)) {
544
+ return { action: "zminus", layout: layout };
545
+ }
546
+ if (this.visibilityIcon.contains(point)) {
547
+ return { action: "visibility", layout: layout };
548
+ }
549
+ if (this.flipHorizontalIcon.contains(point)) {
550
+ return { action: "flip-horizontal", layout: layout };
551
+ }
552
+ if (this.flipVerticalIcon.contains(point)) {
553
+ return { action: "flip-vertical", layout: layout };
554
+ }
555
+ if (this.fitIcon.contains(point)) {
556
+ return { action: "fit", layout: layout };
557
+ }
558
+ if (this.scaleIcon.contains(point)) {
559
+ return { action: "scale", layout: layout };
560
+ }
561
+ if (this.rotateIcon.contains(point)) {
562
+ return { action: "rotate", layout: layout };
563
+ }
564
+ return null;
565
+ }
566
+ acceptsBackground(point, _button) {
567
+ const layout = this.calculateRootLayout();
568
+ const border = findBorderAt(layout, point, BORDER_MARGIN);
569
+ if (border) {
570
+ return { action: "select-border", border: border };
571
+ }
572
+ const layoutlet = findLayoutAt(layout, point, PADDING_HANDLE_OUTER_WIDTH, null);
573
+ if (layoutlet) {
574
+ const r = this.acceptsOnFrame(layoutlet);
575
+ if (r) {
576
+ return r;
577
+ }
578
+ }
579
+ this.selectLayout(null);
580
+ this.redraw();
581
+ return null;
582
+ }
583
+ acceptsOnFrame(layout) {
584
+ if (keyDownFlags["KeyQ"]) {
585
+ return { action: "erase", layout: layout };
586
+ }
587
+ if (keyDownFlags["KeyW"]) {
588
+ return { action: "split-horizontal", layout: layout };
589
+ }
590
+ if (keyDownFlags["KeyS"]) {
591
+ return { action: "split-vertical", layout: layout };
592
+ }
593
+ if (keyDownFlags["KeyD"]) {
594
+ return { action: "discard-films", layout: layout };
595
+ }
596
+ if (keyDownFlags["KeyT"]) {
597
+ return { action: "flip-horizontal", layout: layout };
598
+ }
599
+ if (keyDownFlags["KeyY"]) {
600
+ return { action: "flip-vertical", layout: layout };
601
+ }
602
+ if (keyDownFlags["KeyE"]) {
603
+ return { action: "fit", layout: layout };
604
+ }
605
+ return { action: "select", layout: layout };
606
+ }
607
+ changeFocus(layer) {
608
+ if (layer != this) {
609
+ if (this.selectedLayout || this.selectedBorder) {
610
+ this.selectedBorder = null;
611
+ this.doSelectLayout(null);
612
+ }
613
+ }
614
+ }
615
+ async *pointer(p, payload) {
616
+ switch (payload.action) {
617
+ case "transpose-border":
618
+ this.transposeBorder(this.litBorder);
619
+ break;
620
+ case "swap":
621
+ this.swapContent(payload.element0, payload.element1);
622
+ break;
623
+ case "move-padding":
624
+ yield* this.expandPadding(p, payload.padding);
625
+ break;
626
+ case "reset-padding":
627
+ this.resetPadding();
628
+ this.onCommit();
629
+ this.redraw();
630
+ return "done";
631
+ case "select-border":
632
+ this.selectLayout(null);
633
+ this.selectedBorder = payload.border;
634
+ this.relayoutBorderIcons(payload.border);
635
+ this.redraw();
636
+ break;
637
+ case "expand-border":
638
+ yield* this.expandBorder(p, payload.border);
639
+ break;
640
+ case "slant-border":
641
+ yield* this.slantBorder(p, payload.border);
642
+ break;
643
+ case "move-border":
644
+ yield* this.moveBorder(p, payload.border);
645
+ break;
646
+ case "select":
647
+ this.selectedBorder = null;
648
+ this.selectLayout(payload.layout);
649
+ this.relayoutIcons();
650
+ this.redraw();
651
+ break;
652
+ case "split-horizontal":
653
+ FrameElement.splitElementHorizontal(this.frameTree, payload.layout.element);
654
+ this.litLayout = null;
655
+ this.selectLayout(null);
656
+ this.onCommit();
657
+ this.redraw();
658
+ break;
659
+ case "split-vertical":
660
+ FrameElement.splitElementVertical(this.frameTree, payload.layout.element);
661
+ this.litLayout = null;
662
+ this.selectLayout(null);
663
+ this.onCommit();
664
+ this.redraw();
665
+ break;
666
+ case "erase":
667
+ FrameElement.eraseElement(this.frameTree, payload.layout.element);
668
+ this.litLayout = null;
669
+ this.selectLayout(null);
670
+ this.onCommit();
671
+ this.redraw();
672
+ break;
673
+ case "duplicate":
674
+ FrameElement.duplicateElement(this.frameTree, payload.layout.element);
675
+ this.litLayout = null;
676
+ this.selectLayout(null);
677
+ this.onCommit();
678
+ this.redraw();
679
+ break;
680
+ case "shift":
681
+ this.onShift(payload.layout.element);
682
+ this.redraw();
683
+ break;
684
+ case "unshift":
685
+ this.onUnshift(payload.layout.element);
686
+ this.redraw();
687
+ break;
688
+ case "insert":
689
+ this.onInsert(this.selectedBorder);
690
+ this.selectedBorder = null;
691
+ break;
692
+ case "zplus":
693
+ payload.layout.element.z += 1;
694
+ this.onCommit();
695
+ this.redraw();
696
+ break;
697
+ case "zminus":
698
+ payload.layout.element.z -= 1;
699
+ this.onCommit();
700
+ this.redraw();
701
+ break;
702
+ case "visibility":
703
+ this.visibilityIcon.increment();
704
+ payload.layout.element.visibility = this.visibilityIcon.index;
705
+ this.onCommit();
706
+ this.redraw();
707
+ break;
708
+ case "flip-horizontal":
709
+ payload.layout.element.filmStack.films.forEach((film) => {
710
+ film.reverse[0] *= -1;
711
+ });
712
+ this.redraw();
713
+ break;
714
+ case "flip-vertical":
715
+ payload.layout.element.filmStack.films.forEach((film) => {
716
+ film.reverse[1] *= -1;
717
+ });
718
+ this.redraw();
719
+ break;
720
+ case "fit":
721
+ const paperSize = this.getPaperSize();
722
+ const transformer = new FilmStackTransformer(paperSize, payload.layout.element.filmStack.films);
723
+ transformer.scale(0.01);
724
+ constraintLeaf(paperSize, payload.layout);
725
+ this.onCommit();
726
+ this.redraw();
727
+ break;
728
+ case "discard-films":
729
+ payload.layout.element.filmStack.films = [];
730
+ this.redraw();
731
+ break;
732
+ case "scale":
733
+ yield* this.scaleImage(p, payload.layout);
734
+ break;
735
+ case "rotate":
736
+ yield* this.rotateImage(p, payload.layout);
737
+ break;
738
+ case "translate":
739
+ yield* this.translateImage(p, payload.layout);
740
+ break;
741
+ case "ignore":
742
+ break;
743
+ case "pierce":
744
+ this.pierce();
745
+ break;
746
+ default:
747
+ console.error("unknown action", payload.action);
748
+ }
749
+ }
750
+ *scaleImage(p, layout) {
751
+ const paperSize = this.getPaperSize();
752
+ const element = layout.element;
753
+ const films = element.filmStack.getOperationTargetFilms();
754
+ try {
755
+ const transformer = new FilmStackTransformer(paperSize, films);
756
+ yield* scale(this.getPaperSize(), p, (q) => {
757
+ transformer.scale(Math.max(q[0], q[1]));
758
+ if (keyDownFlags["ShiftLeft"] || keyDownFlags["ShiftRight"]) {
759
+ constraintLeaf(paperSize, layout);
760
+ }
761
+ this.redraw();
762
+ });
763
+ this.onCommit();
764
+ }
765
+ catch (e) {
766
+ if (e === "cancel") {
767
+ this.onRevert();
768
+ }
769
+ }
770
+ }
771
+ *rotateImage(p, layout) {
772
+ const paperSize = this.getPaperSize();
773
+ const element = layout.element;
774
+ const films = element.filmStack.getOperationTargetFilms();
775
+ try {
776
+ const transformer = new FilmStackTransformer(paperSize, films);
777
+ yield* rotate(p, (q) => {
778
+ transformer.rotate(q * -0.2);
779
+ if (keyDownFlags["ShiftLeft"] || keyDownFlags["ShiftRight"]) {
780
+ constraintLeaf(paperSize, layout);
781
+ }
782
+ this.redraw();
783
+ });
784
+ this.onCommit();
785
+ }
786
+ catch (e) {
787
+ if (e === "cancel") {
788
+ this.onRevert();
789
+ }
790
+ }
791
+ }
792
+ *translateImage(p, layout) {
793
+ // クリック判定と兼用
794
+ const startingTime = performance.now();
795
+ const paperSize = this.getPaperSize();
796
+ const element = layout.element;
797
+ const films = element.filmStack.getOperationTargetFilms();
798
+ const origins = films.map(film => film.getShiftedTranslation(paperSize));
799
+ try {
800
+ let lastq = null;
801
+ yield* translate(p, (q) => {
802
+ lastq = [...q];
803
+ films.forEach((film, i) => {
804
+ film.setShiftedTranslation(paperSize, [origins[i][0] + q[0], origins[i][1] + q[1]]);
805
+ });
806
+ if (keyDownFlags["ShiftLeft"] || keyDownFlags["ShiftRight"]) {
807
+ constraintLeaf(paperSize, layout);
808
+ }
809
+ this.redraw();
810
+ });
811
+ if (lastq[0] !== 0 || lastq[1] !== 0) {
812
+ this.onCommit();
813
+ }
814
+ }
815
+ catch (e) {
816
+ if (e === "cancel") {
817
+ this.onRevert();
818
+ }
819
+ }
820
+ if (performance.now() - startingTime < 200) {
821
+ // クリック判定
822
+ this.pierce();
823
+ }
824
+ }
825
+ *moveBorder(p, border) {
826
+ const layout = border.layout;
827
+ const index = border.index;
828
+ let i0 = index - 1;
829
+ let i1 = index;
830
+ if (layout.dir === "h") {
831
+ [i0, i1] = [i1, i0];
832
+ }
833
+ const child0 = layout.children[i0];
834
+ const child1 = layout.children[i1];
835
+ const c0 = child0.element;
836
+ const c1 = child1.element;
837
+ const rawSpacing = layout.children[index - 1].element.divider.spacing; // dont use i0
838
+ const rawSum = c0.rawSize + rawSpacing + c1.rawSize;
839
+ function getRect() {
840
+ return box2Rect([child0.origin, add2D(child1.origin, child1.size)]);
841
+ }
842
+ function getBalance(p) {
843
+ const r = getRect();
844
+ // 0.0 - 1.0, 0.0: top or left of box0, 1.0: right or bottom of box1
845
+ if (layout.dir == "h") {
846
+ return (p[0] - r[0]) / r[2];
847
+ }
848
+ else {
849
+ const r = getRect();
850
+ return (p[1] - r[1]) / r[3];
851
+ }
852
+ }
853
+ try {
854
+ const s = p;
855
+ let q = p;
856
+ const startBalance = (c0.rawSize + rawSpacing * 0.5) / rawSum;
857
+ const delta = getBalance(p) - startBalance;
858
+ while ((p = yield)) {
859
+ const balance = getBalance(p) - delta;
860
+ const t = balance * rawSum;
861
+ c0.rawSize = t - rawSpacing * 0.5;
862
+ c1.rawSize = rawSum - t - rawSpacing * 0.5;
863
+ this.updateBorder(border);
864
+ this.redraw();
865
+ q = p;
866
+ }
867
+ if (!vectorEquals(s, q)) {
868
+ this.onCommit();
869
+ }
870
+ }
871
+ catch (e) {
872
+ if (e === 'cancel') {
873
+ this.onRevert();
874
+ }
875
+ }
876
+ }
877
+ *expandBorder(p, border) {
878
+ const element = border.layout.element;
879
+ const dir = border.layout.dir == "h" ? 0 : 1;
880
+ const prev = border.layout.children[border.index - 1].element;
881
+ const curr = border.layout.children[border.index].element;
882
+ const startSpacing = prev.divider.spacing;
883
+ const s = p;
884
+ const startPrevRawSize = prev.rawSize;
885
+ const startCurrRawSize = curr.rawSize;
886
+ const factor = 0.005 * (startPrevRawSize + startCurrRawSize) * border.layout.size[dir] / this.getPaperSize()[dir];
887
+ try {
888
+ while ((p = yield)) {
889
+ const op = p[dir] - s[dir];
890
+ prev.divider.spacing = Math.max(0, startSpacing + op * factor * 0.1);
891
+ const diff = prev.divider.spacing - startSpacing;
892
+ prev.rawSize = startPrevRawSize - diff * 0.5;
893
+ curr.rawSize = startCurrRawSize - diff * 0.5;
894
+ element.calculateLengthAndBreadth();
895
+ this.updateBorder(border);
896
+ this.redraw();
897
+ }
898
+ }
899
+ catch (e) {
900
+ if (e === 'cancel') {
901
+ this.onRevert();
902
+ }
903
+ }
904
+ this.onCommit();
905
+ }
906
+ *slantBorder(p, border) {
907
+ const dir = border.layout.dir == "h" ? 0 : 1;
908
+ const prev = border.layout.children[border.index - 1].element;
909
+ const rawSlant = prev.divider.slant;
910
+ const s = p;
911
+ try {
912
+ while ((p = yield)) {
913
+ const op = p[dir] - s[dir];
914
+ let newSlant = rawSlant + op * 0.2;
915
+ if (keyDownFlags["AltLeft"] || keyDownFlags["AltRight"]) {
916
+ newSlant = Math.round(newSlant / 15) * 15;
917
+ }
918
+ prev.divider.slant = Math.max(-42, Math.min(42, newSlant));
919
+ this.updateBorder(border);
920
+ this.redraw();
921
+ }
922
+ }
923
+ catch (e) {
924
+ if (e === 'cancel') {
925
+ this.onRevert();
926
+ }
927
+ }
928
+ this.onCommit();
929
+ }
930
+ *expandPadding(p, padding) {
931
+ const element = padding.layout.element;
932
+ const rawSize = padding.layout.rawSize;
933
+ const corners = padding.layout.corners;
934
+ const handle = padding.handle;
935
+ try {
936
+ if (rectCornerHandles.indexOf(handle) !== -1) {
937
+ // padding.handleが角の場合
938
+ const cornerHandle = handle;
939
+ const q = element.cornerOffsets[cornerHandle];
940
+ const s = p;
941
+ while ((p = yield)) {
942
+ const delta = [p[0] - s[0], p[1] - s[1]];
943
+ element.cornerOffsets[handle] = [q[0] + delta[0] / rawSize[0], q[1] + delta[1] / rawSize[1]];
944
+ this.updateSelectedLayout();
945
+ this.redraw();
946
+ }
947
+ }
948
+ else {
949
+ // padding.handleが辺の場合
950
+ let c0, c1;
951
+ let direction;
952
+ switch (handle) {
953
+ case "top":
954
+ c0 = "topLeft";
955
+ c1 = "topRight";
956
+ direction = "vertical";
957
+ break;
958
+ case "bottom":
959
+ c0 = "bottomLeft";
960
+ c1 = "bottomRight";
961
+ direction = "vertical";
962
+ break;
963
+ case "left":
964
+ c0 = "topLeft";
965
+ c1 = "bottomLeft";
966
+ direction = "horizontal";
967
+ break;
968
+ case "right":
969
+ c0 = "topRight";
970
+ c1 = "bottomRight";
971
+ direction = "horizontal";
972
+ break;
973
+ default:
974
+ throw new Error("unknown handle");
975
+ }
976
+ const offsettedCorners = calculateOffsettedCorners(rawSize, corners, element.cornerOffsets);
977
+ const s = p;
978
+ if (direction === "vertical") {
979
+ const y0 = offsettedCorners[c0][1] - corners[c0][1];
980
+ const y1 = offsettedCorners[c1][1] - corners[c1][1];
981
+ while ((p = yield)) {
982
+ const delta = p[1] - s[1];
983
+ element.cornerOffsets[c0][1] = (y0 + delta) / rawSize[1];
984
+ element.cornerOffsets[c1][1] = (y1 + delta) / rawSize[1];
985
+ this.updateSelectedLayout();
986
+ this.redraw();
987
+ }
988
+ }
989
+ else {
990
+ const x0 = offsettedCorners[c0][0] - corners[c0][0];
991
+ const x1 = offsettedCorners[c1][0] - corners[c1][0];
992
+ while ((p = yield)) {
993
+ const delta = p[0] - s[0];
994
+ element.cornerOffsets[c0][0] = (x0 + delta) / rawSize[0];
995
+ element.cornerOffsets[c1][0] = (x1 + delta) / rawSize[0];
996
+ this.updateSelectedLayout();
997
+ this.redraw();
998
+ }
999
+ }
1000
+ }
1001
+ }
1002
+ catch (e) {
1003
+ if (e === 'cancel') {
1004
+ this.onRevert();
1005
+ }
1006
+ else {
1007
+ console.error(e);
1008
+ }
1009
+ }
1010
+ this.onCommit();
1011
+ }
1012
+ constraintAll() {
1013
+ const paperSize = this.getPaperSize();
1014
+ const layout = this.calculateRootLayout();
1015
+ constraintRecursive(paperSize, layout);
1016
+ }
1017
+ importMedia(layoutlet, media) {
1018
+ const paperSize = this.getPaperSize();
1019
+ // calc expansion to longer size
1020
+ const film = new Film(media);
1021
+ film.n_scale = 1;
1022
+ film.rotation = 0;
1023
+ film.reverse = [1, 1];
1024
+ film.n_translation = [0, 0];
1025
+ layoutlet.element.filmStack.films.push(film);
1026
+ const transformer = new FilmStackTransformer(paperSize, layoutlet.element.filmStack.films);
1027
+ transformer.scale(0.01);
1028
+ constraintLeaf(paperSize, layoutlet);
1029
+ this.onCommit();
1030
+ this.redraw();
1031
+ }
1032
+ updateSelectedLayout() {
1033
+ if (!this.selectedLayout) {
1034
+ return;
1035
+ }
1036
+ const rootLayout = this.calculateRootLayout();
1037
+ this.selectLayout(findLayoutOf(rootLayout, this.selectedLayout.element));
1038
+ if (this.focusedPadding) {
1039
+ const handle = this.focusedPadding.handle;
1040
+ this.focusedPadding = findPaddingOf(this.selectedLayout, handle, PADDING_HANDLE_INNER_WIDTH, PADDING_HANDLE_OUTER_WIDTH);
1041
+ }
1042
+ }
1043
+ updateBorder(border) {
1044
+ const rootLayout = this.calculateRootLayout();
1045
+ const newLayout = findLayoutOf(rootLayout, border.layout.element);
1046
+ border.layout = newLayout;
1047
+ this.updateBorderTrapezoid(border);
1048
+ }
1049
+ updateBorderTrapezoid(border) {
1050
+ const corners = makeBorderCorners(border.layout, border.index, 0);
1051
+ const formalCorners = makeBorderFormalCorners(border.layout, border.index);
1052
+ border.corners = corners;
1053
+ border.formalCorners = formalCorners;
1054
+ this.relayoutBorderIcons(border);
1055
+ }
1056
+ relayoutIcons() {
1057
+ if (this.selectedLayout) {
1058
+ this.relayoutFrameIcons(this.selectedLayout);
1059
+ this.redraw();
1060
+ }
1061
+ if (this.selectedBorder) {
1062
+ this.relayoutBorderIcons(this.selectedBorder);
1063
+ this.redraw();
1064
+ }
1065
+ }
1066
+ relayoutFrameIcons(layout) {
1067
+ const grid = this.makeGrid(layout);
1068
+ const cp = grid.calcPosition.bind(grid);
1069
+ this.visibilityIcon.position = cp([0, 0], [0, 0]);
1070
+ this.visibilityIcon.index = layout.element.visibility;
1071
+ this.zplusIcon.position = cp([0, 0], [1, -0.05]);
1072
+ this.zminusIcon.position = cp([0, 0], [1, 0.9]);
1073
+ this.zvalue.position = cp([0, 0], [1.025, -0.075]);
1074
+ this.deleteIcon.position = cp([1, 0], [0, 0]);
1075
+ this.duplicateIcon.position = cp([1, 0], [-1, 0]);
1076
+ this.shiftIcon.position = cp([1, 0], [-2, 0]);
1077
+ this.unshiftIcon.position = cp([1, 0], [-3, 0]);
1078
+ this.resetPaddingIcon.position = cp([1, 0], [-4, 0]);
1079
+ this.splitHorizontalIcon.position = cp([0, 1], [0, 0]);
1080
+ this.splitVerticalIcon.position = cp([0, 1], [1, 0]);
1081
+ this.flipHorizontalIcon.position = cp([1, 1], [-4.5, 0]);
1082
+ this.flipVerticalIcon.position = cp([1, 1], [-3.5, 0]);
1083
+ this.fitIcon.position = cp([1, 1], [-2.5, 0]);
1084
+ this.scaleIcon.position = cp([1, 1], [0, 0]);
1085
+ this.rotateIcon.position = cp([1, 1], [-1, 0]);
1086
+ }
1087
+ makeGrid(layout) {
1088
+ const rscale = 1 / this.paper.matrix.a;
1089
+ const unit = scale2D([...iconUnit], rscale);
1090
+ const grid = new Grid(this.calculateSheetRect(layout.corners), -10, unit);
1091
+ return grid;
1092
+ }
1093
+ relayoutBorderIcons(border) {
1094
+ const bt = border.corners;
1095
+ this.slantVerticalIcon.position = [bt.topLeft[0], (bt.topLeft[1] + bt.bottomLeft[1]) * 0.5];
1096
+ this.expandVerticalIcon.position = [(bt.topRight[0] + bt.topRight[0]) * 0.5, (bt.topRight[1] + bt.bottomRight[1]) * 0.5];
1097
+ this.insertVerticalIcon.position = [bt.topLeft[0] - 40, (bt.topLeft[1] + bt.bottomLeft[1]) * 0.5];
1098
+ this.slantHorizontalIcon.position = [(bt.topLeft[0] + bt.topRight[0]) * 0.5, bt.topLeft[1]];
1099
+ this.expandHorizontalIcon.position = [(bt.bottomLeft[0] + bt.bottomRight[0]) * 0.5, (bt.bottomLeft[1] + bt.bottomRight[1]) * 0.5];
1100
+ this.insertHorizontalIcon.position = [(bt.topLeft[0] + bt.topRight[0]) * 0.5, bt.topLeft[1] - 40];
1101
+ }
1102
+ relayoutLitIcons(layout) {
1103
+ const center = lerp2D(layout.corners.topLeft, layout.corners.topRight, 0.5);
1104
+ getRectCenter([...layout.origin, ...layout.size]);
1105
+ this.swapIcon.position = center;
1106
+ }
1107
+ resetPadding() {
1108
+ if (!this.selectedLayout) {
1109
+ return;
1110
+ }
1111
+ const element = this.selectedLayout.element;
1112
+ element.cornerOffsets = {
1113
+ topLeft: [0, 0],
1114
+ topRight: [0, 0],
1115
+ bottomLeft: [0, 0],
1116
+ bottomRight: [0, 0],
1117
+ };
1118
+ this.updateSelectedLayout();
1119
+ this.redraw();
1120
+ }
1121
+ swapContent(a, b) {
1122
+ console.log("swap", a, b);
1123
+ this.onSwap(a, b);
1124
+ this.onCommit();
1125
+ this.redraw();
1126
+ }
1127
+ beforeDoubleClick(p) {
1128
+ for (let e of this.frameIcons) {
1129
+ if (e.contains(p)) {
1130
+ return true;
1131
+ }
1132
+ }
1133
+ for (let e of this.borderIcons) {
1134
+ if (e.contains(p)) {
1135
+ return true;
1136
+ }
1137
+ }
1138
+ for (let e of this.litIcons) {
1139
+ if (e.contains(p)) {
1140
+ return true;
1141
+ }
1142
+ }
1143
+ return false;
1144
+ }
1145
+ // 基本的には直接呼び出さない
1146
+ // changeFocusとselectLayoutからのみ
1147
+ videoRedrawInterval;
1148
+ doSelectLayout(layout) {
1149
+ if (layout?.element !== this.selectedLayout?.element) {
1150
+ clearInterval(this.videoRedrawInterval);
1151
+ this.videoRedrawInterval = undefined;
1152
+ if (this.selectedLayout) {
1153
+ for (const film of this.selectedLayout.element.filmStack.films) {
1154
+ if (film.media instanceof VideoMedia) {
1155
+ film.media.video.pause();
1156
+ }
1157
+ }
1158
+ }
1159
+ if (layout) {
1160
+ let playFlag = false;
1161
+ for (const film of layout.element.filmStack.films) {
1162
+ if (film.media instanceof VideoMedia) {
1163
+ playFlag = true;
1164
+ film.media.video.play();
1165
+ }
1166
+ }
1167
+ if (playFlag) {
1168
+ this.videoRedrawInterval = setInterval(() => {
1169
+ this.redraw();
1170
+ }, 1000 / 24);
1171
+ }
1172
+ }
1173
+ }
1174
+ this.selectedLayout = layout;
1175
+ this.relayoutIcons();
1176
+ this.onFocus(layout);
1177
+ }
1178
+ selectLayout(layout) {
1179
+ this.doSelectLayout(layout);
1180
+ this.focusKeeper.setFocus(layout == null ? null : this);
1181
+ }
1182
+ drawFilmBorders(ctx, layout) {
1183
+ const paperSize = this.getPaperSize();
1184
+ const element = layout.element;
1185
+ const [x0, y0, w, h] = trapezoidBoundingRect(layout.corners);
1186
+ ctx.save();
1187
+ ctx.translate(x0 + w * 0.5, y0 + h * 0.5);
1188
+ drawFilmStackBorders(ctx, element.filmStack, paperSize);
1189
+ ctx.restore();
1190
+ }
1191
+ transposeBorder(border) {
1192
+ const target = border.layout.element.children[border.index - 1];
1193
+ FrameElement.transposeDivider(this.frameTree, target);
1194
+ this.onCommit();
1195
+ this.litLayout = null;
1196
+ this.selectedBorder = null;
1197
+ this.litBorder = null;
1198
+ this.selectLayout(null);
1199
+ this.redraw();
1200
+ }
1201
+ renderDepths() { return [0]; }
1202
+ get interactable() { return this.mode == null; }
1203
+ }
1204
+ sequentializePointer(FrameLayer);
1205
+ // paper.jsをvitest環境で読み込むとエラーが出るようなので
1206
+ // trapezoid.tsに置けない
1207
+ // https://github.com/paperjs/paper.js/issues/1805
1208
+ export function getTrapezoidPath(t, margin, ignoresInverted) {
1209
+ const [A, B, C, D] = [[...t.topLeft], [...t.topRight], [...t.bottomRight], [...t.bottomLeft]];
1210
+ // PaperOffset.offsetが縮退ポリゴンを正しく扱えていないため、小細工
1211
+ let flag = true;
1212
+ while (flag) {
1213
+ flag = false;
1214
+ if (distance2D(A, B) < 0.05) {
1215
+ B[0] += 0.1;
1216
+ flag = true;
1217
+ }
1218
+ if (distance2D(B, C) < 0.05) {
1219
+ C[1] += 0.1;
1220
+ flag = true;
1221
+ }
1222
+ if (distance2D(C, D) < 0.05) {
1223
+ D[0] -= 0.1;
1224
+ flag = true;
1225
+ }
1226
+ if (distance2D(D, A) < 0.05) {
1227
+ A[1] -= 0.1;
1228
+ flag = true;
1229
+ }
1230
+ }
1231
+ const path = new paper.Path();
1232
+ const join = "round";
1233
+ function addTriangle(a, b, c) {
1234
+ if (!ignoresInverted || isTriangleClockwise([a, b, c])) {
1235
+ path.add(a, b, c);
1236
+ path.closed = true;
1237
+ return 1;
1238
+ }
1239
+ return 0;
1240
+ }
1241
+ // A C
1242
+ // |X|
1243
+ // D B
1244
+ const q = segmentIntersection([A, B], [C, D]);
1245
+ if (q) {
1246
+ let n = 0;
1247
+ n += addTriangle(A, q, D);
1248
+ n += addTriangle(C, q, B);
1249
+ if (n == 0) {
1250
+ return new Path2D();
1251
+ }
1252
+ return new Path2D(PaperOffset.offset(path, margin, { join }).pathData);
1253
+ }
1254
+ // A-B
1255
+ // X
1256
+ // C-D
1257
+ const q2 = segmentIntersection([A, D], [B, C]);
1258
+ if (q2) {
1259
+ let n = 0;
1260
+ n += addTriangle(A, B, q2);
1261
+ n += addTriangle(q2, C, D);
1262
+ if (n == 0) {
1263
+ return new Path2D();
1264
+ }
1265
+ return new Path2D(PaperOffset.offset(path, margin, { join }).pathData);
1266
+ }
1267
+ // A-B
1268
+ // | |
1269
+ // D-C
1270
+ if (ignoresInverted || isTriangleClockwise([A, B, C])) {
1271
+ path.add(A, B, C, D);
1272
+ path.closed = true;
1273
+ try {
1274
+ return new Path2D(PaperOffset.offset(path, margin, { join }).pathData);
1275
+ }
1276
+ catch (e) {
1277
+ console.error(e);
1278
+ console.log(A, B, C, D, margin, ignoresInverted);
1279
+ }
1280
+ }
1281
+ return new Path2D();
1282
+ }
1283
+ //# sourceMappingURL=frameLayer.js.map