From 0863abc4dabc20e18dfbbf6d551fd7ed5e599f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 3 Apr 2020 11:26:29 +0200 Subject: [PATCH] [apps/code] VariableBox cells with node origins --- apps/code/variable_box_controller.cpp | 95 ++++++++++++++++++++++++--- apps/code/variable_box_controller.h | 37 ++++++----- escher/src/even_odd_cell.cpp | 2 +- 3 files changed, 105 insertions(+), 29 deletions(-) diff --git a/apps/code/variable_box_controller.cpp b/apps/code/variable_box_controller.cpp index 89ed28d3d..3cfe89762 100644 --- a/apps/code/variable_box_controller.cpp +++ b/apps/code/variable_box_controller.cpp @@ -54,17 +54,12 @@ VariableBoxController::VariableBoxController(ScriptStore * scriptStore) : m_importedNodesCount(0) { for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) { - m_leafCells[i].setScriptStore(scriptStore); + m_itemCells[i].setScriptStore(scriptStore); } - I18n::Message subtitleMessages[k_scriptOriginsCount] = { - I18n::Message::CurrentScript, - I18n::Message::BuiltinFunctionsAndKeyWords, - I18n::Message::ImportedModulesAndScripts - }; for (int i = 0; i < k_scriptOriginsCount; i++) { - m_subtitleCells[i].setEven(true); + m_subtitleCells[i].setEven(false); m_subtitleCells[i].setAlignment(0.0f, 0.5f); - m_subtitleCells[i].setMessage(subtitleMessages[i], Palette::BlueishGrey); + m_subtitleCells[i].setMessageFont(KDFont::SmallFont); } } @@ -83,10 +78,64 @@ void VariableBoxController::didEnterResponderChain(Responder * previousFirstResp assert(App::app()->pythonIsInited()); } +KDCoordinate VariableBoxController::rowHeight(int j) { + int cellType = typeAndOriginAtLocation(j, nullptr); + if (cellType == k_itemCellType) { + return k_simpleItemRowHeight; // TODO LEA + } + assert(cellType == k_subtitleCellType); + return k_subtitleRowHeight; +} + +int VariableBoxController::numberOfRows() const { + int result = 0; + NodeOrigin origins[] = {NodeOrigin::CurrentScript, NodeOrigin::Builtins, NodeOrigin::Importation}; + for (NodeOrigin origin : origins) { + int nodeCount = nodesCountForOrigin(origin); + if (nodeCount > 0) { + result += nodeCount + 1; // 1 for the subtitle cell + } + } + return result; +} + +HighlightCell * VariableBoxController::reusableCell(int index, int type) { + assert(index >= 0 && index < reusableCellCount(type)); + if (type == k_itemCellType) { + return m_itemCells + index; + } + assert(type == k_subtitleCellType); + return m_subtitleCells + index; +} + +int VariableBoxController::reusableCellCount(int type) { + if (type == k_subtitleCellType) { + return k_scriptOriginsCount; + } + assert(type == k_itemCellType); + return k_maxNumberOfDisplayedRows; +} + void VariableBoxController::willDisplayCellForIndex(HighlightCell * cell, int index) { assert(index >= 0 && index < numberOfRows()); - ScriptNodeCell * myCell = static_cast(cell); - myCell->setScriptNode(scriptNodeAtIndex(index)); + NodeOrigin cellOrigin = NodeOrigin::CurrentScript; + int cellType = typeAndOriginAtLocation(index, &cellOrigin); + if (cellType == k_itemCellType) { + static_cast(cell)->setScriptNode(scriptNodeAtIndex(index)); + return; + } + assert(cellType == k_subtitleCellType); + I18n::Message subtitleMessages[k_scriptOriginsCount] = { + I18n::Message::CurrentScript, + I18n::Message::BuiltinFunctionsAndKeyWords, + I18n::Message::ImportedModulesAndScripts + }; + static_cast(cell)->setMessage(subtitleMessages[(int)cellOrigin], Palette::BlueishGrey); +} + +int VariableBoxController::typeAtLocation(int i, int j) { + assert(i == 0); + return typeAndOriginAtLocation(j, nullptr); } void VariableBoxController::loadFunctionsAndVariables(int scriptIndex, const char * textToAutocomplete, int textToAutocompleteLength) { @@ -228,6 +277,32 @@ ScriptNode * VariableBoxController::scriptNodeAtIndex(int index) { return nullptr; } +int VariableBoxController::typeAndOriginAtLocation(int i, NodeOrigin * resultOrigin) const { + int cellIndex = 0; + NodeOrigin origins[] = {NodeOrigin::CurrentScript, NodeOrigin::Builtins, NodeOrigin::Importation}; + for (NodeOrigin origin : origins) { + int nodeCount = nodesCountForOrigin(origin); + if (nodeCount > 0) { + if (i == cellIndex) { + if (resultOrigin != nullptr) { + *resultOrigin = origin; + } + return k_subtitleCellType; + } + cellIndex += nodeCount + 1; // 1 for the subtitle cell + if (i < cellIndex) { + if (resultOrigin != nullptr) { + *resultOrigin = origin; + } + return k_itemCellType; + } + } + } + assert(false); + return k_itemCellType; + +} + bool VariableBoxController::selectLeaf(int rowIndex) { assert(rowIndex >= 0 && rowIndex < numberOfRows()); m_selectableTableView.deselectTable(); diff --git a/apps/code/variable_box_controller.h b/apps/code/variable_box_controller.h index bd5eae0bf..bf0e7a762 100644 --- a/apps/code/variable_box_controller.h +++ b/apps/code/variable_box_controller.h @@ -16,16 +16,14 @@ public: bool handleEvent(Ion::Events::Event event) override; void didEnterResponderChain(Responder * previousFirstResponder) override; + /* TableViewDataSource */ + KDCoordinate rowHeight(int j) override; + int numberOfRows() const override; + HighlightCell * reusableCell(int index, int type) override; + int reusableCellCount(int type) override; + int typeAtLocation(int i, int j) override; /* ListViewDataSource */ - int numberOfRows() const override { - return m_currentScriptNodesCount + m_builtinNodesCount + m_importedNodesCount; - } - int reusableCellCount(int type) override { - assert(type == 0); - return k_maxNumberOfDisplayedRows; - } void willDisplayCellForIndex(HighlightCell * cell, int index) override; - int typeAtLocation(int i, int j) override { return 0; } /* VariableBoxController */ void loadFunctionsAndVariables(int scriptIndex, const char * textToAutocomplete, int textToAutocompleteLength); @@ -34,14 +32,19 @@ public: private: //TODO LEA use size_t constexpr static int k_maxScriptObjectNameSize = 100; //TODO LEA - constexpr static int k_maxNumberOfDisplayedRows = 6; // 240/40 + constexpr static int k_maxNumberOfDisplayedRows = 8; // (240 - titlebar - margin)/27 //TODO LEA constexpr static int k_maxScriptNodesCount = 32; //TODO LEA constexpr static int k_totalBuiltinNodesCount = 98; constexpr static uint8_t k_scriptOriginsCount = 3; + constexpr static uint8_t k_subtitleCellType = 0; + constexpr static uint8_t k_itemCellType = 1; + constexpr static KDCoordinate k_subtitleRowHeight = 23; + constexpr static KDCoordinate k_simpleItemRowHeight = 27; + constexpr static KDCoordinate k_complexItemRowHeight = 44; enum class NodeOrigin : uint8_t { - CurrentScript, - Builtins, - Importation + CurrentScript = 0, + Builtins = 1, + Importation = 2 }; /* Returns: @@ -70,13 +73,11 @@ private: ScriptNode * scriptNodeAtIndex(int index); // Cell getters - HighlightCell * leafCellAtIndex(int index) override { - assert(index >= 0 && index < k_maxNumberOfDisplayedRows); - return &m_leafCells[index]; - } - HighlightCell * nodeCellAtIndex(int index) override { return nullptr; } + int typeAndOriginAtLocation(int i, NodeOrigin * resultOrigin) const; // NestedMenuController + HighlightCell * leafCellAtIndex(int index) override { assert(false); return nullptr; } + HighlightCell * nodeCellAtIndex(int index) override { assert(false); return nullptr; } bool selectLeaf(int rowIndex) override; void insertTextInCaller(const char * text, int textLength = -1); @@ -98,7 +99,7 @@ private: ScriptNode m_currentScriptNodes[k_maxScriptNodesCount]; ScriptNode m_builtinNodes[k_totalBuiltinNodesCount]; ScriptNode m_importedNodes[k_maxScriptNodesCount]; - ScriptNodeCell m_leafCells[k_maxNumberOfDisplayedRows]; + ScriptNodeCell m_itemCells[k_maxNumberOfDisplayedRows]; EvenOddMessageTextCell m_subtitleCells[k_scriptOriginsCount]; ScriptStore * m_scriptStore; // TODO LEA Put these in an array? diff --git a/escher/src/even_odd_cell.cpp b/escher/src/even_odd_cell.cpp index 4e6b0bb9f..83b432861 100644 --- a/escher/src/even_odd_cell.cpp +++ b/escher/src/even_odd_cell.cpp @@ -16,7 +16,7 @@ void EvenOddCell::setEven(bool even) { KDColor EvenOddCell::backgroundColor() const { // Select the background color according to the even line and the cursor selection - KDColor background = m_even ? KDColorWhite : Palette::WallScreen ; + KDColor background = m_even ? KDColorWhite : Palette::WallScreen; background = isHighlighted() ? Palette::Select : background; return background; }