diff --git a/apps/code/Makefile b/apps/code/Makefile index 59ce9135f..ef124d0cb 100644 --- a/apps/code/Makefile +++ b/apps/code/Makefile @@ -8,13 +8,13 @@ app_objs += $(addprefix apps/code/,\ console_edit_cell.o\ console_line_cell.o\ console_store.o\ - double_buffer_text_cell.o\ editor_controller.o\ helpers.o\ menu_controller.o\ python_toolbox.o\ sandbox_controller.o\ script.o\ + script_node_cell.o\ script_parameter_controller.o\ script_store.o\ script_template.o\ diff --git a/apps/code/double_buffer_text_cell.cpp b/apps/code/double_buffer_text_cell.cpp deleted file mode 100644 index c3037cb88..000000000 --- a/apps/code/double_buffer_text_cell.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "double_buffer_text_cell.h" -#include -#include - -DoubleBufferTextCell::DoubleBufferTextCell(KDText::FontSize size, float horizontalAlignment, float verticalAlignment) : - TableCell(TableCell::Layout::Vertical), - m_firstBufferTextView(size, horizontalAlignment, verticalAlignment), - m_secondBufferTextView(size, horizontalAlignment, verticalAlignment) -{ -} - -const char * DoubleBufferTextCell::firstText() { - return m_firstBufferTextView.text(); -} - -const char * DoubleBufferTextCell::secondText() { - return m_secondBufferTextView.text(); -} - -void DoubleBufferTextCell::setFirstText(const char * textContent) { - m_firstBufferTextView.setText(textContent); -} - -void DoubleBufferTextCell::setSecondText(const char * textContent) { - m_secondBufferTextView.setText(textContent); -} - -void DoubleBufferTextCell::setFirstTextColor(KDColor textColor) { - m_firstBufferTextView.setTextColor(textColor); -} - -void DoubleBufferTextCell::setSecondTextColor(KDColor textColor) { - m_secondBufferTextView.setTextColor(textColor); -} - -void DoubleBufferTextCell::setHighlighted(bool highlight) { - HighlightCell::setHighlighted(highlight); - KDColor background = isHighlighted() ? Palette::Select : KDColorWhite; - m_firstBufferTextView.setBackgroundColor(background); - m_secondBufferTextView.setBackgroundColor(background); -} - -View * DoubleBufferTextCell::subviewAtIndex(int index) { - assert(index >= 0 && index <= 1); - if (index == 0) { - return &m_firstBufferTextView; - } - return &m_secondBufferTextView; -} - -void DoubleBufferTextCell::layoutSubviews() { - TableCell::layoutSubviews(); -} diff --git a/apps/code/double_buffer_text_cell.h b/apps/code/double_buffer_text_cell.h deleted file mode 100644 index 280a455da..000000000 --- a/apps/code/double_buffer_text_cell.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef CODE_DOUBLE_BUFFER_TEXT_CELL_H -#define CODE_DOUBLE_BUFFER_TEXT_CELL_H - -#include -#include - -class DoubleBufferTextCell : public TableCell { -public: - DoubleBufferTextCell(KDText::FontSize size = KDText::FontSize::Small, float horizontalAlignment = 0.0f, float verticalAlignment = 0.5f); - const char * firstText(); - const char * secondText(); - void setFirstText(const char * textContent); - void setSecondText(const char * textContent); - void setFirstTextColor(KDColor textColor); - void setSecondTextColor(KDColor textColor); - - /* TableCell */ - View * labelView() const override { return const_cast(static_cast(&m_firstBufferTextView)); } - View * accessoryView() const override { return const_cast(static_cast(&m_secondBufferTextView)); } - - /* HighlightCell */ - void setHighlighted(bool highlight) override; - - /* View */ - int numberOfSubviews() const override { return 2; } - View * subviewAtIndex(int index) override; - void layoutSubviews() override; -protected: - BufferTextView m_firstBufferTextView; - BufferTextView m_secondBufferTextView; -}; - -#endif diff --git a/apps/code/script_node.h b/apps/code/script_node.h new file mode 100644 index 000000000..1bac0f81d --- /dev/null +++ b/apps/code/script_node.h @@ -0,0 +1,35 @@ +#ifndef CODE_SCRIPT_NODE_H +#define CODE_SCRIPT_NODE_H + +#include + +namespace Code { + +class ScriptNode { +public: + enum class Type { + Function = 0, + Variable = 1 + }; + ScriptNode() : + m_type(Type::Function), m_name(nullptr), m_scriptIndex(0) {} + static ScriptNode FunctionNode(const char * name, uint16_t scriptIndex) { + return ScriptNode(Type::Function, name, scriptIndex); + } + static ScriptNode VariableNode(const char * name, uint16_t scriptIndex) { + return ScriptNode(Type::Variable, name, scriptIndex); + } + Type type() const { return m_type; } + const char * name() const { return m_name; } + uint16_t scriptIndex() const { return m_scriptIndex; } +private: + ScriptNode(Type type, const char * name, uint16_t scriptIndex) : + m_type(type), m_name(name), m_scriptIndex(scriptIndex) {} + Type m_type; + const char * m_name; + uint16_t m_scriptIndex; +}; + +} + +#endif diff --git a/apps/code/script_node_cell.cpp b/apps/code/script_node_cell.cpp new file mode 100644 index 000000000..6dd044305 --- /dev/null +++ b/apps/code/script_node_cell.cpp @@ -0,0 +1,69 @@ +#include "script_node_cell.h" +#include +#include + +namespace Code { + +constexpr char ScriptNodeCell::k_parentheses[]; + +ScriptNodeCell::ScriptNodeView::ScriptNodeView() : + HighlightCell(), + m_scriptNode(nullptr), + m_scriptStore(nullptr) +{ +} + +void ScriptNodeCell::ScriptNodeView::setScriptNode(ScriptNode * scriptNode) { + m_scriptNode = scriptNode; +} + +void ScriptNodeCell::ScriptNodeView::setScriptStore(ScriptStore * scriptStore) { + m_scriptStore = scriptStore; +} + +void ScriptNodeCell::ScriptNodeView::drawRect(KDContext * ctx, KDRect rect) const { + ctx->drawString(m_scriptNode->name(), KDPoint(0, Metric::TableCellLabelTopMargin), k_fontSize, KDColorBlack, isHighlighted()? Palette::Select : KDColorWhite); + KDSize nameSize = KDText::stringSize(m_scriptNode->name(), k_fontSize); + if (m_scriptNode->type() == ScriptNode::Type::Function) { + ctx->drawString(ScriptNodeCell::k_parentheses, KDPoint(nameSize.width(), Metric::TableCellLabelTopMargin), k_fontSize, KDColorBlack, isHighlighted()? Palette::Select : KDColorWhite); + } + ctx->drawString(m_scriptStore->scriptAtIndex(m_scriptNode->scriptIndex()).name(), KDPoint(0, Metric::TableCellLabelTopMargin + nameSize.height() + k_verticalMargin), k_fontSize, Palette::GreyDark, isHighlighted()? Palette::Select : KDColorWhite); +} + +KDSize ScriptNodeCell::ScriptNodeView::minimalSizeForOptimalDisplay() const { + KDSize size1 = KDText::stringSize(m_scriptNode->name(), k_fontSize); + KDSize size2 = KDText::stringSize(m_scriptStore->scriptAtIndex(m_scriptNode->scriptIndex()).name(), k_fontSize); + KDSize size3 = KDSizeZero; + if (m_scriptNode->type() == ScriptNode::Type::Function) { + size3 = KDText::stringSize(ScriptNodeCell::k_parentheses, k_fontSize); + } + return KDSize(size1.width() + size3.width() > size2.width() ? size1.width() + size3.width() : size2.width(), Metric::TableCellLabelTopMargin + size1.width() + k_verticalMargin + size2.width()); +} + +ScriptNodeCell::ScriptNodeCell() : + TableCell(), + m_scriptNodeView() +{ +} + +void ScriptNodeCell::setScriptNode(ScriptNode * scriptNode) { + m_scriptNodeView.setScriptNode(scriptNode); + reloadCell(); +} + +void ScriptNodeCell::setScriptStore(ScriptStore * scriptStore) { + m_scriptNodeView.setScriptStore(scriptStore); + reloadCell(); +} + +void ScriptNodeCell::setHighlighted(bool highlight) { + TableCell::setHighlighted(highlight); + m_scriptNodeView.setHighlighted(highlight); +} + +void ScriptNodeCell::reloadCell() { + layoutSubviews(); + HighlightCell::reloadCell(); +} + +} diff --git a/apps/code/script_node_cell.h b/apps/code/script_node_cell.h new file mode 100644 index 000000000..78053ea84 --- /dev/null +++ b/apps/code/script_node_cell.h @@ -0,0 +1,44 @@ +#ifndef CODE_SCRIPT_NODE_CELL_H +#define CODE_SCRIPT_NODE_CELL_H + +#include "script_node.h" +#include "script_store.h" +#include +#include + +namespace Code { + +class ScriptNodeCell : public TableCell { +public: + ScriptNodeCell(); + void setScriptNode(ScriptNode * node); + void setScriptStore(ScriptStore * scriptStore); + + /* TableCell */ + View * labelView() const override { return const_cast(static_cast(&m_scriptNodeView)); } + + /* HighlightCell */ + void setHighlighted(bool highlight) override; + void reloadCell() override; + + constexpr static char k_parentheses[] = "()"; +protected: + class ScriptNodeView : public HighlightCell { + public: + ScriptNodeView(); + void setScriptNode(ScriptNode * scriptNode); + void setScriptStore(ScriptStore * scriptStore); + void drawRect(KDContext * ctx, KDRect rect) const override; + virtual KDSize minimalSizeForOptimalDisplay() const override; + private: + constexpr static KDText::FontSize k_fontSize = KDText::FontSize::Small; + constexpr static KDCoordinate k_verticalMargin = 7; + ScriptNode * m_scriptNode; + ScriptStore * m_scriptStore; + }; + ScriptNodeView m_scriptNodeView; +}; + +} + +#endif diff --git a/apps/code/variable_box_controller.cpp b/apps/code/variable_box_controller.cpp index bbcd6e9cb..9fe0589b7 100644 --- a/apps/code/variable_box_controller.cpp +++ b/apps/code/variable_box_controller.cpp @@ -20,8 +20,7 @@ VariableBoxController::ContentViewController::ContentViewController(Responder * m_selectableTableView(this, this, 0, 1, 0, 0, 0, 0, this, nullptr, false) { for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) { - m_leafCells[i].setFirstTextColor(KDColorBlack); - m_leafCells[i].setSecondTextColor(Palette::GreyDark); + m_leafCells[i].setScriptStore(scriptStore); } } @@ -87,8 +86,11 @@ bool VariableBoxController::ContentViewController::handleEvent(Ion::Events::Even return true; } if (event == Ion::Events::OK || event == Ion::Events::EXE) { - DoubleBufferTextCell * selectedTextCell = static_cast(m_selectableTableView.selectedCell()); - insertTextInCaller(selectedTextCell->firstText()); + ScriptNode selectedScriptNode = m_scriptNodes[m_selectableTableView.selectedRow()]; + insertTextInCaller(selectedScriptNode.name()); + if (selectedScriptNode.type() == ScriptNode::Type::Function) { + insertTextInCaller(ScriptNodeCell::k_parentheses); + } m_selectableTableView.deselectTable(); app()->dismissModalViewController(); return true; @@ -112,9 +114,8 @@ int VariableBoxController::ContentViewController::reusableCellCount(int type) { } void VariableBoxController::ContentViewController::willDisplayCellForIndex(HighlightCell * cell, int index) { - DoubleBufferTextCell * myCell = static_cast(cell); - myCell->setFirstText(m_scriptNodes[index].name()); - myCell->setSecondText(m_scriptStore->scriptAtIndex(m_scriptNodes[index].scriptIndex()).name()); + ScriptNodeCell * myCell = static_cast(cell); + myCell->setScriptNode(&m_scriptNodes[index]); } KDCoordinate VariableBoxController::ContentViewController::rowHeight(int index) { diff --git a/apps/code/variable_box_controller.h b/apps/code/variable_box_controller.h index f21eb4abb..dfd7cbe3e 100644 --- a/apps/code/variable_box_controller.h +++ b/apps/code/variable_box_controller.h @@ -2,8 +2,9 @@ #define CODE_VARIABLE_BOX_CONTROLLER_H #include -#include "double_buffer_text_cell.h" #include "menu_controller.h" +#include "script_node.h" +#include "script_node_cell.h" #include "script_store.h" namespace Code { @@ -47,31 +48,6 @@ private: int indexFromCumulatedHeight(KDCoordinate offsetY) override; int typeAtLocation(int i, int j) override; private: - class ScriptNode { - public: - enum class Type { - Function = 0, - Variable = 1 - }; - ScriptNode() : - m_type(Type::Function), m_name(nullptr), m_scriptIndex(0) {} - static ScriptNode FunctionNode(const char * name, uint16_t scriptIndex) { - return ScriptNode(Type::Function, name, scriptIndex); - } - static ScriptNode VariableNode(const char * name, uint16_t scriptIndex) { - return ScriptNode(Type::Variable, name, scriptIndex); - } - Type type() const { return m_type; } - const char * name() const { return m_name; } - uint16_t scriptIndex() const { return m_scriptIndex; } - private: - ScriptNode(Type type, const char * name, uint16_t scriptIndex) : - m_type(type), m_name(name), m_scriptIndex(scriptIndex) {} - Type m_type; - const char * m_name; - uint16_t m_scriptIndex; - }; - constexpr static int k_maxNumberOfDisplayedRows = 6; //240/40 constexpr static int k_leafType = 0; constexpr static int k_maxScriptNodesCount = 32; @@ -82,7 +58,7 @@ private: ScriptStore * m_scriptStore; TextField * m_textFieldCaller; TextArea * m_textAreaCaller; - DoubleBufferTextCell m_leafCells[k_maxNumberOfDisplayedRows]; + ScriptNodeCell m_leafCells[k_maxNumberOfDisplayedRows]; SelectableTableView m_selectableTableView; }; ContentViewController m_contentViewController; diff --git a/escher/include/escher/buffer_text_view.h b/escher/include/escher/buffer_text_view.h index 0503f9801..78de5482a 100644 --- a/escher/include/escher/buffer_text_view.h +++ b/escher/include/escher/buffer_text_view.h @@ -9,6 +9,7 @@ public: KDColor textColor = KDColorBlack, KDColor backgroundColor = KDColorWhite); void setText(const char * text) override; const char * text() const override; + void appendText(const char * text); static int maxNumberOfCharsInBuffer() { return k_maxNumberOfChar; } private: static constexpr int k_maxNumberOfChar = 256; diff --git a/escher/src/buffer_text_view.cpp b/escher/src/buffer_text_view.cpp index 8d78f9c80..ea006c69a 100644 --- a/escher/src/buffer_text_view.cpp +++ b/escher/src/buffer_text_view.cpp @@ -18,3 +18,11 @@ void BufferTextView::setText(const char * text) { strlcpy(m_buffer, text, sizeof(m_buffer)); markRectAsDirty(bounds()); } + +void BufferTextView::appendText(const char * text) { + size_t previousTextLength = strlen(m_buffer); + size_t argTextLength = strlen(text); + if (previousTextLength + argTextLength + 1 < k_maxNumberOfChar) { + strlcpy(&m_buffer[previousTextLength], text, argTextLength + 1); + } +}