xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
dot_matrix_display.h
Go to the documentation of this file.
1 #pragma once
5 #include <array>
8 #include "control.h"
9 #include "segments.h"
10 #include "dot_matrix_style.h"
11 
12 namespace xtd {
13  namespace forms {
23  class dot_matrix_display : public control {
24  public:
26  using dots_collection = std::vector<std::vector<bool>>;
27 
29  using points_collection = std::vector<xtd::drawing::point>;
30 
33  auto_size(true);
34  double_buffered(true);
35  size_ = default_size();
36  }
37 
41  virtual drawing::color back_dot_color() {return back_dot_color_.value_or(fore_color());}
47  if (!back_dot_color_.has_value() || back_dot_color_.value() != value) {
48  back_dot_color_ = value;
49  invalidate();
50  }
51  return *this;
52  }
53 
56  virtual double back_dot_opacity() const {return back_dot_opacity_;}
61  virtual dot_matrix_display& back_dot_opacity(double value) {
62  if (value < 0.0 || value > 1.0) throw argument_out_of_range_exception("value must be between 0.0 and 1.0."_t, current_stack_frame_);
63  if (back_dot_opacity_ != value) {
64  back_dot_opacity_ = value;
65  if (back_dot_opacity_ < 0.0) back_dot_opacity_ = 0.0;
66  if (back_dot_opacity_ > 1.0) back_dot_opacity_ = 1.0;
67  invalidate();
68  }
69  return *this;
70  }
71 
74  virtual forms::dot_matrix_style dot_matrix_style() const {return dot_matrix_style_;}
79  if (dot_matrix_style_ != value) {
80  dot_matrix_style_ = value;
81  invalidate();
82  }
83  return *this;
84  }
85 
88  virtual const dots_collection& dots() const {return dots_;}
92  virtual const dot_matrix_display& dots(const dots_collection& dots) {
93  if (dots_ != dots) {
94  dots_ = dots;
95  matrix_size_ = drawing::size(static_cast<int32_t>(dots_[0].size()), static_cast<int32_t>(dots_.size()));
96  invalidate();
97  }
98  return *this;
99  }
100 
103  virtual int32_t matrix_height() const {return matrix_size_.height();}
107  virtual void matrix_height(int32_t value) {matrix_size({matrix_size_.width(), value});}
108 
111  virtual int32_t matrix_width() const {return matrix_size_.width();}
115  virtual void matrix_width(int32_t value) {matrix_size({value, matrix_size_.height()});}
116 
119  virtual const drawing::size& matrix_size() const {return matrix_size_;}
123  virtual void matrix_size(const drawing::size& value) {
124  if (matrix_size_ != value) {
125  matrix_size_ = value;
126  dots_ = dots_collection(matrix_size_.height(), std::vector<bool>(matrix_size_.width(), false));
127  invalidate();
128  }
129  }
130 
133  virtual bool show_back_dot() const {return show_back_dot_;}
137  virtual dot_matrix_display& show_back_dot(bool value) {
138  if (show_back_dot_ != value) {
139  show_back_dot_ = value;
140  invalidate();
141  }
142  return *this;
143  }
144 
147  virtual int32_t thickness() const {return thickness_.value_or(size_.height() < (matrix_size_.height() * 2) ? 1 : (size_.height() - matrix_size_.height()) / matrix_size_.height());}
151  virtual dot_matrix_display& thickness(int32_t value) {
152  if (!thickness_.has_value() || thickness_.value() != value) {
153  thickness_ = value;
154  invalidate();
155  }
156  return *this;
157  }
158 
161  virtual void set_all_dots(bool on) {
162  for (int32_t y = 0; y < static_cast<int32_t>(dots_.size()); y++)
163  for (int32_t x = 0; x < static_cast<int32_t>(dots_[y].size()); x++)
164  dots_[y][x] = on;
165  }
166 
170  virtual bool get_dot(const drawing::point& point) const {return dots_[point.y()][point.x()];}
171 
175  virtual void set_dot(const drawing::point& point, bool on) {
176  if (dots_[point.y()][point.x()] != on) {
177  dots_[point.y()][point.x()] = on;
178  invalidate();
179  }
180  }
181 
184  virtual void set_dots(const points_collection& points) {
185  set_all_dots(false);
186  set_dots(points, true);
187  }
191  virtual void set_dots(const points_collection& points, bool on) {
192  for (auto point : points)
193  set_dot(point, on);
194  }
195 
196  protected:
197  drawing::size default_size() const override {return {25, 25};}
198 
199  void on_back_color_changed(const event_args& e) override {
201  invalidate();
202  }
203 
204  void on_fore_color_changed(const event_args& e) override {
206  invalidate();
207  }
208 
209  void on_paint(paint_event_args& e) override {
210  if (back_color() != default_back_color()) e.graphics().clear(back_color());
211  for (int32_t y = 0; y < static_cast<int32_t>(dots_.size()); y++) {
212  for (int32_t x = 0; x < static_cast<int32_t>(dots_[y].size()); x++) {
213  if (dots_[y][x]) draw_dot(e.graphics(), fore_color(), {x, y});
214  else if (show_back_dot_) draw_dot(e.graphics(), drawing::color::average(back_dot_color(), back_color(), back_dot_opacity_), {x, y});
215  }
216  }
218  }
219 
220  drawing::size measure_control() const override {
221  int32_t width = static_cast<int32_t>(static_cast<double>(height()) / matrix_height() * matrix_width());
222  return drawing::size(width, height());
223  }
224 
229  virtual void draw_dot(drawing::graphics& graphics, const drawing::color& color, const drawing::point& point) {
230  int32_t y = (height() - static_cast<int32_t>(dots_.size())) / static_cast<int32_t>(dots_.size());
231  int32_t x = (width() - static_cast<int32_t>(dots_[point.y()].size())) / static_cast<int32_t>(dots_[point.y()].size());
232  if (dot_matrix_style_ == dot_matrix_style::standard)
233  graphics.fill_pie(drawing::solid_brush(color), (1 + x) * point.x(), (1 + y) * point.y(), thickness(), thickness(), 0, 360);
234  else if (dot_matrix_style_ == dot_matrix_style::square)
235  graphics.fill_rectangle(drawing::solid_brush(color), (1 + x) * point.x(), (1 + y) * point.y(), thickness(), thickness());
236  }
237 
239  drawing::size matrix_size_ = {7, 7};
240  dots_collection dots_ = dots_collection(matrix_size_.width(), std::vector<bool>(matrix_size_.height(), false));
241  bool show_back_dot_ = true;
242  std::optional<drawing::color> back_dot_color_;
243  double back_dot_opacity_ = 0.95;
245  std::optional<int32_t> thickness_;
247  };
248  }
249 }
Contains xtd::argument_out_of_range_exception exception.
The exception that is thrown when one of the arguments provided to a method is out of range.
Definition: argument_out_of_range_exception.h:18
Represents an ARGB (alpha, red, green, blue) color.
Definition: color.h:39
static color average(const color &color1, const color &color2, double weight, bool average_alpha)
Returns the weighted average color between the two given colors.
Definition: color.h:592
Defines an object used to draw lines and curves. This class cannot be inherited.
Definition: graphics.h:48
Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional ...
Definition: point.h:48
Stores an ordered pair of integers, which specify a height and width.
Definition: size.h:25
int32_t width() const
Gets the horizontal component of this Size class.
Definition: size.h:67
int32_t height() const
Gets he vertical component of this Size Class.
Definition: size.h:57
Defines a xtd::drawing::brush of a single color. Brushes are used to fill graphics shapes,...
Definition: solid_brush.h:22
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: event_args.h:18
Defines the base class for controls, which are components with visual representation.
Definition: control.h:67
virtual drawing::color default_back_color() const
Gets the default background color of the control.
Definition: control.h:393
virtual bool auto_size() const
Gets a value that indicates whether the control resizes based on its contents.
Definition: control.h:245
virtual drawing::color fore_color() const
Gets the foreground color of the control.
virtual const drawing::size & size() const
Gets the height and width of the control.
Definition: control.h:620
virtual void invalidate() const
Invalidates the entire surface of the control and causes the control to be redrawn.
Definition: control.h:883
virtual bool double_buffered() const
Gets a value indicating whether this control should redraw its surface using a secondary buffer to re...
Definition: control.h:437
virtual int32_t height() const
Gets the height of the control.
Definition: control.h:486
virtual drawing::color back_color() const
Gets the background color for the control.
virtual int32_t width() const
Gets the width of the control.
Definition: control.h:682
Represents a dot matrix display class.
Definition: dot_matrix_display.h:23
drawing::size measure_control() const override
Measure this control.
Definition: dot_matrix_display.h:220
dot_matrix_display()
Initialize a new instance of dot_matrix_display class.
Definition: dot_matrix_display.h:32
virtual const drawing::size & matrix_size() const
Gets the matrix size. Number of width and height dots.
Definition: dot_matrix_display.h:119
virtual void set_dots(const points_collection &points, bool on)
Sets specified dots with specified boolean.
Definition: dot_matrix_display.h:191
virtual dot_matrix_display & show_back_dot(bool value)
Sets a value indicate if background dots are shown.
Definition: dot_matrix_display.h:137
std::vector< std::vector< bool > > dots_collection
Represents a dots collection.
Definition: dot_matrix_display.h:26
virtual dot_matrix_display & thickness(int32_t value)
Sets thickness of dot.
Definition: dot_matrix_display.h:151
void on_fore_color_changed(const event_args &e) override
Raises the control::fore_color_changed event.
Definition: dot_matrix_display.h:204
virtual forms::dot_matrix_style dot_matrix_style() const
Gets dot matrix style.
Definition: dot_matrix_display.h:74
virtual bool get_dot(const drawing::point &point) const
Gets specified dot point status.
Definition: dot_matrix_display.h:170
virtual dot_matrix_display & back_dot_opacity(double value)
Sets the background dot opacity.
Definition: dot_matrix_display.h:61
virtual drawing::color back_dot_color()
Gets background dot color.
Definition: dot_matrix_display.h:41
virtual void set_dots(const points_collection &points)
Sets specified dots to on.
Definition: dot_matrix_display.h:184
virtual int32_t matrix_width() const
Gets the matrix size. Number height dots.
Definition: dot_matrix_display.h:111
virtual void set_dot(const drawing::point &point, bool on)
Sets specified dot point status.
Definition: dot_matrix_display.h:175
virtual dot_matrix_display & dot_matrix_style(forms::dot_matrix_style value)
Sets dot matrix style.
Definition: dot_matrix_display.h:78
virtual dot_matrix_display & back_dot_color(const drawing::color &value)
Sets background dot color.
Definition: dot_matrix_display.h:46
void on_back_color_changed(const event_args &e) override
Raises the control::back_color_changed event.
Definition: dot_matrix_display.h:199
std::vector< xtd::drawing::point > points_collection
Represents a point collection.
Definition: dot_matrix_display.h:29
virtual bool show_back_dot() const
Gets a value indicate if background dots are shown.
Definition: dot_matrix_display.h:133
virtual double back_dot_opacity() const
Gets the background dot opacity.
Definition: dot_matrix_display.h:56
virtual void set_all_dots(bool on)
Sets all dots with specified boolean.
Definition: dot_matrix_display.h:161
virtual void draw_dot(drawing::graphics &graphics, const drawing::color &color, const drawing::point &point)
Draw specified dot point with specified color on specified graphics.
Definition: dot_matrix_display.h:229
virtual const dot_matrix_display & dots(const dots_collection &dots)
Sets all dots status.
Definition: dot_matrix_display.h:92
virtual void matrix_size(const drawing::size &value)
Gets the matrix size. Number of width and height dots.
Definition: dot_matrix_display.h:123
void on_paint(paint_event_args &e) override
Raises the control::paint event.
Definition: dot_matrix_display.h:209
virtual int32_t thickness() const
Gets thickness of dot.
Definition: dot_matrix_display.h:147
drawing::size default_size() const override
Gets the default size of the control.
Definition: dot_matrix_display.h:197
virtual void matrix_width(int32_t value)
Sets the matrix size. Number height dots.
Definition: dot_matrix_display.h:115
virtual int32_t matrix_height() const
Gets the matrix size. Number of height dots.
Definition: dot_matrix_display.h:103
virtual void matrix_height(int32_t value)
Sets the matrix size. Number of height dots.
Definition: dot_matrix_display.h:107
virtual const dots_collection & dots() const
Gets all dots status.
Definition: dot_matrix_display.h:88
Provides data for the paint event.
Definition: paint_event_args.h:26
Contains xtd::forms::control control.
Contains xtd::forms::dot_matrix_style enum class.
virtual void on_back_color_changed(const event_args &e)
Raises the control::back_color_changed event.
virtual void on_paint(paint_event_args &e)
Raises the control::paint event.
virtual void on_fore_color_changed(const event_args &e)
Raises the control::fore_color_changed event.
#define current_stack_frame_
Provides information about the current stack frame.
Definition: stack_frame.h:201
@ y
The Y key.
@ x
The X key.
@ e
The E key.
dot_matrix_style
Represent dot matrix style used by dot_matrix_display control.
Definition: dot_matrix_style.h:17
@ square
Square dot matrix style.
@ standard
Standard (or round) dot matrix style.
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition: about_box.h:13
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::forms::segments enum class.
Contains xtd::drawing::solid_brush class.