[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 |
|
---|
[51] | 96 | /**
|
---|
| 97 | * The amount of time in seconds after which the message is
|
---|
| 98 | * started to be scrolled, if it should be scrolled.
|
---|
| 99 | */
|
---|
| 100 | static const float scrollTimeout = 1.0;
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * The amount of time in seconds between each character step.
|
---|
| 104 | */
|
---|
| 105 | static const float scrollInterval = 0.1;
|
---|
| 106 |
|
---|
[38] | 107 | private:
|
---|
| 108 | /**
|
---|
| 109 | * The dataref containing the width of the program's window.
|
---|
| 110 | */
|
---|
| 111 | XPLMDataRef widthDataRef;
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
| 114 | * The dataref containing the height of the program's window.
|
---|
| 115 | */
|
---|
| 116 | XPLMDataRef heightDataRef;
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
[50] | 119 | * The width of one character of the font.
|
---|
| 120 | */
|
---|
| 121 | int fontWidth;
|
---|
| 122 |
|
---|
| 123 | /**
|
---|
| 124 | * The height of one character of the font.
|
---|
| 125 | */
|
---|
| 126 | int fontHeight;
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
[38] | 129 | * The window ID.
|
---|
| 130 | */
|
---|
| 131 | XPLMWindowID windowID;
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * The X-coordinate of the left side of the window.
|
---|
| 135 | */
|
---|
| 136 | int left;
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * The Y-coordinate of the top of the window.
|
---|
| 140 | */
|
---|
| 141 | int top;
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * The X-coordinate of the right side of the window.
|
---|
| 145 | */
|
---|
| 146 | int right;
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * The Y-coordinate of the bottom of the window.
|
---|
| 150 | */
|
---|
| 151 | int bottom;
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
[50] | 154 | * The maximal message length.
|
---|
| 155 | */
|
---|
[51] | 156 | size_t maxMessageLength;
|
---|
[50] | 157 |
|
---|
| 158 | /**
|
---|
[42] | 159 | * Indicate if the message window should be shown
|
---|
| 160 | */
|
---|
| 161 | bool showForced;
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
[38] | 164 | * The currently displayed message.
|
---|
| 165 | */
|
---|
| 166 | std::string displayedMessage;
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
[51] | 169 | * The time the current message was shown.
|
---|
| 170 | */
|
---|
| 171 | float showTime;
|
---|
| 172 |
|
---|
| 173 | /**
|
---|
[38] | 174 | * The time to hide the current message. If not positive, no
|
---|
| 175 | * message is currently shown.
|
---|
| 176 | */
|
---|
| 177 | float hideTime;
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
[48] | 180 | * The mouse mode
|
---|
| 181 | */
|
---|
| 182 | enum {
|
---|
| 183 | // Move the message area
|
---|
| 184 | MOUSE_MOVE,
|
---|
| 185 |
|
---|
| 186 | // Resize the message area from the left
|
---|
| 187 | MOUSE_RESIZE_LEFT,
|
---|
| 188 |
|
---|
| 189 | // Resize the message area from the right.
|
---|
| 190 | MOUSE_RESIZE_RIGHT
|
---|
| 191 | } mouseMode;
|
---|
| 192 |
|
---|
| 193 | /**
|
---|
| 194 | * The X-coordinate where the window was clicked.
|
---|
| 195 | */
|
---|
| 196 | int clickedX;
|
---|
| 197 |
|
---|
| 198 | /**
|
---|
| 199 | * The Y-coordinate where the window was clicked.
|
---|
| 200 | */
|
---|
| 201 | int clickedY;
|
---|
| 202 |
|
---|
| 203 | /**
|
---|
[49] | 204 | * The left coordinate of the window when it was clicked.
|
---|
| 205 | */
|
---|
| 206 | int clickedLeft;
|
---|
| 207 |
|
---|
| 208 | /**
|
---|
| 209 | * The top coordinate of the window when it was clicked.
|
---|
| 210 | */
|
---|
| 211 | int clickedTop;
|
---|
| 212 |
|
---|
| 213 | /**
|
---|
| 214 | * The right coordinate of the window when it was clicked.
|
---|
| 215 | */
|
---|
| 216 | int clickedRight;
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
| 219 | * The bottom coordinate of the window when it was clicked.
|
---|
| 220 | */
|
---|
| 221 | int clickedBottom;
|
---|
| 222 |
|
---|
| 223 | /**
|
---|
[38] | 224 | * The mutex protecting some of the data structures that are used
|
---|
| 225 | * from both the simulator loop and the server thread.
|
---|
| 226 | */
|
---|
| 227 | hu::varadiistvan::scpl::Mutex mutex;
|
---|
| 228 |
|
---|
| 229 | /**
|
---|
| 230 | * The message to show.
|
---|
| 231 | */
|
---|
| 232 | std::string message;
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * The number of seconds for which the message should be shown. If
|
---|
| 236 | * positive, there is a new message.
|
---|
| 237 | */
|
---|
| 238 | float duration;
|
---|
| 239 |
|
---|
| 240 | public:
|
---|
| 241 | /**
|
---|
| 242 | * Construct the message window. It creates the X-Plane window and
|
---|
| 243 | * registers the callback functions. The window is hidden
|
---|
| 244 | * initially.
|
---|
| 245 | */
|
---|
| 246 | MessageWindow();
|
---|
| 247 |
|
---|
| 248 | /**
|
---|
| 249 | * Destroy the message window. It destroys the X-Plane window.
|
---|
| 250 | */
|
---|
| 251 | ~MessageWindow();
|
---|
| 252 |
|
---|
[42] | 253 | /**
|
---|
| 254 | * Force showing the message window.
|
---|
| 255 | */
|
---|
| 256 | void show();
|
---|
| 257 |
|
---|
| 258 | /**
|
---|
| 259 | * Hide the message window if it does not have to display an
|
---|
| 260 | * actual message.
|
---|
| 261 | */
|
---|
| 262 | void hide();
|
---|
| 263 |
|
---|
[47] | 264 | /**
|
---|
| 265 | * Reset the location of the message window.
|
---|
| 266 | */
|
---|
| 267 | void reset();
|
---|
| 268 |
|
---|
[38] | 269 | private:
|
---|
| 270 | /**
|
---|
| 271 | * Called from the window drawing callback.
|
---|
| 272 | */
|
---|
| 273 | void drawWindow();
|
---|
| 274 |
|
---|
| 275 | /**
|
---|
| 276 | * Called from the key handling callback.
|
---|
| 277 | */
|
---|
| 278 | void handleKey(char key, XPLMKeyFlags flags, char virtualKey,
|
---|
| 279 | bool losingFocus);
|
---|
| 280 |
|
---|
| 281 | /**
|
---|
| 282 | * Called from the mouse click handling callback.
|
---|
| 283 | *
|
---|
| 284 | * @return true if the click is consumed, false otherwise.
|
---|
| 285 | */
|
---|
| 286 | bool handleMouseClick(int x, int y, XPLMMouseStatus mouse);
|
---|
| 287 |
|
---|
| 288 | /**
|
---|
| 289 | * Get the new message, if any.
|
---|
| 290 | *
|
---|
| 291 | * @return the duration of the message, or 0.0 if there is no new
|
---|
| 292 | * message. The message will be cleared.
|
---|
| 293 | */
|
---|
| 294 | float getNewMessage(std::string& dest);
|
---|
| 295 |
|
---|
| 296 | /**
|
---|
| 297 | * Set the new message, if any.
|
---|
| 298 | */
|
---|
| 299 | void setNewMessage(const std::string& msg, float d);
|
---|
| 300 |
|
---|
[47] | 301 | /**
|
---|
| 302 | * Reset the location of the message window.
|
---|
| 303 | */
|
---|
| 304 | void resetLocation();
|
---|
| 305 |
|
---|
[50] | 306 | /**
|
---|
| 307 | * Recalculate the maximal message length.
|
---|
| 308 | */
|
---|
| 309 | void recalculateMaxMessageLength();
|
---|
| 310 |
|
---|
[38] | 311 | friend class MessageRequest;
|
---|
| 312 | };
|
---|
| 313 |
|
---|
| 314 | //------------------------------------------------------------------------------
|
---|
| 315 |
|
---|
| 316 | } /* namespace xplra */
|
---|
| 317 |
|
---|
| 318 | //------------------------------------------------------------------------------
|
---|
| 319 | #endif // XPLRA_MESSAGEWINDOW_H
|
---|
| 320 |
|
---|
| 321 | // Local Variables:
|
---|
| 322 | // mode: C++
|
---|
| 323 | // c-basic-offset: 4
|
---|
| 324 | // indent-tabs-mode: nil
|
---|
| 325 | // End:
|
---|