Skip to content

Commit 5db2f62

Browse files
committed
Clean up the code.
1 parent c296d48 commit 5db2f62

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
@@ -53,65 +53,82 @@
5353
{
5454
"type": "BuiltinCommonInstructions::JsCode",
5555
"inlineCode": [
56-
"const extension = {",
56+
"",
57+
"class ClickCounter {",
5758
" // Multiple clicks",
58-
" button: -1,",
59-
" count: 0,",
59+
" multipleClickButton = -1;",
60+
" count = 0;",
6061
"",
6162
" // Simple click",
62-
" lastButton: -1,",
63-
" lastPointerId: 0,",
64-
" lastTime: 0,",
65-
" hasMoved: false,",
66-
" simpleClickButton: -1,",
67-
"};",
68-
"gdjs._DoubleClickExtension = extension;",
63+
" lastButton = -1;",
64+
" lastPointerId = 0;",
65+
" lastTime = 0;",
66+
" hasMoved = false;",
67+
" simpleClickButton = -1;",
6968
"",
70-
"window.addEventListener(",
71-
" 'click',",
72-
" event => {",
73-
" if (event.detail > 1) {",
74-
" extension.button = convertHtmlMouseButtonToInputManagerMouseButton(event.button);",
75-
" extension.count = event.detail;",
76-
" }",
77-
" }",
78-
");",
69+
" constructor() {",
70+
" window.addEventListener(",
71+
" 'click',",
72+
" event => {",
73+
" if (event.detail > 1) {",
74+
" this.multipleClickButton = convertHtmlMouseButtonToInputManagerMouseButton(event.button);",
75+
" this.count = event.detail;",
76+
" }",
77+
" }",
78+
" );",
79+
"",
80+
" // The 'click' event doesn't do any constraint on the 1st click.",
81+
" // It doesn't allow to differenciate a dragging from a click.",
82+
" // So, we check it manually.",
83+
"",
84+
" window.addEventListener(",
85+
" 'pointerdown',",
86+
" event => {",
87+
" this.lastButton = event.button;",
88+
" this.lastPointerId = event.pointerId;",
89+
" this.lastTime = Date.now();",
90+
" this.hasMoved = false;",
91+
" }",
92+
" );",
93+
"",
94+
" window.addEventListener(",
95+
" 'pointermove',",
96+
" event => {",
97+
" if (event.pointerId === this.lastPointerId) {",
98+
" this.hasMoved = true;",
99+
" }",
100+
" }",
101+
" );",
79102
"",
80-
"// The 'click' event doesn't do any constraint on the 1st click.",
81-
"// It doesn't allow to differenciate a dragging from a click.",
82-
"// So, we check it manually.",
103+
" window.addEventListener(",
104+
" 'pointerup',",
105+
" (event) => {",
106+
" if (event.button === this.lastButton &&",
107+
" event.pointerId === this.lastPointerId &&",
108+
" (!this.hasMoved || Date.now() - this.lastTime < 500)) {",
109+
" this.simpleClickButton = convertHtmlMouseButtonToInputManagerMouseButton(event.button);",
110+
" this.lastButton = -1;",
111+
" }",
112+
" }",
113+
" );",
83114
"",
84-
"window.addEventListener(",
85-
" 'pointerdown',",
86-
" event => {",
87-
" extension.lastButton = event.button;",
88-
" extension.lastPointerId = event.pointerId;",
89-
" extension.lastTime = Date.now();",
90-
" extension.hasMoved = false;",
91115
" }",
92-
");",
93116
"",
94-
"window.addEventListener(",
95-
" 'pointermove',",
96-
" event => {",
97-
" if (event.pointerId === extension.lastPointerId) {",
98-
" extension.hasMoved = true;",
99-
" }",
117+
" reset() {",
118+
" this.multipleClickButton = -1;",
119+
" this.simpleClickButton = -1;",
100120
" }",
101-
");",
102121
"",
103-
"window.addEventListener(",
104-
" 'pointerup',",
105-
" (event) => {",
106-
" if (extension.button < 0 &&",
107-
" event.button === extension.lastButton &&",
108-
" event.pointerId === extension.lastPointerId &&",
109-
" (!extension.hasMoved || Date.now() - extension.lastTime < 500)) {",
110-
" extension.simpleClickButton = convertHtmlMouseButtonToInputManagerMouseButton(event.button);",
111-
" extension.lastButton = -1;",
122+
" hasClicked(buttonName, count) {",
123+
" const button = gdjs.evtTools.input.mouseButtonsNameToCode[buttonName];",
124+
" if (count === 1) {",
125+
" return button === this.simpleClickButton;",
126+
" }",
127+
" else {",
128+
" return button === this.multipleClickButton && clickCount === this.count",
112129
" }",
113130
" }",
114-
");",
131+
"}",
115132
"",
116133
"// Converts HTML mouse button to InputManager mouse button.",
117134
"// This function is used to align HTML button values with GDevelop 3 C++ SFML Mouse button enum values,",
@@ -126,6 +143,7 @@
126143
" return button;",
127144
"}",
128145
"",
146+
"gdjs._DoubleClickExtension = { clickCounter: new ClickCounter() };",
129147
""
130148
],
131149
"parameterObjects": "",
@@ -145,8 +163,10 @@
145163
{
146164
"type": "BuiltinCommonInstructions::JsCode",
147165
"inlineCode": [
148-
"gdjs._DoubleClickExtension.button = -1;\r",
149-
"gdjs._DoubleClickExtension.simpleClickButton = -1;"
166+
"const { clickCounter } = gdjs._DoubleClickExtension;\r",
167+
"\r",
168+
"clickCounter.reset();\r",
169+
""
150170
],
151171
"parameterObjects": "",
152172
"useStrict": true,
@@ -208,20 +228,30 @@
208228
"sentence": "_PARAM1_ mouse button is clicked",
209229
"events": [
210230
{
211-
"type": "BuiltinCommonInstructions::JsCode",
212-
"inlineCode": [
213-
"const buttonName = eventsFunctionContext.getArgument(\"MouseButton\");",
214-
"const clickCount = eventsFunctionContext.getArgument(\"ClickCount\");",
215-
"",
216-
"const { simpleClickButton } = gdjs._DoubleClickExtension;",
217-
"",
218-
"if (gdjs.evtTools.input.mouseButtonsNameToCode[buttonName] === simpleClickButton) {",
219-
" return eventsFunctionContext.returnValue = true;",
220-
"}"
231+
"type": "BuiltinCommonInstructions::Standard",
232+
"conditions": [
233+
{
234+
"type": {
235+
"value": "DoubleClick::HasClicked"
236+
},
237+
"parameters": [
238+
"",
239+
"MouseButton",
240+
"1",
241+
""
242+
]
243+
}
221244
],
222-
"parameterObjects": "",
223-
"useStrict": true,
224-
"eventsSheetExpanded": true
245+
"actions": [
246+
{
247+
"type": {
248+
"value": "SetReturnBoolean"
249+
},
250+
"parameters": [
251+
"True"
252+
]
253+
}
254+
]
225255
}
226256
],
227257
"parameters": [
@@ -245,15 +275,13 @@
245275
{
246276
"type": "BuiltinCommonInstructions::JsCode",
247277
"inlineCode": [
278+
"",
279+
"const { clickCounter } = gdjs._DoubleClickExtension;",
280+
"",
248281
"const buttonName = eventsFunctionContext.getArgument(\"MouseButton\");",
249282
"const clickCount = eventsFunctionContext.getArgument(\"ClickCount\");",
250283
"",
251-
"const { button, count } = gdjs._DoubleClickExtension;",
252-
"",
253-
"if (gdjs.evtTools.input.mouseButtonsNameToCode[buttonName] === button &&",
254-
" (clickCount === 0 || clickCount === count)) {",
255-
" return eventsFunctionContext.returnValue = true;",
256-
"}"
284+
"eventsFunctionContext.returnValue = clickCounter.hasClicked(buttonName, clickCount);"
257285
],
258286
"parameterObjects": "",
259287
"useStrict": true,
@@ -268,7 +296,7 @@
268296
"type": "mouseButton"
269297
},
270298
{
271-
"description": "Click count (or 0 for any click count)",
299+
"description": "Click count",
272300
"name": "ClickCount",
273301
"type": "expression"
274302
}

0 commit comments

Comments
 (0)