Skip to content

Commit 342bb0a

Browse files
committed
Clean up the code.
1 parent d1dfd7c commit 342bb0a

File tree

1 file changed

+97
-69
lines changed

1 file changed

+97
-69
lines changed

extensions/reviewed/DoubleClick.json

Lines changed: 97 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -52,65 +52,82 @@
5252
{
5353
"type": "BuiltinCommonInstructions::JsCode",
5454
"inlineCode": [
55-
"const extension = {",
55+
"",
56+
"class ClickCounter {",
5657
" // Multiple clicks",
57-
" button: -1,",
58-
" count: 0,",
58+
" multipleClickButton = -1;",
59+
" count = 0;",
5960
"",
6061
" // Simple click",
61-
" lastButton: -1,",
62-
" lastPointerId: 0,",
63-
" lastTime: 0,",
64-
" hasMoved: false,",
65-
" simpleClickButton: -1,",
66-
"};",
67-
"gdjs._DoubleClickExtension = extension;",
62+
" lastButton = -1;",
63+
" lastPointerId = 0;",
64+
" lastTime = 0;",
65+
" hasMoved = false;",
66+
" simpleClickButton = -1;",
6867
"",
69-
"window.addEventListener(",
70-
" 'click',",
71-
" event => {",
72-
" if (event.detail > 1) {",
73-
" extension.button = convertHtmlMouseButtonToInputManagerMouseButton(event.button);",
74-
" extension.count = event.detail;",
75-
" }",
76-
" }",
77-
");",
68+
" constructor() {",
69+
" window.addEventListener(",
70+
" 'click',",
71+
" event => {",
72+
" if (event.detail > 1) {",
73+
" this.multipleClickButton = convertHtmlMouseButtonToInputManagerMouseButton(event.button);",
74+
" this.count = event.detail;",
75+
" }",
76+
" }",
77+
" );",
78+
"",
79+
" // The 'click' event doesn't do any constraint on the 1st click.",
80+
" // It doesn't allow to differenciate a dragging from a click.",
81+
" // So, we check it manually.",
82+
"",
83+
" window.addEventListener(",
84+
" 'pointerdown',",
85+
" event => {",
86+
" this.lastButton = event.button;",
87+
" this.lastPointerId = event.pointerId;",
88+
" this.lastTime = Date.now();",
89+
" this.hasMoved = false;",
90+
" }",
91+
" );",
92+
"",
93+
" window.addEventListener(",
94+
" 'pointermove',",
95+
" event => {",
96+
" if (event.pointerId === this.lastPointerId) {",
97+
" this.hasMoved = true;",
98+
" }",
99+
" }",
100+
" );",
78101
"",
79-
"// The 'click' event doesn't do any constraint on the 1st click.",
80-
"// It doesn't allow to differenciate a dragging from a click.",
81-
"// So, we check it manually.",
102+
" window.addEventListener(",
103+
" 'pointerup',",
104+
" (event) => {",
105+
" if (event.button === this.lastButton &&",
106+
" event.pointerId === this.lastPointerId &&",
107+
" (!this.hasMoved || Date.now() - this.lastTime < 500)) {",
108+
" this.simpleClickButton = convertHtmlMouseButtonToInputManagerMouseButton(event.button);",
109+
" this.lastButton = -1;",
110+
" }",
111+
" }",
112+
" );",
82113
"",
83-
"window.addEventListener(",
84-
" 'pointerdown',",
85-
" event => {",
86-
" extension.lastButton = event.button;",
87-
" extension.lastPointerId = event.pointerId;",
88-
" extension.lastTime = Date.now();",
89-
" extension.hasMoved = false;",
90114
" }",
91-
");",
92115
"",
93-
"window.addEventListener(",
94-
" 'pointermove',",
95-
" event => {",
96-
" if (event.pointerId === extension.lastPointerId) {",
97-
" extension.hasMoved = true;",
98-
" }",
116+
" reset() {",
117+
" this.multipleClickButton = -1;",
118+
" this.simpleClickButton = -1;",
99119
" }",
100-
");",
101120
"",
102-
"window.addEventListener(",
103-
" 'pointerup',",
104-
" (event) => {",
105-
" if (extension.button < 0 &&",
106-
" event.button === extension.lastButton &&",
107-
" event.pointerId === extension.lastPointerId &&",
108-
" (!extension.hasMoved || Date.now() - extension.lastTime < 500)) {",
109-
" extension.simpleClickButton = convertHtmlMouseButtonToInputManagerMouseButton(event.button);",
110-
" extension.lastButton = -1;",
121+
" hasClicked(buttonName, count) {",
122+
" const button = gdjs.evtTools.input.mouseButtonsNameToCode[buttonName];",
123+
" if (count === 1) {",
124+
" return button === this.simpleClickButton;",
125+
" }",
126+
" else {",
127+
" return button === this.multipleClickButton && clickCount === this.count",
111128
" }",
112129
" }",
113-
");",
130+
"}",
114131
"",
115132
"// Converts HTML mouse button to InputManager mouse button.",
116133
"// This function is used to align HTML button values with GDevelop 3 C++ SFML Mouse button enum values,",
@@ -125,6 +142,7 @@
125142
" return button;",
126143
"}",
127144
"",
145+
"gdjs._DoubleClickExtension = { clickCounter: new ClickCounter() };",
128146
""
129147
],
130148
"parameterObjects": "",
@@ -144,8 +162,10 @@
144162
{
145163
"type": "BuiltinCommonInstructions::JsCode",
146164
"inlineCode": [
147-
"gdjs._DoubleClickExtension.button = -1;\r",
148-
"gdjs._DoubleClickExtension.simpleClickButton = -1;"
165+
"const { clickCounter } = gdjs._DoubleClickExtension;\r",
166+
"\r",
167+
"clickCounter.reset();\r",
168+
""
149169
],
150170
"parameterObjects": "",
151171
"useStrict": true,
@@ -207,20 +227,30 @@
207227
"sentence": "_PARAM1_ mouse button is clicked",
208228
"events": [
209229
{
210-
"type": "BuiltinCommonInstructions::JsCode",
211-
"inlineCode": [
212-
"const buttonName = eventsFunctionContext.getArgument(\"MouseButton\");",
213-
"const clickCount = eventsFunctionContext.getArgument(\"ClickCount\");",
214-
"",
215-
"const { simpleClickButton } = gdjs._DoubleClickExtension;",
216-
"",
217-
"if (gdjs.evtTools.input.mouseButtonsNameToCode[buttonName] === simpleClickButton) {",
218-
" return eventsFunctionContext.returnValue = true;",
219-
"}"
230+
"type": "BuiltinCommonInstructions::Standard",
231+
"conditions": [
232+
{
233+
"type": {
234+
"value": "DoubleClick::HasClicked"
235+
},
236+
"parameters": [
237+
"",
238+
"MouseButton",
239+
"1",
240+
""
241+
]
242+
}
220243
],
221-
"parameterObjects": "",
222-
"useStrict": true,
223-
"eventsSheetExpanded": true
244+
"actions": [
245+
{
246+
"type": {
247+
"value": "SetReturnBoolean"
248+
},
249+
"parameters": [
250+
"True"
251+
]
252+
}
253+
]
224254
}
225255
],
226256
"parameters": [
@@ -244,15 +274,13 @@
244274
{
245275
"type": "BuiltinCommonInstructions::JsCode",
246276
"inlineCode": [
277+
"",
278+
"const { clickCounter } = gdjs._DoubleClickExtension;",
279+
"",
247280
"const buttonName = eventsFunctionContext.getArgument(\"MouseButton\");",
248281
"const clickCount = eventsFunctionContext.getArgument(\"ClickCount\");",
249282
"",
250-
"const { button, count } = gdjs._DoubleClickExtension;",
251-
"",
252-
"if (gdjs.evtTools.input.mouseButtonsNameToCode[buttonName] === button &&",
253-
" (clickCount === 0 || clickCount === count)) {",
254-
" return eventsFunctionContext.returnValue = true;",
255-
"}"
283+
"eventsFunctionContext.returnValue = clickCounter.hasClicked(buttonName, clickCount);"
256284
],
257285
"parameterObjects": "",
258286
"useStrict": true,
@@ -267,7 +295,7 @@
267295
"type": "mouseButton"
268296
},
269297
{
270-
"description": "Click count (or 0 for any click count)",
298+
"description": "Click count",
271299
"name": "ClickCount",
272300
"type": "expression"
273301
}

0 commit comments

Comments
 (0)