source: xplra/src/plugin/src/xplra/MessageWindow.cc@ 47:411a69092798

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

Added a menu option to reset the message area

File size: 7.0 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//------------------------------------------------------------------------------
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
40using xplra::MessageWindow;
41
42using hu::varadiistvan::xplcommon::Util;
43
44//------------------------------------------------------------------------------
45
46void MessageWindow::drawWindowCallback(XPLMWindowID /*windowID*/, void* refCon)
47{
48 MessageWindow* messageWindow = reinterpret_cast<MessageWindow*>(refCon);
49 messageWindow->drawWindow();
50}
51
52//------------------------------------------------------------------------------
53
54void MessageWindow::handleKeyCallback(XPLMWindowID /*windowID*/, char key,
55 XPLMKeyFlags flags, char virtualKey,
56 void* refCon, int losingFocus)
57{
58 MessageWindow* messageWindow = reinterpret_cast<MessageWindow*>(refCon);
59 messageWindow->handleKey(key, flags, virtualKey, losingFocus!=0);
60}
61
62//------------------------------------------------------------------------------
63
64int MessageWindow::handleMouseClickCallback(XPLMWindowID /*windowID*/,
65 int x, int y,
66 XPLMMouseStatus mouse, void* refCon)
67{
68 MessageWindow* messageWindow = reinterpret_cast<MessageWindow*>(refCon);
69 return messageWindow->handleMouseClick(x, y, mouse) ? 1 : 0;
70}
71
72//------------------------------------------------------------------------------
73
74MessageWindow::MessageWindow() :
75 showForced(false),
76 hideTime(0.0),
77 duration(0.0)
78{
79 widthDataRef = XPLMFindDataRef("sim/graphics/view/window_width");
80 heightDataRef = XPLMFindDataRef("sim/graphics/view/window_height");
81
82 resetLocation();
83
84 windowID = XPLMCreateWindow(left, top, right, bottom, 0,
85 &drawWindowCallback,
86 &handleKeyCallback,
87 &handleMouseClickCallback, this);
88}
89
90//------------------------------------------------------------------------------
91
92MessageWindow::~MessageWindow()
93{
94 XPLMSetWindowIsVisible(windowID, 0);
95 XPLMDestroyWindow(windowID);
96}
97
98//------------------------------------------------------------------------------
99
100void MessageWindow::show()
101{
102 showForced = true;
103 XPLMSetWindowIsVisible(windowID, 1);
104}
105
106//------------------------------------------------------------------------------
107
108void MessageWindow::hide()
109{
110 showForced = false;
111}
112
113//------------------------------------------------------------------------------
114
115void MessageWindow::reset()
116{
117 resetLocation();
118 XPLMSetWindowGeometry(windowID, left, top, right, bottom);
119}
120
121//------------------------------------------------------------------------------
122
123void MessageWindow::drawWindow()
124{
125 static float colour[3] = { 0.2, 1.0, 0.65 };
126
127 float now = XPLMGetElapsedTime();
128
129 float d = getNewMessage(displayedMessage);
130 if (d>0.0) {
131 hideTime = now + d;
132 }
133
134 if (now>=hideTime) hideTime = 0.0;
135
136 if (hideTime==0.0 && !showForced) {
137 XPLMSetWindowIsVisible(windowID, 0);
138 } else {
139 XPLMDrawTranslucentDarkBox(left, top, right, bottom);
140 if (hideTime!=0.0) {
141 XPLMDrawString(colour, left + 25, top - 15,
142 const_cast<char*>(displayedMessage.c_str()), 0,
143 xplmFont_Basic);
144 }
145 }
146}
147
148//------------------------------------------------------------------------------
149
150void MessageWindow::handleKey(char key, XPLMKeyFlags flags, char virtualKey,
151 bool losingFocus)
152{
153 Util::debug("hu.varadiistvan.xplra.MessageWindow.handleKey: key=0x%02d (%c), flags=0x%04x, virtualKey=0x%02d (%c), losingFocus=%d\n",
154 key, key, flags, virtualKey, virtualKey, losingFocus);
155}
156
157//------------------------------------------------------------------------------
158
159bool MessageWindow::handleMouseClick(int x, int y, XPLMMouseStatus mouse)
160{
161 Util::debug("hu.varadiistvan.xplra.MessageWindow.handleMouseClick: x=%d, y=%d, mouse=0x%04x\n",
162 x, y, mouse);
163 return true;
164}
165
166//------------------------------------------------------------------------------
167
168float MessageWindow::getNewMessage(std::string& dest)
169{
170 mutex.lock();
171
172 float d = duration;
173 if (d>0.0) {
174 dest = message;
175
176 message.clear();
177 duration = 0.0;
178 }
179
180 mutex.unlock();
181
182 return d;
183}
184
185//------------------------------------------------------------------------------
186
187void MessageWindow::setNewMessage(const std::string& msg, float d)
188{
189 mutex.lock();
190 message = msg;
191 duration = d;
192 mutex.unlock();
193
194 XPLMSetWindowIsVisible(windowID, 1);
195}
196
197//------------------------------------------------------------------------------
198
199void MessageWindow::resetLocation()
200{
201 int screenWidth = XPLMGetDatai(widthDataRef);
202 int screenHeight = XPLMGetDatai(heightDataRef);
203
204 // Util::debug("hu.varadiistvan.xplra.MessageWindow: screenWidth=%d, screenHeight=%d\n",
205 // screenWidth, screenHeight);
206
207 left = 50; right = screenWidth - left;
208 top = screenHeight * 9 / 10; bottom = top - 25;
209}
210
211//------------------------------------------------------------------------------
212
213// Local Variables:
214// mode: C++
215// c-basic-offset: 4
216// indent-tabs-mode: nil
217// End:
Note: See TracBrowser for help on using the repository browser.