2022-09-14 13:01:10 +08:00
|
|
|
import QtQuick
|
2022-03-23 19:40:28 +08:00
|
|
|
|
|
|
|
Item {
|
2022-04-30 15:27:56 +08:00
|
|
|
property var cards: []
|
|
|
|
property int length: 0
|
|
|
|
property var pendingInput: []
|
|
|
|
property bool checkExisting: false
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
id: root
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
function add(inputs)
|
|
|
|
{
|
|
|
|
let card;
|
|
|
|
if (inputs instanceof Array) {
|
|
|
|
for (let i = 0; i < inputs.length; i++) {
|
|
|
|
card = inputs[i];
|
|
|
|
pendingInput.push(card);
|
|
|
|
cards.push(card.toData());
|
|
|
|
}
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if (checkExisting)
|
|
|
|
length = cards.length;
|
|
|
|
else
|
|
|
|
length += inputs.length;
|
|
|
|
} else {
|
|
|
|
pendingInput.push(inputs);
|
|
|
|
cards.push(inputs.toData());
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if (checkExisting)
|
|
|
|
length = cards.length;
|
|
|
|
else
|
|
|
|
length++;
|
2022-03-23 19:40:28 +08:00
|
|
|
}
|
2022-04-30 15:27:56 +08:00
|
|
|
}
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
function _contains(cid)
|
|
|
|
{
|
|
|
|
if (!checkExisting)
|
|
|
|
return true;
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
for (let i = 0; i < cards.length; i++)
|
|
|
|
{
|
|
|
|
if (cards[i].cid === cid)
|
|
|
|
return true;
|
2022-03-23 19:40:28 +08:00
|
|
|
}
|
2022-04-30 15:27:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
function remove(outputs)
|
|
|
|
{
|
|
|
|
let component = Qt.createComponent("CardItem.qml");
|
|
|
|
if (component.status !== Component.Ready)
|
|
|
|
return [];
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
let parentPos = roomScene.mapFromItem(root, 0, 0);
|
|
|
|
let card;
|
|
|
|
let items = [];
|
|
|
|
for (let i = 0; i < outputs.length; i++) {
|
|
|
|
if (_contains(outputs[i])) {
|
|
|
|
let state = JSON.parse(Backend.callLuaFunction("GetCardData", [outputs[i]]))
|
|
|
|
state.x = parentPos.x;
|
|
|
|
state.y = parentPos.y;
|
|
|
|
state.opacity = 0;
|
|
|
|
card = component.createObject(roomScene, state);
|
|
|
|
card.x -= card.width / 2;
|
|
|
|
card.x += (i - outputs.length / 2) * 15;
|
|
|
|
card.y -= card.height / 2;
|
|
|
|
items.push(card);
|
|
|
|
if (checkExisting) {
|
|
|
|
//@to-do: remove it from cards
|
|
|
|
cards.splice(i, 1);
|
|
|
|
i--;
|
2022-03-23 19:40:28 +08:00
|
|
|
}
|
2022-04-30 15:27:56 +08:00
|
|
|
}
|
2022-03-23 19:40:28 +08:00
|
|
|
}
|
2022-04-30 15:27:56 +08:00
|
|
|
if (checkExisting)
|
|
|
|
length = cards.length;
|
|
|
|
else
|
|
|
|
length -= outputs.length;
|
|
|
|
return items;
|
|
|
|
}
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
function updateCardPosition(animated)
|
|
|
|
{
|
|
|
|
let i, card;
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
if (animated) {
|
|
|
|
let parentPos = roomScene.mapFromItem(root, 0, 0);
|
|
|
|
for (i = 0; i < pendingInput.length; i++) {
|
|
|
|
card = pendingInput[i];
|
|
|
|
card.origX = parentPos.x - card.width / 2 + ((i - pendingInput.length / 2) * 15);
|
|
|
|
card.origY = parentPos.y - card.height / 2;
|
|
|
|
card.origOpacity = 0;
|
|
|
|
card.destroyOnStop();
|
|
|
|
}
|
2022-03-23 19:40:28 +08:00
|
|
|
|
2022-04-30 15:27:56 +08:00
|
|
|
for (i = 0; i < pendingInput.length; i++)
|
|
|
|
pendingInput[i].goBack(true);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < pendingInput.length; i++) {
|
|
|
|
card = pendingInput[i];
|
|
|
|
card.x = parentPos.x - card.width / 2;
|
|
|
|
card.y = parentPos.y - card.height / 2;
|
|
|
|
card.opacity = 1;
|
|
|
|
card.destroy();
|
|
|
|
}
|
2022-03-23 19:40:28 +08:00
|
|
|
}
|
2022-04-30 15:27:56 +08:00
|
|
|
|
|
|
|
pendingInput = [];
|
|
|
|
}
|
2022-03-23 19:40:28 +08:00
|
|
|
}
|