-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TimeKeeper to extension list #159
base: main
Are you sure you want to change the base?
Changes from 14 commits
55a68f2
6ae7282
cbb9cb0
6cd45b5
b4cae0b
29a3d55
75f7787
c10cbeb
dfa2b57
2a1e8f0
cb65264
a5b2030
0ba9eac
a166fb9
1c8a0e4
2e3c6a3
b49e076
2e86f96
6e8607e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
// Name: TimeKeeper | ||
// ID: TimeSparkyCreations | ||
// Description: Extra blocks to control the timer reporter. | ||
// By: SparkyCreations | ||
|
||
// Version 1.0.0 | ||
|
||
(function(Scratch) { | ||
class PauseResumeExtension { | ||
constructor(runtime) { | ||
this.runtime = runtime; | ||
this.timers = {}; | ||
this.countUpValues = {}; | ||
this.intervals = {}; | ||
} | ||
getInfo() { | ||
return { | ||
id: "TimeSparkyCreations", | ||
name: "TimeKeeper", | ||
color1: "#5CB1D6", | ||
blocks: [ | ||
{ | ||
opcode: "pauseClock", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "Pause Clock" | ||
}, | ||
{ | ||
opcode: "resumeClock", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "Resume Clock" | ||
}, | ||
{ | ||
opcode: "reportTimerInSeconds", | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: "report timer in seconds" | ||
}, | ||
{ | ||
opcode: "reportTimerInMinutes", | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: "report timer in minutes" | ||
}, | ||
{ | ||
opcode: "reportTimerInHours", | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: "report timer in hours" | ||
}, | ||
{ | ||
opcode: "reportTimerInInputSeconds", | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: "report timer in [SECONDS] seconds", | ||
arguments: { | ||
SECONDS: { | ||
type: Scratch.ArgumentType.NUMBER, | ||
defaultValue: 1 | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "timePaused", | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: "Time Paused" | ||
}, | ||
{ | ||
opcode: "createTimerNamed", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "create timer named [TIMERNAME] count up by [COUNTUP]", | ||
arguments: { | ||
TIMERNAME: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "Timer" | ||
}, | ||
COUNTUP: { | ||
type: Scratch.ArgumentType.NUMBER, | ||
defaultValue: 1 | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "reportTimers", | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: "report timers" | ||
}, | ||
{ | ||
opcode: "setTimer", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "set timer [NAME] to [VALUE]", | ||
arguments: { | ||
NAME: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "Timer" | ||
}, | ||
VALUE: { | ||
type: Scratch.ArgumentType.NUMBER, | ||
defaultValue: 0 | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "deleteAllTimers", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "delete all timers" | ||
}, | ||
{ | ||
opcode: "deleteTimerNamed", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "delete timer [TIMERNAME]", | ||
arguments: { | ||
TIMERNAME: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "Timer" | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
pauseClock() { | ||
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.pause === "function") { | ||
Scratch.vm.runtime.ioDevices.clock.pause(); | ||
} | ||
} | ||
|
||
resumeClock() { | ||
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.resume === "function") { | ||
Scratch.vm.runtime.ioDevices.clock.resume(); | ||
} | ||
} | ||
|
||
reportTimerInSeconds() { | ||
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.projectTimer === "function") { | ||
const timer = Scratch.vm.runtime.ioDevices.clock.projectTimer(); | ||
return timer / 1000; | ||
} | ||
return 0; | ||
} | ||
|
||
reportTimerInMinutes() { | ||
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.projectTimer === "function") { | ||
const timer = Scratch.vm.runtime.ioDevices.clock.projectTimer(); | ||
return timer / (1000 * 60); | ||
} | ||
return 0; | ||
} | ||
|
||
reportTimerInHours() { | ||
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.projectTimer === "function") { | ||
const timer = Scratch.vm.runtime.ioDevices.clock.projectTimer(); | ||
return timer / (1000 * 60 * 60); | ||
} | ||
return 0; | ||
} | ||
|
||
async reportTimerInInputSeconds(args) { | ||
const seconds = args.SECONDS; | ||
await new Promise(resolve => setTimeout(resolve, seconds * 1000)); | ||
if (Scratch.vm.runtime.ioDevices.clock && typeof Scratch.vm.runtime.ioDevices.clock.projectTimer === "function") { | ||
return Scratch.vm.runtime.ioDevices.clock.projectTimer(); | ||
} | ||
return 0; | ||
} | ||
|
||
timePaused() { | ||
if (Scratch.vm.runtime.ioDevices.clock && Scratch.vm.runtime.ioDevices.clock._projectTimer) { | ||
return Scratch.vm.runtime.ioDevices.clock._pausedTime | ||
} | ||
return 0; | ||
} | ||
|
||
createTimerNamed(args) { | ||
const timerName = args.TIMERNAME; | ||
const countUpValue = Scratch.Cast.toNumber(args.COUNTUP); | ||
if (!this.timers.hasOwnProperty(timerName)) { | ||
this.timers[timerName] = 0; | ||
this.countUpValues[timerName] = countUpValue; | ||
this.intervals[timerName] = setInterval(() => { | ||
this.timers[timerName] += this.countUpValues[timerName]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't do this, as many timers all doing this at once will cause lag. You should set up a separate function that updates all values at once instead of individually |
||
}, 1); | ||
} | ||
} | ||
|
||
reportTimers() { return JSON.stringify(this.timers) } | ||
|
||
setTimer(args) { | ||
const timerName = args.NAME; | ||
const value = Scratch.Cast.toNumber(args.VALUE); | ||
if (this.timers.hasOwnProperty(timerName)) this.timers[timerName] = value; | ||
} | ||
|
||
deleteAllTimers() { | ||
this.timers = {}; | ||
this.countUpValues = {}; | ||
for (const timerName in this.intervals) { | ||
clearInterval(this.intervals[timerName]); | ||
} | ||
this.intervals = {}; | ||
} | ||
|
||
deleteTimerNamed(args) { | ||
const timerName = args.TIMERNAME; | ||
delete this.timers[timerName]; | ||
delete this.countUpValues[timerName]; | ||
if (this.intervals.hasOwnProperty(timerName)) { | ||
clearInterval(this.intervals[timerName]); | ||
delete this.intervals[timerName]; | ||
} | ||
} | ||
} | ||
|
||
Scratch.extensions.register(new PauseResumeExtension()); | ||
})(Scratch); |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The thumbnail should be 600x300. Otherwise it will be stretched There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable