@@ -70,10 +70,28 @@ void draw(Bitmap &viewport, const Game &game)
7070 }
7171}
7272
73+ struct Displayed_Menu
74+ {
75+ Displayed_Menu (Menu menu_, Game &game) : menu{ std::move (menu_) }
76+ {
77+ ftxui::Components menu_lines;
78+
79+ std::ranges::transform (menu.items , std::back_inserter (menu_lines), [&game](auto &item) {
80+ return ftxui::Button (item.text , [&game, &item]() { item.action (game); });
81+ });
82+
83+ buttons = ftxui::Container::Vertical (menu_lines);
84+ }
85+
86+ Menu menu;
87+ ftxui::Component buttons;
88+ };
89+
7390void game_iteration_canvas ()// NOLINT cognitive complexity
7491{
7592 auto game = make_game ();
7693
94+ Displayed_Menu current_menu{ Menu{}, game };
7795
7896 // this should probably have a `bitmap` helper function that does what you expect
7997 // similar to the other parts of FTXUI
@@ -102,6 +120,10 @@ void game_iteration_canvas()// NOLINT cognitive complexity
102120 game.clock = game_clock;
103121
104122 [&] {
123+ if (game.has_menu ()) {
124+ return ;
125+ }
126+
105127 if (current_event != last_event) {
106128 auto location = game.player .map_location ;
107129 const auto last_location = location;
@@ -133,7 +155,6 @@ void game_iteration_canvas()// NOLINT cognitive complexity
133155 auto enter_action = game.maps .at (game.current_map ).locations .at (location).enter_action ;
134156 if (enter_action) { enter_action (game, location, from); }
135157 }
136-
137158 }
138159 }();
139160
@@ -149,32 +170,43 @@ void game_iteration_canvas()// NOLINT cognitive complexity
149170
150171 std::string last_character;
151172
173+ auto container = ftxui::Container::Vertical ({});
174+
175+ auto key_press = ftxui::CatchEvent (container, [&](const ftxui::Event &event) {
176+ if (game.has_menu ()) {
177+ return false ;
178+ } else {
179+ last_event = std::exchange (current_event, event);
180+ return true ;
181+ }
182+ });
183+
152184 auto make_layout = [&] {
153185 // This code actually processes the draw event
154186 const auto new_time = std::chrono::steady_clock::now ();
155187
188+ if (game.has_new_menu ()) { current_menu = Displayed_Menu{ game.get_menu (), game };
189+ key_press->DetachAllChildren ();
190+ key_press->Add (current_menu.buttons );
191+ }
192+
156193 ++counter;
157194 // we will dispatch to the game_iteration function, where the work happens
158195 game_iteration (new_time - last_time);
159196 last_time = new_time;
160197
161198 // now actually draw the game elements
162- return ftxui::vbox ({ftxui::hbox ({ bm | ftxui::border,
163- ftxui::vbox ({ ftxui::text (" Frame: " + std::to_string (counter)),
164- ftxui::text (" FPS: " + std::to_string (fps)),
165- ftxui::text (" Character: " + last_character),
166- small_bm | ftxui::border }) }), ftxui::text (" Message: " + game.last_message )});
199+ return ftxui::vbox ({ ftxui::hbox ({ (game.has_menu () ? current_menu.buttons ->Render () : bm) | ftxui::border,
200+ ftxui::vbox ({ ftxui::text (" Frame: " + std::to_string (counter)),
201+ ftxui::text (" FPS: " + std::to_string (fps)),
202+ ftxui::text (" Character: " + last_character),
203+ small_bm | ftxui::border }) }),
204+ ftxui::text (" Message: " + game.last_message ) });
167205 };
168206
169- auto container = ftxui::Container::Vertical ({});
170-
171- auto key_press = ftxui::CatchEvent (container, [&](const ftxui::Event &event) {
172- last_event = std::exchange (current_event, event);
173- return false ;
174- });
175207
176208
177- auto renderer = ftxui::Renderer (key_press, make_layout);
209+ auto renderer = ftxui::Renderer (game. has_menu () ? current_menu. buttons : key_press, make_layout);
178210
179211 std::atomic<bool > refresh_ui_continue = true ;
180212
0 commit comments