diff --git a/driver/Makefile b/driver/Makefile index 14a27cd..3dbeb0c 100644 --- a/driver/Makefile +++ b/driver/Makefile @@ -14,7 +14,7 @@ OBJ_CLI = cli.o CFLAGS = -std=c++14 -O3 -Wall -Wextra LDFLAGS = -lcurses OBJECTS_DRV = drv/usart.o drv/b15f.o drv/plottyfile.o drv/dot.o -OBJECTS_UI = ui/view.o ui/view_main.o ui/view_info.o +OBJECTS_UI = ui/view.o ui/view_selection.o ui/view_info.o COMPILE = $(COMPILER_PATH) $(CFLAGS) diff --git a/driver/cli b/driver/cli index 8d652a7..9cbf19d 100755 Binary files a/driver/cli and b/driver/cli differ diff --git a/driver/cli.cpp b/driver/cli.cpp index 86040bc..dc7e688 100644 --- a/driver/cli.cpp +++ b/driver/cli.cpp @@ -1,3 +1,10 @@ +/** TODO + * + * - throw exception -> raise SIGINT + * + */ + + #include #include // sudo apt-get install libncurses5-dev #include @@ -7,9 +14,9 @@ #include #include #include -#include "ui/view_main.h" +#include "ui/view_selection.h" #include "ui/view_info.h" -#include "drv/b15f.h" +//#include "drv/b15f.h" // global error message std::string ERR_MSG; @@ -46,7 +53,7 @@ void signal_handler(int signal) void init() { // init b15 driver - B15F::getInstance(); + //B15F::getInstance(); // init all ncurses stuff initscr(); @@ -108,10 +115,11 @@ void show_info(int) void show_main(int) { - ViewMain* view = new ViewMain(); + ViewSelection* view = new ViewSelection(); view->setTitle("B15F - Command Line Interface"); - view->addCall(&show_info); - view->addCall(&finish); + view->addChoice("[ Informationen ]", &show_info); + view->addChoice("", nullptr); + view->addChoice("[ Beenden ]", &finish); view->repaint(); win_stack.push_back(view); diff --git a/driver/main b/driver/main deleted file mode 100755 index 0cb40b6..0000000 Binary files a/driver/main and /dev/null differ diff --git a/driver/ui/view.cpp b/driver/ui/view.cpp index b1ea7cd..cc50010 100644 --- a/driver/ui/view.cpp +++ b/driver/ui/view.cpp @@ -23,11 +23,24 @@ WINDOW* View::getWinContext() return win; } -void View::addCall(std::function call) +// from: https://stackoverflow.com/a/37454181 +std::vector View::str_split(const std::string& str, const std::string delim) { - calls.push_back(call); + std::vector tokens; + size_t prev = 0, pos = 0; + do + { + pos = str.find(delim, prev); + if (pos == std::string::npos) pos = str.length(); + std::string token = str.substr(prev, pos-prev); + if (!token.empty()) tokens.push_back(token); + prev = pos + delim.length(); + } + while (pos < str.length() && prev < str.length()); + return tokens; } + void View::setTitle(std::string title) { this->title = title; @@ -59,20 +72,3 @@ void View::repaint() refresh(); wrefresh(win); } - -// from: https://stackoverflow.com/a/37454181 -std::vector View::str_split(const std::string& str, const std::string delim) -{ - std::vector tokens; - size_t prev = 0, pos = 0; - do - { - pos = str.find(delim, prev); - if (pos == std::string::npos) pos = str.length(); - std::string token = str.substr(prev, pos-prev); - if (!token.empty()) tokens.push_back(token); - prev = pos + delim.length(); - } - while (pos < str.length() && prev < str.length()); - return tokens; -} diff --git a/driver/ui/view.h b/driver/ui/view.h index 94dda53..7c8fc31 100644 --- a/driver/ui/view.h +++ b/driver/ui/view.h @@ -19,14 +19,11 @@ public: static void setWinContext(WINDOW* win); static WINDOW* getWinContext(void); - - virtual void addCall(std::function call); + static std::vector str_split(const std::string& str, const std::string delim); - void setSize(int width, int height); - void setTitle(std::string title); + virtual void setTitle(std::string title); virtual void repaint(void); - static std::vector str_split(const std::string& str, const std::string delim); virtual void draw(void) = 0; virtual std::function keypress(int& key) = 0; @@ -38,9 +35,8 @@ protected: std::string title; std::vector> calls; - constexpr static int KEY_ENT = 10; - static WINDOW* win; + constexpr static int KEY_ENT = 10; }; #endif // VIEW_H diff --git a/driver/ui/view_info.cpp b/driver/ui/view_info.cpp index f75c64e..b94ff4e 100644 --- a/driver/ui/view_info.cpp +++ b/driver/ui/view_info.cpp @@ -2,6 +2,7 @@ ViewInfo::ViewInfo() { + calls.push_back(nullptr); } void ViewInfo::setText(std::string text) @@ -14,6 +15,11 @@ void ViewInfo::setLabelClose(std::string label) this->label_close = label; } +void ViewInfo::setCall(std::function call) +{ + calls[0] = call; +} + void ViewInfo::draw() { int li = 0; @@ -30,7 +36,6 @@ void ViewInfo::draw() std::function ViewInfo::keypress(int& key) { - std::function ret = nullptr; switch(key) { @@ -53,5 +58,5 @@ std::function ViewInfo::keypress(int& key) default: break; } - return ret; + return calls[0]; } diff --git a/driver/ui/view_info.h b/driver/ui/view_info.h index da811d3..755dfe4 100644 --- a/driver/ui/view_info.h +++ b/driver/ui/view_info.h @@ -8,11 +8,12 @@ class ViewInfo : public View public: ViewInfo(void); virtual void setText(std::string text); - virtual void setLabelClose(std::string label); + virtual void setLabelClose(std::string label);; + virtual void setCall(std::function call); virtual void draw(void) override; virtual std::function keypress(int& key) override; -private: +protected: std::string text; std::string label_close; int close_offset_x = 0; diff --git a/driver/ui/view_main.cpp b/driver/ui/view_selection.cpp similarity index 66% rename from driver/ui/view_main.cpp rename to driver/ui/view_selection.cpp index e1348e4..c6cd98b 100644 --- a/driver/ui/view_main.cpp +++ b/driver/ui/view_selection.cpp @@ -1,6 +1,6 @@ -#include "view_main.h" +#include "view_selection.h" -void ViewMain::draw() +void ViewSelection::draw() { for(size_t i = 0; i < choices.size(); i++) @@ -13,16 +13,26 @@ void ViewMain::draw() } } -std::function ViewMain::keypress(int& key) +void ViewSelection::addChoice(std::string name, std::function call) +{ + choices.push_back(name); + calls.push_back(call); +} + +std::function ViewSelection::keypress(int& key) { std::function ret = nullptr; switch(key) { case KEY_UP: - selection = (selection - 1) % choices.size(); + do + selection = (selection - 1 + choices.size()) % choices.size(); + while(!choices[selection].length() && choices.size()); break; case KEY_DOWN: - selection = (selection + 1) % choices.size(); + do + selection = (selection + 1) % choices.size(); + while(!choices[selection].length() && choices.size()); break; case KEY_MOUSE: { @@ -35,7 +45,7 @@ std::function ViewMain::keypress(int& key) size_t row_start = start_y + choice_offset_y; size_t mouse_x = event.x, mouse_y = event.y; for(size_t i = 0; i < choices.size(); i++) - if(mouse_y == row_start + i && mouse_x >= column_start && mouse_x < column_start + choices[i].length()) + if(choices[i].length() && mouse_y == row_start + i && mouse_x >= column_start && mouse_x < column_start + choices[i].length()) { if(selection == i || event.bstate & BUTTON1_DOUBLE_CLICKED) hit = true; diff --git a/driver/ui/view_main.h b/driver/ui/view_selection.h similarity index 54% rename from driver/ui/view_main.h rename to driver/ui/view_selection.h index 4c7935d..5079b2f 100644 --- a/driver/ui/view_main.h +++ b/driver/ui/view_selection.h @@ -1,24 +1,24 @@ -#ifndef VIEW_MAIN_H -#define VIEW_MAIN_H +#ifndef VIEW_SELECTION_H +#define VIEW_SELECTION_H #include #include #include "view.h" -class ViewMain : public View +class ViewSelection : public View { public: virtual void draw(void) override; + virtual void addChoice(std::string name, std::function call); virtual std::function keypress(int& key) override; + -private: +protected: + size_t selection = 0; + std::vector choices; + constexpr static int choice_offset_x = 2; constexpr static int choice_offset_y = 3; - size_t selection = 0; - const std::vector choices = { - "[ Informationen ]", - "[ Beenden ]" - }; }; -#endif // VIEW_MAIN_H +#endif // VIEW_SELECTION_H