Add Game Of Life Wallpaper

This commit is contained in:
JeremyStar™ 2024-10-20 04:09:20 +02:00
parent 070c22c2c7
commit 0cb0b21bf2
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
5 changed files with 246 additions and 0 deletions

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
<kcfgfile name=""/>
<group name="General">
<entry name="color" type="String">
<label>Color</label>
<default>#00ff00</default>
</entry>
</group>
</kcfg>

View file

@ -0,0 +1,103 @@
const State = {
DEAD: 0,
ALIVE: 1,
DYING: 2,
SPAWNING: 3,
}
var cell_count_x = 0;
var cell_count_y = 0;
var SIZE = 10;
var cells = [];
var cells_back = [];
function fillRandom() {
for(let y=0;y<cell_count_y;y++) {
for(let x=0;x<cell_count_x;x++) {
let idx = y*cell_count_x+x;
cells[idx] = Math.random() < 0.1 ? State.ALIVE : State.DEAD; // fill only 10%
cells_back[idx] = State.DEAD;
}
}
}
function dimensionChanged(width,height) {
cell_count_x = width / SIZE;
cell_count_y = height / SIZE;
fillRandom();
}
function getNeighbourCount( x, y)
{
var sum = 0;
// top
y--;
var idx = y*cell_count_x+x;
if (cells[idx-1] == State.ALIVE)
sum++;
if (cells[idx] == State.ALIVE)
sum++;
if (cells[idx+1] == State.ALIVE)
sum++;
// middle
idx += cell_count_x;
if (cells[idx-1] == State.ALIVE)
sum++;
if (cells[idx+1] == State.ALIVE)
sum++;
// bottom
idx += cell_count_x;
if (cells[idx-1] == State.ALIVE)
sum++;
if (cells[idx] == State.ALIVE)
sum++;
if (cells[idx+1] == State.ALIVE)
sum++;
return sum;
}
function advanceGeneration() {
for(let y=0;y<cell_count_y;y++){
for(let x=0;x<cell_count_x;x++){
let neighbours = getNeighbourCount(x,y);
let idx = y*cell_count_x+x;
if (neighbours < 2 && cells[idx] == State.ALIVE)
cells_back[idx] = State.DEAD;
else if (neighbours == 3 && cells[idx] == State.DEAD)
cells_back[idx] = State.ALIVE;
else if (neighbours > 3 && cells[idx] == State.ALIVE)
cells_back[idx] = State.DEAD;
else
cells_back[idx] = cells[idx];
}
}
// copy back into main matrix
for(let y=0;y<cell_count_y;y++){
for(let x=0;x<cell_count_x;x++){
let idx = y*cell_count_x+x;
cells[idx] = cells_back[idx];
}
}
}
function paintMatrix(ctx, color){
for(let y=0;y<cell_count_y;y++){
for(let x=0;x<cell_count_x;x++) {
let idx = y*cell_count_x+x;
if (cells[idx] == State.ALIVE) {
ctx.fillStyle = color;
ctx.fillRect(x*SIZE,y*SIZE,SIZE,SIZE);
} else {
ctx.fillStyle = "black";
ctx.fillRect(x*SIZE,y*SIZE,SIZE,SIZE);
}
}
}
}

View file

@ -0,0 +1,30 @@
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
// for "units"
import org.kde.plasma.components as PlasmaComponents
import org.kde.kquickcontrols as KQuickControls
import org.kde.plasma.core as PlasmaCore
ColumnLayout {
id: root
property alias cfg_color : colorButton.color;
Row {
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
Label {
anchors.verticalCenter: parent.verticalCenter
rightPadding: 15
text: i18n("Cell color")
}
KQuickControls.ColorButton {
id: colorButton
dialogTitle: i18n("Select cell color")
}
}
}

View file

@ -0,0 +1,75 @@
/*
* Copyright (C) %{CURRENT_YEAR} by %{AUTHOR} <%{EMAIL}>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
*/
import QtQuick
import QtQuick.Layouts
import org.kde.plasma.plasmoid
import "code/life.js" as Life
import org.kde.plasma.core as PlasmaCore
WallpaperItem {
id: root
Canvas {
id: gameCanvas
anchors.fill: parent
property int mleft: 0
property int mtop: 0
width: parent.width
height: parent.height
onPaint: {
var ctx = getContext("2d");
Life.paintMatrix(ctx, wallpaper.configuration.color);
}
onWidthChanged: {
stepTimer.stop();
Life.dimensionChanged(width, height);
stepTimer.start();
}
onHeightChanged: {
stepTimer.stop();
Life.dimensionChanged(width, height);
stepTimer.start();
}
}
Timer {
id: stepTimer
interval: 1000
repeat: true
running: true
triggeredOnStart: true
onTriggered: {
gameCanvas.mleft++;
gameCanvas.mtop++;
Life.advanceGeneration();
gameCanvas.requestPaint();
}
}
Component.onCompleted: {
// Initialize the Life simulation using the actual dimensions
Life.dimensionChanged(width, height);
}
}

View file

@ -0,0 +1,23 @@
{
"KPackageStructure": "Plasma/Wallpaper",
"KPlugin": {
"Authors": [
{
"Email": "%{EMAIL}",
"Name": "%{AUTHOR}"
}
],
"Id": "org.kde.plasma.gameoflife",
"License": "LGPLv2+",
"Name": "Conway's Game of Life",
"Version": "%{VERSION}",
"Description": "Allows you to use Shaders as your wallpaper"
},
"X-KDE-ParentApp": "org.kde.plasmashell",
"X-Plasma-API-Minimum-Version": "6.0",
"X-Plasma-ContainmentType": "Desktop"
}