1 | // Copyright (c) 2013 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of XPLRA, a remote-access plugin for X-Plane
|
---|
4 |
|
---|
5 | // Redistribution and use in source and binary forms, with or without
|
---|
6 | // modification, are permitted provided that the following conditions are met:
|
---|
7 |
|
---|
8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
9 | // list of conditions and the following disclaimer.
|
---|
10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | // this list of conditions and the following disclaimer in the documentation
|
---|
12 | // and/or other materials provided with the distribution.
|
---|
13 |
|
---|
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 |
|
---|
25 | // The views and conclusions contained in the software and documentation are those
|
---|
26 | // of the authors and should not be interpreted as representing official policies,
|
---|
27 | // either expressed or implied, of the FreeBSD Project.
|
---|
28 |
|
---|
29 | //------------------------------------------------------------------------------
|
---|
30 |
|
---|
31 | #include "MessageWindow.h"
|
---|
32 |
|
---|
33 | #include <hu/varadiistvan/xplcommon/Util.h>
|
---|
34 |
|
---|
35 | #include <XPLMProcessing.h>
|
---|
36 | #include <XPLMGraphics.h>
|
---|
37 |
|
---|
38 | //------------------------------------------------------------------------------
|
---|
39 |
|
---|
40 | using xplra::MessageWindow;
|
---|
41 |
|
---|
42 | using hu::varadiistvan::xplcommon::Util;
|
---|
43 |
|
---|
44 | using std::min;
|
---|
45 | using std::max;
|
---|
46 |
|
---|
47 | //------------------------------------------------------------------------------
|
---|
48 |
|
---|
49 | void MessageWindow::drawWindowCallback(XPLMWindowID /*windowID*/, void* refCon)
|
---|
50 | {
|
---|
51 | MessageWindow* messageWindow = reinterpret_cast<MessageWindow*>(refCon);
|
---|
52 | messageWindow->drawWindow();
|
---|
53 | }
|
---|
54 |
|
---|
55 | //------------------------------------------------------------------------------
|
---|
56 |
|
---|
57 | void MessageWindow::handleKeyCallback(XPLMWindowID /*windowID*/, char key,
|
---|
58 | XPLMKeyFlags flags, char virtualKey,
|
---|
59 | void* refCon, int losingFocus)
|
---|
60 | {
|
---|
61 | MessageWindow* messageWindow = reinterpret_cast<MessageWindow*>(refCon);
|
---|
62 | messageWindow->handleKey(key, flags, virtualKey, losingFocus!=0);
|
---|
63 | }
|
---|
64 |
|
---|
65 | //------------------------------------------------------------------------------
|
---|
66 |
|
---|
67 | int MessageWindow::handleMouseClickCallback(XPLMWindowID /*windowID*/,
|
---|
68 | int x, int y,
|
---|
69 | XPLMMouseStatus mouse, void* refCon)
|
---|
70 | {
|
---|
71 | MessageWindow* messageWindow = reinterpret_cast<MessageWindow*>(refCon);
|
---|
72 | return messageWindow->handleMouseClick(x, y, mouse) ? 1 : 0;
|
---|
73 | }
|
---|
74 |
|
---|
75 | //------------------------------------------------------------------------------
|
---|
76 |
|
---|
77 | MessageWindow::MessageWindow() :
|
---|
78 | showForced(false),
|
---|
79 | hideTime(0.0),
|
---|
80 | duration(0.0)
|
---|
81 | {
|
---|
82 | widthDataRef = XPLMFindDataRef("sim/graphics/view/window_width");
|
---|
83 | heightDataRef = XPLMFindDataRef("sim/graphics/view/window_height");
|
---|
84 |
|
---|
85 | resetLocation();
|
---|
86 |
|
---|
87 | windowID = XPLMCreateWindow(left, top, right, bottom, 0,
|
---|
88 | &drawWindowCallback,
|
---|
89 | &handleKeyCallback,
|
---|
90 | &handleMouseClickCallback, this);
|
---|
91 | }
|
---|
92 |
|
---|
93 | //------------------------------------------------------------------------------
|
---|
94 |
|
---|
95 | MessageWindow::~MessageWindow()
|
---|
96 | {
|
---|
97 | XPLMSetWindowIsVisible(windowID, 0);
|
---|
98 | XPLMDestroyWindow(windowID);
|
---|
99 | }
|
---|
100 |
|
---|
101 | //------------------------------------------------------------------------------
|
---|
102 |
|
---|
103 | void MessageWindow::show()
|
---|
104 | {
|
---|
105 | showForced = true;
|
---|
106 | XPLMSetWindowIsVisible(windowID, 1);
|
---|
107 | }
|
---|
108 |
|
---|
109 | //------------------------------------------------------------------------------
|
---|
110 |
|
---|
111 | void MessageWindow::hide()
|
---|
112 | {
|
---|
113 | showForced = false;
|
---|
114 | }
|
---|
115 |
|
---|
116 | //------------------------------------------------------------------------------
|
---|
117 |
|
---|
118 | void MessageWindow::reset()
|
---|
119 | {
|
---|
120 | resetLocation();
|
---|
121 | XPLMSetWindowGeometry(windowID, left, top, right, bottom);
|
---|
122 | }
|
---|
123 |
|
---|
124 | //------------------------------------------------------------------------------
|
---|
125 |
|
---|
126 | void MessageWindow::drawWindow()
|
---|
127 | {
|
---|
128 | static float colour[3] = { 0.2, 1.0, 0.65 };
|
---|
129 |
|
---|
130 | float now = XPLMGetElapsedTime();
|
---|
131 |
|
---|
132 | float d = getNewMessage(displayedMessage);
|
---|
133 | if (d>0.0) {
|
---|
134 | hideTime = now + d;
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (now>=hideTime) hideTime = 0.0;
|
---|
138 |
|
---|
139 | if (hideTime==0.0 && !showForced) {
|
---|
140 | XPLMSetWindowIsVisible(windowID, 0);
|
---|
141 | } else {
|
---|
142 | XPLMDrawTranslucentDarkBox(left, top, right, bottom);
|
---|
143 | if (hideTime!=0.0) {
|
---|
144 | XPLMDrawString(colour, left + 25, top - 15,
|
---|
145 | const_cast<char*>(displayedMessage.c_str()), 0,
|
---|
146 | xplmFont_Basic);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | //------------------------------------------------------------------------------
|
---|
152 |
|
---|
153 | void MessageWindow::handleKey(char key, XPLMKeyFlags flags, char virtualKey,
|
---|
154 | bool losingFocus)
|
---|
155 | {
|
---|
156 | Util::debug("hu.varadiistvan.xplra.MessageWindow.handleKey: key=0x%02d (%c), flags=0x%04x, virtualKey=0x%02d (%c), losingFocus=%d\n",
|
---|
157 | key, key, flags, virtualKey, virtualKey, losingFocus);
|
---|
158 | }
|
---|
159 |
|
---|
160 | //------------------------------------------------------------------------------
|
---|
161 |
|
---|
162 | bool MessageWindow::handleMouseClick(int x, int y, XPLMMouseStatus mouse)
|
---|
163 | {
|
---|
164 | if (mouse==xplm_MouseDown) {
|
---|
165 | if (x <= (left + resizeArea) ) {
|
---|
166 | mouseMode = MOUSE_RESIZE_LEFT;
|
---|
167 | } else if (x > (right-resizeArea) ) {
|
---|
168 | mouseMode = MOUSE_RESIZE_RIGHT;
|
---|
169 | } else {
|
---|
170 | mouseMode = MOUSE_MOVE;
|
---|
171 | }
|
---|
172 | clickedX = x;
|
---|
173 | clickedY = y;
|
---|
174 | } else if (mouse==xplm_MouseDrag) {
|
---|
175 | int xDiff = x - clickedX;
|
---|
176 |
|
---|
177 | if (mouseMode==MOUSE_MOVE) {
|
---|
178 | int newLeft = left + xDiff;
|
---|
179 | int newRight = right + xDiff;
|
---|
180 |
|
---|
181 | int screenWidth = XPLMGetDatai(widthDataRef);
|
---|
182 | if (newLeft>=0 && newRight<screenWidth) {
|
---|
183 | left = newLeft;
|
---|
184 | right = newRight;
|
---|
185 | }
|
---|
186 |
|
---|
187 | int yDiff = y - clickedY;
|
---|
188 | int newTop = top + yDiff;
|
---|
189 | int newBottom = bottom + yDiff;
|
---|
190 |
|
---|
191 | int screenHeight = XPLMGetDatai(heightDataRef);
|
---|
192 | if (newTop<screenHeight && newBottom>=0) {
|
---|
193 | top = newTop;
|
---|
194 | bottom = newBottom;
|
---|
195 | }
|
---|
196 | } else if (mouseMode==MOUSE_RESIZE_LEFT) {
|
---|
197 | int newLeft = left + xDiff;
|
---|
198 | if (newLeft>=0 && (right-newLeft)>=minimalWidth) {
|
---|
199 | left = newLeft;
|
---|
200 | }
|
---|
201 | } else if (mouseMode==MOUSE_RESIZE_RIGHT) {
|
---|
202 | int newRight = right + xDiff;
|
---|
203 | int screenWidth = XPLMGetDatai(widthDataRef);
|
---|
204 | if (newRight<screenWidth && (newRight-left)>=minimalWidth) {
|
---|
205 | right = newRight;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | XPLMSetWindowGeometry(windowID, left, top, right, bottom);
|
---|
210 |
|
---|
211 | clickedX = x;
|
---|
212 | clickedY = y;
|
---|
213 | }
|
---|
214 |
|
---|
215 | return true;
|
---|
216 | }
|
---|
217 |
|
---|
218 | //------------------------------------------------------------------------------
|
---|
219 |
|
---|
220 | float MessageWindow::getNewMessage(std::string& dest)
|
---|
221 | {
|
---|
222 | mutex.lock();
|
---|
223 |
|
---|
224 | float d = duration;
|
---|
225 | if (d>0.0) {
|
---|
226 | dest = message;
|
---|
227 |
|
---|
228 | message.clear();
|
---|
229 | duration = 0.0;
|
---|
230 | }
|
---|
231 |
|
---|
232 | mutex.unlock();
|
---|
233 |
|
---|
234 | return d;
|
---|
235 | }
|
---|
236 |
|
---|
237 | //------------------------------------------------------------------------------
|
---|
238 |
|
---|
239 | void MessageWindow::setNewMessage(const std::string& msg, float d)
|
---|
240 | {
|
---|
241 | mutex.lock();
|
---|
242 | message = msg;
|
---|
243 | duration = d;
|
---|
244 | mutex.unlock();
|
---|
245 |
|
---|
246 | XPLMSetWindowIsVisible(windowID, 1);
|
---|
247 | }
|
---|
248 |
|
---|
249 | //------------------------------------------------------------------------------
|
---|
250 |
|
---|
251 | void MessageWindow::resetLocation()
|
---|
252 | {
|
---|
253 | int screenWidth = XPLMGetDatai(widthDataRef);
|
---|
254 | int screenHeight = XPLMGetDatai(heightDataRef);
|
---|
255 |
|
---|
256 | // Util::debug("hu.varadiistvan.xplra.MessageWindow: screenWidth=%d, screenHeight=%d\n",
|
---|
257 | // screenWidth, screenHeight);
|
---|
258 |
|
---|
259 | left = 50; right = screenWidth - left;
|
---|
260 | top = screenHeight * 9 / 10; bottom = top - 25;
|
---|
261 | }
|
---|
262 |
|
---|
263 | //------------------------------------------------------------------------------
|
---|
264 |
|
---|
265 | // Local Variables:
|
---|
266 | // mode: C++
|
---|
267 | // c-basic-offset: 4
|
---|
268 | // indent-tabs-mode: nil
|
---|
269 | // End:
|
---|