-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathButton.qml
235 lines (194 loc) · 5.95 KB
/
Button.qml
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import QtQuick 2.5
import QtQml 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQml.StateMachine 1.0 as SMF
import "minesweeper.js" as Minesweeper
Item {
property int position: 0
property alias backgroundColor : background.color
property alias border : background.border
property alias font : text.font
property real initialStateOpacity : 0.9
property color initialStateColor : "white"
property real questionStateOpacity : 0.7
property color questionStateColor : "blue"
property real explodedStateOpacity : 0.6
property color explodedStateColor : "darkred"
QtObject {
id: p;
property bool isRightButton: true;
property bool isExplosive: Minesweeper.isExplosivePosition(position);
property int explosiveSiblingCount: Minesweeper.explosiveSiblingCount(position);
function siblingCountButtonTextColor()
{
switch(explosiveSiblingCount) {
case 1:
return "blue";
case 2:
return "green";
case 3:
return "red";
case 4:
return "purple";
case 5:
return "maroon";
case 6:
return "cyan";
case 7:
return "black";
case 8:
return "gray";
default:
break;
}
return "white";
}
}
SMF.StateMachine {
id: stateMachine
initialState: startState
running: true
SMF.State {
id: startState
onEntered: {
text.text = ""
text.color = initialStateColor
background.opacity = initialStateOpacity
}
SMF.SignalTransition {
targetState: questionState
signal: mousearea.onClicked
guard: p.isRightButton
}
SMF.SignalTransition {
targetState: explodeState
signal: mousearea.onClicked
guard: !p.isRightButton && p.isExplosive;
}
SMF.SignalTransition {
targetState: finalState
signal: mousearea.onClicked
guard: !p.isRightButton && !p.isExplosive;
}
}
SMF.State {
id: questionState
onEntered: {
text.text = qsTr("?")
text.color = questionStateColor
background.opacity = questionStateOpacity
}
SMF.SignalTransition {
targetState: startState
signal: mousearea.onClicked
guard: p.isRightButton
}
}
SMF.State {
id: explodeState
SequentialAnimation {
id: explodeStateAnimator
ParallelAnimation {
ScaleAnimator {
target: text
from: 1.2
to: 0.8
easing.type: Easing.InBounce
duration: 400
}
RotationAnimator {
target: text
from: -15
to: 15
duration: 400
}
}
ParallelAnimation {
ScaleAnimator {
target: text
from: 0.8
to: 1.2
easing.type: Easing.OutBounce
duration: 400
}
RotationAnimator {
target: text
from: 15
to: -15
duration: 400
}
}
running: false
loops: Animation.Infinite
}
onEntered: {
text.text = qsTr("\uD83D\uDCA3")
text.color = explodedStateColor
background.opacity = explodedStateOpacity
explodeStateAnimator.running = true
}
}
SMF.State {
id: finalState
onEntered: {
text.text = p.explosiveSiblingCount
text.visible = p.explosiveSiblingCount > 0
text.color = p.siblingCountButtonTextColor()
background.opacity = p.explosiveSiblingCount === 0 ? 0.25 : 0.5
}
}
}
Rectangle {
id: background
anchors.fill: parent
color: "lightgray"
border.color: "black"
border.width: 1
Behavior on opacity {
NumberAnimation {
duration: 250
}
}
}
Text {
id: text
anchors.fill: parent
font.pixelSize: height * 0.7
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
Behavior on color {
ColorAnimation {
duration: 1500
}
}
}
MouseArea {
id: mousearea;
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: {
p.isRightButton = mouse.button == Qt.RightButton
}
}
ParallelAnimation {
id: startupAnimation
running: false
NumberAnimation {
target: background
property: "rotation"
from: 0
to: 360
easing.type: Easing.OutBounce
duration: Minesweeper.randomInt(500, 4000)
}
ColorAnimation {
target: background
property: "color"
from: Qt.rgba(Math.random(),Math.random(),Math.random(),1)
to: backgroundColor
duration: Minesweeper.randomInt(500, 4000)
}
}
Component.onCompleted: startupAnimation.start()
}