Browse Source

* Key handling: restore map

Michaël Van Canneyt 3 months ago
parent
commit
b4be3b8ffc
1 changed files with 52 additions and 43 deletions
  1. 52 43
      src/pas2js/fresnel.browser.pas2js.wasmapi.pp

+ 52 - 43
src/pas2js/fresnel.browser.pas2js.wasmapi.pp

@@ -129,6 +129,7 @@ type
 
 
   TWasmFresnelBrowserApi = class(TWasmFresnelSharedApi)
   TWasmFresnelBrowserApi = class(TWasmFresnelSharedApi)
   private
   private
+    FUseKeyMap: Boolean;
     FWindowsParentRoot : TJSHTMLELement;
     FWindowsParentRoot : TJSHTMLELement;
     FWindowsCanvases : TJSMap;
     FWindowsCanvases : TJSMap;
 
 
@@ -146,12 +147,12 @@ type
 
 
     FSelfieSegmentation : TFresnelSelfieSegmentation;
     FSelfieSegmentation : TFresnelSelfieSegmentation;
 
 
-    class var vWindowID : LongInt;
-    class var vNextMenuID : LongInt;
-    class var vNextVideoID : LongInt;
+    class var _WindowID : LongInt;
+    class var _NextMenuID : LongInt;
+    class var _NextVideoID : LongInt;
 
 
-    class var vKeymap : TJSObject;
-    class var vKeyCodeMap : TJSObject;
+    class var _KeyNamemap : TJSObject;
+//    class var _KeyKindMap : TJSObject;
 
 
   protected
   protected
 
 
@@ -191,6 +192,7 @@ type
 
 
     property WindowsParentRoot : TJSHTMLElement read FWindowsParentRoot write FWindowsParentRoot;
     property WindowsParentRoot : TJSHTMLElement read FWindowsParentRoot write FWindowsParentRoot;
     property MenuSupport : Boolean read FMenuSupport write FMenuSupport;
     property MenuSupport : Boolean read FMenuSupport write FMenuSupport;
+    Property UseKeyMap : Boolean Read FUseKeyMap Write FUseKeyMap;
 
 
     procedure InstallGlobalHandlers;
     procedure InstallGlobalHandlers;
 
 
@@ -225,16 +227,16 @@ type
     // Keys
     // Keys
 
 
     function SetSpecialKeyMap(Map : TWasmPointer; aLen : LongInt) : TCanvasError;
     function SetSpecialKeyMap(Map : TWasmPointer; aLen : LongInt) : TCanvasError;
-    function KeyNameToKeyCode(const aKey: String) : TKeyKind;
+    function KeyNameToKeyKind(const aKey: String) : TKeyKind;
 
 
-    class function GetGlobalKeyMap: TJSObject;
-    class function KeyNameToKeyCode(aMap : TJSObject;aKey : string) : TKeyKind;
+    class function GetGlobalKeyNameMap: TJSObject;
+    class function KeyNameToKeyKind(aMap : TJSObject;aKey : string) : TKeyKind;
     class function CreateSpecialKeyNameMap : TJSObject;
     class function CreateSpecialKeyNameMap : TJSObject;
     class function CreateSpecialKeyCodeMap : TJSObject;
     class function CreateSpecialKeyCodeMap : TJSObject;
 
 
     // Key handlers are global
     // Key handlers are global
 
 
-    class procedure SetWindowEventFromKeyboardEvent(aWindowEvent : TWindowEvent; aKeyboardEvent : TJSKeyboardEvent); static;
+    procedure SetWindowEventFromKeyboardEvent(aWindowEvent: TWindowEvent; aKeyboardEvent: TJSKeyboardEvent);
     function DoKeyDownEvent(aEvent: TJSEvent): Boolean;
     function DoKeyDownEvent(aEvent: TJSEvent): Boolean;
     function DoKeyUpEvent(aEvent: TJSEvent): Boolean;
     function DoKeyUpEvent(aEvent: TJSEvent): Boolean;
 
 
@@ -503,6 +505,7 @@ end;
 //
 //
 function TWindowReference.DoContextMenu(aEvent: TJSEvent): Boolean;
 function TWindowReference.DoContextMenu(aEvent: TJSEvent): Boolean;
 begin
 begin
+  Result:=true;
   aEvent.preventDefault();
   aEvent.preventDefault();
   // TODO : route to FMX context menu
   // TODO : route to FMX context menu
 end;
 end;
@@ -610,7 +613,7 @@ end;
 //
 //
 procedure TVideoReference.StopCapture;
 procedure TVideoReference.StopCapture;
 var
 var
-  tracks : TJSArray;
+  tracks : TJSMediaStreamTracks;
 begin
 begin
   if FRequestVideoFrameID <> 0 then
   if FRequestVideoFrameID <> 0 then
   begin
   begin
