// 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; using std::string; using std::min; using std::max; //------------------------------------------------------------------------------ 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() : maxMessageLength(0), showForced(false), showTime(0.0), hideTime(0.0), clickedX(-1), clickedY(-1), clickedLeft(-1), clickedTop(-1), clickedRight(-1), clickedBottom(-1), duration(0.0) { widthDataRef = XPLMFindDataRef("sim/graphics/view/window_width"); heightDataRef = XPLMFindDataRef("sim/graphics/view/window_height"); int digitsOnly = 0; XPLMGetFontDimensions(xplmFont_Basic, &fontWidth, &fontHeight, &digitsOnly); Util::debug("hu.varadiistvan.xplra.MessageWindow: fontWidth=%d, fontHeight=%d\n", fontWidth, fontHeight); resetLocation(); 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::reset() { resetLocation(); XPLMSetWindowGeometry(windowID, left, top, right, bottom); } //------------------------------------------------------------------------------ void MessageWindow::drawWindow() { static float colour[3] = { 0.2, 1.0, 0.65 }; float now = XPLMGetElapsedTime(); float d = getNewMessage(displayedMessage); if (d>0.0) { showTime = now; 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) { size_t offset = 0; if (displayedMessage.size()>maxMessageLength && now > (showTime + scrollTimeout) ) { offset = (now - showTime - scrollTimeout) / scrollInterval; offset = min(offset, displayedMessage.size() - maxMessageLength); } string dm(displayedMessage, offset, maxMessageLength); XPLMDrawString(colour, left + horizontalMarginSize, bottom + verticalMarginSize + baseLineSize, const_cast(dm.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) { if (mouse==xplm_MouseDown) { if (x <= (left + resizeArea) ) { mouseMode = MOUSE_RESIZE_LEFT; } else if (x > (right-resizeArea) ) { mouseMode = MOUSE_RESIZE_RIGHT; } else { mouseMode = MOUSE_MOVE; } clickedX = x; clickedY = y; clickedLeft = left; clickedTop = top; clickedRight = right; clickedBottom = bottom; } else if (mouse==xplm_MouseDrag) { int xDiff = x - clickedX; if (mouseMode==MOUSE_MOVE) { int newLeft = clickedLeft + xDiff; int newRight = clickedRight + xDiff; int screenWidth = XPLMGetDatai(widthDataRef); if (newLeft>=0 && newRight=0) { top = newTop; bottom = newBottom; } } else if (mouseMode==MOUSE_RESIZE_LEFT) { int newLeft = clickedLeft + xDiff; if (newLeft>=0 && (right-newLeft)>=minimalWidth) { left = newLeft; recalculateMaxMessageLength(); } } else if (mouseMode==MOUSE_RESIZE_RIGHT) { int newRight = clickedRight + xDiff; int screenWidth = XPLMGetDatai(widthDataRef); if (newRight=minimalWidth) { right = newRight; recalculateMaxMessageLength(); } } XPLMSetWindowGeometry(windowID, left, top, right, bottom); } 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); } //------------------------------------------------------------------------------ void MessageWindow::resetLocation() { 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 - fontHeight - 2*verticalMarginSize; recalculateMaxMessageLength(); } //------------------------------------------------------------------------------ void MessageWindow::recalculateMaxMessageLength() { maxMessageLength = (right + 1 - left) - 2 * horizontalMarginSize; maxMessageLength /= fontWidth; } //------------------------------------------------------------------------------ // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: