Allow user to change input style or clear input.

This commit is contained in:
David Vereb 2019-04-03 08:36:08 -05:00
parent bc4a271d17
commit 0d10729b37
3 changed files with 19 additions and 9 deletions

View File

@ -33,7 +33,7 @@ Problem::~Problem()
delwin(win); delwin(win);
} }
void Problem::Draw(bool selected) void Problem::Draw(bool selected, const INPUT_STYLE &input_style)
{ {
if(!win) if(!win)
return; return;
@ -59,7 +59,13 @@ void Problem::Draw(bool selected)
}; };
mvwprintw(win, 4, 2, "========"); mvwprintw(win, 4, 2, "========");
mvwprintw(win, 5, 4, "%'6d", Input()); mvwprintw(win, 5, 4, "%'6d", Input());
mvwprintw(win, 5, 9, "");
// Place the cursor in the correct position!
int length = input.length() + ((input.length() - 1) / 3); // add commas, too!
if(input_style == INPUT_STYLE::INPUT_PREPEND)
mvwprintw(win, 5, 9 - length, "");
else
mvwprintw(win, 5, 10, "");
wrefresh(win); wrefresh(win);
} }

View File

@ -29,8 +29,9 @@ public:
Problem(PROBLEM_TYPE type, unsigned short max_digits, unsigned win_x, unsigned win_y); Problem(PROBLEM_TYPE type, unsigned short max_digits, unsigned win_x, unsigned win_y);
~Problem(); ~Problem();
void Draw(bool selected); void Draw(bool selected, const INPUT_STYLE &input_style);
void Input(const INPUT_STYLE input_style, char character); void Input(const INPUT_STYLE input_style, char character);
void ClearInput() { input = ""; }
bool Correct() const; bool Correct() const;
private: private:

View File

@ -47,25 +47,28 @@ int main(int argc, char *argv[])
std::vector<Problem*> problems = GenerateProblems(addition, subtraction); std::vector<Problem*> problems = GenerateProblems(addition, subtraction);
std::vector<Problem*>::iterator selected = problems.begin(); std::vector<Problem*>::iterator selected = problems.begin();
INPUT_STYLE input_style = INPUT_STYLE::INPUT_APPEND;
for(auto *problem : problems) for(auto *problem : problems)
if(problem) if(problem)
problem->Draw(problem == *selected); problem->Draw(problem == *selected, input_style);
INPUT_STYLE input_style = INPUT_STYLE::INPUT_PREPEND;
while(selected != problems.end() && *selected) while(selected != problems.end() && *selected)
{ {
Problem *current_problem = *selected; Problem *current_problem = *selected;
current_problem->Draw(true); // refresh with new input current_problem->Draw(true, input_style); // refresh with new input
auto input = getch(); auto input = getch();
if(input == 'q') // early exit if(input == 'q') // early exit
break; break;
switch(input) switch(input)
{ {
case KEY_LEFT: case KEY_LEFT:
input_style = INPUT_STYLE::INPUT_APPEND; input_style = INPUT_STYLE::INPUT_PREPEND;
break; break;
case KEY_RIGHT: case KEY_RIGHT:
input_style = INPUT_STYLE::INPUT_PREPEND; input_style = INPUT_STYLE::INPUT_APPEND;
break;
case KEY_BACKSPACE:
current_problem->ClearInput();
break; break;
case '0': case '0':
case '1': case '1':
@ -78,12 +81,12 @@ int main(int argc, char *argv[])
case '8': case '8':
case '9': case '9':
current_problem->Input(input_style, input); current_problem->Input(input_style, input);
current_problem->Draw(true); // refresh with new input
break; break;
}; };
if(current_problem->Correct()) if(current_problem->Correct())
++selected; ++selected;
current_problem->Draw(true, input_style); // refresh with new input
} }
for(auto *problem : problems) for(auto *problem : problems)