Skip to content

Commit

Permalink
Merge branch 'feature/layouts-gui'
Browse files Browse the repository at this point in the history
  • Loading branch information
ASxa86 committed Dec 11, 2024
2 parents 4acb601 + 661ca96 commit 59fe0b1
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 25 deletions.
4 changes: 3 additions & 1 deletion app/edh/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ qt_add_qml_module(${PROJECT_NAME}
URI app.${PROJECT_NAME}
VERSION 1.0
QML_FILES
Counter.qml
LayoutEDH.qml
Main.qml
MenuEDH.qml
MenuLayout.qml
MenuLife.qml
ModelPlayers.qml
Counter.qml
TextEDH.qml
${QML_SINGLETONS}
IMPORTS
Expand Down
74 changes: 74 additions & 0 deletions app/edh/LayoutEDH.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import QtQuick
import QtQuick.Layouts

Item {
id: root

readonly property int columns: 2
readonly property int rows: 2
property int spacing: 2
property int count: 4

required property Component delegate

onCountChanged: updateCount();
onChildrenChanged: updateLayout();
onWidthChanged: updateLayout()
onHeightChanged: updateLayout()
onSpacingChanged: updateLayout()
Component.onCompleted: updateCount();

function updateCount() {
if(root.delegate === null) {
return;
}

for(let i = 0; i < root.children.length; i++) {
root.children[i].destroy();
}

for(let i = 0; i < root.count; i++) {
root.delegate.createObject(root);
}
}

function updateLayout() {
for(let i = 0; i < root.children.length; i++) {
let cell = calcCell(i);
let item = root.children[i];
item.x = cell.x;
item.y = cell.y;
item.width = cell.width;
item.height = cell.height;
}
}

function calcCell(index) {
const maxColumns = root.columns;
const maxRows = Math.ceil(Math.max(root.rows, root.count / root.columns));

let column = index % maxColumns;
let row = root.count > 2 ? Math.floor(index / maxColumns) : index;

// For 1 count, consume the whole size of the containing item.
let cellX = 0;
let cellY = 0;
let cellWidth = root.width;
let cellHeight = root.height;

if(root.count >= 2) {
// All items' heights will be relative to the number of rows in the grid.
cellHeight = (cellHeight - (root.spacing * (maxRows - 1))) / maxRows;
cellY = row * (cellHeight + root.spacing);

// Only change the width from the maximum width if we aren't the last item of an uneven count.
// Otherwise, the last item will remain the maximum width of the grid.
if(root.count > 2 && (root.count % 2 === 0 || index !== root.count - 1)) {
cellWidth = (cellWidth - (root.spacing * maxColumns - 1)) / maxColumns;
cellX = column * (cellWidth + (root.spacing * maxColumns - 1));
}
}

return { x: cellX, y: cellY, width: cellWidth, height: cellHeight };
}
}
10 changes: 8 additions & 2 deletions app/edh/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Window {
Rectangle {
id: shade
anchors.fill: parent
visible: menu.state == "pressed"
visible: menu.state == menu.statePressed

Component.onCompleted: {
shade.color = Style.color.darkcardbackBG;
Expand All @@ -94,7 +94,7 @@ Window {
gesturePolicy: TapHandler.WithinBounds

onTapped: {
menu.state = "released"
menu.state = menu.stateReleased;
}
}
}
Expand All @@ -105,6 +105,12 @@ Window {

menuItemForest: MenuLife {
}

menuItemPlains: MenuLayout {
model: player
background: Style.color.plains
foreground: Style.color.swamp
}
}

// Debugging
Expand Down
29 changes: 12 additions & 17 deletions app/edh/MenuEDH.qml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ Rectangle {
property Component menuItemIsland: Item {}
property Component menuItemSwamp: Item {}

state: stateReleased.name
readonly property string statePressed: "pressed"
readonly property string stateReleased: "released"

state: root.stateReleased

states: [
State {
id: stateReleased
name: "released"
name: root.stateReleased

PropertyChanges {
target: path
radius: root.radius * 0.75 - 2
}
},
State {
id: statePressed
name: "pressed"
name: root.statePressed

PropertyChanges {
target: path
Expand Down Expand Up @@ -58,7 +59,7 @@ Rectangle {
state: root.state
states: [
State {
name: statePressed.name
name: root.statePressed

PropertyChanges {
target: path
Expand Down Expand Up @@ -115,19 +116,11 @@ Rectangle {
state: root.state
states: [
State {
name: statePressed.name

PropertyChanges {
target: rect
width: Style.iconSize
}
},
State {
name: stateReleased.name
name: root.statePressed

PropertyChanges {
target: rect
width: root.width / 5
width: Style.iconSize * 1.25
}
}
]
Expand All @@ -140,11 +133,13 @@ Rectangle {
}

Loader {
active: root.state === root.statePressed
anchors.fill: parent
sourceComponent: component
}

TapHandler {
enabled: root.state === root.statePressed
gesturePolicy: TapHandler.WithinBounds
}
}
Expand All @@ -166,7 +161,7 @@ Rectangle {
gesturePolicy: TapHandler.WithinBounds

onTapped: {
root.state = root.state == statePressed.name ? stateReleased.name : statePressed.name;
root.state = root.state == root.statePressed ? root.stateReleased : root.statePressed;
}
}
}
144 changes: 144 additions & 0 deletions app/edh/MenuLayout.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import QtQuick
import QtQuick.Window

Item {
id: root

readonly property string statePressed: "pressed"
readonly property string stateReleased: "released"
readonly property real layoutFactor: 0.8
readonly property real aspectRatio: Qt.platform.os == "android" ? Screen.width / Screen.height : Screen.height / Screen.width
property color background: "white"
property color foreground: "black"
required property ListModel model

state: root.stateReleased

onStateChanged: {
if(root.state === root.statePressed) {
layout.count = 0;
}
}

Component {
id: componentRect
Rectangle {
color: "transparent"
radius: 3
border.color: root.foreground
border.width: 2
}
}

LayoutEDH {
id: layout

anchors.centerIn: parent
width: root.width * root.layoutFactor * root.aspectRatio
height: root.height * root.layoutFactor
spacing: 1
delegate: componentRect
}

TapHandler {
gesturePolicy: TapHandler.WithinBounds

onTapped: {
root.state = root.state === root.statePressed ? root.stateReleased : root.statePressed;
}
}

PathView {
id: path

anchors.centerIn: parent

property real radius: 0

onRadiusChanged: {
if(radius === 0) {
if(root.state === root.stateReleased) {
layout.count = root.model.count;
}
}
}

state: root.state

states: [
State {
name: root.statePressed

PropertyChanges {
target: path
radius: 80
}
}
]

transitions: Transition {
NumberAnimation {
property: "radius"
duration: 350
}
}

model: [1, 2, 3, 4, 5, 6]

delegate: Rectangle {
id: rect
width: root.width
height: width
radius: width / 2
color: root.background
visible: path.radius > 0

required property int index
required property int modelData

onVisibleChanged: {
if(visible === true) {
rect.z = rect.modelData === root.model.count ? 1 : 0;
}
}

Connections {
target: root.model

function onCountChanged() {
rect.z = 0;
}
}

LayoutEDH {
anchors.centerIn: parent
width: layout.width
height: layout.height
count: modelData
spacing: 1
delegate: componentRect
}

TapHandler {
gesturePolicy: TapHandler.WithinBounds

onTapped: {
Actions.setPlayerTotal(rect.modelData);
root.state = root.stateReleased;
rect.z = 1;
}
}
}

path: Path {
PathAngleArc {
id: radial

radiusX: path.radius
radiusY: path.radius
startAngle: -90
sweepAngle: 360
}
}
}
}
12 changes: 7 additions & 5 deletions app/edh/MenuLife.qml
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ Item {
verticalAlignment: Text.AlignVCenter

anchors.fill: parent
}

TapHandler {
onTapped: {
Actions.reset(life);
root.active = false;
}
TapHandler {
gesturePolicy: TapHandler.WithinBounds

onTapped: {
Actions.reset(life);
root.active = false;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/edh/ModelPlayers.qml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ ListModel {
}

function onSetPlayerTotal(total) {
model.clear();

for(let i = 0; i < total; i++) {
model.append({
background: model.backgrounds[i % model.backgrounds.length],
Expand Down

0 comments on commit 59fe0b1

Please sign in to comment.