VisualComponent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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(FlexRegion newRegion) {
  45. this->region = newRegion;
  46. }
  47. FlexRegion VisualComponent::getRegion() const {
  48. return this->region;
  49. }
  50. void VisualComponent::setHidden(bool hidden) {
  51. this->hidden.value = hidden;
  52. }
  53. bool VisualComponent::getHidden() const {
  54. return this->hidden.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(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->getHidden()) {
  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. ImageRgbaU8 target = image_getSubImage(targetImage, finalRegion);
  100. this->draw(target, offset - finalRegion.upperLeft());
  101. }
  102. }
  103. // A red rectangle is drawn as a placeholder if the class couldn't be found
  104. // TODO: Should the type name be remembered in the base class for serializing missing components?
  105. void VisualComponent::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  106. draw_rectangle(targetImage, relativeLocation, ColorRgbaI32(200, 50, 50, 255));
  107. }
  108. // Manual use with the correct type
  109. void VisualComponent::addChildComponent(std::shared_ptr<VisualComponent> child) {
  110. if (!this->isContainer()) {
  111. throwError(U"Cannot attach a child to a non-container parent component!\n");
  112. } else if (child.get() == this) {
  113. throwError(U"Cannot attach a component to itself!\n");
  114. } else if (child->hasChild(this)) {
  115. throwError(U"Cannot attach to its own parent as a child component!\n");
  116. } else {
  117. // Remove from any previous parent
  118. child->detachFromParent();
  119. // Update layout based on the new parent size
  120. child->applyLayout(this->getSize());
  121. // Connect to the new parent
  122. this->children.push(child);
  123. child->parent = this;
  124. }
  125. }
  126. // Automatic insertion from loading
  127. bool VisualComponent::addChild(std::shared_ptr<Persistent> child) {
  128. // Try to cast from base class Persistent to derived class VisualComponent
  129. std::shared_ptr<VisualComponent> visualComponent = std::dynamic_pointer_cast<VisualComponent>(child);
  130. if (visualComponent.get() == nullptr) {
  131. return false; // Wrong type!
  132. } else {
  133. this->addChildComponent(visualComponent);
  134. return true; // Success!
  135. }
  136. }
  137. int VisualComponent::getChildCount() const {
  138. return this->children.length();
  139. }
  140. std::shared_ptr<Persistent> VisualComponent::getChild(int index) const {
  141. return this->children[index];
  142. }
  143. void VisualComponent::detachFromParent() {
  144. // Check if there's a parent component
  145. VisualComponent *parent = this->parent;
  146. if (parent != nullptr) {
  147. // If the removed component is focused from the parent, then remove focus
  148. if (parent->focusComponent.get() == this) {
  149. parent->focusComponent = std::shared_ptr<VisualComponent>();
  150. }
  151. // Iterate over all children in the parent component
  152. for (int i = 0; i < parent->getChildCount(); i++) {
  153. std::shared_ptr<VisualComponent> current = parent->children[i];
  154. if (current.get() == this) {
  155. current->parent = nullptr; // Assign null
  156. parent->children.remove(i);
  157. return;
  158. }
  159. }
  160. }
  161. }
  162. bool VisualComponent::hasChild(VisualComponent *child) const {
  163. for (int i = 0; i < this->getChildCount(); i++) {
  164. std::shared_ptr<VisualComponent> current = this->children[i];
  165. if (current.get() == child) {
  166. return true; // Found the component
  167. } else {
  168. if (current->hasChild(child)) {
  169. return true; // Found the component recursively
  170. }
  171. }
  172. }
  173. return false; // Could not find the component
  174. }
  175. bool VisualComponent::hasChild(std::shared_ptr<VisualComponent> child) const {
  176. return this->hasChild(child.get());
  177. }
  178. std::shared_ptr<VisualComponent> VisualComponent::findChildByName(ReadableString name, bool mustExist) const {
  179. for (int i = 0; i < this->getChildCount(); i++) {
  180. std::shared_ptr<VisualComponent> current = this->children[i];
  181. if (string_match(current->getName(), name)) {
  182. return current; // Found the component
  183. } else {
  184. std::shared_ptr<VisualComponent> searchResult = current->findChildByName(name, mustExist);
  185. if (searchResult.get() != nullptr) {
  186. return searchResult; // Found the component recursively
  187. }
  188. }
  189. }
  190. return std::shared_ptr<VisualComponent>(); // Could not find the component
  191. }
  192. std::shared_ptr<VisualComponent> VisualComponent::findChildByNameAndIndex(ReadableString name, int index, bool mustExist) const {
  193. for (int i = 0; i < this->getChildCount(); i++) {
  194. std::shared_ptr<VisualComponent> current = this->children[i];
  195. if (string_match(current->getName(), name) && current->getIndex() == index) {
  196. return current; // Found the component
  197. } else {
  198. std::shared_ptr<VisualComponent> searchResult = current->findChildByNameAndIndex(name, index, mustExist);
  199. if (searchResult.get() != nullptr) {
  200. return searchResult; // Found the component recursively
  201. }
  202. }
  203. }
  204. return std::shared_ptr<VisualComponent>(); // Could not find the component
  205. }
  206. bool VisualComponent::pointIsInside(const IVector2D& pixelPosition) {
  207. return pixelPosition.x > this->location.left() && pixelPosition.x < this->location.right()
  208. && pixelPosition.y > this->location.top() && pixelPosition.y < this->location.bottom();
  209. }
  210. // Non-recursive top-down search
  211. std::shared_ptr<VisualComponent> VisualComponent::getDirectChild(const IVector2D& pixelPosition, bool includeInvisible) {
  212. // Iterate child components in reverse drawing order
  213. for (int i = this->getChildCount() - 1; i >= 0; i--) {
  214. std::shared_ptr<VisualComponent> currentChild = this->children[i];
  215. // Check if the point is inside the child component
  216. if ((!currentChild->getHidden() || includeInvisible) && currentChild->pointIsInside(pixelPosition)) {
  217. return currentChild;
  218. }
  219. }
  220. // Return nothing if the point missed all child components
  221. return std::shared_ptr<VisualComponent>();
  222. }
  223. // Recursive top-down search
  224. std::shared_ptr<VisualComponent> VisualComponent::getTopChild(const IVector2D& pixelPosition, bool includeInvisible) {
  225. // Iterate child components in reverse drawing order
  226. for (int i = this->getChildCount() - 1; i >= 0; i--) {
  227. std::shared_ptr<VisualComponent> currentChild = this->children[i];
  228. // Check if the point is inside the child component
  229. if ((!currentChild->getHidden() || includeInvisible) && currentChild->pointIsInside(pixelPosition)) {
  230. // Check if a component inside the child component is even higher up
  231. std::shared_ptr<VisualComponent> subChild = currentChild->getTopChild(pixelPosition - this->getLocation().upperLeft(), includeInvisible);
  232. if (subChild.get() != nullptr) {
  233. return subChild;
  234. } else {
  235. return currentChild;
  236. }
  237. }
  238. }
  239. // Return nothing if the point missed all child components
  240. return std::shared_ptr<VisualComponent>();
  241. }
  242. void VisualComponent::sendMouseEvent(const MouseEvent& event) {
  243. // Convert to local coordinates recursively
  244. MouseEvent localEvent = event - this->getLocation().upperLeft();
  245. std::shared_ptr<VisualComponent> childComponent;
  246. // Grab a component on mouse down
  247. if (event.mouseEventType == MouseEventType::MouseDown) {
  248. childComponent = this->dragComponent = this->focusComponent = this->getDirectChild(localEvent.position, false);
  249. this->holdCount++;
  250. }
  251. if (this->holdCount > 0) {
  252. // If we're grabbing a component, keep sending events to it
  253. childComponent = this->dragComponent;
  254. } else if (!this->getHidden() && this->pointIsInside(event.position)) {
  255. // If we're not grabbing a component, see if we can send the action to another component
  256. childComponent = this->getDirectChild(localEvent.position, false);
  257. }
  258. // Send the signal to a child component or itself
  259. if (childComponent.get() != nullptr) {
  260. childComponent->sendMouseEvent(localEvent);
  261. } else {
  262. this->receiveMouseEvent(event);
  263. }
  264. // Release a component on mouse up
  265. if (event.mouseEventType == MouseEventType::MouseUp) {
  266. this->dragComponent = std::shared_ptr<VisualComponent>(); // Abort drag
  267. this->holdCount--;
  268. if (this->holdCount < 0) {
  269. this->holdCount = 0;
  270. }
  271. }
  272. }
  273. void VisualComponent::receiveMouseEvent(const MouseEvent& event) {
  274. if (event.mouseEventType == MouseEventType::MouseDown) {
  275. this->callback_mouseDownEvent(event);
  276. } else if (event.mouseEventType == MouseEventType::MouseUp) {
  277. this->callback_mouseUpEvent(event);
  278. } else if (event.mouseEventType == MouseEventType::MouseMove) {
  279. this->callback_mouseMoveEvent(event);
  280. } else if (event.mouseEventType == MouseEventType::Scroll) {
  281. this->callback_mouseScrollEvent(event);
  282. }
  283. }
  284. void VisualComponent::sendKeyboardEvent(const KeyboardEvent& event) {
  285. // Send the signal to a focused component or itself
  286. if (this->focusComponent.get() != nullptr) {
  287. this->focusComponent->sendKeyboardEvent(event);
  288. } else {
  289. this->receiveKeyboardEvent(event);
  290. }
  291. }
  292. void VisualComponent::receiveKeyboardEvent(const KeyboardEvent& event) {
  293. if (event.keyboardEventType == KeyboardEventType::KeyDown) {
  294. this->callback_keyDownEvent(event);
  295. } else if (event.keyboardEventType == KeyboardEventType::KeyUp) {
  296. this->callback_keyUpEvent(event);
  297. } else if (event.keyboardEventType == KeyboardEventType::KeyType) {
  298. this->callback_keyTypeEvent(event);
  299. }
  300. }
  301. void VisualComponent::applyTheme(VisualTheme theme) {
  302. this->theme = theme;
  303. this->changedTheme(theme);
  304. for (int i = 0; i < this->getChildCount(); i++) {
  305. this->children[i] -> applyTheme(theme);
  306. }
  307. }
  308. VisualTheme VisualComponent::getTheme() const {
  309. return this->theme;
  310. }
  311. void VisualComponent::changedTheme(VisualTheme newTheme) {}
  312. String VisualComponent::call(const ReadableString &methodName, const ReadableString &arguments) {
  313. throwError("Unimplemented custom call received");
  314. return U"";
  315. }