Debian Codegraph @

src/activities/digital_electricity/components/Comparator.qml

raw
1 /* GCompris - Comparator.qml
2 *
3 * SPDX-FileCopyrightText: 2016 Pulkit Gupta <[email protected]>
4 *
5 * Authors:
6 * Bruno Coudoin <[email protected]> (GTK+ version)
7 * Pulkit Gupta <[email protected]> (Qt Quick port)
8 *
9 * SPDX-License-Identifier: GPL-3.0-or-later
10 */
11 import QtQuick
12 import core 1.0
13
14 ElectricalComponent {
15 id: comparator
16 terminalSize: 0.25
17 noOfInputs: 2
18 noOfOutputs: 3
19 property var inputTerminalPosY: [0.25, 0.75]
20 property var outputTerminalPosY: [0.13, 0.5, 0.87]
21
22 information: qsTr("A comparator takes 2 numbers as input, A and B. It compares them and outputs 3 " +
23 "values. The first output is 1 if A < B, otherwise it's 0. The second output is 1 " +
24 "if A = B, otherwise it's 0. The third output is 1 if A > B, otherwise it's 0. ")
25
26 truthTable: []
27
28 property alias inputTerminals: inputTerminals
29 property alias outputTerminals: outputTerminals
30
31 Repeater {
32 id: inputTerminals
33 model: 2
34 delegate: inputTerminal
35 Component {
36 id: inputTerminal
37 TerminalPoint {
38 posX: 0.05
39 posY: inputTerminalPosY[index]
40 type: "In"
41 }
42 }
43 }
44
45 Repeater {
46 id: outputTerminals
47 model: 3
48 delegate: outputTerminal
49 Component {
50 id: outputTerminal
51 TerminalPoint {
52 posX: 0.95
53 posY: outputTerminalPosY[index]
54 type: "Out"
55 }
56 }
57 }
58
59 function updateOutput(wireVisited) {
60 outputTerminals.itemAt(0).value = (inputTerminals.itemAt(0).value < inputTerminals.itemAt(1).value)
61 outputTerminals.itemAt(1).value = (inputTerminals.itemAt(0).value == inputTerminals.itemAt(1).value)
62 outputTerminals.itemAt(2).value = (inputTerminals.itemAt(0).value > inputTerminals.itemAt(1).value)
63
64 for(var i = 0 ; i < noOfOutputs ; ++i) {
65 var terminal = outputTerminals.itemAt(i)
66 for(var j = 0 ; j < terminal.wires.length ; ++j)
67 terminal.wires[j].to.value = terminal.value
68 }
69
70 var componentVisited = []
71 for(var i = 0 ; i < noOfOutputs ; ++i) {
72 var terminal = outputTerminals.itemAt(i)
73 for(var j = 0 ; j < terminal.wires.length ; ++j) {
74 var wire = terminal.wires[j]
75 var component = wire.to.parent
76 if(componentVisited[component] != true && wireVisited[wire] != true) {
77 componentVisited[component] = true
78 wireVisited[wire] = true
79 component.updateOutput(wireVisited)
80 }
81 }
82 }
83 }
84 }