Line data Source code
1 : #pragma once 2 : 3 : #include <Display/DisplayInterface.hpp> 4 : #include <AH/Containers/LinkedList.hpp> 5 : 6 : BEGIN_CS_NAMESPACE 7 : 8 : /** 9 : * @brief An interface for elements that draw to a display. 10 : */ 11 : class DisplayElement : public DoublyLinkable<DisplayElement> { 12 : protected: 13 : /** 14 : * @brief Create a new DisplayElement. 15 : * 16 : * @param display 17 : * The display that this display element draws to. 18 : */ 19 : DisplayElement(DisplayInterface &display) : display(display) { 20 : // The elements are sorted by the address of their displays. 21 : // This way, all display elements that draw to the same display are next 22 : // to each other. This means that the display buffer can be reused. 23 : elements.insertSorted( 24 : this, [](const DisplayElement &lhs, const DisplayElement &rhs) { 25 : return &lhs.getDisplay() < &rhs.getDisplay(); 26 : }); 27 : } 28 : 29 : public: 30 : virtual ~DisplayElement() { elements.remove(this); } 31 : 32 : /// Draw this DisplayElement to the display buffer. 33 : virtual void draw() = 0; 34 : 35 : /// Get a reference to the display that this element draws to. 36 0 : DisplayInterface &getDisplay() { return display; } 37 : /// Get a const reference to the display that this element draws to. 38 : const DisplayInterface &getDisplay() const { return display; } 39 : 40 : /// Get the list of all DisplayElement instances. 41 0 : static DoublyLinkedList<DisplayElement> &getAll() { return elements; } 42 : 43 : protected: 44 : DisplayInterface &display; 45 : 46 : static DoublyLinkedList<DisplayElement> elements; 47 : }; 48 : 49 : END_CS_NAMESPACE