source: xplra/src/plugin/src/xplra/MessageWindow.cc@ 38:128b9ced9779

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

Added basic support for showing a message

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