main.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ChessWidget.h"
  7. #include <LibConfig/Client.h>
  8. #include <LibCore/DirIterator.h>
  9. #include <LibCore/System.h>
  10. #include <LibGUI/ActionGroup.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/Clipboard.h>
  13. #include <LibGUI/FilePicker.h>
  14. #include <LibGUI/Icon.h>
  15. #include <LibGUI/Menu.h>
  16. #include <LibGUI/Menubar.h>
  17. #include <LibGUI/MessageBox.h>
  18. #include <LibGUI/Window.h>
  19. #include <LibMain/Main.h>
  20. ErrorOr<int> serenity_main(Main::Arguments arguments)
  21. {
  22. TRY(Core::System::pledge("stdio rpath wpath cpath recvfd sendfd thread proc exec unix", nullptr));
  23. auto app = GUI::Application::construct(arguments);
  24. Config::pledge_domains("Chess");
  25. TRY(Core::System::pledge("stdio rpath wpath cpath recvfd sendfd thread proc exec", nullptr));
  26. auto app_icon = GUI::Icon::default_icon("app-chess");
  27. auto window = GUI::Window::construct();
  28. auto& widget = window->set_main_widget<ChessWidget>();
  29. TRY(Core::System::unveil("/res", "r"));
  30. TRY(Core::System::unveil("/bin/ChessEngine", "x"));
  31. TRY(Core::System::unveil("/etc/passwd", "r"));
  32. TRY(Core::System::unveil(Core::StandardPaths::home_directory().characters(), "wcbr"));
  33. TRY(Core::System::unveil(nullptr, nullptr));
  34. auto size = Config::read_i32("Chess", "Display", "size", 512);
  35. window->set_title("Chess");
  36. window->set_base_size({ 4, 4 });
  37. window->set_size_increment({ 8, 8 });
  38. window->resize(size - 4, size - 4);
  39. window->set_icon(app_icon.bitmap_for_size(16));
  40. widget.set_piece_set(Config::read_string("Chess", "Style", "PieceSet", "stelar7"));
  41. widget.set_board_theme(Config::read_string("Chess", "Style", "BoardTheme", "Beige"));
  42. widget.set_coordinates(Config::read_bool("Chess", "Style", "Coordinates", true));
  43. widget.set_show_available_moves(Config::read_bool("Chess", "Style", "ShowAvailableMoves", true));
  44. auto& game_menu = window->add_menu("&Game");
  45. game_menu.add_action(GUI::Action::create("&Resign", { Mod_None, Key_F3 }, [&](auto&) {
  46. widget.resign();
  47. }));
  48. game_menu.add_action(GUI::Action::create("&Flip Board", { Mod_Ctrl, Key_F }, [&](auto&) {
  49. widget.flip_board();
  50. }));
  51. game_menu.add_separator();
  52. game_menu.add_action(GUI::Action::create("&Import PGN...", { Mod_Ctrl, Key_O }, [&](auto&) {
  53. Optional<String> import_path = GUI::FilePicker::get_open_filepath(window);
  54. if (!import_path.has_value())
  55. return;
  56. if (!widget.import_pgn(import_path.value())) {
  57. GUI::MessageBox::show(window, "Unable to import game.\n", "Error", GUI::MessageBox::Type::Error);
  58. return;
  59. }
  60. dbgln("Imported PGN file from {}", import_path.value());
  61. }));
  62. game_menu.add_action(GUI::Action::create("&Export PGN...", { Mod_Ctrl, Key_S }, [&](auto&) {
  63. Optional<String> export_path = GUI::FilePicker::get_save_filepath(window, "Untitled", "pgn");
  64. if (!export_path.has_value())
  65. return;
  66. if (!widget.export_pgn(export_path.value())) {
  67. GUI::MessageBox::show(window, "Unable to export game.\n", "Error", GUI::MessageBox::Type::Error);
  68. return;
  69. }
  70. dbgln("Exported PGN file to {}", export_path.value());
  71. }));
  72. game_menu.add_action(GUI::Action::create("&Copy FEN", { Mod_Ctrl, Key_C }, [&](auto&) {
  73. GUI::Clipboard::the().set_data(widget.get_fen().bytes());
  74. GUI::MessageBox::show(window, "Board state copied to clipboard as FEN.", "Copy FEN", GUI::MessageBox::Type::Information);
  75. }));
  76. game_menu.add_separator();
  77. game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
  78. if (widget.board().game_result() == Chess::Board::Result::NotFinished) {
  79. if (widget.resign() < 0)
  80. return;
  81. }
  82. widget.reset();
  83. }));
  84. game_menu.add_separator();
  85. game_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  86. GUI::Application::the()->quit();
  87. }));
  88. auto& style_menu = window->add_menu("&Style");
  89. GUI::ActionGroup piece_set_action_group;
  90. piece_set_action_group.set_exclusive(true);
  91. auto& piece_set_menu = style_menu.add_submenu("&Piece Set");
  92. piece_set_menu.set_icon(app_icon.bitmap_for_size(16));
  93. Core::DirIterator di("/res/icons/chess/sets/", Core::DirIterator::SkipParentAndBaseDir);
  94. while (di.has_next()) {
  95. auto set = di.next_path();
  96. auto action = GUI::Action::create_checkable(set, [&](auto& action) {
  97. widget.set_piece_set(action.text());
  98. widget.update();
  99. Config::write_string("Chess", "Style", "PieceSet", action.text());
  100. });
  101. piece_set_action_group.add_action(*action);
  102. if (widget.piece_set() == set)
  103. action->set_checked(true);
  104. piece_set_menu.add_action(*action);
  105. }
  106. GUI::ActionGroup board_theme_action_group;
  107. board_theme_action_group.set_exclusive(true);
  108. auto& board_theme_menu = style_menu.add_submenu("Board Theme");
  109. board_theme_menu.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/chess/mini-board.png").release_value_but_fixme_should_propagate_errors());
  110. for (auto& theme : Vector({ "Beige", "Green", "Blue" })) {
  111. auto action = GUI::Action::create_checkable(theme, [&](auto& action) {
  112. widget.set_board_theme(action.text());
  113. widget.update();
  114. Config::write_string("Chess", "Style", "BoardTheme", action.text());
  115. });
  116. board_theme_action_group.add_action(*action);
  117. if (widget.board_theme().name == theme)
  118. action->set_checked(true);
  119. board_theme_menu.add_action(*action);
  120. }
  121. auto coordinates_action = GUI::Action::create_checkable("Coordinates", [&](auto& action) {
  122. widget.set_coordinates(action.is_checked());
  123. widget.update();
  124. Config::write_bool("Chess", "Style", "Coordinates", action.is_checked());
  125. });
  126. coordinates_action->set_checked(widget.coordinates());
  127. style_menu.add_action(coordinates_action);
  128. auto show_available_moves_action = GUI::Action::create_checkable("Show Available Moves", [&](auto& action) {
  129. widget.set_show_available_moves(action.is_checked());
  130. widget.update();
  131. Config::write_bool("Chess", "Style", "ShowAvailableMoves", action.is_checked());
  132. });
  133. show_available_moves_action->set_checked(widget.show_available_moves());
  134. style_menu.add_action(show_available_moves_action);
  135. auto& engine_menu = window->add_menu("&Engine");
  136. GUI::ActionGroup engines_action_group;
  137. engines_action_group.set_exclusive(true);
  138. auto& engine_submenu = engine_menu.add_submenu("&Engine");
  139. for (auto& engine : Vector({ "Human", "ChessEngine" })) {
  140. auto action = GUI::Action::create_checkable(engine, [&](auto& action) {
  141. if (action.text() == "Human") {
  142. widget.set_engine(nullptr);
  143. } else {
  144. widget.set_engine(Engine::construct(action.text()));
  145. widget.input_engine_move();
  146. }
  147. });
  148. engines_action_group.add_action(*action);
  149. if (engine == String("Human"))
  150. action->set_checked(true);
  151. engine_submenu.add_action(*action);
  152. }
  153. auto& help_menu = window->add_menu("&Help");
  154. help_menu.add_action(GUI::CommonActions::make_about_action("Chess", app_icon, window));
  155. window->show();
  156. widget.reset();
  157. return app->exec();
  158. }