FreeKill/Fk/RoomElement/HandcardArea.qml
notify dedf769e97
Fixbug2 (#405)
* 修复录像可见性问题 并增加查看未知牌功能
* 给qml绑定上role_shown属性,以便更新
* 修复从非隐藏卡牌区域删除卡牌时卡牌的known不受moveData干涉
* 给askForCardChosen和poxi贴了个visible_data
* 修复捡垃圾会导致手牌重复触发onClicked
2024-10-25 23:13:05 +08:00

168 lines
3.5 KiB
QML

// SPDX-License-Identifier: GPL-3.0-or-later
import QtQuick
import Fk
Item {
property alias cards: cardArea.cards
property alias length: cardArea.length
property var selectedCards: []
property var movepos
signal cardSelected(int cardId, bool selected)
id: area
CardArea {
anchors.fill: parent
id: cardArea
onLengthChanged: area.updateCardPosition(true);
}
function add(inputs)
{
cardArea.add(inputs);
if (inputs instanceof Array) {
for (let i = 0; i < inputs.length; i++)
filterInputCard(inputs[i]);
} else {
filterInputCard(inputs);
}
}
function filterInputCard(card)
{
card.autoBack = true;
card.draggable = lcall("CanSortHandcards", Self.id);
card.selectable = false;
card.clicked.connect(selectCard);
card.clicked.connect(adjustCards);
card.released.connect(updateCardReleased);
card.xChanged.connect(updateCardDragging);
}
function remove(outputs)
{
const result = cardArea.remove(outputs);
let card;
for (let i = 0; i < result.length; i++) {
card = result[i];
card.draggable = false;
card.selectable = false;
card.clicked.disconnect(selectCard);
card.selectedChanged.disconnect(adjustCards);
card.released.disconnect(updateCardReleased);
card.xChanged.disconnect(updateCardDragging);
card.prohibitReason = "";
}
return result;
}
function updateCardPosition(animated)
{
cardArea.updateCardPosition(false);
cards.forEach(card => {
if (card.selected) {
card.origY -= 20;
}
if (!card.selectable) {
if (config.hideUseless) {
card.origY += 60;
}
}
});
if (animated) {
cards.forEach(card => card.goBack(true));
}
}
function updateCardDragging()
{
let _card, c;
let index;
for (index = 0; index < cards.length; index++) {
c = cards[index];
if (c.dragging) {
_card = c;
break;
}
}
if (!_card) return;
_card.goBackAnim.stop();
_card.opacity = 0.8
let card;
movepos = null;
for (let i = 0; i < cards.length; i++) {
card = cards[i];
if (card.dragging) continue;
if (card.x > _card.x) {
movepos = i - (index < i ? 1 : 0);
break;
}
}
if (movepos == null) { // 最右
movepos = cards.length;
}
}
function updateCardReleased(_card)
{
let i;
if (movepos != null) {
i = cards.indexOf(_card);
cards.splice(i, 1);
cards.splice(movepos, 0, _card);
movepos = null;
}
updateCardPosition(true);
}
function adjustCards()
{
area.updateCardPosition(true);
}
function selectCard(card) {
if (card.selectable) cardSelected(card.cid, card.selected);
adjustCards();
}
function enableCards(cardIds)
{
let card, i;
cards.forEach(card => {
card.selectable = cardIds.includes(card.cid);
if (!card.selectable) {
card.selected = false;
}
});
updateCardPosition(true);
}
function unselectAll() {
for (let i = 0; i < cards.length; i++) {
const card = cards[i];
card.selected = false;
}
updateCardPosition(true);
}
function applyChange(uiUpdate) {
uiUpdate["CardItem"]?.forEach(cdata => {
for (let i = 0; i < cards.length; i++) {
const card = cards[i];
if (card.cid === cdata.id) {
card.selectable = cdata.enabled;
card.selected = cdata.selected;
updateCardPosition(true);
break;
}
}
})
}
}