Blame view

build3/apps/code/menu_controller.h 4.71 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
  #ifndef CODE_MENU_CONTROLLER_H
  #define CODE_MENU_CONTROLLER_H
  
  #include <escher.h>
  #include <apps/shared/new_function_cell.h>
  #include "console_controller.h"
  #include "editor_controller.h"
  #include "script_parameter_controller.h"
  #include "script_store.h"
  
  namespace Code {
  
  class ScriptParameterController;
  
  class MenuController : public ViewController, public TableViewDataSource, public SelectableTableViewDataSource, public SelectableTableViewDelegate, public TextFieldDelegate, public ButtonRowDelegate {
  public:
    MenuController(Responder * parentResponder, ScriptStore * scriptStore, ButtonRowController * footer);
    ConsoleController * consoleController() { return &m_consoleController; }
    StackViewController * stackViewController();
    void renameSelectedScript();
    void deleteScriptAtIndex(int i);
    void reloadConsole();
    void loadPythonIfNeeded();
    void openConsoleWithScriptAtIndex(int scriptIndex);
    void scriptContentEditionDidFinish();
  
    /* ViewController */
    View * view() override { return &m_selectableTableView; }
    bool handleEvent(Ion::Events::Event event) override;
    void didBecomeFirstResponder() override;
    void viewWillAppear() override;
  
    /* TableViewDataSource */
    int numberOfRows() override;
    int numberOfColumns() override { return 2; }
    void willDisplayCellAtLocation(HighlightCell * cell, int i, int j) override;
    KDCoordinate columnWidth(int i) override;
    KDCoordinate rowHeight(int j) override { return Metric::StoreRowHeight; }
    KDCoordinate cumulatedWidthFromIndex(int i) override;
    KDCoordinate cumulatedHeightFromIndex(int j) override;
    int indexFromCumulatedWidth(KDCoordinate offsetX) override;
    int indexFromCumulatedHeight(KDCoordinate offsetY) override;
    HighlightCell * reusableCell(int index, int type) override;
    int reusableCellCount(int type) override;
    int typeAtLocation(int i, int j) override;
    void willDisplayScriptTitleCellForIndex(HighlightCell * cell, int index);
  
    /* SelectableTableViewDelegate */
    void tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) override;
  
    /* TextFieldDelegate */
    bool textFieldShouldFinishEditing(TextField * textField, Ion::Events::Event event) override;
    bool textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) override;
    bool textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) override;
    bool textFieldDidAbortEditing(TextField * textField, const char * text) override;
    bool textFieldDidHandleEvent(TextField * textField, Ion::Events::Event event, bool returnValue, bool textHasChanged) override;
    Toolbox * toolboxForTextField(TextField * textField) override { return nullptr; }
  
    /* ButtonRowDelegate */
    int numberOfButtons(ButtonRowController::Position position) const override { return 1; }
    Button * buttonAtIndex(int index, ButtonRowController::Position position) const override {
      assert(index == 0);
      return const_cast<Button *>(&m_consoleButton);
    }
  
  private:
    static constexpr int k_maxNumberOfDisplayableScriptCells = 5; // = 240/50
    static constexpr int k_parametersColumnWidth = 37;
    static constexpr int AddScriptCellType = 0;
    static constexpr int ScriptCellType = 1;
    static constexpr int ScriptParameterCellType = 2;
    static constexpr int EmptyCellType = 3;
    static constexpr int k_defaultScriptNameMaxSize = 9 + 2 + 1;
    // k_defaultScriptNameMaxSize is the length of a name between script1.py and
    // script99.py.
    // 9 = strlen("script.py")
    // 2 = maxLength of integers between 1 and 99.
    // 1 = length of null terminating char.
    void addScript();
    void configureScript();
    void editScriptAtIndex(int scriptIndex);
    void numberedDefaultScriptName(char * buffer);
    void intToText(int i, char * buffer);
    void updateAddScriptRowDisplay();
    ScriptStore * m_scriptStore;
    class EvenOddEditableTextCell : public ::EvenOddEditableTextCell {
    public:
      Responder * responder() override {
        return nullptr;
      }
    };
    EvenOddEditableTextCell m_scriptCells[k_maxNumberOfDisplayableScriptCells];
    /* In the initializer list of the MenuController constructor, we initialize
     * m_scriptCells by copying k_maxNumberOfDisplayableScriptCells times the
     * constructor of an EvenOddEditableTextCell. */
    char m_draftTextBuffer[TextField::maxBufferSize()];
    EvenOddCellWithEllipsis m_scriptParameterCells[k_maxNumberOfDisplayableScriptCells];
    Shared::NewFunctionCell m_addNewScriptCell;
    EvenOddCell m_emptyCell;
    Button m_consoleButton;
    SelectableTableView m_selectableTableView;
    ConsoleController m_consoleController;
    ScriptParameterController m_scriptParameterController;
    EditorController m_editorController;
    bool m_reloadConsoleWhenBecomingFirstResponder;
    bool m_shouldDisplayAddScriptRow;
  };
  
  }
  
  #endif