CustomGameDialog.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2021, Pedro Pereira <pmh.pereira@gmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "CustomGameDialog.h"
  8. #include "Field.h"
  9. #include <Games/Minesweeper/MinesweeperCustomGameWindowGML.h>
  10. GUI::Dialog::ExecResult CustomGameDialog::show(GUI::Window* parent_window, Field& field)
  11. {
  12. auto dialog = CustomGameDialog::construct(parent_window);
  13. if (parent_window) {
  14. dialog->set_icon(parent_window->icon());
  15. dialog->center_within(*parent_window);
  16. }
  17. dialog->m_columns_spinbox->set_value(field.columns());
  18. dialog->m_rows_spinbox->set_value(field.rows());
  19. dialog->m_mines_spinbox->set_value(field.mine_count());
  20. auto result = dialog->exec();
  21. if (result != ExecResult::OK)
  22. return result;
  23. field.set_field_size(Field::Difficulty::Custom, dialog->m_rows_spinbox->value(), dialog->m_columns_spinbox->value(), dialog->m_mines_spinbox->value());
  24. return ExecResult::OK;
  25. }
  26. void CustomGameDialog::set_max_mines()
  27. {
  28. // Generating a field with > 50% mines takes too long.
  29. // FIXME: Allow higher amount of mines to be placed.
  30. m_mines_spinbox->set_max((m_rows_spinbox->value() * m_columns_spinbox->value()) / 2);
  31. }
  32. CustomGameDialog::CustomGameDialog(Window* parent_window)
  33. : Dialog(parent_window)
  34. {
  35. resize(305, 90);
  36. center_on_screen();
  37. set_resizable(false);
  38. set_title("Custom game");
  39. auto& main_widget = set_main_widget<GUI::Widget>();
  40. if (!main_widget.load_from_gml(minesweeper_custom_game_window_gml))
  41. VERIFY_NOT_REACHED();
  42. m_columns_spinbox = *main_widget.find_descendant_of_type_named<GUI::SpinBox>("columns_spinbox");
  43. m_rows_spinbox = *main_widget.find_descendant_of_type_named<GUI::SpinBox>("rows_spinbox");
  44. m_mines_spinbox = *main_widget.find_descendant_of_type_named<GUI::SpinBox>("mines_spinbox");
  45. m_ok_button = *main_widget.find_descendant_of_type_named<GUI::Button>("ok_button");
  46. m_cancel_button = *main_widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
  47. m_columns_spinbox->on_change = [this](auto) {
  48. set_max_mines();
  49. };
  50. m_rows_spinbox->on_change = [this](auto) {
  51. set_max_mines();
  52. };
  53. m_ok_button->on_click = [this](auto) {
  54. done(ExecResult::OK);
  55. };
  56. m_cancel_button->on_click = [this](auto) {
  57. done(ExecResult::Cancel);
  58. };
  59. set_max_mines();
  60. }