[apps/code] VariableBox cells with node origins

This commit is contained in:
Léa Saviot
2020-04-03 11:26:29 +02:00
committed by Émilie Feral
parent 52644bf76d
commit 0863abc4da
3 changed files with 105 additions and 29 deletions

View File

@@ -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<ScriptNodeCell *>(cell);
myCell->setScriptNode(scriptNodeAtIndex(index));
NodeOrigin cellOrigin = NodeOrigin::CurrentScript;
int cellType = typeAndOriginAtLocation(index, &cellOrigin);
if (cellType == k_itemCellType) {
static_cast<ScriptNodeCell *>(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<EvenOddMessageTextCell *>(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();

View File

@@ -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?