• Skip to content
  • Skip to link menu
KDE API Documentation - AnswerButton.qml Source File (GCompris-qt)
  • KDE Home
  • Contact Us
 

GCompris-qt

  • src
  • core
AnswerButton.qml
1 /*
2  Copyed in GCompris from Touch'n'learn
3 
4  Touch'n'learn - Fun and easy mobile lessons for kids
5  Copyright (C) 2010, 2011 by Alessandro Portale <alessandro@casaportale.de>
6  http://touchandlearn.sourceforge.net
7 
8  This file is part of Touch'n'learn
9 
10  Touch'n'learn is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  Touch'n'learn is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with Touch'n'learn; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24 
25 import QtQuick 2.1
26 import GCompris 1.0
27 
28 Item {
29  id: button
30 
31  property string textLabel
32  property bool isCorrectAnswer: false
33 
34  property color normalStateColor: "#fff"
35  property color correctStateColor: "#ffa"
36  property color wrongStateColor: "#f66"
37  property bool blockClicks: false
38 
39  property int wrongAnswerShakeAmplitudeCalc: width * 0.2
40  property int wrongAnswerShakeAmplitudeMin: 45
41  property int wrongAnswerShakeAmplitude: wrongAnswerShakeAmplitudeCalc < wrongAnswerShakeAmplitudeMin ? wrongAnswerShakeAmplitudeMin : wrongAnswerShakeAmplitudeCalc
42 
43  // If you want the sound effects just pass the audioEffects
44  property GCAudio audioEffects
45 
46  signal correctlyPressed
47  signal incorrectlyPressed
48 
49  signal pressed
50  onPressed: {
51  if (!blockClicks) {
52  if (isCorrectAnswer) {
53  if(audioEffects)
54  audioEffects.play("qrc:/gcompris/src/core/resource/sounds/win.wav")
55  correctAnswerAnimation.start();
56  } else {
57  if(audioEffects)
58  audioEffects.play("qrc:/gcompris/src/core/resource/sounds/crash.wav")
59  wrongAnswerAnimation.start();
60  }
61  }
62  }
63 
64  Rectangle {
65  id: rect
66  anchors.fill: parent
67  color: normalStateColor
68  opacity: 0.5
69  }
70  ParticleSystemStarLoader {
71  id: particles
72  }
73  Image {
74  source: "qrc:/gcompris/src/core/resource/button.svg"
75  sourceSize { height: parent.height; width: parent.width }
76  width: sourceSize.width
77  height: sourceSize.height
78  smooth: false
79  }
80  GCText {
81  id: label
82  anchors.verticalCenter: parent.verticalCenter
83  // We need to manually horizonally center the text, because in wrongAnswerAnimation,
84  // the x of the text is changed, which would not work if we use an anchor layout.
85  property int horizontallyCenteredX: (button.width - width) >> 1;
86  x: horizontallyCenteredX;
87  fontSize: 18
88  font.bold: true
89  text: textLabel
90  }
91 
92  MouseArea {
93  id: mouseArea
94  anchors.fill: parent
95  onPressed: button.pressed()
96  }
97 
98  SequentialAnimation {
99  id: correctAnswerAnimation
100  ScriptAction {
101  script: {
102  if (typeof(feedback) === "object")
103  feedback.playCorrectSound();
104  blockClicks = true;
105  if (typeof(particles) === "object")
106  particles.burst(40);
107  }
108  }
109  PropertyAction {
110  target: rect
111  property: "color"
112  value: correctStateColor
113  }
114  PropertyAnimation {
115  target: rect
116  property: "color"
117  to: normalStateColor
118  duration: 700
119  }
120  PauseAnimation {
121  duration: 300 // Wait for particles to finish
122  }
123  ScriptAction {
124  script: {
125  blockClicks = false;
126  correctlyPressed();
127  }
128  }
129  }
130 
131  SequentialAnimation {
132  id: wrongAnswerAnimation
133  ParallelAnimation {
134  SequentialAnimation {
135  PropertyAction {
136  target: rect
137  property: "color"
138  value: wrongStateColor
139  }
140  ScriptAction {
141  script: {
142  if (typeof(feedback) === "object")
143  feedback.playIncorrectSound();
144  }
145  }
146  PropertyAnimation {
147  target: rect
148  property: "color"
149  to: normalStateColor
150  duration: 600
151  }
152  }
153  SequentialAnimation {
154  PropertyAnimation {
155  target: label
156  property: "x"
157  to: label.horizontallyCenteredX - wrongAnswerShakeAmplitude
158  easing.type: Easing.InCubic
159  duration: 120
160  }
161  PropertyAnimation {
162  target: label
163  property: "x"
164  to: label.horizontallyCenteredX + wrongAnswerShakeAmplitude
165  easing.type: Easing.InOutCubic
166  duration: 220
167  }
168  PropertyAnimation {
169  target: label
170  property: "x"
171  to: label.horizontallyCenteredX
172  easing { type: Easing.OutBack; overshoot: 3 }
173  duration: 180
174  }
175  }
176  }
177  PropertyAnimation {
178  target: rect
179  property: "color"
180  to: normalStateColor
181  duration: 450
182  }
183  ScriptAction {
184  script: {
185  incorrectlyPressed();
186  }
187  }
188  }
189 }
GCText
A QML component unifying text presentation in GCompris.
Definition: GCText.qml:47
QtQuick
GCAudio
A QML component for audio playback.
Definition: GCAudio.qml:44
GCAudio::play
void play(string file)
When mute is changed we set the volume to 0 to mute a potential playing sound.
GCompris
ParticleSystemStarLoader
A QML loader that wraps ParticleSystemStar.
Definition: ParticleSystemStarLoader.qml:34
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Tue Jun 2 2015 21:47:47 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

GCompris-qt

Skip menu "GCompris-qt"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Modules

Class Picker

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal