source: xplra/src/plugin/src/xplra/MessageWindow.cc@ 42:719f590e4878

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

Added initial support for the menu

File size: 6.6 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 int screenWidth = XPLMGetDatai(widthDataRef);
83 int screenHeight = XPLMGetDatai(heightDataRef);
84
85 Util::debug("hu.varadiistvan.xplra.MessageWindow: screenWidth=%d, screenHeight=%d\n",
86 screenWidth, screenHeight);
87
88 left = 50; right = screenWidth - left;
89 top = screenHeight * 9 / 10; bottom = top - 25;
90
91 windowID = XPLMCreateWindow(left, top, right, bottom, 0,
92 &drawWindowCallback,
93 &handleKeyCallback,
94 &handleMouseClickCallback, this);
95}
96
97//------------------------------------------------------------------------------
98
99MessageWindow::~MessageWindow()
100{
101 XPLMSetWindowIsVisible(windowID, 0);
102 XPLMDestroyWindow(windowID);
103}
104
105//------------------------------------------------------------------------------
106
107void MessageWindow::show()
108{
109 showForced = true;
110 XPLMSetWindowIsVisible(windowID, 1);
111}
112
113//------------------------------------------------------------------------------
114
115void MessageWindow::hide()
116{
117 showForced = false;
118}
119
120//------------------------------------------------------------------------------
121
122void MessageWindow::drawWindow()
123{
124 static float colour[3] = { 0.2, 1.0, 0.65 };
125
126 float now = XPLMGetElapsedTime();
127
128 float d = getNewMessage(displayedMessage);
129 if (d>0.0) {
130 hideTime = now + d;
131 }
132
133 if (now>=hideTime) hideTime = 0.0;
134
135 if (hideTime==0.0 && !showForced) {
136 XPLMSetWindowIsVisible(windowID, 0);
137 } else {
138 XPLMDrawTranslucentDarkBox(left, top, right, bottom);
139 if (hideTime!=0.0) {
140 XPLMDrawString(colour, left + 25, top - 15,
141 const_cast<char*>(displayedMessage.c_str()), 0,
142 xplmFont_Basic);
143 }
144 }
145}
146
147//------------------------------------------------------------------------------
148
149void MessageWindow::handleKey(char key, XPLMKeyFlags flags, char virtualKey,
150 bool losingFocus)
151{
152 Util::debug("hu.varadiistvan.xplra.MessageWindow.handleKey: key=0x%02d (%c), flags=0x%04x, virtualKey=0x%02d (%c), losingFocus=%d\n",
153 key, key, flags, virtualKey, virtualKey, losingFocus);
154}
155
156//------------------------------------------------------------------------------
157
158bool MessageWindow::handleMouseClick(int x, int y, XPLMMouseStatus mouse)
159{
160 Util::debug("hu.varadiistvan.xplra.MessageWindow.handleMouseClick: x=%d, y=%d, mouse=0x%04x\n",
161 x, y, mouse);
162 return true;
163}
164
165//------------------------------------------------------------------------------
166
167float MessageWindow::getNewMessage(std::string& dest)
168{
169 mutex.lock();
170
171 float d = duration;
172 if (d>0.0) {
173 dest = message;
174
175 message.clear();
176 duration = 0.0;
177 }
178
179 mutex.unlock();
180
181 return d;
182}
183
184//------------------------------------------------------------------------------
185
186void MessageWindow::setNewMessage(const std::string& msg, float d)
187{
188 mutex.lock();
189 message = msg;
190 duration = d;
191 mutex.unlock();
192
193 XPLMSetWindowIsVisible(windowID, 1);
194}
195
196//------------------------------------------------------------------------------
197
198// Local Variables:
199// mode: C++
200// c-basic-offset: 4
201// indent-tabs-mode: nil
202// End:
Note: See TracBrowser for help on using the repository browser.