@@ -620,7 +623,10 @@ begin
   if JSValue(FVideoElement.srcObject) then
   if JSValue(FVideoElement.srcObject) then
   begin
   begin
     tracks := FVideoElement.srcObject.getTracks;
     tracks := FVideoElement.srcObject.getTracks;
-    asm tracks.forEach(track => track.stop()) end;
+    tracks.forEach(procedure(track : TJSMediaStreamTrack)
+      begin
+      track.stop
+      end);
     FVideoElement.srcObject := nil;
     FVideoElement.srcObject := nil;
   end;
   end;
 end;
 end;
@@ -672,8 +678,8 @@ end;
 //
 //
 function TWasmFresnelBrowserApi.GetNewWindowID : TWindowCanvasID;
 function TWasmFresnelBrowserApi.GetNewWindowID : TWindowCanvasID;
 begin
 begin
-  Inc(vWindowID);
-  Result := vWindowID;
+  Inc(_WindowID);
+  Result := _WindowID;
 end;
 end;
 
 
 // CreateMenuBuilder
 // CreateMenuBuilder
@@ -858,7 +864,7 @@ function TWasmFresnelBrowserApi.AllocateWindowCanvas(
 var
 var
   lRef : TWindowReference;
   lRef : TWindowReference;
   lPixelRatio : TFresnelFloat;
   lPixelRatio : TFresnelFloat;
-  V: TJSDataView;
+
 begin
 begin
   {$IFNDEF NOLOGAPICALLS}
   {$IFNDEF NOLOGAPICALLS}
   if LogAPICalls then
   if LogAPICalls then
@@ -959,6 +965,7 @@ var
 begin
 begin
   lCursor := GetUTF16FromMem(aTextUTF16, aUTF16Size);
   lCursor := GetUTF16FromMem(aTextUTF16, aUTF16Size);
   WindowsParentRoot.style.setProperty('cursor', lCursor);
   WindowsParentRoot.style.setProperty('cursor', lCursor);
+  result:=ECANVAS_SUCCESS;
 end;
 end;
 
 
 // WakeMainThread
 // WakeMainThread
@@ -999,8 +1006,8 @@ begin
   if not Assigned(Ref.MenuBuilder)then
   if not Assigned(Ref.MenuBuilder)then
     Exit(ECANVAS_NOMENUSUPPORT);
     Exit(ECANVAS_NOMENUSUPPORT);
 
 
-  Inc(vNextMenuID);
-  lMenuID := vNextMenuID;
+  Inc(_NextMenuID);
+  lMenuID := _NextMenuID;
 
 
   LFlags:=FlagsToMenuFlags(aFlags);
   LFlags:=FlagsToMenuFlags(aFlags);
   if Ref.MenuBuilder.AddMenuItem(aParentID,lMenuID,S,lFlags,aShortCut,aData)<>Nil then
   if Ref.MenuBuilder.AddMenuItem(aParentID,lMenuID,S,lFlags,aShortCut,aData)<>Nil then
@@ -1076,29 +1083,29 @@ end;
 
 
 // KeyNameToKeyCode
 // KeyNameToKeyCode
 //
 //
-function TWasmFresnelBrowserApi.KeyNameToKeyCode(const aKey: String): TKeyKind;
+function TWasmFresnelBrowserApi.KeyNameToKeyKind(const aKey: String): TKeyKind;
 var
 var
   lMap : TJSObject;
   lMap : TJSObject;
 begin
 begin
   lMap:=FKeyMap;
   lMap:=FKeyMap;
   if lMap=Nil then
   if lMap=Nil then
-    lmap:=GetGlobalKeyMap;
-  Result:=KeyNameToKeyCode(lMap,aKey);
+    lmap:=GetGlobalKeyNameMap;
+  Result:=KeyNameToKeyKind(lMap,aKey);
 //  Writeln('Mapped ',aKey,' to ',TJSJSON.StringIfy(Result));
 //  Writeln('Mapped ',aKey,' to ',TJSJSON.StringIfy(Result));
 end;
 end;
 
 
 // GetGlobalKeyMap
 // GetGlobalKeyMap
 //
 //
-class function TWasmFresnelBrowserApi.GetGlobalKeyMap: TJSObject;
+class function TWasmFresnelBrowserApi.GetGlobalKeyNameMap: TJSObject;
 begin
 begin
-  if vKeyMap = nil then
-    vKeyMap := CreateSpecialKeyNameMap;
-  Result := vKeyMap;
+  if _KeyNameMap = nil then
+    _KeyNameMap := CreateSpecialKeyNameMap;
+  Result := _KeyNameMap;
 end;
 end;
 
 
 // KeyNameToKeyCode
 // KeyNameToKeyCode
 //
 //
-class function TWasmFresnelBrowserApi.KeyNameToKeyCode(aMap : TJSObject; aKey: string): TKeyKind;
+class function TWasmFresnelBrowserApi.KeyNameToKeyKind(aMap : TJSObject; aKey: string): TKeyKind;
 var
 var
   num: JSValue;
   num: JSValue;
 begin
 begin
@@ -1142,26 +1149,30 @@ end;
 
 
 // SetWindowEventFromKeyboardEvent
 // SetWindowEventFromKeyboardEvent
 //
 //
-class procedure TWasmFresnelBrowserApi.SetWindowEventFromKeyboardEvent(
-  aWindowEvent : TWindowEvent; aKeyboardEvent : TJSKeyboardEvent
-  );
+procedure TWasmFresnelBrowserApi.SetWindowEventFromKeyboardEvent(
+  aWindowEvent : TWindowEvent;
+  aKeyboardEvent : TJSKeyboardEvent);
 var
 var
   isNormalChar : Boolean;
   isNormalChar : Boolean;
 begin
 begin
-  isNormalChar :=     (Length(aKeyboardEvent.Key) = 1)
+  isNormalChar := (Length(aKeyboardEvent.Key) = 1)
                   and not (aKeyboardEvent.ctrlKey or aKeyboardEvent.altKey or aKeyboardEvent.metaKey);
                   and not (aKeyboardEvent.ctrlKey or aKeyboardEvent.altKey or aKeyboardEvent.metaKey);
 
 
-  if isNormalChar then begin
-
+  if isNormalChar then
+    begin
     aWindowEvent.param0 := TJSString(aKeyboardEvent.Key).charCodeAt(0);
     aWindowEvent.param0 := TJSString(aKeyboardEvent.Key).charCodeAt(0);
-    aWindowEvent.param1 := 1;
-
-  end else begin
-
-    aWindowEvent.param0 := Longint(TJSObject(aKeyboardEvent)['keyCode']);
-    aWindowEvent.param1 := 0;
-
-  end;
+    aWindowEvent.param1 := WASMSG_KEYKIND_CHAR;
+    end
+  else if not UseKeyMap then
+    begin
+    aWindowEvent.param0 :=  Longint(TJSObject(aKeyboardEvent)['keyCode']);
+    aWindowEvent.param1 := WASMSG_KEYKIND_SPECIAL_SCANCODE;
+    end
+  else
+    begin
+    aWindowEvent.param0 := KeyNameToKeyKind(aKeyboardEvent.Key).KeyCode;
+    aWindowEvent.param1 := WASMSG_KEYKIND_SPECIAL_MAP;
+    end;
 
 
   aWindowEvent.param2 := TFresnelHelper.EncodeKeyboardShiftState(aKeyboardEvent);
   aWindowEvent.param2 := TFresnelHelper.EncodeKeyboardShiftState(aKeyboardEvent);
   aWindowEvent.param3 := 0;
   aWindowEvent.param3 := 0;
@@ -1400,10 +1411,10 @@ function TWasmFresnelBrowserApi.AllocateVideoReference: TVideoReference;
 var
 var
   lID : TVideoElementID;
   lID : TVideoElementID;
 begin
 begin
-  Inc(vNextVideoID);
-  lID := vNextVideoID;
+  Inc(_NextVideoID);
+  lID := _NextVideoID;
 
 
-  Result := TVideoReference.Create(vNextVideoID, Self);
+  Result := TVideoReference.Create(_NextVideoID, Self);
 
 
   FVideoReferences.&set(lID, Result);
   FVideoReferences.&set(lID, Result);
 end;
 end;
@@ -1518,7 +1529,6 @@ var
 
 
   procedure BlurCapture(aTimeStamp : TJSDOMHighResTimeStamp; aSnapShot : TJSImageBitmap);
   procedure BlurCapture(aTimeStamp : TJSDOMHighResTimeStamp; aSnapShot : TJSImageBitmap);
   var
   var
-    lCanvasRef : TOffscreenCanvasReference;
     lCallback : JSValue;
     lCallback : JSValue;
     lBitmapID : Integer;
     lBitmapID : Integer;
   begin
   begin
@@ -1549,7 +1559,6 @@ var
 
 
   procedure DirectCapture(aTimeStamp : TJSDOMHighResTimeStamp; aSnapShot : TJSImageBitmap);
   procedure DirectCapture(aTimeStamp : TJSDOMHighResTimeStamp; aSnapShot : TJSImageBitmap);
   var
   var
-    lCanvasRef : TOffscreenCanvasReference;
     lCallback : JSValue;
     lCallback : JSValue;
     lBitmapID : Integer;
     lBitmapID : Integer;
   begin
   begin