-
-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert change to button example. (#835)
It was introduced mistakenly by: f495ce0
- Loading branch information
1 parent
ce5ac6b
commit f609c12
Showing
1 changed file
with
51 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,64 @@ | ||
#include <ftxui/component/component.hpp> | ||
#include <ftxui/component/screen_interactive.hpp> | ||
#include <ftxui/dom/elements.hpp> | ||
#include <ftxui/screen/screen.hpp> | ||
|
||
#include <array> | ||
#include <iostream> | ||
#include <string> | ||
// Copyright 2020 Arthur Sonzogni. All rights reserved. | ||
// Use of this source code is governed by the MIT license that can be found in | ||
// the LICENSE file. | ||
#include <memory> // for shared_ptr, __shared_ptr_access | ||
#include <string> // for operator+, to_string | ||
|
||
#include "ftxui/component/captured_mouse.hpp" // for ftxui | ||
#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer | ||
#include "ftxui/component/component_base.hpp" // for ComponentBase | ||
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive | ||
#include "ftxui/dom/elements.hpp" // for separator, gauge, text, Element, operator|, vbox, border | ||
|
||
using namespace ftxui; | ||
using namespace std; | ||
|
||
enum class ConnectionMethod { ByName, ByAddress }; | ||
|
||
void promptForConnectionMethod(); | ||
|
||
void promptForServerName(); | ||
void promptForServerAddress(); | ||
void promptForServerManager(); | ||
|
||
int main() { | ||
promptForConnectionMethod(); | ||
|
||
return 0; | ||
} | ||
|
||
void promptForConnectionMethod() { | ||
auto screen = ScreenInteractive::TerminalOutput(); | ||
std::vector<std::string> connect_choice{ | ||
"Connect by name...", | ||
"Connect by address and port...", | ||
"Exit", | ||
}; | ||
int selected = 0; | ||
MenuOption option; | ||
option.on_enter = [&] { | ||
if (selected == 0) { | ||
promptForServerManager(); | ||
} else if (selected == 1) { | ||
promptForServerAddress(); | ||
} else if (selected == 2) { | ||
screen.Exit(); | ||
// This is a helper function to create a button with a custom style. | ||
// The style is defined by a lambda function that takes an EntryState and | ||
// returns an Element. | ||
// We are using `center` to center the text inside the button, then `border` to | ||
// add a border around the button, and finally `flex` to make the button fill | ||
// the available space. | ||
ButtonOption Style() { | ||
auto option = ButtonOption::Animated(); | ||
option.transform = [](const EntryState& s) { | ||
auto element = text(s.label); | ||
if (s.focused) { | ||
element |= bold; | ||
} | ||
return element | center | borderEmpty | flex; | ||
}; | ||
|
||
auto connect_menu = Menu(&connect_choice, &selected, option); | ||
|
||
auto renderer = Renderer(connect_menu, [&] { | ||
return vbox({ | ||
text("Welcome, my client!") | color(Color::Red3Bis) | bold | | ||
center, | ||
text(""), | ||
text("Selected = " + std::to_string(selected)) | | ||
color(Color::LightGreenBis) | bold | center, | ||
|
||
text(""), | ||
text("Welcome to my first working multiplayer game, Medium " | ||
"Boxes.") | | ||
color(Color::LightSkyBlue1), | ||
text(""), | ||
text("Now, choose how you'd prefer to connect to a server!") | | ||
color(Color::LightCyan3) | center, | ||
text(""), | ||
connect_menu->Render() | border, | ||
}) | | ||
center; | ||
}); | ||
|
||
screen.Loop(renderer); | ||
return option; | ||
} | ||
|
||
void promptForServerName() {} | ||
|
||
void promptForServerAddress() { | ||
auto screen = ScreenInteractive::TerminalOutput(); | ||
} | ||
int main() { | ||
int value = 50; | ||
|
||
// clang-format off | ||
auto btn_dec_01 = Button("-1", [&] { value += 1; }, Style()); | ||
auto btn_inc_01 = Button("+1", [&] { value -= 1; }, Style()); | ||
auto btn_dec_10 = Button("-10", [&] { value -= 10; }, Style()); | ||
auto btn_inc_10 = Button("+10", [&] { value += 10; }, Style()); | ||
// clang-format on | ||
|
||
// The tree of components. This defines how to navigate using the keyboard. | ||
// The selected `row` is shared to get a grid layout. | ||
int row = 0; | ||
auto buttons = Container::Vertical({ | ||
Container::Horizontal({btn_dec_01, btn_inc_01}, &row) | flex, | ||
Container::Horizontal({btn_dec_10, btn_inc_10}, &row) | flex, | ||
}); | ||
|
||
void promptForServerManager() { | ||
auto screen = ScreenInteractive::TerminalOutput(); | ||
auto renderer = Renderer([&] { | ||
// Modify the way to render them on screen: | ||
auto component = Renderer(buttons, [&] { | ||
return vbox({ | ||
text("Now, please enter the server manager's address and " | ||
"port, so that you'll be able to see all the available " | ||
"public servers!") | | ||
color(Color::LightGreenBis), | ||
gauge(0), | ||
text("value = " + std::to_string(value)), | ||
separator(), | ||
buttons->Render() | flex, | ||
}) | | ||
center; | ||
flex | border; | ||
}); | ||
|
||
screen.Loop(renderer); | ||
auto screen = ScreenInteractive::FitComponent(); | ||
screen.Loop(component); | ||
return 0; | ||
} |