-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
211 lines (186 loc) · 7.93 KB
/
main.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
define(function (require, exports, module) {
var AppInit = brackets.getModule("utils/AppInit"),
CodeMirror = brackets.getModule("thirdparty/CodeMirror2/lib/codemirror"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus");
var editor = {},
cm = {},
root = {},
canvas = {},
c = {},
cursors = [],
bubbles = [],
changes = 0,
offsetTop = 0,
offsetLeft = 0,
resetTimer = 0;
var CONFETTI_COMMAND_NAME = "Enable Confetti",
CONFETTI_COMMAND_ID = "torcado.toggleConfetti",
SCREENSHAKE_COMMAND_NAME = "Enable Screenshake",
SCREENSHAKE_COMMAND_ID = "torcado.toggleScreenshake",
GUIDE_CLASS = "torcado.confetti";
//USER VARIABLES
var enableConfetti = true,
enableScreenshake = false, //DOM redraw events from CodeMirror makes screenshake pretty laggy, but it's here if you want it.
shakeIntensity = 6,
confettiAmount = 15,
gravity = false, //Makes confetti fall
insaneMode = false, //insane
prefs = PreferencesManager.getExtensionPrefs("torcado.confetti");
prefs.definePreference("enableConfetti", "boolean", enableConfetti, {
description: "Toggle Confetti"
});
prefs.definePreference("enableScreenshake", "boolean", enableScreenshake, {
description: "Toggle Screenshake"
});
prefs.definePreference("confettiAmount", "number", confettiAmount, {
description: "Number of Spawned Confetti Objects"
});
prefs.definePreference("shakeIntensity", "number", shakeIntensity, {
description: "Screenshake Intensity"
});
AppInit.appReady(function () {
setTimeout(function () {
$("body").append('<link rel="stylesheet" href="' + ExtensionUtils.getModulePath(module) + 'main.less">');
$("body").prepend('<canvas id="torcado-canvas"></canvas>');
canvas = document.getElementById('torcado-canvas');
c = canvas.getContext('2d');
init();
changeFile();
window.requestAnimationFrame(step);
$(window).resize(init);
CommandManager.register(CONFETTI_COMMAND_NAME, CONFETTI_COMMAND_ID, toggleConfetti);
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).addMenuItem(CONFETTI_COMMAND_ID);
CommandManager.register(SCREENSHAKE_COMMAND_NAME, SCREENSHAKE_COMMAND_ID, toggleScreenshake);
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).addMenuItem(SCREENSHAKE_COMMAND_ID);
prefs.on("change", function () {
applyPreferences();
});
applyPreferences();
$(DocumentManager).on("currentDocumentChange", changeFile);
}, 2000);
});
function Bubble(x, y, dx, dy, r, c){
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.g = 0;
this.r = r;
this.c = c;
this.update = function(){
this.x += this.dx;
this.y += this.dy;
if(gravity){
this.y += this.g;
this.g += 0.1;
}
if(insaneMode){
this.dx *= 0.985;
this.dy *= 0.985;
this.r *= 0.985;
} else {
this.dx *= 0.9;
this.dy *= 0.9;
this.r *= 0.9;
}
if(this.r < 0.5){
bubbles.splice(bubbles.indexOf(this), 1);
}
}
}
function init(){
setTimeout(function () {
editor = EditorManager.getCurrentFullEditor();
root = editor.getRootElement();
canvas.width = $(root).width();
canvas.height = $(root).height();
offsetTop = $(root).offset().top;
offsetLeft = $(root).offset().left;
$("#torcado-canvas").css({"top": offsetTop, "left": offsetLeft});
}, 100);
}
function changeFile(){
editor = EditorManager.getCurrentFullEditor();
root = editor.getRootElement();
if (!editor) {
return;
}
cm = editor._codeMirror;
cm.on("change", function (codeMirror, change) {
burst(changes, (change.text[0] == ";" ? 5 : 1) * (insaneMode ? 1.5 : 1)); //Second parameter of Burst, mult, multiplies confetti amount by this much. Typing a semicolon multiplies by 5. Insane mode defaults 2x
changes++;
});
init();
}
function step(time){
if(enableConfetti){
if(bubbles.length > 0){
window.requestAnimationFrame(step);
}
c.clearRect(0,0,canvas.width,canvas.height);
for(var i = 0; i < bubbles.length; i++){
var b = bubbles[i];
b.update();
c.fillStyle = b.c;
c.beginPath();
c.arc(b.x, b.y, b.r, 0, Math.PI * 2, false);
c.fill();
}
if(resetTimer < 0){
$(root).css({"transform": "none"});
resetTimer = 0;
} else {
resetTimer--;
}
changes = 0;
}
}
function burst(i, mult){
if(bubbles.length == 0){
window.requestAnimationFrame(step);
}
if(enableScreenshake){
if(resetTimer <= 0){
$(root).css({"transform": "translate(" + (Math.random() * (shakeIntensity) - (shakeIntensity / 2)) + "px, " + (Math.random() * (shakeIntensity) - (shakeIntensity / 2)) + "px)"})
resetTimer = 3;
}
}
if(enableConfetti){
cursors = $(".CodeMirror-cursor, .CodeMirror-selected", root);
if(cursors != undefined && cursors[i] != undefined){
for(var j = 0; j < Math.ceil((confettiAmount == 1 ? 1 : (confettiAmount / (cursors.length / 1.75))) * mult); j++){
var dir = Math.random() * Math.PI * 2;
bubbles.push(new Bubble($(cursors[i]).offset().left - offsetLeft, $(cursors[i]).offset().top - offsetTop + 7, Math.cos(dir) * (Math.random() * 15 + 2), Math.sin(dir) * (Math.random() * 15 + 2), Math.random() * 12 + 8, "hsl(" + (Math.random() * 360) + ", 100%, 75%)")); //You can change the colors here if you want
}
}
}
}
function toggleConfetti() {
enableConfetti = !enableConfetti;
prefs.set("enableConfetti", enableConfetti);
prefs.save();
CommandManager.get(CONFETTI_COMMAND_ID).setChecked(enableConfetti);
if(enableConfetti){
window.requestAnimationFrame(step);
}
}
function toggleScreenshake() {
enableScreenshake = !enableScreenshake;
prefs.set("enableScreenshake", enableScreenshake);
prefs.save();
CommandManager.get(SCREENSHAKE_COMMAND_ID).setChecked(enableScreenshake);
}
function applyPreferences() {
enableConfetti = prefs.get("enableConfetti");
enableScreenshake = prefs.get("enableScreenshake");
shakeIntensity = prefs.get("shakeIntensity");
confettiAmount = prefs.get("confettiAmount");
CommandManager.get(CONFETTI_COMMAND_ID).setChecked(enableConfetti);
CommandManager.get(SCREENSHAKE_COMMAND_ID).setChecked(enableScreenshake);
}
});