mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-25 10:15:43 +08:00
refs #271 CSimulatorXPlane sends text messages to XBus and they are displayed in X-Plane.
This commit is contained in:
124
src/xbus/messages.h
Normal file
124
src/xbus/messages.h
Normal file
@@ -0,0 +1,124 @@
|
||||
/* Copyright (C) 2015
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef BLACKSIM_XBUS_MESSAGES_H
|
||||
#define BLACKSIM_XBUS_MESSAGES_H
|
||||
|
||||
//! \file
|
||||
|
||||
#include "drawable.h"
|
||||
#include "command.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <array>
|
||||
#include <algorithm>
|
||||
|
||||
namespace XBus
|
||||
{
|
||||
|
||||
/*!
|
||||
* Class representing a single line of text to be drawn in a message box.
|
||||
*/
|
||||
struct CMessage
|
||||
{
|
||||
//! Constructor.
|
||||
CMessage(const std::string &text, float r, float g, float b) : m_text(text)
|
||||
{
|
||||
// MSVC initializer list bug
|
||||
std::tie(m_rgb[0], m_rgb[1], m_rgb[2]) = std::tie(r, g, b);
|
||||
}
|
||||
|
||||
//! Text.
|
||||
std::string m_text;
|
||||
|
||||
//! Color.
|
||||
std::array<float, 3> m_rgb;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Class for drawing a gray box with text messages.
|
||||
*/
|
||||
class CMessageBox : public CDrawable
|
||||
{
|
||||
public:
|
||||
//! Constructor.
|
||||
CMessageBox() : CDrawable(xplm_Phase_Window, true) {}
|
||||
|
||||
//! Set messages to draw in message box, from a pair of iterators.
|
||||
template <typename Iterator>
|
||||
void setMessages(Iterator begin, Iterator end)
|
||||
{
|
||||
m_messages.clear();
|
||||
std::copy(begin, end, std::back_inserter(m_messages));
|
||||
}
|
||||
|
||||
//! Set whether to draw a small arrow at the bottom of the box.
|
||||
void enableArrows(bool up, bool down)
|
||||
{
|
||||
m_upArrow = up;
|
||||
m_downArrow = down;
|
||||
}
|
||||
|
||||
//! Returns the maximum number of characters per line.
|
||||
int maxLineLength() const;
|
||||
|
||||
protected:
|
||||
virtual void draw() override;
|
||||
|
||||
private:
|
||||
std::vector<CMessage> m_messages;
|
||||
bool m_upArrow = false;
|
||||
bool m_downArrow = false;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Class which builds upon CMessageBox with a scrollback buffer and commands for user control.
|
||||
*/
|
||||
class CMessageBoxControl
|
||||
{
|
||||
public:
|
||||
//! Constructor.
|
||||
CMessageBoxControl();
|
||||
|
||||
//! Add a new message to the bottom of the list.
|
||||
void addMessage(const CMessage &message);
|
||||
|
||||
//! \copydoc XBus::CMessageBox::maxLineLength
|
||||
int maxLineLength() const { return m_messageBox.maxLineLength(); }
|
||||
|
||||
private:
|
||||
void show() { m_messageBox.show(); m_visible = true; }
|
||||
void hide() { m_messageBox.hide(); m_visible = false; }
|
||||
void toggle() { if (m_visible) { hide(); } else { show(); } }
|
||||
void scrollUp();
|
||||
void scrollDown();
|
||||
void scrollToTop();
|
||||
void scrollToBottom();
|
||||
void updateVisibleLines();
|
||||
|
||||
bool m_visible = false;
|
||||
std::vector<CMessage> m_messages;
|
||||
size_t m_position = 0;
|
||||
const size_t c_maxVisibleLines = 8;
|
||||
const size_t c_maxTotalLines = 1024;
|
||||
CMessageBox m_messageBox;
|
||||
|
||||
CCommand m_showCommand;
|
||||
CCommand m_hideCommand;
|
||||
CCommand m_toggleCommand;
|
||||
CCommand m_scrollUpCommand;
|
||||
CCommand m_scrollDownCommand;
|
||||
CCommand m_scrollToTopCommand;
|
||||
CCommand m_scrollToBottomCommand;
|
||||
CCommand m_debugCommand;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user