swift
Loading...
Searching...
No Matches
messages.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#ifndef SWIFT_SIM_XSWIFTBUS_MESSAGES_H
5#define SWIFT_SIM_XSWIFTBUS_MESSAGES_H
6
8
9#include <algorithm>
10#include <array>
11#include <iterator>
12#include <string>
13#include <vector>
14
15#include "command.h"
16#include "datarefs.h"
17#include "drawable.h"
18
19namespace XSwiftBus
20{
22 namespace Private
23 {
24 inline auto empty_u8string()
25 {
26 using namespace std::literals;
27 return u8""s;
28 }
29 } // namespace Private
31
35 struct CMessage
36 {
38 using string = decltype(Private::empty_u8string());
39
41 CMessage(const string &text, float r = 1, float g = 1, float b = 1) : m_text(text), m_rgb { { r, g, b } } {}
42
44 string m_text;
45
47 std::array<float, 3> m_rgb;
48 };
49
53 class CMessageBox : public CDrawable
54 {
55 public:
60 CMessageBox(int left, int right, int top)
61 : CDrawable(xplm_Phase_Window, true), m_boxLeft(left), m_boxRight(right), m_boxTop(top)
62 {}
63
65 template <typename Iterator>
66 void setMessages(Iterator begin, Iterator end)
67 {
68 m_messages.clear();
69 std::copy(begin, end, std::back_inserter(m_messages));
70 }
71
73 void setValues(int leftPx, int topPx, int rightPx, int bottomPx, int lines, int durationMs);
74
76 void enableArrows(bool up, bool down)
77 {
78 m_upArrow = up;
79 m_downArrow = down;
80 }
81
83 int maxLineLength() const;
84
86 static int lineHeight();
87
88 protected:
89 virtual void draw() override;
90
91 private:
92 std::vector<CMessage> m_messages;
93 bool m_upArrow = false;
94 bool m_downArrow = false;
95 int m_boxLeft = 0;
96 int m_boxRight = 0;
97 int m_boxTop = 0;
98 int m_boxBottom = 0;
99 int m_lines = 10;
100 int m_durationMs = 2500;
101
102 // Screen
105 };
106
111 {
112 public:
114 CMessageBoxControl(int left, int right, int top);
115
117 void addMessage(const CMessage &message);
118
120 int maxLineLength() const { return m_messageBox.maxLineLength(); }
121
123 void setValues(int leftPx, int topPx, int rightPx, int bottomPx, int lines, int durationMs)
124 {
125 m_messageBox.setValues(leftPx, topPx, rightPx, bottomPx, lines, durationMs);
126 this->setMaxVisibleLines(static_cast<size_t>(lines));
127 }
128
130 void setMaxVisibleLines(size_t lines) { m_maxVisibleLines = lines; }
131
133 void toggle()
134 {
135 if (m_visible)
136 {
137 hide();
138 }
139 else
140 {
141 show();
142 }
143 }
144
146 bool isVisible() const { return m_visible; }
147
148 private:
149 void show()
150 {
151 m_messageBox.show();
152 m_visible = true;
153 }
154 void hide()
155 {
156 m_messageBox.hide();
157 m_visible = false;
158 }
159
160 void scrollUp();
161 void scrollDown();
162 void scrollToTop();
163 void scrollToBottom();
164 void updateVisibleLines();
165
166 bool m_visible = false;
167 std::vector<CMessage> m_messages;
168 size_t m_position = 0;
169 size_t m_maxVisibleLines = 5;
170 const size_t c_maxTotalLines = 1024;
171 CMessageBox m_messageBox;
172
173 CCommand m_showCommand;
174 CCommand m_hideCommand;
175 CCommand m_toggleCommand;
176 CCommand m_scrollUpCommand;
177 CCommand m_scrollDownCommand;
178 CCommand m_scrollToTopCommand;
179 CCommand m_scrollToBottomCommand;
180 };
181} // namespace XSwiftBus
182
183#endif // SWIFT_SIM_XSWIFTBUS_MESSAGES_H
CDrawable(XPLMDrawingPhase phase, bool before)
Constructor.
Definition drawable.h:21
bool isVisible() const
Is message box currently visible?
Definition messages.h:146
void addMessage(const CMessage &message)
Add a new message to the bottom of the list.
int maxLineLength() const
Returns the maximum number of characters per line.
Definition messages.h:120
void setValues(int leftPx, int topPx, int rightPx, int bottomPx, int lines, int durationMs)
Set margin values.
Definition messages.h:123
CMessageBoxControl(int left, int right, int top)
Constructor.
void setMaxVisibleLines(size_t lines)
Set max. visible lines.
Definition messages.h:130
void toggle()
Toggles the visibility of the message box.
Definition messages.h:133
virtual void draw()
Callback to draw the thing.
void setValues(int leftPx, int topPx, int rightPx, int bottomPx, int lines, int durationMs)
Set margin values.
void setMessages(Iterator begin, Iterator end)
Set messages to draw in message box, from a pair of iterators.
Definition messages.h:66
int maxLineLength() const
Returns the maximum number of characters per line.
void enableArrows(bool up, bool down)
Set whether to draw a small arrow at the bottom of the box.
Definition messages.h:76
CMessageBox(int left, int right, int top)
Constructor.
Definition messages.h:60
static int lineHeight()
Line height based on font.
Class providing access to a single X-Plane dataref.
Definition datarefs.h:92
Plugin loaded by X-Plane which publishes a DBus service.
Definition command.h:14
Class representing a single line of text to be drawn in a message box.
Definition messages.h:36
CMessage(const string &text, float r=1, float g=1, float b=1)
Constructor.
Definition messages.h:41
std::array< float, 3 > m_rgb
Color.
Definition messages.h:47
string m_text
Text.
Definition messages.h:44
decltype(Private::empty_u8string()) string
String type.
Definition messages.h:38