Label.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef LABEL_H_
  2. #define LABEL_H_
  3. #include "Control.h"
  4. #include "Theme.h"
  5. namespace gameplay
  6. {
  7. /**
  8. * A label is the most basic type of control, capable only of rendering text within its border.
  9. *
  10. * The following properties are available for labels:
  11. @verbatim
  12. label <labelID>
  13. {
  14. style = <styleID>
  15. alignment = <Control::Alignment constant> // Note: 'position' will be ignored.
  16. position = <x, y>
  17. autoWidth = <bool>
  18. autoHeight = <bool>
  19. size = <width, height>
  20. width = <width> // Can be used in place of 'size', e.g. with 'autoHeight = true'
  21. height = <height> // Can be used in place of 'size', e.g. with 'autoWidth = true'
  22. text = <string>
  23. consumeEvents = <bool> // Whether the label propagates input events to the Game's input event handler. Default is true.
  24. }
  25. @endverbatim
  26. */
  27. class Label : public Control
  28. {
  29. friend class Container;
  30. public:
  31. /**
  32. * Create a new label control.
  33. *
  34. * @param id The control's ID.
  35. * @param style The control's style.
  36. *
  37. * @return The new label.
  38. * @script{create}
  39. */
  40. static Label* create(const char*id, Theme::Style* style);
  41. /**
  42. * Set the text for this label to display.
  43. *
  44. * @param text The text to display.
  45. */
  46. void setText(const char* text);
  47. /**
  48. * Get the text displayed by this label.
  49. *
  50. * @return The text displayed by this label.
  51. */
  52. const char* getText();
  53. /**
  54. * @see Control::getType
  55. */
  56. const char* getType() const;
  57. /**
  58. * Add a listener to be notified of specific events affecting
  59. * this control. Event types can be OR'ed together.
  60. * E.g. To listen to touch-press and touch-release events,
  61. * pass <code>Control::Listener::TOUCH | Control::Listener::RELEASE</code>
  62. * as the second parameter.
  63. *
  64. * @param listener The listener to add.
  65. * @param eventFlags The events to listen for.
  66. */
  67. virtual void addListener(Control::Listener* listener, int eventFlags);
  68. protected:
  69. /**
  70. * Constructor.
  71. */
  72. Label();
  73. /**
  74. * Destructor.
  75. */
  76. virtual ~Label();
  77. /**
  78. * Create a label with a given style and properties.
  79. *
  80. * @param style The style to apply to this label.
  81. * @param properties The properties to set on this label.
  82. *
  83. * @return The new label.
  84. */
  85. static Label* create(Theme::Style* style, Properties* properties);
  86. /**
  87. * Initialize this label.
  88. */
  89. virtual void initialize(Theme::Style* style, Properties* properties);
  90. /**
  91. * Called when a label's properties change. Updates this label's internal rendering
  92. * properties, such as its text viewport.
  93. *
  94. * @param container This label's parent container.
  95. * @param offset The scroll offset of this label's parent container.
  96. */
  97. void update(const Control* container, const Vector2& offset);
  98. /**
  99. * Draw this label's text.
  100. *
  101. * @param clip The clipping rectangle of this label's parent container.
  102. */
  103. void drawText(const Rectangle& clip);
  104. /**
  105. * The text displayed by this label.
  106. */
  107. std::string _text;
  108. /**
  109. * The font being used to display the label.
  110. */
  111. Font* _font;
  112. /**
  113. * The text color being used to display the label.
  114. */
  115. Vector4 _textColor;
  116. /**
  117. * The position and size of this control's text area, before clipping. Used for text alignment.
  118. */
  119. Rectangle _textBounds;
  120. private:
  121. /**
  122. * Constructor.
  123. */
  124. Label(const Label& copy);
  125. };
  126. }
  127. #endif