// Copyright (c) 2013 by István Váradi // This file is part of XPLRA, a remote-access plugin for X-Plane // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. //------------------------------------------------------------------------------ #include "MessageWindow.h" #include #include #include //------------------------------------------------------------------------------ using xplra::MessageWindow; using hu::varadiistvan::xplcommon::Util; //------------------------------------------------------------------------------ void MessageWindow::drawWindowCallback(XPLMWindowID /*windowID*/, void* refCon) { MessageWindow* messageWindow = reinterpret_cast(refCon); messageWindow->drawWindow(); } //------------------------------------------------------------------------------ void MessageWindow::handleKeyCallback(XPLMWindowID /*windowID*/, char key, XPLMKeyFlags flags, char virtualKey, void* refCon, int losingFocus) { MessageWindow* messageWindow = reinterpret_cast(refCon); messageWindow->handleKey(key, flags, virtualKey, losingFocus!=0); } //------------------------------------------------------------------------------ int MessageWindow::handleMouseClickCallback(XPLMWindowID /*windowID*/, int x, int y, XPLMMouseStatus mouse, void* refCon) { MessageWindow* messageWindow = reinterpret_cast(refCon); return messageWindow->handleMouseClick(x, y, mouse) ? 1 : 0; } //------------------------------------------------------------------------------ MessageWindow::MessageWindow() : showForced(false), hideTime(0.0), duration(0.0) { widthDataRef = XPLMFindDataRef("sim/graphics/view/window_width"); heightDataRef = XPLMFindDataRef("sim/graphics/view/window_height"); int screenWidth = XPLMGetDatai(widthDataRef); int screenHeight = XPLMGetDatai(heightDataRef); Util::debug("hu.varadiistvan.xplra.MessageWindow: screenWidth=%d, screenHeight=%d\n", screenWidth, screenHeight); left = 50; right = screenWidth - left; top = screenHeight * 9 / 10; bottom = top - 25; windowID = XPLMCreateWindow(left, top, right, bottom, 0, &drawWindowCallback, &handleKeyCallback, &handleMouseClickCallback, this); } //------------------------------------------------------------------------------ MessageWindow::~MessageWindow() { XPLMSetWindowIsVisible(windowID, 0); XPLMDestroyWindow(windowID); } //------------------------------------------------------------------------------ void MessageWindow::show() { showForced = true; XPLMSetWindowIsVisible(windowID, 1); } //------------------------------------------------------------------------------ void MessageWindow::hide() { showForced = false; } //------------------------------------------------------------------------------ void MessageWindow::drawWindow() { static float colour[3] = { 0.2, 1.0, 0.65 }; float now = XPLMGetElapsedTime(); float d = getNewMessage(displayedMessage); if (d>0.0) { hideTime = now + d; } if (now>=hideTime) hideTime = 0.0; if (hideTime==0.0 && !showForced) { XPLMSetWindowIsVisible(windowID, 0); } else { XPLMDrawTranslucentDarkBox(left, top, right, bottom); if (hideTime!=0.0) { XPLMDrawString(colour, left + 25, top - 15, const_cast(displayedMessage.c_str()), 0, xplmFont_Basic); } } } //------------------------------------------------------------------------------ void MessageWindow::handleKey(char key, XPLMKeyFlags flags, char virtualKey, bool losingFocus) { Util::debug("hu.varadiistvan.xplra.MessageWindow.handleKey: key=0x%02d (%c), flags=0x%04x, virtualKey=0x%02d (%c), losingFocus=%d\n", key, key, flags, virtualKey, virtualKey, losingFocus); } //------------------------------------------------------------------------------ bool MessageWindow::handleMouseClick(int x, int y, XPLMMouseStatus mouse) { Util::debug("hu.varadiistvan.xplra.MessageWindow.handleMouseClick: x=%d, y=%d, mouse=0x%04x\n", x, y, mouse); return true; } //------------------------------------------------------------------------------ float MessageWindow::getNewMessage(std::string& dest) { mutex.lock(); float d = duration; if (d>0.0) { dest = message; message.clear(); duration = 0.0; } mutex.unlock(); return d; } //------------------------------------------------------------------------------ void MessageWindow::setNewMessage(const std::string& msg, float d) { mutex.lock(); message = msg; duration = d; mutex.unlock(); XPLMSetWindowIsVisible(windowID, 1); } //------------------------------------------------------------------------------ // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: