157 lines
5.7 KiB
QML
157 lines
5.7 KiB
QML
import QtQuick
|
|
|
|
// A glass sphere with lines of latitude and longitude that turn with it.
|
|
// Used for the big workspace globe and the small planet in the window loop.
|
|
// Set width and height to the same size.
|
|
Item {
|
|
id: root
|
|
|
|
required property Theme theme
|
|
// How far the sphere has turned sideways and down, in radians.
|
|
required property real yaw
|
|
required property real pitch
|
|
// Space between grid lines, in radians. 0 or less leaves those out.
|
|
required property real lonStep
|
|
required property real latStep
|
|
// Same as for the cards on it, so the lines curve the same way.
|
|
required property real focalLength
|
|
|
|
onYawChanged: grid.requestPaint()
|
|
onPitchChanged: grid.requestPaint()
|
|
onLonStepChanged: grid.requestPaint()
|
|
onLatStepChanged: grid.requestPaint()
|
|
onFocalLengthChanged: grid.requestPaint()
|
|
|
|
// The body: dark glass, lighter toward the middle, an amber glow at the
|
|
// edge, a shine at the top left and a rim. Only painted when resized.
|
|
Canvas {
|
|
id: body
|
|
anchors.fill: parent
|
|
|
|
onWidthChanged: body.requestPaint()
|
|
onHeightChanged: body.requestPaint()
|
|
|
|
onPaint: {
|
|
const ctx = body.getContext("2d");
|
|
ctx.reset();
|
|
const r = Math.min(body.width, body.height) / 2;
|
|
if (r <= 2)
|
|
return;
|
|
const cx = body.width / 2;
|
|
const cy = body.height / 2;
|
|
|
|
const fill = ctx.createRadialGradient(cx - r * 0.3, cy - r * 0.35, 0, cx, cy, r);
|
|
fill.addColorStop(0, root.theme.globeCore);
|
|
fill.addColorStop(0.75, root.theme.globeTint);
|
|
fill.addColorStop(1, root.theme.globeEdge);
|
|
ctx.fillStyle = fill;
|
|
ctx.beginPath();
|
|
ctx.arc(cx, cy, r, 0, 2 * Math.PI);
|
|
ctx.fill();
|
|
|
|
const sx = cx - r * 0.35;
|
|
const sy = cy - r * 0.45;
|
|
const shine = ctx.createRadialGradient(sx, sy, 0, sx, sy, r * 0.6);
|
|
shine.addColorStop(0, root.theme.glassShine);
|
|
shine.addColorStop(1, "rgba(255, 255, 255, 0)");
|
|
ctx.fillStyle = shine;
|
|
ctx.beginPath();
|
|
ctx.arc(cx, cy, r, 0, 2 * Math.PI);
|
|
ctx.fill();
|
|
|
|
const rim = ctx.createLinearGradient(0, cy - r, 0, cy + r);
|
|
rim.addColorStop(0, root.theme.globeRimTop);
|
|
rim.addColorStop(1, root.theme.globeRimBottom);
|
|
ctx.strokeStyle = rim;
|
|
ctx.lineWidth = 1.5;
|
|
ctx.beginPath();
|
|
ctx.arc(cx, cy, r - 1, 0, 2 * Math.PI);
|
|
ctx.stroke();
|
|
}
|
|
}
|
|
|
|
// The grid lines, painted again whenever the sphere turns. Lines sit
|
|
// halfway between the cards, so they do not run through them.
|
|
Canvas {
|
|
id: grid
|
|
anchors.fill: parent
|
|
|
|
onWidthChanged: grid.requestPaint()
|
|
onHeightChanged: grid.requestPaint()
|
|
|
|
onPaint: {
|
|
const ctx = grid.getContext("2d");
|
|
ctx.reset();
|
|
const r = Math.min(grid.width, grid.height) / 2;
|
|
const f = root.focalLength;
|
|
if (r <= 2 || f <= r)
|
|
return;
|
|
const cx = grid.width / 2;
|
|
const cy = grid.height / 2;
|
|
// Radius of the sphere itself, so its edge is drawn at r.
|
|
const big = r * f / (f - r);
|
|
const cosP = Math.cos(root.pitch);
|
|
const sinP = Math.sin(root.pitch);
|
|
const samples = 48;
|
|
|
|
// Draws a line along the sphere, given the longitude and
|
|
// latitude at each point from 0 to 1. Skips the back half.
|
|
function trace(pointAt) {
|
|
let drawing = false;
|
|
for (let i = 0; i <= samples; i++) {
|
|
const p = pointAt(i / samples);
|
|
const x = Math.cos(p.lat) * Math.sin(p.lon);
|
|
const y0 = Math.sin(p.lat);
|
|
const z0 = Math.cos(p.lat) * Math.cos(p.lon);
|
|
// Turn down by the pitch. z is 1 at the front.
|
|
const y = y0 * cosP - z0 * sinP;
|
|
const z = y0 * sinP + z0 * cosP;
|
|
if (z <= 0.02) {
|
|
drawing = false;
|
|
continue;
|
|
}
|
|
const s = f / (f + big * (1 - z));
|
|
const px = cx + big * x * s;
|
|
const py = cy + big * y * s;
|
|
if (drawing)
|
|
ctx.lineTo(px, py);
|
|
else
|
|
ctx.moveTo(px, py);
|
|
drawing = true;
|
|
}
|
|
}
|
|
|
|
ctx.strokeStyle = root.theme.globeGrid;
|
|
ctx.lineWidth = 1;
|
|
ctx.beginPath();
|
|
|
|
// Lines of longitude, from pole to pole.
|
|
if (root.lonStep > 0) {
|
|
const half = Math.ceil(Math.PI / root.lonStep);
|
|
const first = Math.round(root.yaw / root.lonStep) - half;
|
|
for (let k = first; k < first + 2 * half; k++) {
|
|
const lon = (k + 0.5) * root.lonStep - root.yaw;
|
|
trace(t => ({
|
|
lon: lon,
|
|
lat: (t - 0.5) * Math.PI
|
|
}));
|
|
}
|
|
}
|
|
|
|
// Lines of latitude, each a full circle.
|
|
if (root.latStep > 0) {
|
|
const count = Math.floor(Math.PI / 2 / root.latStep);
|
|
for (let j = -count; j < count; j++) {
|
|
const lat = (j + 0.5) * root.latStep;
|
|
trace(t => ({
|
|
lon: t * 2 * Math.PI,
|
|
lat: lat
|
|
}));
|
|
}
|
|
}
|
|
|
|
ctx.stroke();
|
|
}
|
|
}
|
|
}
|