Skip to content

Commit

Permalink
Merge pull request #5 from Skyler84:combined-demos
Browse files Browse the repository at this point in the history
Add all_example combined demo
  • Loading branch information
Skyler84 authored Aug 21, 2023
2 parents b6b0f51 + b1ef9c9 commit 2b12f05
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 1 deletion.
6 changes: 6 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ function(add_simple_example NAME)
add_executable(${filename}_example_app main.cpp)
target_compile_definitions(${filename}_example_app PRIVATE CLASSNAME=${NAME}View INCLUDE="examples/${filename}_view.hpp")
target_link_libraries(${filename}_example_app PRIVATE gui ${filename}_example)
if(NOT ${filename} STREQUAL all)
target_link_libraries(all_example PUBLIC ${filename}_example)
endif()

pico_add_extra_outputs(${filename}_example_app)
pico_enable_stdio_uart(${filename}_example_app 0)
Expand All @@ -21,7 +24,10 @@ function(add_simple_example NAME)
endif()
endfunction(add_simple_example)

add_simple_example(All)
add_simple_example(Divider)
add_simple_example(List)
add_simple_example(QRCode)
add_simple_example(Text)

# add_subdirectory(all)
97 changes: 97 additions & 0 deletions examples/all/all_view.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "examples/all_view.hpp"

#include "gui/view_controller.hpp"

#include "drivers/button.hpp"
#include "buttons.hpp"


AllView::AllView(gui::View *parent) :
View(parent),
tv{this},
dv{this},
lv{this},
qv{this}{
ui.div.addWidget(&ui.header);
ui.header.setText("All Examples");
ui.header.setFontSize(3);
ui.div.addWidget(&ui.list);

ui.list.addItem(&ui.li_text);
ui.li_text.setText("Text example");
ui.li_text.setFontSize(2);
ui.li_text.setUserData(&tv);

ui.list.addItem(&ui.li_dividier);
ui.li_dividier.setText("Divider example");
ui.li_dividier.setFontSize(2);
ui.li_dividier.setUserData(&dv);

ui.list.addItem(&ui.li_list);
ui.li_list.setText("List example");
ui.li_list.setFontSize(2);
ui.li_list.setUserData(&lv);

ui.list.addItem(&ui.li_qrcode);
ui.li_qrcode.setText("QR code example");
ui.li_qrcode.setFontSize(2);
ui.li_qrcode.setUserData(&qv);

setWidget(&ui.div);
}

void AllView::handleInput(InputEvent ev){
bool handled = false;
MyButton *b = (MyButton*)ev.data;
switch(ev.type){
case InputEvent::ButtonClick:{
switch(b->get_pin()){
case Buttons::A:{
if(ui.list.getSelectedRow() != -1);
selectApplication(ui.list.getSelectedRow());
break;
}
case Buttons::B:{
if(ui.list.getSelectedRow() >= 0){
ui.list.setFocused(false);
ui.list.setSelectedRow(-1);
}
else
close();
break;
}
case Buttons::X:{
ui.list.setSelectedRow(std::max<int>(ui.list.getSelectedRow()-1, -1));
ui.list.setFocused(ui.list.getSelectedRow() != -1);
break;
}
case Buttons::Y:{
ui.list.setSelectedRow(std::min<int>(ui.list.getSelectedRow()+1, ui.list.getNumItems()-1));
ui.list.setFocused(ui.list.getSelectedRow() != -1);
break;
}
}
handled = true;
break;
}
}
if(!handled)
View::handleInput(ev);
}

void AllView::selectApplication(size_t idx)
{
Widget *w = ui.list.getItemAt(idx);
if (!w){
printf("No item\n");
return;
}
void *d = w->getUserData();
if (!d){
printf("No user data\n");
return;
}
setChildView((gui::View*)d);
auto &controller = gui::ViewController::get();
controller.set_view((gui::View*)d);
}
32 changes: 32 additions & 0 deletions examples/all/include/examples/all_view.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "gui/view.hpp"
#include "gui/widgets/divider_widget.hpp"
#include "gui/widgets/text_widget.hpp"
#include "gui/widgets/list_widget.hpp"

#include "examples/text_view.hpp"
#include "examples/divider_view.hpp"
#include "examples/list_view.hpp"
#include "examples/qrcode_view.hpp"

class AllView : public gui::View{
public:
AllView(gui::View *parent);
protected:
void handleInput(InputEvent ev);
private:
struct{
gui::VDividerWidget div;
gui::TextWidget header;
gui::ListWidget list;
gui::TextWidget li_text, li_dividier, li_list, li_qrcode;
}ui;

TextView tv;
DividerView dv;
ListView lv;
QRCodeView qv;

void selectApplication(size_t idx);
};
2 changes: 1 addition & 1 deletion examples/list/list_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ListView::ListView(gui::View *parent) : View(parent){
}

void ListView::handleInput(InputEvent ev){
bool handled = false;
bool handled = false;
MyButton *b = (MyButton*)ev.data;
switch(ev.type){
case InputEvent::ButtonClick:{
Expand Down
6 changes: 6 additions & 0 deletions include/gui/widgets/widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ namespace gui
// assert(w != this);
m_nextChild = w;
}
void setUserData(void *d) {
m_userData = d;
}
void *getUserData() const {
return m_userData;
}
protected:
virtual void onFocusChange(){}
private:
Expand Down

0 comments on commit 2b12f05

Please sign in to comment.