Skip to content
This repository was archived by the owner on Nov 3, 2020. It is now read-only.

Adds heart.system namespace functions, corresponding to love.system #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions examples/system.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<html>
<head>
<title>heart.js test</title>
<script src="../heart.js"></script>
<script>
/* Shows a white square traveling to the bottom-right of the screen at 10 Hz. */

var square = {x: 50, y: 50, speed: 10};

heart.load = function() {
heart.attach("cnv");
}

heart.update = function(dt) {
square.x += square.speed * dt;
square.y += square.speed * dt;
}

heart.keypressed = function(key) {
console.log(key);
if (key === 's') {
heart.system.setClipboardText(JSON.stringify({x: square.x, y: square.y}));
} else if (key === 'l') {
var load = JSON.parse(heart.system.getClipboardText());
square.x = load.x; square.y = load.y;
}
}

heart.draw = function() {
heart.graphics.setColor(255, 255, 255)
heart.graphics.rectangle("fill", square.x, square.y, 100, 100);

heart.graphics.setColor(255, 255, 0);
heart.graphics.print("fps: " + heart.timer.getFPS(), 10, 10);
heart.graphics.print("os: " + heart.system.getOS(), 10, 20);

var b = heart.system.getPowerInfo();
if (b.state !== "unknown") {
heart.graphics.print("battery level: "+b.percent+"%", 10, 30);
}
}

</script>
</head>
<body>

<canvas id="cnv" width="800" height="600">your browser doesn't support &lt;canvas&gt;</canvas>

</body>
</html>
52 changes: 52 additions & 0 deletions heart.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,58 @@ heart.mouse = {
}
};

heart.system = {
// JavaScript is not permitted to access the clipboard in most browsers. We
// can use a backdoor in IE, but for other browsers we just use
// window.prompt() to make the user do it manually.
getClipboardText: function() {
if (typeof window.clipboardData !== "undefined") {
return window.clipboardData.getData('Text');
} else {
return window.prompt("Please paste from clipboard:");
}
},
getOS: function() {
var agent = window.navigator.userAgent;
if (agent.match(/Windows/)) {
return "Windows";
} else if (agent.match(/Mac/)) {
return "OS X";
} else if (agent.match(/X11|Linux/)) {
return "Linux";
}
},
getPowerInfo: function() {
if (typeof window.navigator.battery !== 'undefined') {
var b = window.navigator.battery, state;
if (b.charging) {
state = 'charging';
} else if (b.level === 1) {
state = 'charged';
} else {
state = 'battery';
}
return {
state: state,
percent: b.level*100,
seconds: b.dischargingTime,
};
} else {
return { state: 'unknown' };
}
},
// no implementation of heart.system.getProcessorCount() yet!
// this might be done using an extra library:
// https://github.com/oftn/core-estimator
setClipboardText: function(text) {
if (typeof window.clipboardData !== "undefined") {
window.clipboardData.setData('Text', text);
} else {
window.prompt("Please copy to clipboard:", text);
}
}
};

heart._init = function() {
/* if we're waiting on images to load, spinlock */
if(heart._imagesLoading.length !== 0) {
Expand Down