diff --git a/escher/Makefile b/escher/Makefile index 6723110b5..f1a39b081 100644 --- a/escher/Makefile +++ b/escher/Makefile @@ -3,6 +3,7 @@ SFLAGS += -Iescher/include objs += $(addprefix escher/src/,\ alternate_empty_view_controller.o\ app.o\ + bank_view_controller.o\ buffer_text_view.o\ button.o\ button_row_controller.o\ diff --git a/escher/include/escher.h b/escher/include/escher.h index 9a31e8d8c..c1e6896a8 100644 --- a/escher/include/escher.h +++ b/escher/include/escher.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/escher/include/escher/bank_view_controller.h b/escher/include/escher/bank_view_controller.h new file mode 100644 index 000000000..5da61235b --- /dev/null +++ b/escher/include/escher/bank_view_controller.h @@ -0,0 +1,46 @@ +#ifndef ESCHER_BANK_VIEW_CONTROLLER_H +#define ESCHER_BANK_VIEW_CONTROLLER_H + +#include + +class BankViewController : public ViewController { +public: + BankViewController(Responder * parentViewController); + + virtual ViewController * childAtIndex(int i) = 0; + virtual int numberOfChildren() = 0; + + void setActiveIndex(int i); + int activeIndex() const { return m_activeIndex; } + + View * view() override { return &m_view; } + void didEnterResponderChain(Responder * previousResponder) override; + void viewWillAppear() override; + void viewDidDisappear() override; +private: + class ContentView : public View { + public: + ContentView(): View(), m_subview(nullptr) {} + void setSubview(View * view); + private: + int numberOfSubviews() const override { + return m_subview == nullptr ? 0 : 1; + } + View * subviewAtIndex(int index) override { + assert(index == 0); + return m_subview; + } + void layoutSubviews() override { + m_subview->setFrame(bounds()); + } + View * m_subview; + }; + ViewController * activeViewController() { + assert(m_activeIndex >= 0 && m_activeIndex < numberOfChildren()); + return childAtIndex(m_activeIndex); + } + ContentView m_view; + int m_activeIndex; +}; + +#endif diff --git a/escher/src/bank_view_controller.cpp b/escher/src/bank_view_controller.cpp new file mode 100644 index 000000000..6e6146ad0 --- /dev/null +++ b/escher/src/bank_view_controller.cpp @@ -0,0 +1,40 @@ +#include +#include + +BankViewController::BankViewController(Responder * parentViewController) : + ViewController(parentViewController), + m_activeIndex(0) +{ +} + +void BankViewController::setActiveIndex(int i) { + assert(i >= 0 && i < numberOfChildren()); + if (i == m_activeIndex) { + return; + } + ViewController * upcomingVC = childAtIndex(i); + upcomingVC->viewWillAppear(); + app()->setFirstResponder(upcomingVC); + childAtIndex(m_activeIndex)->viewDidDisappear(); + m_activeIndex = i; + m_view.setSubview(upcomingVC->view()); +} + +void BankViewController::didEnterResponderChain(Responder * previousResponder) { + app()->setFirstResponder(activeViewController()); +} + +void BankViewController::viewWillAppear() { + m_view.setSubview(activeViewController()->view()); + activeViewController()->viewWillAppear(); +} + +void BankViewController::viewDidDisappear() { + activeViewController()->viewDidDisappear(); +} + +void BankViewController::ContentView::setSubview(View * view) { + m_subview = view; + layoutSubviews(); + markRectAsDirty(bounds()); +}