solitare/qml/ScoreBar.qml

181 lines
5.6 KiB
QML
Raw Normal View History

2024-11-30 19:56:20 +00:00
import QtQuick
2024-12-09 22:02:16 +00:00
import QtQuick.Layouts
import QtQuick.Controls
2024-12-09 18:35:58 +00:00
import Solitare
2024-11-30 19:56:20 +00:00
Rectangle {
id: scoreBarRoot
property int score: 0
property int time: 0
property int moves: 0
2024-12-12 19:02:12 +00:00
signal restart
2024-12-05 02:24:09 +00:00
color: "lightgray"
2024-11-30 19:56:20 +00:00
2024-12-09 23:05:06 +00:00
RoundButton {
2024-12-12 19:02:12 +00:00
onClicked: {
GameState.dealCards()
scoreBarRoot.restart()
}
2024-12-09 23:05:06 +00:00
anchors.left: parent.left
anchors.leftMargin: parent.width * 0.02
anchors.verticalCenter: parent.verticalCenter
height: parent.height * 0.5
width: parent.width * 0.05
radius: Math.min(width, height) * 0.2
Image {
source: "qrc:/img/cards.svg"
width: parent.width * 0.7
height: parent.height * 0.7
anchors.centerIn: parent
anchors.margins: Math.min(width, height) * 0.06
// This makes sure the SVG scales properly, otherwise it would
// attempt to scale the image from the original source size, which
// can end up being blurry.
sourceSize: Qt.size(width, height)
fillMode: Image.PreserveAspectFit
}
}
2024-12-09 22:02:16 +00:00
Item {
id: firstRow
anchors.top: parent.top
2024-12-09 18:35:58 +00:00
anchors.left: parent.left
anchors.right: parent.right
2024-12-09 22:02:16 +00:00
anchors.topMargin: parent.height * 0.1
anchors.leftMargin: parent.width * 0.02
anchors.rightMargin: parent.width * 0.02
height: parent.height * 0.5
2024-12-09 18:35:58 +00:00
Row {
2024-12-09 22:02:16 +00:00
anchors.top: parent.top
height: parent.height
2024-12-09 18:35:58 +00:00
anchors.horizontalCenter: parent.horizontalCenter
2024-12-09 22:02:16 +00:00
spacing: parent.width * 0.05
2024-11-30 19:56:20 +00:00
2024-12-09 18:35:58 +00:00
ScoreItem {
title: "SCORE"
value: scoreBarRoot.score
2024-12-09 22:02:16 +00:00
height: parent.height
2024-11-30 19:56:20 +00:00
}
2024-12-09 18:35:58 +00:00
ScoreItem {
/**
* Formats a given time in seconds as a string in the format HH:MM:SS.
* If hours is 0, it will instead format as HH:SS.
*
* @param {number} seconds - The time in seconds to be formatted.
* @returns {string} The formatted time string.
*/
function formatTime(seconds) {
var hours = Math.floor(seconds / 3600);
var minutes = Math.floor((seconds % 3600) / 60);
var remainingSeconds = seconds % 60;
// Format with leading zeros
var formattedTime = "";
if (hours > 0)
formattedTime = (hours < 10 ? "0" : "") + hours + ":";
formattedTime += (minutes < 10 ? "0" : "") + minutes + ":" + (remainingSeconds < 10 ? "0" : "") + remainingSeconds;
return formattedTime;
}
title: "TIME"
value: formatTime(scoreBarRoot.time)
2024-12-09 22:02:16 +00:00
height: parent.height
2024-12-09 18:35:58 +00:00
}
ScoreItem {
title: "MOVES"
value: scoreBarRoot.moves
2024-12-09 22:02:16 +00:00
height: parent.height
2024-12-09 18:35:58 +00:00
}
2024-11-30 19:56:20 +00:00
}
2024-12-09 22:02:16 +00:00
}
2024-11-30 19:56:20 +00:00
2024-12-09 22:02:16 +00:00
Item {
id: statusRow
anchors.top: firstRow.bottom
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: parent.height * 0.1
anchors.bottomMargin: parent.height * 0.1
anchors.leftMargin: parent.width * 0.02
anchors.rightMargin: parent.height * 0.02
height: parent.height * 0.2
ScoreItem {
visible: GameState.isWinnable.winnable === false
title: "GAME LOST"
titleColor: "red"
anchors.fill: parent
}
2024-12-09 18:35:58 +00:00
2024-12-09 22:02:16 +00:00
ScoreItem {
visible: GameState.preliminaryWin === true && GameState.gameWon === false
title: "PRE-WON"
titleColor: "green"
anchors.fill: parent
}
2024-12-09 18:35:58 +00:00
2024-12-09 22:02:16 +00:00
ScoreItem {
visible: GameState.gameWon === true
title: "GAME WON"
titleColor: "green"
anchors.fill: parent
}
2024-12-09 18:35:58 +00:00
2024-12-09 22:02:16 +00:00
ScoreItem {
2024-12-09 22:24:37 +00:00
visible: GameState.gameWon === false && GameState.preliminaryWin === false && GameState.isWinnable.winnable === undefined
2024-12-09 22:02:16 +00:00
title: "PLAYING"
anchors.fill: parent
2024-11-30 19:56:20 +00:00
}
2024-12-09 22:24:37 +00:00
ScoreItem {
visible: GameState.gameWon === false && GameState.preliminaryWin === false && GameState.isWinnable.winnable === true
2024-12-09 23:06:55 +00:00
title: "PRE-WON IN " + GameState.isWinnable.depth
2024-12-09 22:24:37 +00:00
titleColor: "blue"
anchors.fill: parent
}
2024-11-30 19:56:20 +00:00
}
2024-12-05 02:24:09 +00:00
component ScoreItem: Column {
required property string title
2024-12-09 18:35:58 +00:00
property string value
property color titleColor
property color valueColor
2024-12-05 02:24:09 +00:00
2024-12-09 22:02:16 +00:00
property real fontHeight: height * 0.95 // Use 95% of the height for the font pixel size
property int titleFontSize: Math.round(value ? fontHeight * 0.3 : fontHeight) // 30% of the height, or full height if no value
property int valueFontSize: Math.round(fontHeight - titleFontSize) // Remaining height
spacing: height * 0.05 // Remaining 5% of the height for spacing between the title & value
2024-12-05 02:24:09 +00:00
Text {
text: parent.title
2024-12-09 18:35:58 +00:00
color: parent.titleColor
2024-12-09 22:02:16 +00:00
font.pixelSize: parent.titleFontSize
2024-12-05 02:24:09 +00:00
font.bold: true
horizontalAlignment: Text.AlignHCenter
anchors.horizontalCenter: parent.horizontalCenter
}
Text {
2024-12-09 22:02:16 +00:00
visible: parent.value
2024-12-05 02:24:09 +00:00
text: parent.value
2024-12-09 18:35:58 +00:00
color: parent.valueColor
2024-12-09 22:02:16 +00:00
font.pixelSize: parent.valueFontSize
2024-12-05 02:24:09 +00:00
horizontalAlignment: Text.AlignHCenter
anchors.horizontalCenter: parent.horizontalCenter
}
}
2024-11-30 19:56:20 +00:00
}