-
Notifications
You must be signed in to change notification settings - Fork 295
New extension: Multi Touch #1432
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
Open
Skyhigh173
wants to merge
12
commits into
TurboWarp:master
Choose a base branch
from
Skyhigh173:patch-8
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
de0c66f
Create touch.js
Skyhigh173 8bcce4c
remove debug log
Skyhigh173 a3e3d79
new block & fix bug
Skyhigh173 79db0fb
Update touch.js
Skyhigh173 c9bd9b7
refactor - 1
GarboMuffin 1a9a916
format and l10n
GarboMuffin 0af3ef4
refactor scratch x conversion and fix custom stage size
GarboMuffin eddb8c3
use monotonic time
GarboMuffin 9064cb8
accumulate deltas between ticks
GarboMuffin c2c263d
unbreak some stuff, and scale force to [0, 100]
GarboMuffin d53b544
npm run format
GarboMuffin 345be6c
false
GarboMuffin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,258 @@ | ||
| // Name: Multi Touch | ||
| // ID: skyhigh173touch | ||
| // Description: Multiple fingers at once! | ||
| // By: Skyhigh173 | ||
| // License: MIT | ||
|
|
||
| (function (Scratch) { | ||
| 'use strict'; | ||
|
|
||
| class MultiTouchExtension { | ||
| constructor() { | ||
| /** | ||
| * @type {HTMLDivElement} | ||
| */ | ||
| this.canvasDiv = null; | ||
| /** | ||
| * @type {HTMLCanvasElement} | ||
| */ | ||
| this.canvas = null; | ||
| /** | ||
| * @type {Array.<Touch>} | ||
| */ | ||
| this._touches = []; | ||
| /** | ||
| * @type {Array.<null|Touch>} | ||
| */ | ||
| this._fingers = []; | ||
| this._setup(); | ||
| } | ||
|
|
||
| get bound() { | ||
| return this.canvas.getBoundingClientRect(); | ||
| } | ||
|
|
||
| _clamp(min, x, max) { | ||
| return Math.max(Math.min(x, max), min); | ||
| } | ||
| _scale(x, sRmin, sRmax, tRmin, tRmax) { | ||
| return (tRmax - tRmin) / (sRmax - sRmin) * (x - sRmin) + tRmin; | ||
| } | ||
|
|
||
| _propMap = { | ||
| // map client coord to scratch coord | ||
| _x: (clientX) => this._clamp(-240, this._scale(clientX, this.bound.left, this.bound.right, -240, 240), 240), | ||
| _y: (clientY) => this._clamp(-180, this._scale(clientY, this.bound.bottom, this.bound.top, -180, 180), 180), | ||
|
|
||
| x: (t) => this._propMap._x(t.clientX), | ||
| y: (t) => this._propMap._y(t.clientY), | ||
| dx: (t) => (this._propMap._x(t.clientX) - this._propMap._x(t.prevX)), | ||
| dy: (t) => (this._propMap._y(t.clientY) - this._propMap._y(t.prevY)), | ||
| sx: (t) => this._propMap.dx(t) / ((t.nowDate - t.prevDate) / 1000), | ||
| sy: (t) => this._propMap.dy(t) / ((t.nowDate - t.prevDate) / 1000), | ||
| duration: (t) => (Date.now() - t.date) / 1000, | ||
| force: (t) => t.force, | ||
| }; | ||
|
|
||
| getInfo() { | ||
| return { | ||
| id: 'skyhigh173touch', | ||
| name: 'Multi Touch', | ||
| color1: '#F76AB3', | ||
| blocks: [ | ||
| { | ||
| opcode: 'touchAvailable', | ||
| blockType: Scratch.BlockType.BOOLEAN, | ||
| text: 'is touch available?' | ||
| }, | ||
| { | ||
| opcode: 'maxMultiTouch', | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: 'maximum finger count' | ||
| }, | ||
| '---', | ||
| { | ||
| opcode: 'numOfFingers', | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: 'number of fingers', | ||
| }, | ||
| { | ||
| opcode: 'numOfFingersID', | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: 'number of fingers ID', | ||
| }, | ||
| { | ||
| opcode: 'propOfFinger', | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: '[PROP] of finger [ID]', | ||
| arguments: { | ||
| PROP: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: 'x', | ||
| menu: 'prop', | ||
| }, | ||
| ID: { | ||
| type: Scratch.ArgumentType.NUMBER, | ||
| defaultValue: 1 | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| opcode: 'fingerExists', | ||
| blockType: Scratch.BlockType.BOOLEAN, | ||
| text: 'finger [ID] exists?', | ||
| arguments: { | ||
| ID: { | ||
| type: Scratch.ArgumentType.NUMBER, | ||
| defaultValue: 1 | ||
| } | ||
| } | ||
| }, | ||
| '---', | ||
| { | ||
| opcode: 'touchingFinger', | ||
| blockType: Scratch.BlockType.BOOLEAN, | ||
| text: 'touching any finger?', | ||
| disableMonitor: true, | ||
| filter: [Scratch.TargetType.SPRITE] | ||
| }, | ||
| { | ||
| opcode: 'touchingFingerCount', | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: 'current touching finger count', | ||
| disableMonitor: true, | ||
| filter: [Scratch.TargetType.SPRITE] | ||
| }, | ||
| { | ||
| opcode: 'touchingFingerID', | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: 'current touching finger [X] ID', | ||
| disableMonitor: true, | ||
| filter: [Scratch.TargetType.SPRITE], | ||
| arguments: { | ||
| X: { | ||
| type: Scratch.ArgumentType.NUMBER, | ||
| defaultValue: 1 | ||
| } | ||
| } | ||
| }, | ||
| ], | ||
| menus: { | ||
| prop: { | ||
| acceptReporters: true, | ||
| /** | ||
| * x: x position | ||
| * y: y position | ||
| * dx: change of x position compared to previous frame | ||
| * dy: change of y position compared to previous frame | ||
| * sx: avg speed x of finger in a second | ||
| * sy: avg speed y of finger in a second | ||
| * duration: time since finger press | ||
| * force (some devices only): force of finger press | ||
| */ | ||
| items: ['x', 'y', 'dx', 'dy', 'sx', 'sy', 'duration', 'force'], | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| _setup() { | ||
| this.canvas = Scratch.vm.runtime.renderer.canvas; | ||
| this.canvasDiv = this.canvas.parentElement; | ||
|
|
||
| // update touchList | ||
| /** | ||
| * @param {TouchEvent} e | ||
| */ | ||
| const upd = e => { | ||
| this._touches = [...e.touches]; | ||
| // update position | ||
| this._touches.forEach(t => { | ||
| // if theres a new finger... | ||
| const idx = this._fingers.findIndex(f => f?.identifier === t.identifier); | ||
| if (idx == -1) { | ||
| this._fingers.push(t); | ||
| // extra infos | ||
| this._fingers.at(-1).date = Date.now(); | ||
| this._fingers.at(-1).prevX = t.clientX; | ||
| this._fingers.at(-1).prevY = t.clientY; | ||
| this._fingers.at(-1).prevDate = Date.now(); | ||
| this._fingers.at(-1).nowDate = Date.now(); | ||
| } else { | ||
| const finger = this._fingers[idx]; | ||
| const date = finger.date, oldX = finger.clientX, oldY = finger.clientY, oldDate = finger.nowDate; | ||
| this._fingers[idx] = t; | ||
| this._fingers[idx].date = date; | ||
| this._fingers[idx].prevX = oldX; | ||
| this._fingers[idx].prevY = oldY; | ||
| this._fingers[idx].prevDate = oldDate; | ||
| this._fingers[idx].nowDate = Date.now(); | ||
| } | ||
| }) | ||
| this._fingers.forEach((t, index) => { | ||
| // if the finger releases... | ||
| if (this._touches.findIndex(f => f.identifier === t?.identifier) == -1) { | ||
| this._fingers[index] = null; | ||
| } | ||
| }) | ||
| // clear trailing null values | ||
| while (this._fingers.length > 0 && this._fingers.at(-1) === null) { this._fingers.pop(); } | ||
| } | ||
|
|
||
| // do not use this.canvasDiv because event will lost after 'see inside' or change page | ||
| window.addEventListener('touchstart', upd); | ||
| window.addEventListener('touchmove', upd); | ||
| window.addEventListener('touchend', upd); | ||
| } | ||
|
|
||
| touchAvailable() { | ||
| return window.navigator.maxTouchPoints > 0; | ||
| } | ||
| maxMultiTouch() { | ||
| return window.navigator.maxTouchPoints; | ||
| } | ||
|
|
||
| numOfFingers() { | ||
| return this._touches.length; | ||
| } | ||
|
|
||
| numOfFingersID() { | ||
| return this._fingers.length; | ||
| } | ||
|
|
||
| propOfFinger({ PROP, ID }) { | ||
| PROP = this._propMap[PROP]; | ||
| ID = Scratch.Cast.toNumber(ID) - 1; | ||
| if (ID >= this._fingers.length || this._fingers[ID] === null) return ''; | ||
| return PROP(this._fingers[ID]); | ||
| } | ||
|
|
||
| fingerExists({ ID }) { | ||
| ID = Scratch.Cast.toNumber(ID) - 1; | ||
| return ID < this._fingers.length && this._fingers[ID] !== null; | ||
| } | ||
|
|
||
| touchingCondition = (f, util) => f !== null && util.target.isTouchingPoint(this._propMap.x(f) + this.bound.width / 2, this.bound.height / 2 - this._propMap.y(f)); | ||
Skyhigh173 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| touchingFinger({}, util) { | ||
Skyhigh173 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return this._fingers.find((f) => { | ||
| return this.touchingCondition(f, util); | ||
| }) !== undefined; | ||
| } | ||
|
|
||
| touchingFingerCount({}, util) { | ||
Skyhigh173 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return this._fingers.reduce((total, f) => { | ||
| return total + (this.touchingCondition(f, util) ? 1 : 0); | ||
| }, 0); | ||
| } | ||
|
|
||
| touchingFingerID({ ID }, util) { | ||
| ID = Scratch.Cast.toNumber(ID); | ||
| const result = this._fingers.findIndex(f => { | ||
| return this.touchingCondition(f, util) && (--ID <= 0); | ||
| }) + 1; | ||
| return result == 0 ? '' : result; | ||
| } | ||
| } | ||
| Scratch.extensions.register(new MultiTouchExtension()); | ||
| })(Scratch); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.