source: xplra/src/plugin/src/xplra/MessageWindow.h@ 49:2f52c296803c

Last change on this file since 49:2f52c296803c was 49:2f52c296803c, checked in by István Váradi <ivaradi@…>, 11 years ago

Made the moving and resizing better looking

File size: 6.8 KB
Line 
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
42namespace xplra {
43
44//------------------------------------------------------------------------------
45
46/**
47 * Class to handle the activities related to the message window.
48 */
49class MessageWindow
50{
51private:
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
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
81private:
82 /**
83 * The dataref containing the width of the program's window.
84 */
85 XPLMDataRef widthDataRef;
86
87 /**
88 * The dataref containing the height of the program's window.
89 */
90 XPLMDataRef heightDataRef;
91
92 /**
93 * The window ID.
94 */
95 XPLMWindowID windowID;
96
97 /**
98 * The X-coordinate of the left side of the window.
99 */
100 int left;
101
102 /**
103 * The Y-coordinate of the top of the window.
104 */
105 int top;
106
107 /**
108 * The X-coordinate of the right side of the window.
109 */
110 int right;
111
112 /**
113 * The Y-coordinate of the bottom of the window.
114 */
115 int bottom;
116
117 /**
118 * Indicate if the message window should be shown
119 */
120 bool showForced;
121
122 /**
123 * The currently displayed message.
124 */
125 std::string displayedMessage;
126
127 /**
128 * The time to hide the current message. If not positive, no
129 * message is currently shown.
130 */
131 float hideTime;
132
133 /**
134 * The mouse mode
135 */
136 enum {
137 // Move the message area
138 MOUSE_MOVE,
139
140 // Resize the message area from the left
141 MOUSE_RESIZE_LEFT,
142
143 // Resize the message area from the right.
144 MOUSE_RESIZE_RIGHT
145 } mouseMode;
146
147 /**
148 * The X-coordinate where the window was clicked.
149 */
150 int clickedX;
151
152 /**
153 * The Y-coordinate where the window was clicked.
154 */
155 int clickedY;
156
157 /**
158 * The left coordinate of the window when it was clicked.
159 */
160 int clickedLeft;
161
162 /**
163 * The top coordinate of the window when it was clicked.
164 */
165 int clickedTop;
166
167 /**
168 * The right coordinate of the window when it was clicked.
169 */
170 int clickedRight;
171
172 /**
173 * The bottom coordinate of the window when it was clicked.
174 */
175 int clickedBottom;
176
177 /**
178 * The mutex protecting some of the data structures that are used
179 * from both the simulator loop and the server thread.
180 */
181 hu::varadiistvan::scpl::Mutex mutex;
182
183 /**
184 * The message to show.
185 */
186 std::string message;
187
188 /**
189 * The number of seconds for which the message should be shown. If
190 * positive, there is a new message.
191 */
192 float duration;
193
194public:
195 /**
196 * Construct the message window. It creates the X-Plane window and
197 * registers the callback functions. The window is hidden
198 * initially.
199 */
200 MessageWindow();
201
202 /**
203 * Destroy the message window. It destroys the X-Plane window.
204 */
205 ~MessageWindow();
206
207 /**
208 * Force showing the message window.
209 */
210 void show();
211
212 /**
213 * Hide the message window if it does not have to display an
214 * actual message.
215 */
216 void hide();
217
218 /**
219 * Reset the location of the message window.
220 */
221 void reset();
222
223private:
224 /**
225 * Called from the window drawing callback.
226 */
227 void drawWindow();
228
229 /**
230 * Called from the key handling callback.
231 */
232 void handleKey(char key, XPLMKeyFlags flags, char virtualKey,
233 bool losingFocus);
234
235 /**
236 * Called from the mouse click handling callback.
237 *
238 * @return true if the click is consumed, false otherwise.
239 */
240 bool handleMouseClick(int x, int y, XPLMMouseStatus mouse);
241
242 /**
243 * Get the new message, if any.
244 *
245 * @return the duration of the message, or 0.0 if there is no new
246 * message. The message will be cleared.
247 */
248 float getNewMessage(std::string& dest);
249
250 /**
251 * Set the new message, if any.
252 */
253 void setNewMessage(const std::string& msg, float d);
254
255 /**
256 * Reset the location of the message window.
257 */
258 void resetLocation();
259
260 friend class MessageRequest;
261};
262
263//------------------------------------------------------------------------------
264
265} /* namespace xplra */
266
267//------------------------------------------------------------------------------
268#endif // XPLRA_MESSAGEWINDOW_H
269
270// Local Variables:
271// mode: C++
272// c-basic-offset: 4
273// indent-tabs-mode: nil
274// End:
Note: See TracBrowser for help on using the repository browser.