|
53 | 53 | { |
54 | 54 | "type": "BuiltinCommonInstructions::JsCode", |
55 | 55 | "inlineCode": [ |
56 | | - "const extension = {", |
| 56 | + "", |
| 57 | + "class ClickCounter {", |
57 | 58 | " // Multiple clicks", |
58 | | - " button: -1,", |
59 | | - " count: 0,", |
| 59 | + " multipleClickButton = -1;", |
| 60 | + " count = 0;", |
60 | 61 | "", |
61 | 62 | " // 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;", |
69 | 68 | "", |
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 | + " );", |
79 | 102 | "", |
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 | + " );", |
83 | 114 | "", |
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;", |
91 | 115 | " }", |
92 | | - ");", |
93 | 116 | "", |
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;", |
100 | 120 | " }", |
101 | | - ");", |
102 | 121 | "", |
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", |
112 | 129 | " }", |
113 | 130 | " }", |
114 | | - ");", |
| 131 | + "}", |
115 | 132 | "", |
116 | 133 | "// Converts HTML mouse button to InputManager mouse button.", |
117 | 134 | "// This function is used to align HTML button values with GDevelop 3 C++ SFML Mouse button enum values,", |
|
126 | 143 | " return button;", |
127 | 144 | "}", |
128 | 145 | "", |
| 146 | + "gdjs._DoubleClickExtension = { clickCounter: new ClickCounter() };", |
129 | 147 | "" |
130 | 148 | ], |
131 | 149 | "parameterObjects": "", |
|
145 | 163 | { |
146 | 164 | "type": "BuiltinCommonInstructions::JsCode", |
147 | 165 | "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 | + "" |
150 | 170 | ], |
151 | 171 | "parameterObjects": "", |
152 | 172 | "useStrict": true, |
|
208 | 228 | "sentence": "_PARAM1_ mouse button is clicked", |
209 | 229 | "events": [ |
210 | 230 | { |
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 | + } |
221 | 244 | ], |
222 | | - "parameterObjects": "", |
223 | | - "useStrict": true, |
224 | | - "eventsSheetExpanded": true |
| 245 | + "actions": [ |
| 246 | + { |
| 247 | + "type": { |
| 248 | + "value": "SetReturnBoolean" |
| 249 | + }, |
| 250 | + "parameters": [ |
| 251 | + "True" |
| 252 | + ] |
| 253 | + } |
| 254 | + ] |
225 | 255 | } |
226 | 256 | ], |
227 | 257 | "parameters": [ |
|
245 | 275 | { |
246 | 276 | "type": "BuiltinCommonInstructions::JsCode", |
247 | 277 | "inlineCode": [ |
| 278 | + "", |
| 279 | + "const { clickCounter } = gdjs._DoubleClickExtension;", |
| 280 | + "", |
248 | 281 | "const buttonName = eventsFunctionContext.getArgument(\"MouseButton\");", |
249 | 282 | "const clickCount = eventsFunctionContext.getArgument(\"ClickCount\");", |
250 | 283 | "", |
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);" |
257 | 285 | ], |
258 | 286 | "parameterObjects": "", |
259 | 287 | "useStrict": true, |
|
268 | 296 | "type": "mouseButton" |
269 | 297 | }, |
270 | 298 | { |
271 | | - "description": "Click count (or 0 for any click count)", |
| 299 | + "description": "Click count", |
272 | 300 | "name": "ClickCount", |
273 | 301 | "type": "expression" |
274 | 302 | } |
|
0 commit comments