From b7656aba130af1d7da0e3930d2b2bb6fd13b9cd6 Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Wed, 15 Jun 2016 14:44:03 +0200 Subject: [PATCH] Escher: Bigger demo Change-Id: I91a9e6b70dce7449a912a77238994653edee04f8 --- app/escher_demo.cpp | 149 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 136 insertions(+), 13 deletions(-) diff --git a/app/escher_demo.cpp b/app/escher_demo.cpp index d16de4896..484b34793 100644 --- a/app/escher_demo.cpp +++ b/app/escher_demo.cpp @@ -5,24 +5,53 @@ extern "C" { } #include -class MyFunCell : public ChildlessView { +class MyFunCell : public ChildlessView, public Responder { public: MyFunCell(); + void setMessage(char * message); + void setEven(bool even); + void drawRect(KDRect rect) const override; + void setFocused(bool focused) override; +private: + char * m_message; + bool m_focused; + bool m_even; }; MyFunCell::MyFunCell() : - ChildlessView() + ChildlessView(), + m_focused(false), + m_even(false) { + m_message = "NULL"; } void MyFunCell::drawRect(KDRect rect) const { - KDDrawString("Cell", KDPointZero, 0); + KDColor background = m_even ? KDColorRGB(0xEE, 0xEE, 0xEE) : KDColorRGB(0x77,0x77,0x77); + KDFillRect(rect, background); + KDDrawString(m_message, KDPointZero, m_focused); +} + +void MyFunCell::setMessage(char * message) { + m_message = message; +} + +void MyFunCell::setFocused(bool focused) { + m_focused = focused; + markAsNeedingRedraw(); +} + +void MyFunCell::setEven(bool even) { + m_even = even; } class ListController : public ViewController, public TableViewDataSource { public: ListController(); + + void setActiveCell(int index); + View * view() override; const char * title() const override; bool handleEvent(ion_event_t event) override; @@ -33,17 +62,31 @@ public: View * reusableCell(int index) override; int reusableCellCount() override; private: + constexpr static int k_totalNumberOfModels = 20; constexpr static int k_maxNumberOfCells = 10; // !!! CAUTION: The order here is important // The cells should be initialized *before* the tableview! MyFunCell m_cells[k_maxNumberOfCells]; TableView m_tableView; + char ** m_messages; + int m_activeCell; + KDCoordinate m_manualScrolling; +}; + +static char * sMessages[] = { + "AAA 0", "BBB 1", "CCC 2", "DDD 3", "EEE 4", + "FFF 5", "GGG 6", "HHH 7", "III 8", "JJJ 9", + "KKK10", "LLL11", "MMM12", "NNN13", "OOO14", + "PPP15", "QQQ16", "RRR17", "SSS18", "TTT19" }; ListController::ListController() : ViewController(), - m_tableView(TableView(this)) + m_tableView(TableView(this)), + m_activeCell(0), + m_manualScrolling(0) { + m_messages = sMessages; } View * ListController::view() { @@ -54,16 +97,37 @@ const char * ListController::title() const { return "List"; } -bool ListController::handleEvent(ion_event_t event) { - if (event != ENTER) { - return false; +void ListController::setActiveCell(int index) { + if (index < 0 || index >= k_totalNumberOfModels) { + return; + } + + m_activeCell = index; + m_tableView.scrollToRow(index); + MyFunCell * cell = (MyFunCell *)(m_tableView.cellAtIndex(index)); + cell->setParentResponder(this); + App::runningApp()->focus(cell); +} + +bool ListController::handleEvent(ion_event_t event) { + switch (event) { + case DOWN_ARROW: + setActiveCell(m_activeCell+1); + return true; + case UP_ARROW: + setActiveCell(m_activeCell-1); + return true; + case ENTER: + m_manualScrolling += 10; + m_tableView.setContentOffset({0, m_manualScrolling}); + return true; + default: + return false; } - m_tableView.scrollToRow(0); - return true; } int ListController::numberOfCells() { - return 3; + return k_totalNumberOfModels; }; View * ListController::reusableCell(int index) { @@ -77,25 +141,76 @@ int ListController::reusableCellCount() { } void ListController::willDisplayCellForIndex(View * cell, int index) { + MyFunCell * myCell = (MyFunCell *)cell; + myCell->setMessage(m_messages[index]); + myCell->setEven(index%2 == 0); } KDCoordinate ListController::cellHeight() { return 40; } +class GraphView : public ChildlessView { +public: + using ChildlessView::ChildlessView; + void drawRect(KDRect rect) const override; +}; + +void GraphView::drawRect(KDRect rect) const { + KDFillRect(rect, KDColorWhite); + KDCoordinate x_grid_step = m_frame.width/10; + KDCoordinate y_grid_step = m_frame.height/10; + KDColor gridColor = KDColorGray(0xEE); + for (KDCoordinate x=rect.x; xfocus(&m_view); + } + */ +} + class MyTestApp : public App { public: MyTestApp(); @@ -119,7 +242,7 @@ private: MyTestApp::MyTestApp() : App(), - m_demoViewController(DemoViewController(0x1235)), + m_demoViewController(DemoViewController(KDColorWhite)), m_listViewController(ListController()), m_tabViewController(&m_demoViewController, &m_listViewController) {