Changeset View
Changeset View
Standalone View
Standalone View
src/activities/analog_electricity/ElectricalComponent.qml
- This file was added.
1 | /* GCompris - ElectricalComponent.qml | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright (C) 2019 Deepak Kumar <deepakdk2431@gmail.com> | ||||
4 | * | ||||
5 | * This program is free software; you can redistribute it and/or modify | ||||
6 | * it under the terms of the GNU General Public License as published by | ||||
7 | * the Free Software Foundation; either version 3 of the License, or | ||||
8 | * (at your option) any later version. | ||||
9 | * | ||||
10 | * This program is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | * GNU General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU General Public License | ||||
16 | * along with this program; if not, see <https://www.gnu.org/licenses/>. | ||||
17 | */ | ||||
18 | import QtQuick 2.6 | ||||
19 | import GCompris 1.0 | ||||
20 | | ||||
21 | import "analog_electricity.js" as Activity | ||||
22 | | ||||
23 | Image { | ||||
24 | id: electricalComponent | ||||
25 | property double posX | ||||
26 | property double posY | ||||
27 | property double imgWidth | ||||
28 | property double imgHeight | ||||
29 | property string imgSrc | ||||
30 | property string information | ||||
31 | property string toolTipTxt | ||||
32 | property int index | ||||
33 | property int noOfNodes | ||||
34 | property int rotationAngle: 0 | ||||
35 | property int initialAngle: 0 | ||||
36 | property int startingAngle: 0 | ||||
37 | property double terminalSize | ||||
38 | property bool destructible | ||||
39 | | ||||
40 | property alias rotateComponent: rotateComponent | ||||
41 | | ||||
42 | x: posX * parent.width | ||||
43 | y: posY * parent.height | ||||
44 | width: imgWidth * parent.width | ||||
45 | height: imgHeight * parent.height | ||||
46 | fillMode: Image.PreserveAspectFit | ||||
47 | | ||||
48 | source: Activity.url + imgSrc | ||||
49 | z: 2 | ||||
50 | mipmap: true | ||||
51 | antialiasing: true | ||||
52 | | ||||
53 | onPaintedWidthChanged: { | ||||
54 | updateDragConstraints() | ||||
55 | } | ||||
56 | | ||||
57 | | ||||
58 | PropertyAnimation { | ||||
59 | id: rotateComponent | ||||
60 | target: electricalComponent | ||||
61 | property: "rotation" | ||||
62 | from: initialAngle; to: initialAngle + rotationAngle | ||||
63 | duration: 1 | ||||
64 | onStarted:{ Activity.animationInProgress = true } | ||||
65 | onStopped: { | ||||
66 | initialAngle = initialAngle + rotationAngle | ||||
67 | Activity.updateWires(index) | ||||
68 | if(initialAngle == startingAngle + rotationAngle * 45) { | ||||
69 | if(initialAngle == 360 || initialAngle == -360) | ||||
70 | initialAngle = 0 | ||||
71 | startingAngle = initialAngle | ||||
72 | Activity.animationInProgress = false | ||||
73 | updateDragConstraints() | ||||
74 | } | ||||
75 | else | ||||
76 | rotateComponent.start() | ||||
77 | } | ||||
78 | easing.type: Easing.InOutQuad | ||||
79 | } | ||||
80 | | ||||
81 | function updateDragConstraints() { | ||||
82 | if(initialAngle == 0 || initialAngle == 180 || initialAngle == 360 || initialAngle == -360 | ||||
83 | || initialAngle == -180) { | ||||
84 | mouseArea.drag.minimumX = (electricalComponent.paintedWidth - electricalComponent.width)/2 | ||||
85 | mouseArea.drag.minimumY = (electricalComponent.paintedHeight - electricalComponent.height)/2 | ||||
86 | | ||||
87 | mouseArea.drag.maximumX = electricalComponent.parent.width - | ||||
88 | (electricalComponent.width + electricalComponent.paintedWidth)/2 | ||||
89 | mouseArea.drag.maximumY = electricalComponent.parent.height - | ||||
90 | (electricalComponent.height + electricalComponent.paintedHeight)/2 | ||||
91 | } | ||||
92 | else { | ||||
93 | mouseArea.drag.minimumX = (electricalComponent.paintedHeight - electricalComponent.width)/2 | ||||
94 | mouseArea.drag.minimumY = (electricalComponent.paintedWidth - electricalComponent.height)/2 | ||||
95 | | ||||
96 | mouseArea.drag.maximumX = electricalComponent.parent.width - | ||||
97 | (electricalComponent.width + electricalComponent.paintedHeight)/2 | ||||
98 | mouseArea.drag.maximumY = electricalComponent.parent.height - | ||||
99 | (electricalComponent.height + electricalComponent.paintedWidth)/2 | ||||
100 | } | ||||
101 | } | ||||
102 | | ||||
103 | | ||||
104 | MouseArea { | ||||
105 | id: mouseArea | ||||
106 | width: parent.paintedWidth | ||||
107 | height: parent.paintedHeight | ||||
108 | anchors.centerIn: parent | ||||
109 | drag.target: electricalComponent | ||||
110 | onPressed: { | ||||
111 | Activity.updateToolTip(toolTipTxt) | ||||
112 | Activity.componentSelected(index) | ||||
113 | } | ||||
114 | onClicked: { | ||||
115 | if(Activity.toolDelete || Activity.toolDeleteSticky) { | ||||
116 | if (destructible) { | ||||
117 | Activity.removeComponent(index) | ||||
118 | } else { | ||||
119 | Activity.deselect() | ||||
120 | } | ||||
121 | } | ||||
122 | } | ||||
123 | onReleased: { | ||||
124 | parent.posX = parent.x / parent.parent.width | ||||
125 | parent.posY = parent.y / parent.parent.height | ||||
126 | Activity.updateToolTip("") | ||||
127 | } | ||||
128 | | ||||
129 | onPositionChanged: { | ||||
130 | updateDragConstraints() | ||||
131 | } | ||||
132 | } | ||||
133 | } |