Allow user to change input style or clear input.
This commit is contained in:
parent
bc4a271d17
commit
0d10729b37
10
Problem.cpp
10
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);
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
15
main.cpp
15
main.cpp
@ -47,25 +47,28 @@ int main(int argc, char *argv[])
|
||||
std::vector<Problem*> problems = GenerateProblems(addition, subtraction);
|
||||
std::vector<Problem*>::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)
|
||||
|
Loading…
Reference in New Issue
Block a user