[38] | 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 | #ifndef XPLRA_MESSAGEWINDOW_H
|
---|
| 30 | #define XPLRA_MESSAGEWINDOW_H
|
---|
| 31 | //------------------------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | #include <hu/varadiistvan/scpl/Mutex.h>
|
---|
| 34 |
|
---|
| 35 | #include <string>
|
---|
| 36 |
|
---|
| 37 | #include <XPLMDataAccess.h>
|
---|
| 38 | #include <XPLMDisplay.h>
|
---|
| 39 |
|
---|
| 40 | //------------------------------------------------------------------------------
|
---|
| 41 |
|
---|
| 42 | namespace xplra {
|
---|
| 43 |
|
---|
| 44 | //------------------------------------------------------------------------------
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Class to handle the activities related to the message window.
|
---|
| 48 | */
|
---|
| 49 | class MessageWindow
|
---|
| 50 | {
|
---|
| 51 | private:
|
---|
| 52 | /**
|
---|
| 53 | * Window drawing callback.
|
---|
| 54 | */
|
---|
| 55 | static void drawWindowCallback(XPLMWindowID windowID, void* refCon);
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * Handle the keypresses.
|
---|
| 59 | */
|
---|
| 60 | static void handleKeyCallback(XPLMWindowID windowID, char key,
|
---|
| 61 | XPLMKeyFlags flags, char virtualKey,
|
---|
| 62 | void* refCon, int losingFocus);
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * Handle the mouse clicks.
|
---|
| 66 | */
|
---|
| 67 | static int handleMouseClickCallback(XPLMWindowID windowID, int x, int y,
|
---|
| 68 | XPLMMouseStatus mouse, void* refCon);
|
---|
| 69 |
|
---|
[48] | 70 |
|
---|
| 71 | /**
|
---|
| 72 | * The minimal width of the message area.
|
---|
| 73 | */
|
---|
| 74 | static const int minimalWidth = 150;
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * The size of the resize sensitivity area.
|
---|
| 78 | */
|
---|
| 79 | static const int resizeArea = 50;
|
---|
| 80 |
|
---|
[50] | 81 | /**
|
---|
| 82 | * The size of the margins at each side of the window.
|
---|
| 83 | */
|
---|
| 84 | static const int horizontalMarginSize = 25;
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * The size of the margins at the top and the bottom of the window.
|
---|
| 88 | */
|
---|
| 89 | static const int verticalMarginSize = 5;
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * The size for the baseline above the bottom of the letters.
|
---|
| 93 | */
|
---|
| 94 | static const int baseLineSize = 2;
|
---|
| 95 |
|
---|
[38] | 96 | private:
|
---|
| 97 | /**
|
---|
| 98 | * The dataref containing the width of the program's window.
|
---|
| 99 | */
|
---|
| 100 | XPLMDataRef widthDataRef;
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * The dataref containing the height of the program's window.
|
---|
| 104 | */
|
---|
| 105 | XPLMDataRef heightDataRef;
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
[50] | 108 | * The width of one character of the font.
|
---|
| 109 | */
|
---|
| 110 | int fontWidth;
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
| 113 | * The height of one character of the font.
|
---|
| 114 | */
|
---|
| 115 | int fontHeight;
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
[38] | 118 | * The window ID.
|
---|
| 119 | */
|
---|
| 120 | XPLMWindowID windowID;
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
| 123 | * The X-coordinate of the left side of the window.
|
---|
| 124 | */
|
---|
| 125 | int left;
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * The Y-coordinate of the top of the window.
|
---|
| 129 | */
|
---|
| 130 | int top;
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * The X-coordinate of the right side of the window.
|
---|
| 134 | */
|
---|
| 135 | int right;
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * The Y-coordinate of the bottom of the window.
|
---|
| 139 | */
|
---|
| 140 | int bottom;
|
---|
| 141 |
|
---|
| 142 | /**
|
---|
[50] | 143 | * The maximal message length.
|
---|
| 144 | */
|
---|
| 145 | int maxMessageLength;
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
[42] | 148 | * Indicate if the message window should be shown
|
---|
| 149 | */
|
---|
| 150 | bool showForced;
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
[38] | 153 | * The currently displayed message.
|
---|
| 154 | */
|
---|
| 155 | std::string displayedMessage;
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * The time to hide the current message. If not positive, no
|
---|
| 159 | * message is currently shown.
|
---|
| 160 | */
|
---|
| 161 | float hideTime;
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
[48] | 164 | * The mouse mode
|
---|
| 165 | */
|
---|
| 166 | enum {
|
---|
| 167 | // Move the message area
|
---|
| 168 | MOUSE_MOVE,
|
---|
| 169 |
|
---|
| 170 | // Resize the message area from the left
|
---|
| 171 | MOUSE_RESIZE_LEFT,
|
---|
| 172 |
|
---|
| 173 | // Resize the message area from the right.
|
---|
| 174 | MOUSE_RESIZE_RIGHT
|
---|
| 175 | } mouseMode;
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
| 178 | * The X-coordinate where the window was clicked.
|
---|
| 179 | */
|
---|
| 180 | int clickedX;
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
| 183 | * The Y-coordinate where the window was clicked.
|
---|
| 184 | */
|
---|
| 185 | int clickedY;
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
[49] | 188 | * The left coordinate of the window when it was clicked.
|
---|
| 189 | */
|
---|
| 190 | int clickedLeft;
|
---|
| 191 |
|
---|
| 192 | /**
|
---|
| 193 | * The top coordinate of the window when it was clicked.
|
---|
| 194 | */
|
---|
| 195 | int clickedTop;
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
| 198 | * The right coordinate of the window when it was clicked.
|
---|
| 199 | */
|
---|
| 200 | int clickedRight;
|
---|
| 201 |
|
---|
| 202 | /**
|
---|
| 203 | * The bottom coordinate of the window when it was clicked.
|
---|
| 204 | */
|
---|
| 205 | int clickedBottom;
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
[38] | 208 | * The mutex protecting some of the data structures that are used
|
---|
| 209 | * from both the simulator loop and the server thread.
|
---|
| 210 | */
|
---|
| 211 | hu::varadiistvan::scpl::Mutex mutex;
|
---|
| 212 |
|
---|
| 213 | /**
|
---|
| 214 | * The message to show.
|
---|
| 215 | */
|
---|
| 216 | std::string message;
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
| 219 | * The number of seconds for which the message should be shown. If
|
---|
| 220 | * positive, there is a new message.
|
---|
| 221 | */
|
---|
| 222 | float duration;
|
---|
| 223 |
|
---|
| 224 | public:
|
---|
| 225 | /**
|
---|
| 226 | * Construct the message window. It creates the X-Plane window and
|
---|
| 227 | * registers the callback functions. The window is hidden
|
---|
| 228 | * initially.
|
---|
| 229 | */
|
---|
| 230 | MessageWindow();
|
---|
| 231 |
|
---|
| 232 | /**
|
---|
| 233 | * Destroy the message window. It destroys the X-Plane window.
|
---|
| 234 | */
|
---|
| 235 | ~MessageWindow();
|
---|
| 236 |
|
---|
[42] | 237 | /**
|
---|
| 238 | * Force showing the message window.
|
---|
| 239 | */
|
---|
| 240 | void show();
|
---|
| 241 |
|
---|
| 242 | /**
|
---|
| 243 | * Hide the message window if it does not have to display an
|
---|
| 244 | * actual message.
|
---|
| 245 | */
|
---|
| 246 | void hide();
|
---|
| 247 |
|
---|
[47] | 248 | /**
|
---|
| 249 | * Reset the location of the message window.
|
---|
| 250 | */
|
---|
| 251 | void reset();
|
---|
| 252 |
|
---|
[38] | 253 | private:
|
---|
| 254 | /**
|
---|
| 255 | * Called from the window drawing callback.
|
---|
| 256 | */
|
---|
| 257 | void drawWindow();
|
---|
| 258 |
|
---|
| 259 | /**
|
---|
| 260 | * Called from the key handling callback.
|
---|
| 261 | */
|
---|
| 262 | void handleKey(char key, XPLMKeyFlags flags, char virtualKey,
|
---|
| 263 | bool losingFocus);
|
---|
| 264 |
|
---|
| 265 | /**
|
---|
| 266 | * Called from the mouse click handling callback.
|
---|
| 267 | *
|
---|
| 268 | * @return true if the click is consumed, false otherwise.
|
---|
| 269 | */
|
---|
| 270 | bool handleMouseClick(int x, int y, XPLMMouseStatus mouse);
|
---|
| 271 |
|
---|
| 272 | /**
|
---|
| 273 | * Get the new message, if any.
|
---|
| 274 | *
|
---|
| 275 | * @return the duration of the message, or 0.0 if there is no new
|
---|
| 276 | * message. The message will be cleared.
|
---|
| 277 | */
|
---|
| 278 | float getNewMessage(std::string& dest);
|
---|
| 279 |
|
---|
| 280 | /**
|
---|
| 281 | * Set the new message, if any.
|
---|
| 282 | */
|
---|
| 283 | void setNewMessage(const std::string& msg, float d);
|
---|
| 284 |
|
---|
[47] | 285 | /**
|
---|
| 286 | * Reset the location of the message window.
|
---|
| 287 | */
|
---|
| 288 | void resetLocation();
|
---|
| 289 |
|
---|
[50] | 290 | /**
|
---|
| 291 | * Recalculate the maximal message length.
|
---|
| 292 | */
|
---|
| 293 | void recalculateMaxMessageLength();
|
---|
| 294 |
|
---|
[38] | 295 | friend class MessageRequest;
|
---|
| 296 | };
|
---|
| 297 |
|
---|
| 298 | //------------------------------------------------------------------------------
|
---|
| 299 |
|
---|
| 300 | } /* namespace xplra */
|
---|
| 301 |
|
---|
| 302 | //------------------------------------------------------------------------------
|
---|
| 303 | #endif // XPLRA_MESSAGEWINDOW_H
|
---|
| 304 |
|
---|
| 305 | // Local Variables:
|
---|
| 306 | // mode: C++
|
---|
| 307 | // c-basic-offset: 4
|
---|
| 308 | // indent-tabs-mode: nil
|
---|
| 309 | // End:
|
---|