main.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Field.h"
  27. #include <LibCore/ConfigFile.h>
  28. #include <LibGUI/AboutDialog.h>
  29. #include <LibGUI/Action.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/BoxLayout.h>
  32. #include <LibGUI/Button.h>
  33. #include <LibGUI/Label.h>
  34. #include <LibGUI/Menu.h>
  35. #include <LibGUI/MenuBar.h>
  36. #include <LibGUI/Window.h>
  37. #include <stdio.h>
  38. int main(int argc, char** argv)
  39. {
  40. if (pledge("stdio rpath accept wpath cpath shared_buffer unix fattr", nullptr) < 0) {
  41. perror("pledge");
  42. return 1;
  43. }
  44. GUI::Application app(argc, argv);
  45. if (pledge("stdio rpath accept wpath cpath shared_buffer", nullptr) < 0) {
  46. perror("pledge");
  47. return 1;
  48. }
  49. auto window = GUI::Window::construct();
  50. window->set_resizable(false);
  51. window->set_title("Minesweeper");
  52. window->set_rect(100, 100, 139, 175);
  53. auto& widget = window->set_main_widget<GUI::Widget>();
  54. widget.set_layout<GUI::VerticalBoxLayout>();
  55. widget.layout()->set_spacing(0);
  56. auto container = widget.add<GUI::Widget>();
  57. container->set_fill_with_background_color(true);
  58. container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  59. container->set_preferred_size(0, 36);
  60. container->set_layout<GUI::HorizontalBoxLayout>();
  61. auto flag_icon_label = container->add<GUI::Label>();
  62. flag_icon_label->set_icon(Gfx::Bitmap::load_from_file("/res/icons/minesweeper/flag.png"));
  63. auto flag_label = container->add<GUI::Label>();
  64. auto face_button = container->add<GUI::Button>();
  65. face_button->set_button_style(Gfx::ButtonStyle::CoolBar);
  66. face_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  67. face_button->set_preferred_size(36, 0);
  68. auto time_icon_label = container->add<GUI::Label>();
  69. time_icon_label->set_icon(Gfx::Bitmap::load_from_file("/res/icons/minesweeper/timer.png"));
  70. auto time_label = container->add<GUI::Label>();
  71. auto field = widget.add<Field>(*flag_label, *time_label, *face_button, [&](auto size) {
  72. size.set_height(size.height() + container->preferred_size().height());
  73. window->resize(size);
  74. });
  75. auto menubar = make<GUI::MenuBar>();
  76. auto app_menu = GUI::Menu::construct("Minesweeper");
  77. app_menu->add_action(GUI::Action::create("New game", { Mod_None, Key_F2 }, [&](const GUI::Action&) {
  78. field->reset();
  79. }));
  80. app_menu->add_separator();
  81. NonnullRefPtr<GUI::Action> chord_toggler_action = GUI::Action::create("Single-click chording", [&](const GUI::Action&) {
  82. bool toggled = !field->is_single_chording();
  83. field->set_single_chording(toggled);
  84. chord_toggler_action->set_checked(toggled);
  85. });
  86. chord_toggler_action->set_checkable(true);
  87. chord_toggler_action->set_checked(field->is_single_chording());
  88. app_menu->add_action(*chord_toggler_action);
  89. app_menu->add_separator();
  90. app_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  91. GUI::Application::the().quit(0);
  92. return;
  93. }));
  94. menubar->add_menu(move(app_menu));
  95. auto difficulty_menu = GUI::Menu::construct("Difficulty");
  96. difficulty_menu->add_action(GUI::Action::create("Beginner", { Mod_Ctrl, Key_B }, [&](const GUI::Action&) {
  97. field->set_field_size(9, 9, 10);
  98. }));
  99. difficulty_menu->add_action(GUI::Action::create("Intermediate", { Mod_Ctrl, Key_I }, [&](const GUI::Action&) {
  100. field->set_field_size(16, 16, 40);
  101. }));
  102. difficulty_menu->add_action(GUI::Action::create("Expert", { Mod_Ctrl, Key_E }, [&](const GUI::Action&) {
  103. field->set_field_size(16, 30, 99);
  104. }));
  105. difficulty_menu->add_action(GUI::Action::create("Madwoman", { Mod_Ctrl, Key_M }, [&](const GUI::Action&) {
  106. field->set_field_size(32, 60, 350);
  107. }));
  108. menubar->add_menu(move(difficulty_menu));
  109. auto help_menu = GUI::Menu::construct("Help");
  110. help_menu->add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  111. GUI::AboutDialog::show("Minesweeper", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-minesweeper.png"), window);
  112. }));
  113. menubar->add_menu(move(help_menu));
  114. app.set_menubar(move(menubar));
  115. window->show();
  116. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/minesweeper/mine.png"));
  117. return app.exec();
  118. }