annotate Applications/Platforms/WebAssembly/WebAssemblyViewport.cpp @ 1621:575f512cdf48

Used a weak_ptr as a cookie to RequestAnimationFrame to prevent accessing a deleted viewport.
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 02 Nov 2020 20:45:04 +0100
parents 9a52bac0c2a7
children 74be0f498b08
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
1270
2d8ab34c8c91 upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1239
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
16 *
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
1549
a48ae10857b1 packaging of the WebAssembly module
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
22 #if defined(ORTHANC_BUILDING_STONE_LIBRARY) && ORTHANC_BUILDING_STONE_LIBRARY == 1
a48ae10857b1 packaging of the WebAssembly module
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
23 # include "WebAssemblyViewport.h"
1591
5887a4f8594b moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1578
diff changeset
24 # include "../../../OrthancStone/Sources/Scene2DViewport/ViewportController.h"
5887a4f8594b moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1578
diff changeset
25 # include "../../../OrthancStone/Sources/Toolbox/GenericToolbox.h"
5887a4f8594b moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1578
diff changeset
26 # include "../../../OrthancStone/Sources/Viewport/DefaultViewportInteractor.h"
1549
a48ae10857b1 packaging of the WebAssembly module
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
27 #else
a48ae10857b1 packaging of the WebAssembly module
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
28 // This is the case when using the WebAssembly side module, and this
a48ae10857b1 packaging of the WebAssembly module
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
29 // source file must be compiled within the WebAssembly main module
a48ae10857b1 packaging of the WebAssembly module
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
30 # include <Viewport/WebAssemblyViewport.h>
a48ae10857b1 packaging of the WebAssembly module
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
31 # include <Toolbox/GenericToolbox.h>
1558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1557
diff changeset
32 # include <Scene2DViewport/ViewportController.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1557
diff changeset
33 # include <Viewport/DefaultViewportInteractor.h>
1549
a48ae10857b1 packaging of the WebAssembly module
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
34 #endif
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
35
1557
a6f339d8e4c2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1549
diff changeset
36
1455
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1356
diff changeset
37 #include <OrthancException.h>
1050
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1045
diff changeset
38
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
39 #include <boost/make_shared.hpp>
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
40 #include <boost/enable_shared_from_this.hpp>
1576
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
41 #include <boost/math/special_functions/round.hpp>
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 namespace OrthancStone
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
45 static void ConvertMouseEvent(PointerEvent& target,
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
46 const EmscriptenMouseEvent& source,
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
47 const ICompositor& compositor)
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
49 int x = static_cast<int>(source.targetX);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
50 int y = static_cast<int>(source.targetY);
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
52 switch (source.button)
997
d6b83ee3a950 fix WebAssemblyOpenGLViewport::UpdateSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 964
diff changeset
53 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
54 case 0:
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
55 target.SetMouseButton(MouseButton_Left);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
56 break;
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
58 case 1:
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
59 target.SetMouseButton(MouseButton_Middle);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
60 break;
997
d6b83ee3a950 fix WebAssemblyOpenGLViewport::UpdateSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 964
diff changeset
61
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
62 case 2:
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
63 target.SetMouseButton(MouseButton_Right);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
64 break;
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
65
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
66 default:
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
67 target.SetMouseButton(MouseButton_None);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
68 break;
997
d6b83ee3a950 fix WebAssemblyOpenGLViewport::UpdateSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 964
diff changeset
69 }
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
70
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
71 target.AddPosition(compositor.GetPixelCenterCoordinates(x, y));
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
72 target.SetAltModifier(source.altKey);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
73 target.SetControlModifier(source.ctrlKey);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
74 target.SetShiftModifier(source.shiftKey);
1054
3c9529edf5fd fixing WebAssemblyViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1050
diff changeset
75 }
3c9529edf5fd fixing WebAssemblyViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1050
diff changeset
76
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
77
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
78 class WebAssemblyViewport::WasmLock : public ILock
1239
ce3052f28f2e Added a lazy size update system in WebAssemblyOpenGLViewport::Refresh
Benjamin Golinvaux <bgo@osimis.io>
parents: 1054
diff changeset
79 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
80 private:
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
81 WebAssemblyViewport& that_;
1239
ce3052f28f2e Added a lazy size update system in WebAssemblyOpenGLViewport::Refresh
Benjamin Golinvaux <bgo@osimis.io>
parents: 1054
diff changeset
82
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
83 public:
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
84 WasmLock(WebAssemblyViewport& that) :
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
85 that_(that)
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
86 {
1239
ce3052f28f2e Added a lazy size update system in WebAssemblyOpenGLViewport::Refresh
Benjamin Golinvaux <bgo@osimis.io>
parents: 1054
diff changeset
87 }
ce3052f28f2e Added a lazy size update system in WebAssemblyOpenGLViewport::Refresh
Benjamin Golinvaux <bgo@osimis.io>
parents: 1054
diff changeset
88
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
89 virtual bool HasCompositor() const ORTHANC_OVERRIDE
1239
ce3052f28f2e Added a lazy size update system in WebAssemblyOpenGLViewport::Refresh
Benjamin Golinvaux <bgo@osimis.io>
parents: 1054
diff changeset
90 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
91 return that_.compositor_.get() != NULL;
1239
ce3052f28f2e Added a lazy size update system in WebAssemblyOpenGLViewport::Refresh
Benjamin Golinvaux <bgo@osimis.io>
parents: 1054
diff changeset
92 }
ce3052f28f2e Added a lazy size update system in WebAssemblyOpenGLViewport::Refresh
Benjamin Golinvaux <bgo@osimis.io>
parents: 1054
diff changeset
93
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
94 virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
95 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
96 if (that_.compositor_.get() == NULL)
1054
3c9529edf5fd fixing WebAssemblyViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1050
diff changeset
97 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
98 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
1054
3c9529edf5fd fixing WebAssemblyViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1050
diff changeset
99 }
3c9529edf5fd fixing WebAssemblyViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1050
diff changeset
100 else
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 947
diff changeset
101 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
102 return *that_.compositor_;
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 947
diff changeset
103 }
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
104 }
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
105
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
106 virtual ViewportController& GetController() ORTHANC_OVERRIDE
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
107 {
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
108 assert(that_.controller_);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
109 return *that_.controller_;
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
110 }
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
111
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
112 virtual void Invalidate() ORTHANC_OVERRIDE
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
113 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
114 that_.Invalidate();
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
115 }
1576
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
116
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
117 virtual void RefreshCanvasSize() ORTHANC_OVERRIDE
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
118 {
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
119 that_.RefreshCanvasSize();
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
120 }
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
121 };
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
122
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
123 EM_BOOL WebAssemblyViewport::OnRequestAnimationFrame(double time, void *userData)
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
124 {
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
125 LOG(TRACE) << __func__;
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
126
1621
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
127 WebAssemblyViewport* that =
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
128 WebAssemblyViewport::DereferenceObjectCookie(userData);
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
129
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
130 if (that != NULL)
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
131 {
1621
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
132 if (that->compositor_.get() != NULL &&
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
133 that->controller_ /* should always be true */)
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
134 {
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
135 that->Paint(*that->compositor_, *that->controller_);
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
136 }
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
137 }
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
138 else
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
139 {
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
140 LOG(INFO) << "WebAssemblyViewport::OnRequestAnimationFrame "
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
141 << "-- the WebAssemblyViewport is deleted and Paint will "
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
142 << "not be called";
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
143 }
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
144 LOG(TRACE) << "Exiting: " << __func__;
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
145 return true;
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
146 }
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 947
diff changeset
147
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
148 EM_BOOL WebAssemblyViewport::OnResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
149 {
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
150 LOG(TRACE) << __func__;
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
151 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
152
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
153 if (that->compositor_.get() != NULL)
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
154 {
1576
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
155 that->RefreshCanvasSize();
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
156 that->Invalidate();
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
157 }
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
158
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
159 LOG(TRACE) << "Exiting: " << __func__;
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
160 return true;
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
161 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
162
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
163
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
164 EM_BOOL WebAssemblyViewport::OnMouseDown(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
165 {
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
166 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
167
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
168 LOG(TRACE) << "mouse down: " << that->GetCanvasCssSelector();
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
169
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
170 if (that->compositor_.get() != NULL &&
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
171 that->interactor_.get() != NULL)
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
172 {
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
173 PointerEvent pointer;
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
174 ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_);
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
175
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
176 that->controller_->HandleMousePress(*that->interactor_, pointer,
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
177 that->compositor_->GetCanvasWidth(),
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
178 that->compositor_->GetCanvasHeight());
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
179 that->Invalidate();
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
180 }
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
181
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
182 LOG(TRACE) << "Exiting: " << __func__;
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
183 return true;
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
184 }
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
185
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
186
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
187 EM_BOOL WebAssemblyViewport::OnMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
188 {
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
189 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
190
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
191 if (that->compositor_.get() != NULL &&
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
192 that->controller_->HasActiveTracker())
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
193 {
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
194 PointerEvent pointer;
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
195 ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_);
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
196 if (that->controller_->HandleMouseMove(pointer))
1050
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1045
diff changeset
197 {
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
198 that->Invalidate();
1050
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1045
diff changeset
199 }
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
200 }
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 947
diff changeset
201
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
202 LOG(TRACE) << "Exiting: " << __func__;
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
203 return true;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
204 }
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
205
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
206 EM_BOOL WebAssemblyViewport::OnMouseUp(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
207 {
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
208 LOG(TRACE) << __func__;
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
209 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
210
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
211 if (that->compositor_.get() != NULL)
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
212 {
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
213 PointerEvent pointer;
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
214 ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_);
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
215 that->controller_->HandleMouseRelease(pointer);
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
216 that->Invalidate();
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
217 }
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
218
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
219 LOG(TRACE) << "Exiting: " << __func__;
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
220 return true;
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1318
diff changeset
221 }
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
222
1621
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
223 void* WebAssemblyViewport::GetObjectCookie()
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
224 {
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
225 if(objectCookie_ != NULL)
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
226 return objectCookie_;
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
227
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
228 boost::shared_ptr<WebAssemblyViewport>* sharedFromThisPtr =
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
229 new boost::shared_ptr<WebAssemblyViewport>();
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
230
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
231 *sharedFromThisPtr = shared_from_this();
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
232
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
233 objectCookie_ = reinterpret_cast<void*>(sharedFromThisPtr);
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
234
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
235 return objectCookie_;
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
236 }
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
237
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
238 void WebAssemblyViewport::ReleaseObjectCookie(void* cookie)
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
239 {
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
240 WebAssemblyViewport* This = DereferenceObjectCookie(cookie);
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
241
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
242 if (This != NULL)
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
243 This->objectCookie_ = NULL;
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
244
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
245 boost::weak_ptr<WebAssemblyViewport>* weakThisPtr =
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
246 reinterpret_cast<boost::weak_ptr<WebAssemblyViewport>*>(cookie);
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
247
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
248 delete weakThisPtr;
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
249 }
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
250
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
251 WebAssemblyViewport* WebAssemblyViewport::DereferenceObjectCookie(void* cookie)
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
252 {
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
253 boost::weak_ptr<WebAssemblyViewport>* weakThisPtr =
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
254 reinterpret_cast<boost::weak_ptr<WebAssemblyViewport>*>(cookie);
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
255
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
256 boost::shared_ptr<WebAssemblyViewport> sharedThis = weakThisPtr->lock();
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
257
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
258 return sharedThis.get();
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
259 }
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
260
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
261 void WebAssemblyViewport::Invalidate()
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
262 {
1618
9a52bac0c2a7 Added code to clear pending calls to RequestAnimationFrame
Benjamin Golinvaux <bgo@osimis.io>
parents: 1591
diff changeset
263 long id = emscripten_request_animation_frame(OnRequestAnimationFrame,
1621
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
264 GetObjectCookie());
1618
9a52bac0c2a7 Added code to clear pending calls to RequestAnimationFrame
Benjamin Golinvaux <bgo@osimis.io>
parents: 1591
diff changeset
265 animationFrameCallbackIds_.push_back(id);
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
266 }
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
267
1575
e4a52cbbdd70 working on print
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1566
diff changeset
268 void WebAssemblyViewport::FitForPrint()
e4a52cbbdd70 working on print
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1566
diff changeset
269 {
e4a52cbbdd70 working on print
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1566
diff changeset
270 if (compositor_.get() != NULL &&
e4a52cbbdd70 working on print
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1566
diff changeset
271 controller_ /* should always be true */)
e4a52cbbdd70 working on print
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1566
diff changeset
272 {
1576
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
273 RefreshCanvasSize();
1575
e4a52cbbdd70 working on print
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1566
diff changeset
274 compositor_->FitContent(controller_->GetScene());
1578
1f812f4c95be comments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
275 OnRequestAnimationFrame(0, reinterpret_cast<void*>(this)); // Mandatory to work with Firefox
1575
e4a52cbbdd70 working on print
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1566
diff changeset
276 }
e4a52cbbdd70 working on print
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1566
diff changeset
277 }
e4a52cbbdd70 working on print
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1566
diff changeset
278
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
279 void WebAssemblyViewport::AcquireCompositor(ICompositor* compositor /* takes ownership */)
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
280 {
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
281 if (compositor == NULL)
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
282 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
283 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
284 }
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
285 else
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
286 {
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
287 compositor_.reset(compositor);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
288 }
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
289 }
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
290
1356
d8bb885e9b0a Checking DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
Benjamin Golinvaux <bgo@osimis.io>
parents: 1351
diff changeset
291 #if DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR == 1
d8bb885e9b0a Checking DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
Benjamin Golinvaux <bgo@osimis.io>
parents: 1351
diff changeset
292 // everything OK..... we're using the new setting
d8bb885e9b0a Checking DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
Benjamin Golinvaux <bgo@osimis.io>
parents: 1351
diff changeset
293 #else
d8bb885e9b0a Checking DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
Benjamin Golinvaux <bgo@osimis.io>
parents: 1351
diff changeset
294 #pragma message("WARNING: DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR is not defined or equal to 0. Stone will use the OLD Emscripten rules for DOM element selection.")
d8bb885e9b0a Checking DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
Benjamin Golinvaux <bgo@osimis.io>
parents: 1351
diff changeset
295 #endif
d8bb885e9b0a Checking DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
Benjamin Golinvaux <bgo@osimis.io>
parents: 1351
diff changeset
296
1318
cbfdba08e039 removed SDL code in wasm + added undostack CTOR in Wasm viewport + build fix + ORTHANC_OVERRIDE
Benjamin Golinvaux <bgo@osimis.io>
parents: 1315
diff changeset
297 WebAssemblyViewport::WebAssemblyViewport(
1346
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
298 const std::string& canvasId, bool enableEmscriptenMouseEvents) :
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
299 canvasId_(canvasId),
1356
d8bb885e9b0a Checking DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
Benjamin Golinvaux <bgo@osimis.io>
parents: 1351
diff changeset
300 #if DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR == 1
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
301 canvasCssSelector_("#" + canvasId),
1356
d8bb885e9b0a Checking DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
Benjamin Golinvaux <bgo@osimis.io>
parents: 1351
diff changeset
302 #else
d8bb885e9b0a Checking DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
Benjamin Golinvaux <bgo@osimis.io>
parents: 1351
diff changeset
303 canvasCssSelector_(canvasId),
d8bb885e9b0a Checking DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
Benjamin Golinvaux <bgo@osimis.io>
parents: 1351
diff changeset
304 #endif
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
305 interactor_(new DefaultViewportInteractor),
1576
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
306 enableEmscriptenMouseEvents_(enableEmscriptenMouseEvents),
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
307 canvasWidth_(0),
1621
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
308 canvasHeight_(0),
575f512cdf48 Used a weak_ptr as a cookie to RequestAnimationFrame to prevent
Benjamin Golinvaux <bgo@osimis.io>
parents: 1618
diff changeset
309 objectCookie_(NULL)
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
310 {
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
311 }
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
312
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
313 void WebAssemblyViewport::PostConstructor()
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
314 {
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
315 boost::shared_ptr<IViewport> viewport = shared_from_this();
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
316 controller_.reset(new ViewportController(viewport));
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
317
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
318 LOG(INFO) << "Initializing Stone viewport on HTML canvas: "
1480
4de884c95cd8 fix includes in WebAssemblyCairoViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
319 << canvasId_;
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
320
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
321 if (canvasId_.empty() ||
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
322 canvasId_[0] == '#')
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
323 {
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
324 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange,
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
325 "The canvas identifier must not start with '#'");
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
326 }
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
327
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
328 // Disable right-click on the canvas (i.e. context menu)
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
329 EM_ASM({
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
330 document.getElementById(UTF8ToString($0)).oncontextmenu =
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
331 function(event)
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
332 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
333 event.preventDefault();
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
334 }
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
335 },
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
336 canvasId_.c_str() // $0
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
337 );
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
338
1346
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
339 // It is not possible to monitor the resizing of individual
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
340 // canvas, so we track the full window of the browser
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
341 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW,
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
342 reinterpret_cast<void*>(this),
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
343 false,
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
344 OnResize);
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
345
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
346 if (enableEmscriptenMouseEvents_)
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
347 {
1318
cbfdba08e039 removed SDL code in wasm + added undostack CTOR in Wasm viewport + build fix + ORTHANC_OVERRIDE
Benjamin Golinvaux <bgo@osimis.io>
parents: 1315
diff changeset
348
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
349 // if any of this function causes an error in the console, please
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
350 // make sure you are using the new (as of 1.39.x) version of
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
351 // emscripten element lookup rules( pass
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
352 // "-s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1" to the linker.
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
353
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
354 emscripten_set_mousedown_callback(canvasCssSelector_.c_str(),
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
355 reinterpret_cast<void*>(this),
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
356 false,
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
357 OnMouseDown);
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
358
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
359 emscripten_set_mousemove_callback(canvasCssSelector_.c_str(),
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
360 reinterpret_cast<void*>(this),
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
361 false,
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
362 OnMouseMove);
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
363
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
364 emscripten_set_mouseup_callback(canvasCssSelector_.c_str(),
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
365 reinterpret_cast<void*>(this),
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
366 false,
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
367 OnMouseUp);
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
368 }
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
369 }
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
370
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
371 WebAssemblyViewport::~WebAssemblyViewport()
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
372 {
1618
9a52bac0c2a7 Added code to clear pending calls to RequestAnimationFrame
Benjamin Golinvaux <bgo@osimis.io>
parents: 1591
diff changeset
373 // cancel the pending RequestAnimationFrame callbacks
9a52bac0c2a7 Added code to clear pending calls to RequestAnimationFrame
Benjamin Golinvaux <bgo@osimis.io>
parents: 1591
diff changeset
374 for(size_t i = 0; i < animationFrameCallbackIds_.size(); ++i)
9a52bac0c2a7 Added code to clear pending calls to RequestAnimationFrame
Benjamin Golinvaux <bgo@osimis.io>
parents: 1591
diff changeset
375 {
9a52bac0c2a7 Added code to clear pending calls to RequestAnimationFrame
Benjamin Golinvaux <bgo@osimis.io>
parents: 1591
diff changeset
376 long id = animationFrameCallbackIds_[i];
9a52bac0c2a7 Added code to clear pending calls to RequestAnimationFrame
Benjamin Golinvaux <bgo@osimis.io>
parents: 1591
diff changeset
377 emscripten_cancel_animation_frame(id);
9a52bac0c2a7 Added code to clear pending calls to RequestAnimationFrame
Benjamin Golinvaux <bgo@osimis.io>
parents: 1591
diff changeset
378 }
9a52bac0c2a7 Added code to clear pending calls to RequestAnimationFrame
Benjamin Golinvaux <bgo@osimis.io>
parents: 1591
diff changeset
379
1346
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
380 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW,
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
381 reinterpret_cast<void*>(this),
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
382 false,
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
383 NULL);
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
384
df8bf351c23f The flag that allows the WebAssemblyViewport to
Benjamin Golinvaux <bgo@osimis.io>
parents: 1331
diff changeset
385 if (enableEmscriptenMouseEvents_)
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
386 {
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
387
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
388 emscripten_set_mousedown_callback(canvasCssSelector_.c_str(),
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
389 reinterpret_cast<void*>(this),
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
390 false,
1480
4de884c95cd8 fix includes in WebAssemblyCairoViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
391 NULL);
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
392
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
393 emscripten_set_mousemove_callback(canvasCssSelector_.c_str(),
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
394 reinterpret_cast<void*>(this),
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
395 false,
1480
4de884c95cd8 fix includes in WebAssemblyCairoViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
396 NULL);
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
397
1351
1b8e37770d78 ID vs CSS selector distinction in API and field names.
Benjamin Golinvaux <bgo@osimis.io>
parents: 1346
diff changeset
398 emscripten_set_mouseup_callback(canvasCssSelector_.c_str(),
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
399 reinterpret_cast<void*>(this),
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
400 false,
1480
4de884c95cd8 fix includes in WebAssemblyCairoViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
401 NULL);
1331
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
402 }
ab81ee8fce1f - Viewport is not passed and stored as a shared_ptr instead
Benjamin Golinvaux <bgo@osimis.io>
parents: 1328
diff changeset
403 }
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
404
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
405 IViewport::ILock* WebAssemblyViewport::Lock()
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
406 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
407 return new WasmLock(*this);
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
408 }
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
409
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
410 void WebAssemblyViewport::AcquireInteractor(IViewportInteractor* interactor)
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
411 {
1232
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
412 if (interactor == NULL)
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
413 {
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
414 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
415 }
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
416 else
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
417 {
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
418 interactor_.reset(interactor);
a28861abf888 viewports for WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1054
diff changeset
419 }
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
420 }
1576
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
421
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
422
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
423 void WebAssemblyViewport::RefreshCanvasSize()
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
424 {
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
425 double w, h;
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
426 emscripten_get_element_css_size(GetCanvasCssSelector().c_str(), &w, &h);
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
427
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
428 /**
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
429 * Emscripten has the function emscripten_get_element_css_size()
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
430 * to query the width and height of a named HTML element. I'm
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
431 * calling this first to get the initial size of the canvas DOM
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
432 * element, and then call emscripten_set_canvas_size() to
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
433 * initialize the framebuffer size of the canvas to the same
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
434 * size as its DOM element.
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
435 * https://floooh.github.io/2017/02/22/emsc-html.html
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
436 **/
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
437 if (w > 0 &&
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
438 h > 0)
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
439 {
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
440 canvasWidth_ = static_cast<unsigned int>(boost::math::iround(w));
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
441 canvasHeight_ = static_cast<unsigned int>(boost::math::iround(h));
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
442 }
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
443 else
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
444 {
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
445 canvasWidth_ = 0;
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
446 canvasHeight_ = 0;
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
447 }
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
448
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
449 emscripten_set_canvas_element_size(GetCanvasCssSelector().c_str(), canvasWidth_, canvasHeight_);
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
450
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
451 if (compositor_.get() != NULL)
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
452 {
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
453 compositor_->SetCanvasSize(canvasWidth_, canvasHeight_);
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
454 }
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1575
diff changeset
455 }
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456 }