Blame view

Modif/epsilon-master/apps/code/variable_box_controller.cpp 5.36 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  #include "variable_box_controller.h"
  #include "script.h"
  #include "../shared/toolbox_helpers.h"
  #include <apps/i18n.h>
  #include <assert.h>
  #include <escher/buffer_text_view.h>
  #include <escher/palette.h>
  #include <string.h>
  
  namespace Code {
  
  /* ContentViewController */
  
  VariableBoxController::ContentViewController::ContentViewController(Responder * parentResponder, MenuController * menuController, ScriptStore * scriptStore) :
    ViewController(parentResponder),
    m_scriptNodesCount(0),
    m_menuController(menuController),
    m_scriptStore(scriptStore),
    m_selectableTableView(this)
  {
    m_selectableTableView.setMargins(0);
    m_selectableTableView.setShowsIndicators(false);
    for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) {
      m_leafCells[i].setScriptStore(scriptStore);
    }
  }
  
  void VariableBoxController::ContentViewController::setTextInputCaller(TextInput * textInput) {
    m_textInputCaller = textInput;
  }
  
  void VariableBoxController::ContentViewController::reloadData() {
    m_selectableTableView.reloadData();
  }
  
  void VariableBoxController::ContentViewController::addFunctionAtIndex(const char * functionName, int scriptIndex) {
    m_scriptNodes[m_scriptNodesCount] = ScriptNode::FunctionNode(functionName, scriptIndex);
    m_scriptNodesCount++;
  }
  
  void VariableBoxController::ContentViewController::addVariableAtIndex(const char * variableName, int scriptIndex) {
    m_scriptNodes[m_scriptNodesCount] = ScriptNode::VariableNode(variableName, scriptIndex);
    m_scriptNodesCount++;
  }
  
  const char * VariableBoxController::ContentViewController::title() {
    return  I18n::translate(I18n::Message::FunctionsAndVariables);
  }
  
  void VariableBoxController::ContentViewController::viewWillAppear() {
    m_menuController->loadPythonIfNeeded();
    m_scriptNodesCount = 0;
    m_scriptStore->scanScriptsForFunctionsAndVariables(
      this,
      [](void * context, const char * functionName, int scriptIndex) {
        VariableBoxController::ContentViewController * cvc = static_cast<VariableBoxController::ContentViewController *>(context);
        cvc->addFunctionAtIndex(functionName, scriptIndex);},
      [](void * context, const char * variableName, int scriptIndex) {
        VariableBoxController::ContentViewController * cvc = static_cast<VariableBoxController::ContentViewController *>(context);
        cvc->addVariableAtIndex(variableName, scriptIndex);});
  }
  
  void VariableBoxController::ContentViewController::viewDidDisappear() {
    m_selectableTableView.deselectTable();
    for (int i = 0; i < k_maxScriptNodesCount; i++) {
      m_scriptNodes[i] = ScriptNode();
    }
    ViewController::viewDidDisappear();
  }
  
  void VariableBoxController::ContentViewController::didBecomeFirstResponder() {
    m_selectableTableView.reloadData();
    m_selectableTableView.scrollToCell(0,0);
    selectCellAtLocation(0, 0);
    app()->setFirstResponder(&m_selectableTableView);
  }
  
  bool VariableBoxController::ContentViewController::handleEvent(Ion::Events::Event event) {
    if (event == Ion::Events::Back) {
      app()->dismissModalViewController();
      return true;
    }
    if (event == Ion::Events::Left) {
      return true;
    }
    if (event == Ion::Events::OK || event == Ion::Events::EXE) {
      if (m_selectableTableView.selectedRow() < 0 || m_selectableTableView.selectedRow() >= m_scriptNodesCount) {
        return false;
      }
      ScriptNode selectedScriptNode = m_scriptNodes[m_selectableTableView.selectedRow()];
      insertTextInCaller(selectedScriptNode.name());
      if (selectedScriptNode.type() == ScriptNode::Type::Function) {
        insertTextInCaller(ScriptNodeCell::k_parenthesesWithEmpty);
      }
      m_selectableTableView.deselectTable();
      app()->dismissModalViewController();
      return true;
    }
    return false;
  }
  
  int VariableBoxController::ContentViewController::numberOfRows() {
    return m_scriptNodesCount < k_maxScriptNodesCount ? m_scriptNodesCount : k_maxScriptNodesCount;
  }
  
  HighlightCell * VariableBoxController::ContentViewController::reusableCell(int index) {
    assert(index >= 0 && index < k_maxNumberOfDisplayedRows);
    return &m_leafCells[index];
  }
  
  int VariableBoxController::ContentViewController::reusableCellCount() {
    return k_maxNumberOfDisplayedRows;
  }
  
  void VariableBoxController::ContentViewController::willDisplayCellForIndex(HighlightCell * cell, int index) {
    ScriptNodeCell * myCell = static_cast<ScriptNodeCell *>(cell);
    myCell->setScriptNode(&m_scriptNodes[index]);
  }
  
  void VariableBoxController::ContentViewController::insertTextInCaller(const char * text) {
    int commandBufferMaxSize = strlen(text)+1;
    char commandBuffer[commandBufferMaxSize];
    Shared::ToolboxHelpers::TextToInsertForCommandText(text, commandBuffer, commandBufferMaxSize, true);
    m_textInputCaller->handleEventWithText(commandBuffer);
  }
  
  VariableBoxController::VariableBoxController(MenuController * menuController, ScriptStore * scriptStore) :
    StackViewController(nullptr, &m_contentViewController, KDColorWhite, Palette::PurpleBright, Palette::PurpleDark),
    m_contentViewController(this, menuController, scriptStore)
  {
  }
  
  void VariableBoxController::didBecomeFirstResponder() {
    app()->setFirstResponder(&m_contentViewController);
  }
  
  void VariableBoxController::setTextInputCaller(TextInput * textInput) {
    m_contentViewController.setTextInputCaller(textInput);
  }
  
  void VariableBoxController::viewWillAppear() {
    StackViewController::viewWillAppear();
    m_contentViewController.reloadData();
  }
  
  void VariableBoxController::viewDidDisappear() {
    StackViewController::viewDidDisappear();
  }
  
  }