diff --git a/poincare/Makefile b/poincare/Makefile index 66edfe856..09f3b9b62 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -11,6 +11,7 @@ objs += $(addprefix poincare/src/,\ bracket_layout_node.o\ bracket_pair_layout_node.o\ char_layout_node.o\ + condensed_sum_layout_node.o\ conjugate_layout_node.o\ empty_layout_node.o\ horizontal_layout_node.o\ diff --git a/poincare/include/poincare.h b/poincare/include/poincare.h index 06e38ea79..fd36df4be 100644 --- a/poincare/include/poincare.h +++ b/poincare/include/poincare.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include diff --git a/poincare/include/poincare/condensed_sum_layout_node.h b/poincare/include/poincare/condensed_sum_layout_node.h new file mode 100644 index 000000000..2cd0980f2 --- /dev/null +++ b/poincare/include/poincare/condensed_sum_layout_node.h @@ -0,0 +1,64 @@ +#ifndef POINCARE_CONDENSED_SUM_LAYOUT_NODE_H +#define POINCARE_CONDENSED_SUM_LAYOUT_NODE_H + +#include +#include +#include +#include + +namespace Poincare { + +class CondensedSumLayoutNode : public LayoutNode { +public: + using LayoutNode::LayoutNode; + + /* CondensedSumLayout is only used in apps/shared/sum_graph_controller.cpp, in + * a view with no cursor. */ + void moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) override { assert(false); } + void moveCursorRight(LayoutCursor * cursor, bool * shouldRecomputeLayout) override { assert(false); } + int writeTextInBuffer(char * buffer, int bufferSize, PrintFloat::Mode floatDisplayMode, int numberOfSignificantDigits) const override { + return LayoutEngine::writePrefixSerializableRefTextInBuffer(SerializableRef(const_cast(this)), buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "sum"); + } + + LayoutNode * layoutToPointWhenInserting() override { + assert(false); + return nullptr; + } + + // TreeNode + size_t size() const override { return sizeof(CondensedSumLayoutNode); } + int numberOfChildren() const override { return 3; } +#if TREE_LOG + const char * description() const override { + return "CondensedSumLayout"; + } +#endif + +protected: + // LayoutNode + void computeSize() override; + void computeBaseline() override; + KDPoint positionOfChild(LayoutNode * child) override; +private: + void render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) override {} + LayoutNode * baseLayout() { return childAtIndex(0); } + LayoutNode * subscriptLayout() { return childAtIndex(1); } + LayoutNode * superscriptLayout() { return childAtIndex(2); } +}; + +class CondensedSumLayoutRef : public LayoutReference { +public: + CondensedSumLayoutRef(LayoutRef base, LayoutRef subscript, LayoutRef superscript) : + LayoutReference() + { + addChildTreeAtIndex(base, 0); + addChildTreeAtIndex(subscript, 1); + addChildTreeAtIndex(superscript, 2); + } + + CondensedSumLayoutRef(TreeNode * t) : LayoutReference(t) {} +}; + +} + +#endif diff --git a/poincare/src/condensed_sum_layout_node.cpp b/poincare/src/condensed_sum_layout_node.cpp new file mode 100644 index 000000000..28ef6b8b9 --- /dev/null +++ b/poincare/src/condensed_sum_layout_node.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +namespace Poincare { + +void CondensedSumLayoutNode::computeBaseline() { + KDSize superscriptSize = superscriptLayout() == nullptr ? KDSizeZero : superscriptLayout()->layoutSize(); + m_baseline = baseLayout()->baseline() + max(0, superscriptSize.height() - baseLayout()->layoutSize().height()/2); + m_baselined = true; +} + +void CondensedSumLayoutNode::computeSize() { + KDSize baseSize = baseLayout()->layoutSize(); + KDSize subscriptSize = subscriptLayout()->layoutSize(); + KDSize superscriptSize = superscriptLayout() == nullptr ? KDSizeZero : superscriptLayout()->layoutSize(); + KDCoordinate sizeWidth = baseSize.width() + max(subscriptSize.width(), superscriptSize.width()); + KDCoordinate sizeHeight = max(baseSize.height()/2, subscriptSize.height()) + max(baseSize.height()/2, superscriptSize.height()); + m_frame.setSize(KDSize(sizeWidth, sizeHeight)); + m_sized = true; +} + +KDPoint CondensedSumLayoutNode::positionOfChild(LayoutNode * child) { + KDCoordinate x = 0; + KDCoordinate y = 0; + KDSize baseSize = baseLayout()->layoutSize(); + KDSize superscriptSize = superscriptLayout() == nullptr ? KDSizeZero : superscriptLayout()->layoutSize(); + if (child == baseLayout()) { + y = max(0, superscriptSize.height() - baseSize.height()/2); + } + if (child == subscriptLayout()) { + x = baseSize.width(); + y = max(baseSize.height()/2, superscriptSize.height()); + } + if (child == superscriptLayout()) { + x = baseSize.width(); + } + return KDPoint(x,y); +} + +}