-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy.CropArea.ux
116 lines (108 loc) · 4.63 KB
/
my.CropArea.ux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<Panel ux:Class="my.CropArea" Layer="Overlay" Width="100%" Height="100%">
<object ux:Property="CornerList" />
<JavaScript>
var Observable = require("FuseJS/Observable");
var cornerList = this.CornerList.innerTwoWay();
var tmpCornerList = Observable();
var strokePairList = tmpCornerList.map(function(c) {
var pairIndex = (c.id + 1 <= 3) ? c.id + 1 : 0;
var strokePair = {start: c, end: tmpCornerList.getAt(pairIndex)};
return strokePair;
});
// マーカー表示用の設定値
var circleSize = 80;
var radius = 40;
var offset = - circleSize / 2;
var insideCircleSize = 20;
// ドラッグ対象のマーカーのID
var selected_id = null;
var lastX = null, lastY = null, localX = null, localY = null,
initialX = null, initialY = null;
function onMoved(args) {
if (selected_id == null) {
return;
}
if (localX == null && localY == null) {
// localX,localYは、イベントを発行したエレメント内における発火点のX,Y座標のこと
localX = -radius + args.localX;
localY = -radius + args.localY;
initialX = args.x;
initialY = args.y;
}
lastX = Math.floor(initialX + (args.x - initialX) - localX);
lastY = Math.floor(initialY + (args.y - initialY) - localY);
updatePosition(selected_id, lastX, lastY);
}
function onDragCorner(args) {
selected_id = args.data.id;
}
function onReleaseCorner(args) {
// 指を話した時のみ、このux:Classの呼び出し元へと変数の更新を伝播
updatePosition(selected_id, lastX, lastY, true);
selected_id = null;
lastX = null;
lastY = null;
localX = null;
localY = null;
initialX = null;
initialY = null;
}
function updatePosition(id, x, y, updateParent=false) {
if (id == null || x == null || y == null) {
return;
}
tmpCornerList.getAt(id).x.value = x;
tmpCornerList.getAt(id).y.value = y;
if (updateParent) {
cornerList.replaceAll(tmpCornerList.toArray());
}
}
var initialCornerList = Observable();
cornerList.onValueChanged(module, function(v) {
if (!initialCornerList.value && cornerList.toArray().length > 0) {
initialCornerList.replaceAll(cornerList.toArray());
tmpCornerList.replaceAll(cornerList.toArray());
}
});
module.exports = {
circleSize: circleSize,
offset: offset,
insideCircleSize: insideCircleSize,
initialCornerList: initialCornerList,
strokePairList: strokePairList,
onDragCorner: onDragCorner,
onReleaseCorner: onReleaseCorner,
onMoved: onMoved,
}
</JavaScript>
<!-- 頂点を指定するためのマーカー表示の雛形 -->
<Panel ux:InnerClass="CornerPoint" ux:Name="CornerPointElm" Layer="Overlay" X="{x}" Y="{y}">
<Circle Color="Blue" Width="{circleSize}" Height="{circleSize}" Offset="{offset}, {offset}" HitTestMode="LocalBounds" Opacity="0.3">
<Pressed>
<Callback Handler="{onDragCorner}" />
</Pressed>
</Circle>
<Circle Color="Blue" Width="{insideCircleSize}" Height="{insideCircleSize}" Offset="{offset}, {offset}" />
</Panel>
<!-- 実際のマーカー表示 -->
<ClientPanel Moved="{onMoved}" HitTestMode="LocalBoundsAndChildren">
<Released>
<Callback Handler="{onReleaseCorner}" />
</Released>
<Each Items="{initialCornerList}">
<Text Value="({x}, {y})" Color="Gray" TextWrapping="Wrap" X="{x}" Y="{y}" Offset="-30,10"/>
<!-- InnerClass:CornerPointを呼び出し -->
<CornerPoint />
</Each>
</ClientPanel>
<!-- 各マーカーを結ぶ直線の表示 -->
<ClientPanel>
<!-- https://www.fusetools.com/community/forums/bug_reports/curve_dont_draw_line -->
<Each Items="{strokePairList}">
<Curve StrokeWidth="3" Width="1" Height="1" StrokeColor="Blue" Style="Straight" Layer="Overlay" X="0" Y="0" Offset="0, 0" Opacity="0.5">
<CurvePoint At="{start.x}, {start.y}" />
<CurvePoint At="{end.x}, {end.y}" />
</Curve>
</Each>
</ClientPanel>
</Panel>