VisualComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2019 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. #include <stdint.h>
  24. #include "VisualComponent.h"
  25. #include "../image/internal/imageInternal.h"
  26. using namespace dsr;
  27. PERSISTENT_DEFINITION(VisualComponent)
  28. VisualComponent::VisualComponent() {}
  29. VisualComponent::~VisualComponent() {
  30. // Let the children know that the parent component no longer exists.
  31. for (int i = 0; i < this->getChildCount(); i++) {
  32. this->children[i]->parent = nullptr;
  33. }
  34. }
  35. bool VisualComponent::isContainer() const {
  36. return true;
  37. }
  38. IRect VisualComponent::getLocation() const {
  39. return this->location;
  40. }
  41. IVector2D VisualComponent::getSize() const {
  42. return this->location.size();
  43. }
  44. void VisualComponent::setRegion(const FlexRegion &newRegion) {
  45. this->region = newRegion;
  46. }
  47. FlexRegion VisualComponent::getRegion() const {
  48. return this->region;
  49. }
  50. void VisualComponent::setVisible(bool visible) {
  51. this->visible.value = visible;
  52. }
  53. bool VisualComponent::getVisible() const {
  54. return this->visible.value;
  55. }
  56. void VisualComponent::setName(const String& newName) {
  57. this->name.value = newName;
  58. }
  59. String VisualComponent::getName() const {
  60. return this->name.value;
  61. }
  62. void VisualComponent::setIndex(int newIndex) {
  63. this->index.value = newIndex;
  64. }
  65. int VisualComponent::getIndex() const {
  66. return this->index.value;
  67. }
  68. void VisualComponent::setLocation(const IRect &newLocation) {
  69. IRect oldLocation = this->location;
  70. this->location = newLocation;
  71. if (oldLocation != newLocation) {
  72. this->updateLocationEvent(oldLocation, newLocation);
  73. }
  74. this->changedLocation(oldLocation, newLocation);
  75. }
  76. void VisualComponent::applyLayout(IVector2D parentSize) {
  77. this->setLocation(this->region.getNewLocation(parentSize));
  78. }
  79. void VisualComponent::updateLocationEvent(const IRect& oldLocation, const IRect& newLocation) {
  80. // Place each child component
  81. for (int i = 0; i < this->getChildCount(); i++) {
  82. this->children[i]->applyLayout(newLocation.size());
  83. }
  84. }
  85. // Offset may become non-zero when the origin is outside of targetImage from being clipped outside of the parent region
  86. void VisualComponent::draw(ImageRgbaU8& targetImage, const IVector2D& offset) {
  87. if (this->getVisible()) {
  88. IRect containerBound = this->getLocation() + offset;
  89. this->drawSelf(targetImage, containerBound);
  90. // Draw each child component
  91. for (int i = 0; i < this->getChildCount(); i++) {
  92. this->children[i]->drawClipped(targetImage, containerBound.upperLeft(), containerBound);
  93. }
  94. }
  95. }
  96. void VisualComponent::drawClipped(ImageRgbaU8 targetImage, const IVector2D& offset, const IRect& clipRegion) {
  97. IRect finalRegion = IRect::cut(clipRegion, IRect(0, 0, image_getWidth(targetImage), image_getHeight(targetImage)));
  98. if (finalRegion.hasArea()) {
  99. // TODO: Optimize allocation of sub-images
  100. ImageRgbaU8 target = image_getSubImage(targetImage, finalRegion);
  101. this->draw(target, offset - finalRegion.upperLeft());
  102. }
  103. }
  104. // A red rectangle is drawn as a placeholder if the class couldn't be found
  105. // TODO: Should the type name be remembered in the base class for serializing missing components?
  106. void VisualComponent::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  107. draw_rectangle(targetImage, relativeLocation, ColorRgbaI32(200, 50, 50, 255));
  108. }
  109. // Manual use with the correct type
  110. void VisualComponent::addChildComponent(std::shared_ptr<VisualComponent> child) {
  111. if (!this->isContainer()) {
  112. throwError(U"Cannot attach a child to a non-container parent component!\n");
  113. } else if (child.get() == this) {
  114. throwError(U"Cannot attach a component to itself!\n");
  115. } else if (child->hasChild(this)) {
  116. throwError(U"Cannot attach to its own parent as a child component!\n");
  117. } else {
  118. // Remove from any previous parent
  119. child->detachFromParent();
  120. // Update layout based on the new parent size
  121. child->applyLayout(this->getSize());
  122. // Connect to the new parent
  123. this->children.push(child);
  124. child->parent = this;
  125. }
  126. }
  127. // Automatic insertion from loading
  128. bool VisualComponent::addChild(std::shared_ptr<Persistent> child) {
  129. // Try to cast from base class Persistent to derived class VisualComponent
  130. std::shared_ptr<VisualComponent> visualComponent = std::dynamic_pointer_cast<VisualComponent>(child);
  131. if (visualComponent.get() == nullptr) {
  132. return false; // Wrong type!
  133. } else {
  134. this->addChildComponent(visualComponent);
  135. return true; // Success!
  136. }
  137. }
  138. int VisualComponent::getChildCount() const {
  139. return this->children.length();
  140. }
  141. std::shared_ptr<Persistent> VisualComponent::getChild(int index) const {
  142. return this->children[index];
  143. }
  144. void VisualComponent::detachFromParent() {
  145. // Check if there's a parent component
  146. VisualComponent *parent = this->parent;
  147. if (parent != nullptr) {
  148. // If the removed component is focused from the parent, then remove focus
  149. if (parent->focusComponent.get() == this) {
  150. parent->focusComponent = std::shared_ptr<VisualComponent>();
  151. }
  152. // Iterate over all children in the parent component
  153. for (int i = 0; i < parent->getChildCount(); i++) {
  154. std::shared_ptr<VisualComponent> current = parent->children[i];
  155. if (current.get() == this) {
  156. current->parent = nullptr; // Assign null
  157. parent->children.remove(i);
  158. return;
  159. }
  160. }
  161. }
  162. }
  163. bool VisualComponent::hasChild(VisualComponent *child) const {
  164. for (int i = 0; i < this->getChildCount(); i++) {
  165. std::shared_ptr<VisualComponent> current = this->children[i];
  166. if (current.get() == child) {
  167. return true; // Found the component
  168. } else {
  169. if (current->hasChild(child)) {
  170. return true; // Found the component recursively
  171. }
  172. }
  173. }
  174. return false; // Could not find the component
  175. }
  176. bool VisualComponent::hasChild(std::shared_ptr<VisualComponent> child) const {
  177. return this->hasChild(child.get());
  178. }
  179. std::shared_ptr<VisualComponent> VisualComponent::findChildByName(ReadableString name, bool mustExist) const {
  180. for (int i = 0; i < this->getChildCount(); i++) {
  181. std::shared_ptr<VisualComponent> current = this->children[i];
  182. if (string_match(current->getName(), name)) {
  183. return current; // Found the component
  184. } else {
  185. std::shared_ptr<VisualComponent> searchResult = current->findChildByName(name, mustExist);
  186. if (searchResult.get() != nullptr) {
  187. return searchResult; // Found the component recursively
  188. }
  189. }
  190. }
  191. return std::shared_ptr<VisualComponent>(); // Could not find the component
  192. }
  193. std::shared_ptr<VisualComponent> VisualComponent::findChildByNameAndIndex(ReadableString name, int index, bool mustExist) const {
  194. for (int i = 0; i < this->getChildCount(); i++) {
  195. std::shared_ptr<VisualComponent> current = this->children[i];
  196. if (string_match(current->getName(), name) && current->getIndex() == index) {
  197. return current; // Found the component
  198. } else {
  199. std::shared_ptr<VisualComponent> searchResult = current->findChildByNameAndIndex(name, index, mustExist);
  200. if (searchResult.get() != nullptr) {
  201. return searchResult; // Found the component recursively
  202. }
  203. }
  204. }
  205. return std::shared_ptr<VisualComponent>(); // Could not find the component
  206. }
  207. bool VisualComponent::pointIsInside(const IVector2D& pixelPosition) {
  208. return pixelPosition.x > this->location.left() && pixelPosition.x < this->location.right()
  209. && pixelPosition.y > this->location.top() && pixelPosition.y < this->location.bottom();
  210. }
  211. // Non-recursive top-down search
  212. std::shared_ptr<VisualComponent> VisualComponent::getDirectChild(const IVector2D& pixelPosition, bool includeInvisible) {
  213. // Iterate child components in reverse drawing order
  214. for (int i = this->getChildCount() - 1; i >= 0; i--) {
  215. std::shared_ptr<VisualComponent> currentChild = this->children[i];
  216. // Check if the point is inside the child component
  217. if ((currentChild->getVisible() || includeInvisible) && currentChild->pointIsInside(pixelPosition)) {
  218. return currentChild;
  219. }
  220. }
  221. // Return nothing if the point missed all child components
  222. return std::shared_ptr<VisualComponent>();
  223. }
  224. // Recursive top-down search
  225. std::shared_ptr<VisualComponent> VisualComponent::getTopChild(const IVector2D& pixelPosition, bool includeInvisible) {
  226. // Iterate child components in reverse drawing order
  227. for (int i = this->getChildCount() - 1; i >= 0; i--) {
  228. std::shared_ptr<VisualComponent> currentChild = this->children[i];
  229. // Check if the point is inside the child component
  230. if ((currentChild->getVisible() || includeInvisible) && currentChild->pointIsInside(pixelPosition)) {
  231. // Check if a component inside the child component is even higher up
  232. std::shared_ptr<VisualComponent> subChild = currentChild->getTopChild(pixelPosition - this->getLocation().upperLeft(), includeInvisible);
  233. if (subChild.get() != nullptr) {
  234. return subChild;
  235. } else {
  236. return currentChild;
  237. }
  238. }
  239. }
  240. // Return nothing if the point missed all child components
  241. return std::shared_ptr<VisualComponent>();
  242. }
  243. void VisualComponent::sendMouseEvent(const MouseEvent& event) {
  244. // Convert to local coordinates recursively
  245. MouseEvent localEvent = event - this->getLocation().upperLeft();
  246. std::shared_ptr<VisualComponent> childComponent;
  247. // Grab a component on mouse down
  248. if (event.mouseEventType == MouseEventType::MouseDown) {
  249. childComponent = this->dragComponent = this->focusComponent = this->getDirectChild(localEvent.position, false);
  250. this->holdCount++;
  251. }
  252. if (this->holdCount > 0) {
  253. // If we're grabbing a component, keep sending events to it
  254. childComponent = this->dragComponent;
  255. } else if (this->getVisible() && this->pointIsInside(event.position)) {
  256. // If we're not grabbing a component, see if we can send the action to another component
  257. childComponent = this->getDirectChild(localEvent.position, false);
  258. }
  259. // Send the signal to a child component or itself
  260. if (childComponent.get() != nullptr) {
  261. childComponent->sendMouseEvent(localEvent);
  262. } else {
  263. this->receiveMouseEvent(event);
  264. }
  265. // Release a component on mouse up
  266. if (event.mouseEventType == MouseEventType::MouseUp) {
  267. this->dragComponent = std::shared_ptr<VisualComponent>(); // Abort drag
  268. this->holdCount--;
  269. if (this->holdCount < 0) {
  270. this->holdCount = 0;
  271. }
  272. }
  273. }
  274. void VisualComponent::receiveMouseEvent(const MouseEvent& event) {
  275. if (event.mouseEventType == MouseEventType::MouseDown) {
  276. this->callback_mouseDownEvent(event);
  277. } else if (event.mouseEventType == MouseEventType::MouseUp) {
  278. this->callback_mouseUpEvent(event);
  279. } else if (event.mouseEventType == MouseEventType::MouseMove) {
  280. this->callback_mouseMoveEvent(event);
  281. } else if (event.mouseEventType == MouseEventType::Scroll) {
  282. this->callback_mouseScrollEvent(event);
  283. }
  284. }
  285. void VisualComponent::sendKeyboardEvent(const KeyboardEvent& event) {
  286. // Send the signal to a focused component or itself
  287. if (this->focusComponent.get() != nullptr) {
  288. this->focusComponent->sendKeyboardEvent(event);
  289. } else {
  290. this->receiveKeyboardEvent(event);
  291. }
  292. }
  293. void VisualComponent::receiveKeyboardEvent(const KeyboardEvent& event) {
  294. if (event.keyboardEventType == KeyboardEventType::KeyDown) {
  295. this->callback_keyDownEvent(event);
  296. } else if (event.keyboardEventType == KeyboardEventType::KeyUp) {
  297. this->callback_keyUpEvent(event);
  298. } else if (event.keyboardEventType == KeyboardEventType::KeyType) {
  299. this->callback_keyTypeEvent(event);
  300. }
  301. }
  302. void VisualComponent::applyTheme(VisualTheme theme) {
  303. this->theme = theme;
  304. this->changedTheme(theme);
  305. for (int i = 0; i < this->getChildCount(); i++) {
  306. this->children[i] -> applyTheme(theme);
  307. }
  308. }
  309. VisualTheme VisualComponent::getTheme() const {
  310. return this->theme;
  311. }
  312. void VisualComponent::changedTheme(VisualTheme newTheme) {}
  313. String VisualComponent::call(const ReadableString &methodName, const ReadableString &arguments) {
  314. throwError("Unimplemented custom call received");
  315. return U"";
  316. }