basic choices view

This commit is contained in:
Tristan Krause 2019-04-05 08:33:31 +02:00
parent 9f4aebef64
commit d508c308b5
10 changed files with 69 additions and 53 deletions

View file

@ -14,7 +14,7 @@ OBJ_CLI = cli.o
CFLAGS = -std=c++14 -O3 -Wall -Wextra CFLAGS = -std=c++14 -O3 -Wall -Wextra
LDFLAGS = -lcurses LDFLAGS = -lcurses
OBJECTS_DRV = drv/usart.o drv/b15f.o drv/plottyfile.o drv/dot.o 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) COMPILE = $(COMPILER_PATH) $(CFLAGS)

Binary file not shown.

View file

@ -1,3 +1,10 @@
/** TODO
*
* - throw exception -> raise SIGINT
*
*/
#include <stdio.h> #include <stdio.h>
#include <ncurses.h> // sudo apt-get install libncurses5-dev #include <ncurses.h> // sudo apt-get install libncurses5-dev
#include <vector> #include <vector>
@ -7,9 +14,9 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include "ui/view_main.h" #include "ui/view_selection.h"
#include "ui/view_info.h" #include "ui/view_info.h"
#include "drv/b15f.h" //#include "drv/b15f.h"
// global error message // global error message
std::string ERR_MSG; std::string ERR_MSG;
@ -46,7 +53,7 @@ void signal_handler(int signal)
void init() void init()
{ {
// init b15 driver // init b15 driver
B15F::getInstance(); //B15F::getInstance();
// init all ncurses stuff // init all ncurses stuff
initscr(); initscr();
@ -108,10 +115,11 @@ void show_info(int)
void show_main(int) void show_main(int)
{ {
ViewMain* view = new ViewMain(); ViewSelection* view = new ViewSelection();
view->setTitle("B15F - Command Line Interface"); view->setTitle("B15F - Command Line Interface");
view->addCall(&show_info); view->addChoice("[ Informationen ]", &show_info);
view->addCall(&finish); view->addChoice("", nullptr);
view->addChoice("[ Beenden ]", &finish);
view->repaint(); view->repaint();
win_stack.push_back(view); win_stack.push_back(view);

Binary file not shown.

View file

@ -23,11 +23,24 @@ WINDOW* View::getWinContext()
return win; return win;
} }
void View::addCall(std::function<void(int)> call) // from: https://stackoverflow.com/a/37454181
std::vector<std::string> View::str_split(const std::string& str, const std::string delim)
{ {
calls.push_back(call); std::vector<std::string> 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) void View::setTitle(std::string title)
{ {
this->title = title; this->title = title;
@ -59,20 +72,3 @@ void View::repaint()
refresh(); refresh();
wrefresh(win); wrefresh(win);
} }
// from: https://stackoverflow.com/a/37454181
std::vector<std::string> View::str_split(const std::string& str, const std::string delim)
{
std::vector<std::string> 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;
}

View file

@ -19,14 +19,11 @@ public:
static void setWinContext(WINDOW* win); static void setWinContext(WINDOW* win);
static WINDOW* getWinContext(void); static WINDOW* getWinContext(void);
static std::vector<std::string> str_split(const std::string& str, const std::string delim);
virtual void addCall(std::function<void(int)> call);
void setSize(int width, int height); virtual void setTitle(std::string title);
void setTitle(std::string title);
virtual void repaint(void); virtual void repaint(void);
static std::vector<std::string> str_split(const std::string& str, const std::string delim);
virtual void draw(void) = 0; virtual void draw(void) = 0;
virtual std::function<void(int)> keypress(int& key) = 0; virtual std::function<void(int)> keypress(int& key) = 0;
@ -38,9 +35,8 @@ protected:
std::string title; std::string title;
std::vector<std::function<void(int)>> calls; std::vector<std::function<void(int)>> calls;
constexpr static int KEY_ENT = 10;
static WINDOW* win; static WINDOW* win;
constexpr static int KEY_ENT = 10;
}; };
#endif // VIEW_H #endif // VIEW_H

View file

@ -2,6 +2,7 @@
ViewInfo::ViewInfo() ViewInfo::ViewInfo()
{ {
calls.push_back(nullptr);
} }
void ViewInfo::setText(std::string text) void ViewInfo::setText(std::string text)
@ -14,6 +15,11 @@ void ViewInfo::setLabelClose(std::string label)
this->label_close = label; this->label_close = label;
} }
void ViewInfo::setCall(std::function<void(int)> call)
{
calls[0] = call;
}
void ViewInfo::draw() void ViewInfo::draw()
{ {
int li = 0; int li = 0;
@ -30,7 +36,6 @@ void ViewInfo::draw()
std::function<void(int)> ViewInfo::keypress(int& key) std::function<void(int)> ViewInfo::keypress(int& key)
{ {
std::function<void(int)> ret = nullptr;
switch(key) switch(key)
{ {
@ -53,5 +58,5 @@ std::function<void(int)> ViewInfo::keypress(int& key)
default: default:
break; break;
} }
return ret; return calls[0];
} }

View file

@ -8,11 +8,12 @@ class ViewInfo : public View
public: public:
ViewInfo(void); ViewInfo(void);
virtual void setText(std::string text); virtual void setText(std::string text);
virtual void setLabelClose(std::string label); virtual void setLabelClose(std::string label);;
virtual void setCall(std::function<void(int)> call);
virtual void draw(void) override; virtual void draw(void) override;
virtual std::function<void(int)> keypress(int& key) override; virtual std::function<void(int)> keypress(int& key) override;
private: protected:
std::string text; std::string text;
std::string label_close; std::string label_close;
int close_offset_x = 0; int close_offset_x = 0;

View file

@ -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++) for(size_t i = 0; i < choices.size(); i++)
@ -13,16 +13,26 @@ void ViewMain::draw()
} }
} }
std::function<void(int)> ViewMain::keypress(int& key) void ViewSelection::addChoice(std::string name, std::function<void(int)> call)
{
choices.push_back(name);
calls.push_back(call);
}
std::function<void(int)> ViewSelection::keypress(int& key)
{ {
std::function<void(int)> ret = nullptr; std::function<void(int)> ret = nullptr;
switch(key) switch(key)
{ {
case KEY_UP: case KEY_UP:
selection = (selection - 1) % choices.size(); do
selection = (selection - 1 + choices.size()) % choices.size();
while(!choices[selection].length() && choices.size());
break; break;
case KEY_DOWN: case KEY_DOWN:
selection = (selection + 1) % choices.size(); do
selection = (selection + 1) % choices.size();
while(!choices[selection].length() && choices.size());
break; break;
case KEY_MOUSE: case KEY_MOUSE:
{ {
@ -35,7 +45,7 @@ std::function<void(int)> ViewMain::keypress(int& key)
size_t row_start = start_y + choice_offset_y; size_t row_start = start_y + choice_offset_y;
size_t mouse_x = event.x, mouse_y = event.y; size_t mouse_x = event.x, mouse_y = event.y;
for(size_t i = 0; i < choices.size(); i++) 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) if(selection == i || event.bstate & BUTTON1_DOUBLE_CLICKED)
hit = true; hit = true;

View file

@ -1,24 +1,24 @@
#ifndef VIEW_MAIN_H #ifndef VIEW_SELECTION_H
#define VIEW_MAIN_H #define VIEW_SELECTION_H
#include <vector> #include <vector>
#include <string> #include <string>
#include "view.h" #include "view.h"
class ViewMain : public View class ViewSelection : public View
{ {
public: public:
virtual void draw(void) override; virtual void draw(void) override;
virtual void addChoice(std::string name, std::function<void(int)> call);
virtual std::function<void(int)> keypress(int& key) override; virtual std::function<void(int)> keypress(int& key) override;
private: protected:
size_t selection = 0;
std::vector<std::string> choices;
constexpr static int choice_offset_x = 2; constexpr static int choice_offset_x = 2;
constexpr static int choice_offset_y = 3; constexpr static int choice_offset_y = 3;
size_t selection = 0;
const std::vector<std::string> choices = {
"[ Informationen ]",
"[ Beenden ]"
};
}; };
#endif // VIEW_MAIN_H #endif // VIEW_SELECTION_H