comparison Applications/Samples/Sdl/SdlHelpers.h @ 1538:d1806b4e4839

moving OrthancStone/Samples/ as Applications/Samples/
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Aug 2020 13:24:38 +0200
parents OrthancStone/Samples/Sdl/SdlHelpers.h@244ad1e4e76a
children 6e0da8370270
comparison
equal deleted inserted replaced
1537:de8cf5859e84 1538:d1806b4e4839
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #pragma once
23
24 #if ORTHANC_ENABLE_SDL != 1
25 # error This file cannot be used if ORTHANC_ENABLE_SDL != 1
26 #endif
27
28 #include "../../Sources/Viewport/SdlViewport.h"
29
30 #include <boost/shared_ptr.hpp>
31
32 #include <SDL.h>
33
34 #include <map>
35 #include <string>
36
37 namespace OrthancStoneHelpers
38 {
39
40 inline OrthancStone::KeyboardModifiers GetKeyboardModifiers(const uint8_t* keyboardState,
41 const int scancodeCount)
42 {
43 using namespace OrthancStone;
44 int result = KeyboardModifiers_None;
45
46 if (keyboardState != NULL)
47 {
48 if (SDL_SCANCODE_LSHIFT < scancodeCount &&
49 keyboardState[SDL_SCANCODE_LSHIFT])
50 {
51 result |= KeyboardModifiers_Shift;
52 }
53
54 if (SDL_SCANCODE_RSHIFT < scancodeCount &&
55 keyboardState[SDL_SCANCODE_RSHIFT])
56 {
57 result |= KeyboardModifiers_Shift;
58 }
59
60 if (SDL_SCANCODE_LCTRL < scancodeCount &&
61 keyboardState[SDL_SCANCODE_LCTRL])
62 {
63 result |= KeyboardModifiers_Control;
64 }
65
66 if (SDL_SCANCODE_RCTRL < scancodeCount &&
67 keyboardState[SDL_SCANCODE_RCTRL])
68 {
69 result |= KeyboardModifiers_Control;
70 }
71
72 if (SDL_SCANCODE_LALT < scancodeCount &&
73 keyboardState[SDL_SCANCODE_LALT])
74 {
75 result |= KeyboardModifiers_Alt;
76 }
77
78 if (SDL_SCANCODE_RALT < scancodeCount &&
79 keyboardState[SDL_SCANCODE_RALT])
80 {
81 result |= KeyboardModifiers_Alt;
82 }
83 }
84
85 return static_cast<KeyboardModifiers>(result);
86 }
87
88
89 inline void GetPointerEvent(OrthancStone::PointerEvent& p,
90 const OrthancStone::ICompositor& compositor,
91 SDL_Event event,
92 const uint8_t* keyboardState,
93 const int scancodeCount)
94 {
95 using namespace OrthancStone;
96 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount);
97
98 switch (event.button.button)
99 {
100 case SDL_BUTTON_LEFT:
101 p.SetMouseButton(OrthancStone::MouseButton_Left);
102 break;
103
104 case SDL_BUTTON_RIGHT:
105 p.SetMouseButton(OrthancStone::MouseButton_Right);
106 break;
107
108 case SDL_BUTTON_MIDDLE:
109 p.SetMouseButton(OrthancStone::MouseButton_Middle);
110 break;
111
112 default:
113 p.SetMouseButton(OrthancStone::MouseButton_None);
114 break;
115 }
116
117 p.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
118 p.SetAltModifier( (modifiers & KeyboardModifiers_Alt) != 0);
119 p.SetControlModifier( (modifiers & KeyboardModifiers_Control) != 0);
120 p.SetShiftModifier( (modifiers & KeyboardModifiers_Shift) != 0);
121 }
122
123
124 inline boost::shared_ptr<OrthancStone::SdlViewport> GetSdlViewportFromWindowId(
125 const std::vector<boost::shared_ptr<OrthancStone::SdlViewport> >& viewports,
126 Uint32 windowID)
127 {
128 using namespace OrthancStone;
129 for (size_t i = 0; i < viewports.size(); ++i)
130 {
131 boost::shared_ptr<IViewport> viewport = viewports[i];
132 boost::shared_ptr<SdlViewport> sdlViewport = boost::dynamic_pointer_cast<SdlViewport>(viewport);
133 Uint32 curWindowID = sdlViewport->GetSdlWindowId();
134 if (windowID == curWindowID)
135 return sdlViewport;
136 }
137
138 return boost::shared_ptr<OrthancStone::SdlViewport>();
139 }
140 }
141
142