Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit 69d0684

Browse files
committed
fix ios/mac/windows
1 parent 10f9911 commit 69d0684

File tree

6 files changed

+76
-166
lines changed

6 files changed

+76
-166
lines changed

com.unity.uiwidgets/Runtime/engine/UIWidgetsPanelWrapper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ public void OnMouseMove(Vector2? pos) {
289289
if (pos == null) {
290290
return;
291291
}
292-
293-
UIWidgetsPanel_onMouseMove(ptr: _ptr, x: pos.Value.x, y: pos.Value.y);
292+
293+
UIWidgetsPanel_onMouseMove(ptr: _ptr, x: pos.Value.x, y: pos.Value.y, -1);
294294
}
295295

296296
public void OnMouseScroll(Vector2 delta, Vector2? pos) {
@@ -330,7 +330,7 @@ public void OnDrag(Vector2? pos, int pointerId) {
330330

331331
// mouse event
332332
if (pointerId < 0) {
333-
UIWidgetsPanel_onMouseMove(ptr: _ptr, x: pos.Value.x, y: pos.Value.y);
333+
UIWidgetsPanel_onMouseMove(ptr: _ptr, x: pos.Value.x, y: pos.Value.y, pointerId);
334334
}
335335
}
336336

@@ -360,7 +360,7 @@ public void OnKeyDown(Event e) {
360360
static extern void UIWidgetsPanel_onMouseUp(IntPtr ptr, float x, float y, int button);
361361

362362
[DllImport(dllName: NativeBindings.dllName)]
363-
static extern void UIWidgetsPanel_onMouseMove(IntPtr ptr, float x, float y);
363+
static extern void UIWidgetsPanel_onMouseMove(IntPtr ptr, float x, float y, int button);
364364

365365
[DllImport(dllName: NativeBindings.dllName)]
366366
static extern void UIWidgetsPanel_onMouseLeave(IntPtr ptr);

engine/src/shell/platform/unity/android/uiwidgets_panel.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ namespace uiwidgets
561561
}
562562

563563
UIWIDGETS_API(void)
564-
UIWidgetsPanel_onMouseMove(UIWidgetsPanel *panel, float x, float y)
564+
UIWidgetsPanel_onMouseMove(UIWidgetsPanel *panel, float x, float y, int button)
565565
{
566566
panel->OnMouseMove(x, y);
567567
}

engine/src/shell/platform/unity/darwin/ios/uiwidgets_panel.h

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <flutter/fml/memory/ref_counted.h>
88
#include "shell/platform/unity/gfx_worker_task_runner.h"
9+
#include "lib/ui/window/pointer_data.h"
910
#include "runtime/mono_api.h"
1011
#include "cocoa_task_runner.h"
1112
#include "unity_surface_manager.h"
@@ -18,10 +19,12 @@ enum UIWidgetsWindowType {
1819
EditorWindowPanel = 2
1920
};
2021

21-
struct MouseState {
22-
bool state_is_down = false;
23-
bool state_is_added = false;
24-
uint64_t buttons = 0;
22+
enum UIWidgetsTouchPhase
23+
{
24+
TouchBegan,
25+
TouchEnded,
26+
TouchMoved,
27+
TouchCancelled
2528
};
2629

2730
class UIWidgetsPanel : public fml::RefCountedThreadSafe<UIWidgetsPanel> {
@@ -60,7 +63,7 @@ class UIWidgetsPanel : public fml::RefCountedThreadSafe<UIWidgetsPanel> {
6063

6164
void OnKeyDown(int keyCode, bool isKeyDown);
6265

63-
void OnMouseMove(float x, float y);
66+
void OnMouseMove(float x, float y, int button);
6467

6568
void OnScroll(float x, float y, float px, float py);
6669

@@ -79,31 +82,11 @@ class UIWidgetsPanel : public fml::RefCountedThreadSafe<UIWidgetsPanel> {
7982

8083
void CreateInternalUIWidgetsEngine(size_t width, size_t height, float device_pixel_ratio, const char* streaming_assets_path, const char* settings);
8184

82-
MouseState GetMouseState() { return mouse_state_; }
85+
void dispatchTouches(float x, float y, int button, UIWidgetsTouchPhase evtType);
8386

84-
void ResetMouseState() { mouse_state_ = MouseState(); }
87+
static PointerData::Change PointerDataChangeFromUITouchPhase(UIWidgetsTouchPhase phase);
8588

86-
void SetMouseStateDown(bool is_down) { mouse_state_.state_is_down = is_down; }
87-
88-
void SetMouseStateAdded(bool is_added) {
89-
mouse_state_.state_is_added = is_added;
90-
}
91-
92-
void SetMouseButtons(uint64_t buttons) { mouse_state_.buttons = buttons; }
93-
94-
void SendMouseMove(float x, float y);
95-
96-
void SendMouseDown(float x, float y);
97-
98-
void SendMouseUp(float x, float y);
99-
100-
void SendMouseLeave();
101-
102-
void SendScroll(float delta_x, float delta_y, float px, float py);
103-
104-
void SetEventPhaseFromCursorButtonState(UIWidgetsPointerEvent* event_data);
105-
106-
void SendPointerEventWithData(const UIWidgetsPointerEvent& event_data);
89+
static PointerData::DeviceKind DeviceKindFromTouchType();
10790

10891
Mono_Handle handle_;
10992
EntrypointCallback entrypoint_callback_;
@@ -117,7 +100,6 @@ class UIWidgetsPanel : public fml::RefCountedThreadSafe<UIWidgetsPanel> {
117100

118101
std::vector<intptr_t> vsync_batons_;
119102

120-
MouseState mouse_state_;
121103
bool process_events_ = false;
122104
};
123105

engine/src/shell/platform/unity/darwin/ios/uiwidgets_panel.mm

Lines changed: 56 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -256,105 +256,56 @@
256256
vsync_batons_.push_back(baton);
257257
}
258258

259-
void UIWidgetsPanel::SetEventPhaseFromCursorButtonState(
260-
UIWidgetsPointerEvent* event_data) {
261-
MouseState state = GetMouseState();
262-
event_data->phase = state.buttons == 0
263-
? state.state_is_down ? UIWidgetsPointerPhase::kUp
264-
: UIWidgetsPointerPhase::kHover
265-
: state.state_is_down ? UIWidgetsPointerPhase::kMove
266-
: UIWidgetsPointerPhase::kDown;
267-
}
268-
269-
void UIWidgetsPanel::SendMouseMove(float x, float y) {
270-
UIWidgetsPointerEvent event = {};
271-
event.x = x;
272-
event.y = y;
273-
SetEventPhaseFromCursorButtonState(&event);
274-
SendPointerEventWithData(event);
275-
}
276-
277-
void UIWidgetsPanel::SendMouseDown(float x, float y) {
278-
UIWidgetsPointerEvent event = {};
279-
SetEventPhaseFromCursorButtonState(&event);
280-
event.x = x;
281-
event.y = y;
282-
SendPointerEventWithData(event);
283-
SetMouseStateDown(true);
284-
}
285-
286-
void UIWidgetsPanel::SendMouseUp(float x, float y) {
287-
UIWidgetsPointerEvent event = {};
288-
SetEventPhaseFromCursorButtonState(&event);
289-
event.x = x;
290-
event.y = y;
291-
SendPointerEventWithData(event);
292-
if (event.phase == UIWidgetsPointerPhase::kUp) {
293-
SetMouseStateDown(false);
294-
}
295-
}
296-
297-
void UIWidgetsPanel::SendMouseLeave() {
298-
UIWidgetsPointerEvent event = {};
299-
event.phase = UIWidgetsPointerPhase::kRemove;
300-
SendPointerEventWithData(event);
301-
}
302-
303-
void UIWidgetsPanel::SendScroll(float delta_x, float delta_y, float px, float py) {
304-
UIWidgetsPointerEvent event = {};
305-
// TODO: this is a native method, use unity position instead.
306-
event.x = px;
307-
event.y = py;
308-
SetEventPhaseFromCursorButtonState(&event);
309-
event.signal_kind = UIWidgetsPointerSignalKind::kUIWidgetsPointerSignalKindScroll;
310-
// TODO: See if this can be queried from the OS; this value is chosen
311-
// arbitrarily to get something that feels reasonable.
312-
const int kScrollOffsetMultiplier = 20;
313-
event.scroll_delta_x = delta_x * kScrollOffsetMultiplier;
314-
event.scroll_delta_y = delta_y * kScrollOffsetMultiplier;
315-
SendPointerEventWithData(event);
316-
}
317-
318-
void UIWidgetsPanel::SendPointerEventWithData(
319-
const UIWidgetsPointerEvent& event_data) {
320-
MouseState mouse_state = GetMouseState();
321-
// If sending anything other than an add, and the pointer isn't already added,
322-
// synthesize an add to satisfy Flutter's expectations about events.
323-
if (!mouse_state.state_is_added &&
324-
event_data.phase != UIWidgetsPointerPhase::kAdd) {
325-
UIWidgetsPointerEvent event = {};
326-
event.phase = UIWidgetsPointerPhase::kAdd;
327-
event.x = event_data.x;
328-
event.y = event_data.y;
329-
event.buttons = 0;
330-
SendPointerEventWithData(event);
331-
}
332-
// Don't double-add (e.g., if events are delivered out of order, so an add has
333-
// already been synthesized).
334-
if (mouse_state.state_is_added &&
335-
event_data.phase == UIWidgetsPointerPhase::kAdd) {
336-
return;
337-
}
259+
void UIWidgetsPanel::dispatchTouches(float x, float y, int button, UIWidgetsTouchPhase phase)
260+
{
261+
PointerData pointer_data;
262+
pointer_data.Clear();
338263

339-
UIWidgetsPointerEvent event = event_data;
340-
event.device_kind = kUIWidgetsPointerDeviceKindMouse;
341-
event.buttons = mouse_state.buttons;
264+
auto packet = std::make_unique<PointerDataPacket>(1);
342265

343-
// Set metadata that's always the same regardless of the event.
344-
event.struct_size = sizeof(event);
345-
event.timestamp =
346-
std::chrono::duration_cast<std::chrono::microseconds>(
347-
std::chrono::high_resolution_clock::now().time_since_epoch())
348-
.count();
266+
pointer_data.time_stamp = std::chrono::duration_cast<std::chrono::microseconds>(
267+
std::chrono::high_resolution_clock::now().time_since_epoch())
268+
.count();
269+
270+
pointer_data.change = UIWidgetsPanel::PointerDataChangeFromUITouchPhase(phase);
271+
pointer_data.kind = UIWidgetsPanel::DeviceKindFromTouchType();
272+
pointer_data.device = -button;
273+
pointer_data.pointer_identifier = 0;
274+
pointer_data.physical_x = x;
275+
pointer_data.physical_y = y;
276+
pointer_data.physical_delta_x = 0.0;
277+
pointer_data.physical_delta_y = 0.0;
278+
pointer_data.pressure = 1.0;
279+
pointer_data.pressure_max = 1.0;
280+
pointer_data.signal_kind = PointerData::SignalKind::kNone;
281+
packet->SetPointerData(0, pointer_data);
282+
283+
reinterpret_cast<EmbedderEngine*>(engine_)->DispatchPointerDataPacket(
284+
std::move(packet));
285+
}
286+
287+
PointerData::Change UIWidgetsPanel::PointerDataChangeFromUITouchPhase(UIWidgetsTouchPhase phase)
288+
{
289+
switch(phase) {
290+
case TouchBegan:
291+
return PointerData::Change::kDown;
292+
case TouchMoved:
293+
return PointerData::Change::kMove;
294+
case TouchEnded:
295+
return PointerData::Change::kUp;
296+
case TouchCancelled:
297+
return PointerData::Change::kCancel;
298+
default:
299+
std::cerr << "Unhandled touch phase: " << phase << std::endl;
300+
break;
301+
}
349302

350-
UIWidgetsEngineSendPointerEvent(engine_, &event, 1);
303+
return PointerData::Change::kCancel;
304+
}
351305

352-
if (event_data.phase == UIWidgetsPointerPhase::kAdd) {
353-
SetMouseStateAdded(true);
354-
} else if (event_data.phase == UIWidgetsPointerPhase::kRemove) {
355-
SetMouseStateAdded(false);
356-
ResetMouseState();
357-
}
306+
PointerData::DeviceKind UIWidgetsPanel::DeviceKindFromTouchType()
307+
{
308+
return PointerData::DeviceKind::kTouch;
358309
}
359310

360311
void UIWidgetsPanel::OnKeyDown(int keyCode, bool isKeyDown) {
@@ -374,57 +325,32 @@
374325
}
375326
}
376327

377-
void UIWidgetsPanel::OnMouseMove(float x, float y) {
328+
void UIWidgetsPanel::OnMouseMove(float x, float y, int button) {
378329
if (process_events_) {
379-
SendMouseMove(x, y);
330+
dispatchTouches(x, y, button, TouchMoved);
380331
}
381332
}
382333

383334
void UIWidgetsPanel::OnScroll(float x, float y, float px, float py) {
384-
if (process_events_) {
385-
SendScroll(x, y, px, py);
386-
}
387-
}
388-
389-
static uint64_t ConvertToUIWidgetsButton(int button) {
390-
switch (button) {
391-
case -1:
392-
return kUIWidgetsPointerButtonMousePrimary;
393-
case -2:
394-
return kUIWidgetsPointerButtonMouseSecondary;
395-
case -3:
396-
return kUIWidgetsPointerButtonMouseMiddle;
397-
}
398-
std::cerr << "Mouse button not recognized: " << button << std::endl;
399-
return 0;
335+
//there should not be scroll events on iPhone!
336+
std::cerr << "Invalid input on iPhone: scroll" << std::endl;
400337
}
401338

402339
void UIWidgetsPanel::OnMouseDown(float x, float y, int button) {
403340
if (process_events_) {
404-
uint64_t uiwidgets_button = ConvertToUIWidgetsButton(button);
405-
if (uiwidgets_button != 0) {
406-
uint64_t mouse_buttons = GetMouseState().buttons | uiwidgets_button;
407-
SetMouseButtons(mouse_buttons);
408-
SendMouseDown(x, y);
409-
}
341+
dispatchTouches(x, y, button, TouchBegan);
410342
}
411343
}
412344

413345
void UIWidgetsPanel::OnMouseUp(float x, float y, int button) {
414346
if (process_events_) {
415-
uint64_t uiwidgets_button = ConvertToUIWidgetsButton(button);
416-
if (uiwidgets_button != 0) {
417-
uint64_t mouse_buttons = GetMouseState().buttons & ~uiwidgets_button;
418-
SetMouseButtons(mouse_buttons);
419-
SendMouseUp(x, y);
420-
}
347+
dispatchTouches(x, y, button, TouchEnded);
421348
}
422349
}
423350

424351
void UIWidgetsPanel::OnMouseLeave() {
425-
if (process_events_) {
426-
SendMouseLeave();
427-
}
352+
//there should not be mouse leave events on iPhone!
353+
std::cerr << "Invalid input on iPhone: mouse leave" << std::endl;
428354
}
429355

430356
UIWIDGETS_API(UIWidgetsPanel*)
@@ -493,8 +419,8 @@ static uint64_t ConvertToUIWidgetsButton(int button) {
493419
}
494420

495421
UIWIDGETS_API(void)
496-
UIWidgetsPanel_onMouseMove(UIWidgetsPanel* panel, float x, float y) {
497-
panel->OnMouseMove(x, y);
422+
UIWidgetsPanel_onMouseMove(UIWidgetsPanel* panel, float x, float y, int button) {
423+
panel->OnMouseMove(x, y, button);
498424
}
499425

500426
UIWIDGETS_API(void)

engine/src/shell/platform/unity/darwin/macos/uiwidgets_panel.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ static uint64_t ConvertToUIWidgetsButton(int button) {
493493
}
494494

495495
UIWIDGETS_API(void)
496-
UIWidgetsPanel_onMouseMove(UIWidgetsPanel* panel, float x, float y) {
496+
UIWidgetsPanel_onMouseMove(UIWidgetsPanel* panel, float x, float y, int button) {
497+
//button id is not useful for desktop since the motions happens on the mouse (including all buttons)
497498
panel->OnMouseMove(x, y);
498499
}
499500

engine/src/shell/platform/unity/windows/uiwidgets_panel.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ UIWidgetsPanel_onMouseUp(UIWidgetsPanel* panel, float x, float y, int button) {
539539
}
540540

541541
UIWIDGETS_API(void)
542-
UIWidgetsPanel_onMouseMove(UIWidgetsPanel* panel, float x, float y) {
542+
UIWidgetsPanel_onMouseMove(UIWidgetsPanel* panel, float x, float y, int button) {
543+
//button id is not useful for desktop since the motions happens on the mouse (including all buttons)
543544
panel->OnMouseMove(x, y);
544545
}
545546

0 commit comments

Comments
 (0)