diff --git a/Demos/Eyes/EyesWidget.cpp b/Demos/Eyes/EyesWidget.cpp index 036cd8375e..c31d4782e1 100644 --- a/Demos/Eyes/EyesWidget.cpp +++ b/Demos/Eyes/EyesWidget.cpp @@ -57,16 +57,21 @@ void EyesWidget::paint_event(GUI::PaintEvent& event) painter.clear_rect(event.rect(), Gfx::Color()); - for (int i = 0; i < m_num_eyes; i++) - render_eyeball(i, painter); + for (int i = 0; i < m_full_rows; i++) { + for (int j = 0; j < m_eyes_in_row; j++) + render_eyeball(i, j, painter); + } + for (int i = 0; i < m_extra_columns; ++i) + render_eyeball(m_full_rows, i, painter); } -void EyesWidget::render_eyeball(int index, GUI::Painter& painter) const +void EyesWidget::render_eyeball(int row, int column, GUI::Painter& painter) const { - auto eye_width = width() / m_num_eyes; - Gfx::IntRect bounds { index * eye_width, 0, eye_width, height() }; + auto eye_width = width() / m_eyes_in_row; + auto eye_height = height() / m_num_rows; + Gfx::IntRect bounds { column * eye_width, row * eye_height, eye_width, eye_height }; auto width_thickness = max(int(eye_width / 5.5), 1); - auto height_thickness = max(int(height() / 5.5), 1); + auto height_thickness = max(int(eye_height / 5.5), 1); bounds.shrink(int(eye_width / 12.5), 0); painter.fill_ellipse(bounds, palette().base_text()); @@ -103,6 +108,7 @@ Gfx::IntPoint EyesWidget::pupil_center(Gfx::IntRect& eyeball_bounds) const double max_distance_along_this_direction; + // clang-format off if (dx != 0 && abs(dx) >= abs(dy)) { double slope = dy / dx; double slope_squared = slope * slope; @@ -120,6 +126,7 @@ Gfx::IntPoint EyesWidget::pupil_center(Gfx::IntRect& eyeball_bounds) const } else { ASSERT_NOT_REACHED(); } + // clang-format on double scale = min(1.0, max_distance_along_this_direction / mouse_distance); diff --git a/Demos/Eyes/EyesWidget.h b/Demos/Eyes/EyesWidget.h index 44505e707e..c03f87b291 100644 --- a/Demos/Eyes/EyesWidget.h +++ b/Demos/Eyes/EyesWidget.h @@ -37,17 +37,23 @@ public: void track_cursor_globally(); private: - EyesWidget(int num_eyes) - : m_num_eyes(num_eyes) + EyesWidget(int num_eyes, int full_rows, int extra) + : m_full_rows(full_rows) + , m_extra_columns(extra) { + m_num_rows = m_extra_columns > 0 ? m_full_rows + 1 : m_full_rows; + m_eyes_in_row = (num_eyes - extra) / full_rows; } virtual void mousemove_event(GUI::MouseEvent&) override; virtual void paint_event(GUI::PaintEvent&) override; - void render_eyeball(int index, GUI::Painter&) const; + void render_eyeball(int row, int column, GUI::Painter&) const; Gfx::IntPoint pupil_center(Gfx::IntRect& eyeball_bounds) const; Gfx::IntPoint m_mouse_position; - int m_num_eyes { -1 }; + int m_eyes_in_row { -1 }; + int m_full_rows { -1 }; + int m_extra_columns { -1 }; + int m_num_rows { -1 }; }; diff --git a/Demos/Eyes/main.cpp b/Demos/Eyes/main.cpp index 9058ec5657..11bcea21e9 100644 --- a/Demos/Eyes/main.cpp +++ b/Demos/Eyes/main.cpp @@ -32,9 +32,17 @@ int main(int argc, char* argv[]) { int num_eyes = 2; + int max_in_row = 13; + + // Alternatively, allow the user to ask for a grid. + int grid_rows = -1; + int grid_columns = -1; Core::ArgsParser args_parser; args_parser.add_option(num_eyes, "Number of eyes", "num-eyes", 'n', "number"); + args_parser.add_option(max_in_row, "Maximum number of eyes in a row", "max-in-row", 'm', "number"); + args_parser.add_option(grid_rows, "Number of rows in grid (incompatible with --number)", "grid-rows", 'r', "number"); + args_parser.add_option(grid_columns, "Number of columns in grid (incompatible with --number)", "grid-cols", 'c', "number"); args_parser.parse(argc, argv); if (pledge("stdio shared_buffer accept rpath unix cpath wpath fattr thread", nullptr) < 0) { @@ -49,12 +57,29 @@ int main(int argc, char* argv[]) return 1; } + if ((grid_rows > 0) ^ (grid_columns > 0)) { + fprintf(stderr, "Expected either both or none of 'grid-rows' and 'grid-cols' to be passed.\n"); + return 1; + } + + int full_rows, extra_columns; + + if (grid_rows > 0) { + full_rows = grid_rows; + extra_columns = 0; + num_eyes = grid_rows * grid_columns; + max_in_row = grid_columns; + } else { + full_rows = num_eyes / max_in_row; + extra_columns = num_eyes % max_in_row; + } + auto window = GUI::Window::construct(); window->set_title("Eyes"); - window->set_rect(350, 270, 75 * num_eyes, 100); + window->set_rect(350, 270, 75 * (full_rows > 0 ? max_in_row : extra_columns), 100 * (full_rows + (extra_columns > 0 ? 1 : 0))); window->set_has_alpha_channel(true); - auto& eyes = window->set_main_widget(num_eyes); + auto& eyes = window->set_main_widget(num_eyes, full_rows, extra_columns); window->show(); eyes.track_cursor_globally();