File indexing completed on 2024-05-12 04:04:13

0001 /*
0002     SPDX-FileCopyrightText: 2013 Ashwin Rajeev <ashwin_rajeev@hotmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 var border = 40
0008 var cellComponent = Qt.createComponent("Cell.qml");
0009 var cableComponent = Qt.createComponent("Cable.qml");
0010 var canvasComponent = Qt.createComponent("CanvasItem.qml");
0011 var cells = []
0012 var nodes = []
0013 
0014 function overlaySize() {
0015     var size = (width > height)? height - border : width - border;
0016     return (size > 0)? size : 0;
0017 }
0018 
0019 function gridWidth() {
0020     var widthRatio = (width - border) / grid.columns;
0021     var heightRatio = (height - border) / grid.rows;
0022     var gWidth = grid.columns * Math.min(widthRatio, heightRatio);
0023     return (gWidth > 0)? gWidth : 0;
0024 }
0025 
0026 function gridHeight() {
0027     var widthRatio = (width - border) / grid.columns;
0028     var heightRatio = (height - border) / grid.rows;
0029     var gHeight = grid.rows * Math.min(widthRatio, heightRatio);
0030     return (gHeight > 0)? gHeight : 0;
0031 }
0032 
0033 function addCell(cable, type) {
0034     var cell = cellComponent.createObject(grid);
0035     cell.sprite = cable;
0036     cell.index = cells.length;
0037     cell.type = type
0038     if(cable !== "") {
0039         cableComponent.createObject(cell);
0040         createNode(type, cell)
0041     }
0042     cells.push(cell);
0043 }
0044 
0045 function createNode(type, cell) {
0046     if(type !== "none") {
0047         var node = canvasComponent.createObject(cell);
0048         node.spriteKey = cell.type;
0049         node.anchors.fill = cell;
0050         node.anchors.margins = cell.width / 10;
0051         nodes[cell]=node;
0052     }
0053 }
0054 
0055 function reset(width, height) {
0056     selected = 0
0057     state = "running"
0058     while(cells.length > 0) {
0059         var cell = cells.pop();
0060         cell.visible = false;
0061         cell.destroy();
0062     }
0063     rows = height;
0064     columns = width;
0065 }
0066 
0067 function setSprite(index, cable, type) {
0068     cells[index].angle = 0;
0069     cells[index].sprite = cable;
0070     if (type !== "none"){
0071         cells[index].type = type;
0072         nodes[cells[index]].spriteKey = type;
0073     }
0074 }
0075 
0076 function rotate(direction) {
0077     if(cells[selected].locked || cells[selected].sprite === "") {
0078         clicked(-1); //invalid click
0079     }
0080     else if(state === "running") {
0081         cells[selected].angle += (direction === "clockwise")? 90 : -90;
0082         clicked(selected);
0083     }
0084 }
0085 
0086 function unlockAll() {
0087     for(var i = 0; i < cells.length; i++) {
0088         cells[i].locked = false;
0089     }
0090 }