From 0d10729b37bb3b23a2a566d0ebac71f0cbc70bfa Mon Sep 17 00:00:00 2001 From: "David Vereb (Home)" Date: Wed, 3 Apr 2019 08:36:08 -0500 Subject: [PATCH] Allow user to change input style or clear input. --- Problem.cpp | 10 ++++++++-- Problem.h | 3 ++- main.cpp | 15 +++++++++------ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Problem.cpp b/Problem.cpp index cdb5428..f37ba51 100644 --- a/Problem.cpp +++ b/Problem.cpp @@ -33,7 +33,7 @@ Problem::~Problem() delwin(win); } -void Problem::Draw(bool selected) +void Problem::Draw(bool selected, const INPUT_STYLE &input_style) { if(!win) return; @@ -59,7 +59,13 @@ void Problem::Draw(bool selected) }; mvwprintw(win, 4, 2, "========"); 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); } diff --git a/Problem.h b/Problem.h index a458710..ff2e567 100644 --- a/Problem.h +++ b/Problem.h @@ -29,8 +29,9 @@ public: Problem(PROBLEM_TYPE type, unsigned short max_digits, unsigned win_x, unsigned win_y); ~Problem(); - void Draw(bool selected); + void Draw(bool selected, const INPUT_STYLE &input_style); void Input(const INPUT_STYLE input_style, char character); + void ClearInput() { input = ""; } bool Correct() const; private: diff --git a/main.cpp b/main.cpp index bf9f468..18393db 100644 --- a/main.cpp +++ b/main.cpp @@ -47,25 +47,28 @@ int main(int argc, char *argv[]) std::vector problems = GenerateProblems(addition, subtraction); std::vector::iterator selected = problems.begin(); + INPUT_STYLE input_style = INPUT_STYLE::INPUT_APPEND; for(auto *problem : problems) 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) { 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(); if(input == 'q') // early exit break; switch(input) { case KEY_LEFT: - input_style = INPUT_STYLE::INPUT_APPEND; + input_style = INPUT_STYLE::INPUT_PREPEND; break; case KEY_RIGHT: - input_style = INPUT_STYLE::INPUT_PREPEND; + input_style = INPUT_STYLE::INPUT_APPEND; + break; + case KEY_BACKSPACE: + current_problem->ClearInput(); break; case '0': case '1': @@ -78,12 +81,12 @@ int main(int argc, char *argv[]) case '8': case '9': current_problem->Input(input_style, input); - current_problem->Draw(true); // refresh with new input break; }; if(current_problem->Correct()) ++selected; + current_problem->Draw(true, input_style); // refresh with new input } for(auto *problem : problems)