swift
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 
19 namespace 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) { hide(); }
136  else { show(); }
137  }
138 
140  bool isVisible() const { return m_visible; }
141 
142  private:
143  void show()
144  {
145  m_messageBox.show();
146  m_visible = true;
147  }
148  void hide()
149  {
150  m_messageBox.hide();
151  m_visible = false;
152  }
153 
154  void scrollUp();
155  void scrollDown();
156  void scrollToTop();
157  void scrollToBottom();
158  void updateVisibleLines();
159 
160  bool m_visible = false;
161  std::vector<CMessage> m_messages;
162  size_t m_position = 0;
163  size_t m_maxVisibleLines = 5;
164  const size_t c_maxTotalLines = 1024;
165  CMessageBox m_messageBox;
166 
167  CCommand m_showCommand;
168  CCommand m_hideCommand;
169  CCommand m_toggleCommand;
170  CCommand m_scrollUpCommand;
171  CCommand m_scrollDownCommand;
172  CCommand m_scrollToTopCommand;
173  CCommand m_scrollToBottomCommand;
174  };
175 } // namespace XSwiftBus
176 
177 #endif // SWIFT_SIM_XSWIFTBUS_MESSAGES_H
Class-based interface to X-Plane's drawing callback API.
Definition: drawable.h:18
void show()
Register the draw callback.
Definition: drawable.h:30
void hide()
Unregister the draw callback.
Definition: drawable.h:38
Class which builds upon CMessageBox with a scrollback buffer and commands for user control.
Definition: messages.h:111
bool isVisible() const
Is message box currently visible?
Definition: messages.h:140
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
Class for drawing a gray box with text messages.
Definition: messages.h:54
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.
Plugin loaded by X-Plane which publishes a DBus service.
Definition: command.h:14
T::const_iterator begin(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition: lockfree.h:332
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition: lockfree.h:338
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