Skip to content

Commit e1aef86

Browse files
committed
ncurses character creation screen complete
1 parent e619f0a commit e1aef86

File tree

3 files changed

+105
-49
lines changed

3 files changed

+105
-49
lines changed

ncurses/charactercreator.cpp

+93-35
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
//
44

55
#include "charactercreator.h"
6-
76
#include <ranges>
87

9-
#define CONTROLS 3
8+
#define CONTROLS 6
9+
#define LEFT 2
10+
#define COLUMN_2 16
11+
#define COLUMN_3 48
12+
13+
using namespace ui;
1014

1115
CharacterCreator::CharacterCreator(const std::shared_ptr<Game> &game, const MessageHandler& message_handler) : NCursesView(game, message_handler) {
1216
new_game = game->StartNewGame();
@@ -16,7 +20,7 @@ CharacterCreator::CharacterCreator(const std::shared_ptr<Game> &game, const Mess
1620

1721
void CharacterCreator::ShowPopup(const std::string &label, const std::vector<std::string> &items, const int current_index) const {
1822
const int desired_rows = static_cast<int>(items.size());
19-
const int rows = std::min(screen_height - 4, desired_rows);
23+
const int rows = std::min(24 - 4, desired_rows);
2024
int skip = 0;
2125
WINDOW *w = subwin(win, rows + 2, 30, 1, 4);
2226
wclear(w);
@@ -53,7 +57,9 @@ void CharacterCreator::ShowPopup(const std::string &label, const std::vector<std
5357
void CharacterCreator::Resize(const int new_screen_width, const int new_screen_height) {
5458
NCursesView::Resize(new_screen_width, new_screen_height);
5559
delwin(win);
56-
win = newwin(0, 0, 0, 0);
60+
constexpr int required_width = 80;
61+
constexpr int required_height = 24;
62+
win = newwin(required_height, required_width, 0, 0);
5763
keypad(win, true);
5864
nodelay(win, true);
5965
wtimeout(win, 100);
@@ -62,35 +68,79 @@ void CharacterCreator::Resize(const int new_screen_width, const int new_screen_h
6268
void CharacterCreator::Render() {
6369
wclear(win);
6470
box(win, 0, 0);
65-
LeftAlign(win, "Progress Quest 2", 2, 0);
66-
LeftAlign(win, "Name:", 2, 2);
71+
LeftAlign(win, "Progress Quest 2", LEFT, 0);
72+
LeftAlign(win, "Name:", LEFT, 2);
6773
if (selected_control == 0) {
6874
wattron(win, A_UNDERLINE);
6975
}
7076
const auto name = new_game->GetName();
71-
LeftAlign(win, name, 9, 2);
77+
LeftAlign(win, name, COLUMN_2, 2);
7278
wattroff(win, A_UNDERLINE);
7379
if (selected_control == 0) {
7480
wattron(win, A_REVERSE);
75-
mvwaddch(win, 2, 9 + name.size(), ' ');
81+
mvwaddch(win, 2, COLUMN_2 + name.size(), ' ');
7682
wattroff(win, A_REVERSE);
7783
}
7884
if (selected_control == 1) {
7985
wattron(win, A_STANDOUT);
8086
}
81-
LeftAlign(win, "?", 40, 2);
87+
RightAlign(win, "Random", COLUMN_3, 2);
8288
wattroff(win, A_STANDOUT);
83-
LeftAlign(win, "Race:", 2, 3);
89+
LeftAlign(win, "Race:", LEFT, 3);
8490
if (selected_control == 2) {
8591
wattron(win, A_STANDOUT);
8692
}
87-
LeftAlign(win, new_game->GetRace(), 9, 3);
93+
LeftAlign(win, new_game->GetRace(), COLUMN_2, 3);
8894
wattroff(win, A_STANDOUT);
89-
LeftAlign(win, "Class:", 2, 4);
95+
LeftAlign(win, "Class:", LEFT, 4);
9096
if (selected_control == 3) {
9197
wattron(win, A_STANDOUT);
9298
}
93-
LeftAlign(win, new_game->GetClass(), 9, 4);
99+
LeftAlign(win, new_game->GetClass(), COLUMN_2, 4);
100+
wattroff(win, A_STANDOUT);
101+
LeftAlign(win, "Stats:", LEFT, 6);
102+
LeftAlign(win, "STR:", LEFT, 7);
103+
LeftAlign(win, std::to_string(new_game->GetSTR()), COLUMN_2, 7);
104+
LeftAlign(win, "CON:", LEFT, 8);
105+
LeftAlign(win, std::to_string(new_game->GetCON()), COLUMN_2, 8);
106+
LeftAlign(win, "DEX:", LEFT, 9);
107+
LeftAlign(win, std::to_string(new_game->GetDEX()), COLUMN_2, 9);
108+
LeftAlign(win, "INT:", LEFT, 10);
109+
LeftAlign(win, std::to_string(new_game->GetINT()), COLUMN_2, 10);
110+
LeftAlign(win, "WIS:", LEFT, 11);
111+
LeftAlign(win, std::to_string(new_game->GetWIS()), COLUMN_2, 11);
112+
LeftAlign(win, "CHA:", LEFT, 12);
113+
LeftAlign(win, std::to_string(new_game->GetCHA()), COLUMN_2, 12);
114+
LeftAlign(win, std::string(COLUMN_2 + 6, '-'), LEFT, 13);
115+
LeftAlign(win, "TOTAL:", LEFT, 14);
116+
LeftAlign(win, std::to_string(new_game->GetTotal()), COLUMN_2, 14);
117+
const auto colour = new_game->GetTotalColor();
118+
if (colour == ColorRed) {
119+
LeftAlign(win, "++", COLUMN_2 + 3, 14);
120+
}
121+
else if (colour == ColorYellow) {
122+
LeftAlign(win, "+", COLUMN_2 + 3, 14);
123+
}
124+
else if (colour == ColorSilver) {
125+
RightAlign(win, "-", COLUMN_2 - 1, 14);
126+
}
127+
else if (colour == ColorGray) {
128+
RightAlign(win, "--", COLUMN_2 - 1, 14);
129+
}
130+
if (selected_control == 4) {
131+
wattron(win, A_STANDOUT);
132+
}
133+
LeftAlign(win, "Roll", LEFT, 16);
134+
wattroff(win, A_STANDOUT);
135+
if (selected_control == 5) {
136+
wattron(win, A_STANDOUT);
137+
}
138+
LeftAlign(win, "Unroll", COLUMN_2, 16);
139+
wattroff(win, A_STANDOUT);
140+
if (selected_control == 6) {
141+
wattron(win, A_STANDOUT);
142+
}
143+
LeftAlign(win, "Sold!", LEFT, 22);
94144
wattroff(win, A_STANDOUT);
95145
if (show_race_popup) {
96146
ShowPopup("Race:", races, race_index);
@@ -141,7 +191,7 @@ void CharacterCreator::Render() {
141191
}
142192
else {
143193
selected_control = std::min(selected_control + 1, CONTROLS);
144-
if (selected_control == 1) { // 2nd column controls go down to next row
194+
if (selected_control == 1 || selected_control == 5) { // 2nd column controls go down to next row
145195
selected_control++;
146196
}
147197
}
@@ -155,47 +205,55 @@ void CharacterCreator::Render() {
155205
}
156206
else {
157207
selected_control = std::max(selected_control - 1, 0);
158-
if (selected_control == 1) { // skip 2nd column controls
208+
if (selected_control == 1 || selected_control == 5) { // skip 2nd column controls
159209
selected_control--;
160210
}
161211
}
162212
}
163213
else if (ch == KEY_LEFT) {
164-
if (!show_race_popup && !show_class_popup && selected_control == 1) {
214+
if (!show_race_popup && !show_class_popup && (selected_control == 1 || selected_control == 5)) {
165215
selected_control--;
166216
}
167217
}
168218
else if (ch == KEY_RIGHT) {
169-
if (!show_race_popup && !show_class_popup && selected_control == 0) {
219+
if (!show_race_popup && !show_class_popup && (selected_control == 0 || selected_control == 4)) {
170220
selected_control++;
171221
}
172222
}
173223
else if (ch == '\n' || ch == ' ') {
174-
if (selected_control == 1) {
224+
if (show_race_popup) {
225+
const auto race = races[race_index];
226+
new_game->SetRace(race);
227+
show_race_popup = false;
228+
}
229+
else if (show_class_popup) {
230+
const auto cls = classes[class_index];
231+
new_game->SetClass(cls);
232+
show_class_popup = false;
233+
}
234+
else if (selected_control == 1) {
175235
new_game->GenerateName();
176236
}
177237
else if (selected_control == 2) {
178-
if (show_race_popup) {
179-
const auto race = races[race_index];
180-
new_game->SetRace(race);
181-
show_race_popup = false;
182-
}
183-
else {
184-
show_race_popup = true;
185-
race_index = static_cast<int>(std::ranges::find(races, new_game->GetRace()) - races.begin());
186-
}
238+
show_race_popup = true;
239+
race_index = static_cast<int>(std::ranges::find(races, new_game->GetRace()) - races.begin());
187240
}
188241
else if (selected_control == 3) {
189-
if (show_class_popup) {
190-
const auto cls = classes[class_index];
191-
new_game->SetClass(cls);
192-
show_class_popup = false;
193-
}
194-
else {
195-
show_class_popup = true;
196-
class_index = static_cast<int>(std::ranges::find(classes, new_game->GetClass()) - classes.begin());
242+
show_class_popup = true;
243+
class_index = static_cast<int>(std::ranges::find(classes, new_game->GetClass()) - classes.begin());
244+
}
245+
else if (selected_control == 4) {
246+
new_game->ReRoll();
247+
}
248+
else if (selected_control == 5) {
249+
if (new_game->CanUnroll()) {
250+
new_game->UnRoll();
197251
}
198252
}
253+
else if (selected_control == 6) {
254+
new_game->ConfirmCharacter();
255+
message_handler("start");
256+
}
199257
}
200258
else if (ch == KEY_BACKSPACE) {
201259
if (selected_control == 0) {

ncurses/mainmenu.cpp

+9-13
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
#include <iostream>
99
#include <ranges>
1010

11-
using namespace std;
12-
13-
typedef pair<string, function<void()>> MenuItem;
11+
typedef std::pair<std::string, std::function<void()>> MenuItem;
1412
typedef std::vector<MenuItem>::iterator MenuItemIterator;
1513

1614
MainMenu::MainMenu(const std::shared_ptr<Game>& game, const MessageHandler& message_handler) : NCursesView(game, message_handler) {
@@ -29,28 +27,26 @@ MainMenu::MainMenu(const std::shared_ptr<Game>& game, const MessageHandler& mess
2927
void MainMenu::Resize(const int new_screen_width, const int new_screen_height) {
3028
NCursesView::Resize(new_screen_width, new_screen_height);
3129
delwin(win);
32-
constexpr int required_width = 26;
33-
const int required_height = static_cast<int>(menu_options.size()) + 6;
34-
const int menu_top = max((screen_height / 3) - required_height / 2, 0);
35-
const int menu_left = max(0, (screen_width / 2) - required_width / 2);
36-
win = newwin(required_height, required_width, menu_top, menu_left);
30+
constexpr int required_width = 80;
31+
constexpr int required_height = 24;
32+
win = newwin(required_height, required_width, 0, 0);
3733
keypad(win, true);
3834
nodelay(win, true);
3935
wtimeout(win, 100);
4036
}
4137

4238

4339
void MainMenu::Render() {
44-
constexpr int horizontal_middle = 13;
40+
constexpr int horizontal_middle = 40;
4541
wclear(win);
4642
box(win, 0, 0);
47-
CenterAlign(win, "Progress Quest 2", horizontal_middle, 2);
43+
CenterAlign(win, "Progress Quest 2", horizontal_middle, 8);
4844
int index = 0;
4945
for (const auto& label : std::views::keys(menu_options)) {
5046
if (index == selected_index) {
5147
wattron(win, A_STANDOUT);
5248
}
53-
CenterAlign(win, label, horizontal_middle, 4 + index);
49+
CenterAlign(win, label, horizontal_middle, 10 + index);
5450
if (index == selected_index) {
5551
wattroff(win, A_STANDOUT);
5652
}
@@ -63,10 +59,10 @@ void MainMenu::Render() {
6359
message_handler("quit");
6460
}
6561
else if (ch == KEY_DOWN) {
66-
selected_index = min(selected_index + 1, static_cast<int>(menu_options.size()) - 1);
62+
selected_index = std::min(selected_index + 1, static_cast<int>(menu_options.size()) - 1);
6763
}
6864
else if (ch == KEY_UP) {
69-
selected_index = max(selected_index - 1, 0);
65+
selected_index = std::max(selected_index - 1, 0);
7066
}
7167
else if (ch == '\n' || ch == ' ') {
7268
const auto [_, func] = menu_options[selected_index];

ncurses/pq2ncurses.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ void NCursesGUI::ShowCharacterCreator() {
8888
}
8989

9090
void NCursesGUI::ShowGameScreen() {
91-
91+
// TODO: game screen
92+
// for now just go back to main menu
93+
ShowMainMenu();
9294
}
9395

9496
void NCursesGUI::Close() {

0 commit comments

Comments
 (0)