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