-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcode.js
More file actions
138 lines (138 loc) · 5.31 KB
/
Copy pathcode.js
File metadata and controls
138 lines (138 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// This plugin will open a modal to prompt the user to enter a number, and
// it will then create that many rectangles on the screen.
// This file holds the main code for the plugins. It has access to the *document*.
// You can access browser APIs in the <script> tag inside "ui.html" which has a
// full browser enviroment (see documentation).
// This shows the HTML page in "ui.html".
figma.showUI(__html__, { width: 400, height: 400 });
function main() {
const selection = figma.currentPage.selection;
if (selection.length !== 1) {
figma.notify("Select exactly 1 frame to start game");
figma.closePlugin();
return;
}
if (selection[0].type !== "FRAME") {
figma.notify("Must select a frame node");
figma.closePlugin();
return;
}
const PLAYER_PREFIX = "Player: ";
const HIDE_OFFSET = 20000;
const MARGIN = 100;
const pageNodes = {};
let assetNode = null;
const game = selection[0];
let players = new Set();
for (const node of game.children) {
if (node.type === "COMPONENT" && node.name === "assets") {
assetNode = node;
game.insertChild(game.children.length - 1, node);
}
}
for (const page of figma.root.children) {
if (page.name.substring(0, PLAYER_PREFIX.length) === PLAYER_PREFIX) {
pageNodes[page.name] = page;
}
else if (page.name === "Assets" && assetNode === null) {
assetNode = figma.createComponent();
assetNode.name = "assets";
game.appendChild(assetNode);
let xOffset = MARGIN;
for (const frame of page.children) {
if (frame.type !== "FRAME")
continue;
let back = null;
let yOffset = MARGIN;
for (const child of frame.children) {
if (back === null) {
back = child;
}
else if (child.type === "COMPONENT") {
continue;
}
else {
const widget = figma.createFrame();
const backClone = back.clone();
backClone.x = 0;
backClone.y = 0;
backClone.locked = true;
widget.name = frame.name;
widget.clipsContent = false;
assetNode.appendChild(widget);
widget.appendChild(backClone);
const clone = child.clone();
widget.appendChild(clone);
clone.x = 0;
clone.y = HIDE_OFFSET;
clone.locked = true;
widget.resize(clone.width, clone.height);
widget.x += xOffset;
widget.y += yOffset;
yOffset--;
}
}
xOffset += 20 + frame.width;
}
}
}
assetNode.resize(game.width, game.height + HIDE_OFFSET);
assetNode.x = 0;
assetNode.y = 0;
for (const node of game.children) {
if (["COMPONENT", "INSTANCE", "FRAME", "RECTANGLE"].indexOf(node.type) >= 0 &&
node.name.substring(0, PLAYER_PREFIX.length) == PLAYER_PREFIX) {
const name = node.name.substring(PLAYER_PREFIX.length);
if (players.has(name)) {
figma.notify("Player names must be unique");
figma.closePlugin();
return;
}
let playerPage = pageNodes[node.name];
if (!playerPage) {
playerPage = figma.createPage();
playerPage.name = node.name;
}
let viewerFrame = null;
for (const node of playerPage.children) {
if (node.type === "FRAME" && node.name === "viewer") {
viewerFrame = node;
}
else {
node.remove();
}
}
if (viewerFrame === null) {
viewerFrame = figma.createFrame();
playerPage.appendChild(viewerFrame);
viewerFrame.name = "viewer";
}
viewerFrame.resize(node.width, node.height);
let periscope = null;
for (const child of viewerFrame.children) {
if (child.type === "INSTANCE" && child.name === "periscope" && child.masterComponent === assetNode) {
periscope = child;
}
else {
child.remove();
}
}
if (periscope === null) {
periscope = assetNode.createInstance();
viewerFrame.appendChild(periscope);
}
console.log(playerPage.name);
console.log(node);
console.log(assetNode);
periscope.x = assetNode.x - node.x;
periscope.y = assetNode.y - node.y - HIDE_OFFSET;
}
}
figma.ui.onmessage = msg => {
// One way of distinguishing between different types of messages sent from
// your HTML page is to use an object with a "type" property like this.
if (msg.type === 'create-rectangles') {
}
};
}
main();