TextBox.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2022 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_GUI_COMPONENT_TEXTBOX
  24. #define DFPSR_GUI_COMPONENT_TEXTBOX
  25. #include "../VisualComponent.h"
  26. #include "../../api/fontAPI.h"
  27. #include "../../math/LVector.h"
  28. namespace dsr {
  29. struct LineIndex {
  30. // Exclusive interval of characters in the line.
  31. int64_t lineStartIndex = 0;
  32. int64_t lineEndIndex = 0;
  33. LineIndex(int64_t lineStartIndex, int64_t lineEndIndex)
  34. : lineStartIndex(lineStartIndex), lineEndIndex(lineEndIndex) {}
  35. };
  36. class TextBox : public VisualComponent {
  37. PERSISTENT_DECLARATION(TextBox)
  38. public:
  39. // Attributes
  40. PersistentColor foreColor;
  41. PersistentColor backColor;
  42. PersistentString text;
  43. PersistentBoolean multiLine;
  44. // TODO: A setting for monospace?
  45. void declareAttributes(StructureDefinition &target) const override;
  46. Persistent* findAttribute(const ReadableString &name) override;
  47. // Temporary
  48. bool mousePressed = false;
  49. uint32_t combinationKeys = 0;
  50. // Selection goes from selectionStart to beamLocation using bi-directional exclusive character indices.
  51. // Empty with selectionStart = beamLocation.
  52. // From the left with selectionStart < beamLocation.
  53. // From the right with beamLocation < selectionStart.
  54. int64_t selectionStart = 0;
  55. int64_t beamLocation = 0;
  56. // TODO: Implement scrolling in pixels.
  57. // Begin by automatically following the beam and using the scroll wheel.
  58. // Panorate with right mouse button?
  59. int64_t verticalScroll = 0;
  60. int64_t horizontalScroll = 0;
  61. // Pre-splitted version of text for fast rendering of large documents.
  62. bool indexedLines = false;
  63. List<LineIndex> lines;
  64. void updateLines();
  65. void limitSelection();
  66. LVector2D getTextOrigin();
  67. int64_t findBeamLocationInLine(int64_t rowIndex, int64_t pixelX);
  68. int64_t findBeamLocation(const LVector2D &pixelLocation);
  69. void replaceSelection(const ReadableString replacingText);
  70. void replaceSelection(DsrChar replacingCharacter);
  71. void placeBeamAtCharacter(int64_t characterIndex, bool removeSelection);
  72. void moveBeamVertically(int64_t rowIndexOffset, bool removeSelection);
  73. private:
  74. // Given from the style
  75. MediaMethod textBox;
  76. RasterFont font;
  77. void completeAssets();
  78. void generateGraphics();
  79. // Generated
  80. bool hasImages = false;
  81. bool drawnAsFocused = false;
  82. OrderedImageRgbaU8 image;
  83. public:
  84. TextBox();
  85. public:
  86. bool isContainer() const override;
  87. void drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) override;
  88. void receiveMouseEvent(const MouseEvent& event) override;
  89. void receiveKeyboardEvent(const KeyboardEvent& event) override;
  90. bool pointIsInside(const IVector2D& pixelPosition) override;
  91. void changedTheme(VisualTheme newTheme) override;
  92. void changedLocation(const IRect &oldLocation, const IRect &newLocation) override;
  93. void changedAttribute(const ReadableString &name) override;
  94. };
  95. }
  96. #endif