From 5f9654d3b4864495153fe3908d8ca7ae974dd3ba Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 8 Jan 2020 20:33:16 +0100 Subject: [PATCH] Basic input of email & password --- loginscreen.cpp | 147 ++++++++++++++++++++++++++++++++++++------------ loginscreen.h | 5 ++ 2 files changed, 117 insertions(+), 35 deletions(-) diff --git a/loginscreen.cpp b/loginscreen.cpp index ba482c5..a6aec64 100644 --- a/loginscreen.cpp +++ b/loginscreen.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "loginscreen.h" @@ -12,50 +13,126 @@ LoginScreen::LoginScreen() } +enum CursorPos { + EMAIL = 0, + PASSWORD = 1, + SUBMIT = 2 +}; + bool LoginScreen::exec() { initscr(); /* Start curses mode */ keypad(stdscr, TRUE); // Listen to function keys + noecho(); + cbreak(); /* Line buffering disabled. pass on everything */ - printw("ComunicTerm"); /* Print Hello World */ - refresh(); /* Print it on to the real screen */ + string email, password; + getCredentials(email, password); - - // Get screen size - int row, col; - getmaxyx(stdscr, row, col); - - const string msg = "Light client"; - mvprintw(0, (col-static_cast(msg.length())), msg.c_str()); - refresh(); - - - int startX = (row-4*2)/2; - int startY = (col-50)/2; - - - mvprintw(startX, startY, "Please login to your Comunic account:"); - - // Ask for email - wattron(stdscr, A_REVERSE); - mvprintw(startX+2, startY, "Email address:"); - wattroff(stdscr, A_REVERSE); - - - // Ask for password - wattron(stdscr, A_REVERSE); - mvprintw(startX+4, startY, "Password:"); - wattroff(stdscr, A_REVERSE); - - // Validate button - wattron(stdscr, A_REVERSE); - mvprintw(startX+6, startY, "Login"); - wattroff(stdscr, A_REVERSE); - - refresh(); + mvprintw(3,0, "Done!"); getch(); /* Wait for user input */ endwin(); /* End curses mode */ } + +void LoginScreen::getCredentials(string &email, string &pass) +{ + int currPos = 0; + int currX = 0, currY = 0; + + bool stop = false; + + do { + + erase(); + + printw("ComunicTerm"); /* Print Hello World */ + + + // Get screen size + int row, col; + getmaxyx(stdscr, row, col); + + const string msg = "Light client"; + mvprintw(0, (col-static_cast(msg.length())), msg.c_str()); + refresh(); + + + int startX = (row-4*2)/2; + int startY = (col-50)/2; + + + mvprintw(startX, startY, "Please login to your Comunic account:"); + + + + + // Ask for email + if(currPos == EMAIL) wattron(stdscr, A_REVERSE); + mvprintw(startX+2, startY, "Email address:"); + wattroff(stdscr, A_REVERSE); + printw(" "); + printw(email.c_str()); + if(currPos == EMAIL) getyx(stdscr, currY, currX); + + + // Ask for password + if(currPos == PASSWORD) wattron(stdscr, A_REVERSE); + mvprintw(startX+4, startY, "Password:"); + wattroff(stdscr, A_REVERSE); + printw(" "); + printw(pass.c_str()); + if(currPos == PASSWORD) getyx(stdscr, currY, currX); + + // Validate button + if(currPos == SUBMIT) wattron(stdscr, A_REVERSE); + mvprintw(startX+6, startY, "Login"); + wattroff(stdscr, A_REVERSE); + + if(currPos == SUBMIT) {curs_set(0);} else {curs_set(2);} + + move(currY, currX); + + refresh(); + + int c = wgetch(stdscr); + + switch(c) { + case KEY_UP: + currPos--; + if(currPos < 0) currPos = 0; + break; + + case KEY_DOWN: + currPos++; + if(currPos > 2) currPos = 0; + break; + + case 10: // ENTER + if(currPos == EMAIL) + currPos++; + else + stop = true; + break; + + + case 263: // Erase + if(currPos == EMAIL && email.length() > 0) + email.pop_back(); + else if(currPos == PASSWORD && pass.length() > 0) + pass.pop_back(); + break; + + default: + if(c > 126) break; // We can not process this value + + if(currPos == EMAIL) + email += static_cast(c); + else if(currPos == PASSWORD) + pass += static_cast(c); + } + + } while(!stop); +} diff --git a/loginscreen.h b/loginscreen.h index 3579f81..b8868ab 100644 --- a/loginscreen.h +++ b/loginscreen.h @@ -4,10 +4,15 @@ * @author Pierre HUBERT */ +#include + class LoginScreen { public: LoginScreen(); bool exec(); + +private: + void getCredentials(std::string &email, std::string &pass); };