mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:37:45 +00:00
Eyes: Allow constructing an eye-grid
Because a line of eyes is just not impressive enough. This does not change any of the default behaviours except breaking the line of eyes at 13 eyes (the 'sweet spot' for the default resolution)
This commit is contained in:
parent
9ee1edae2a
commit
2c6a983b34
3 changed files with 50 additions and 12 deletions
|
@ -57,16 +57,21 @@ void EyesWidget::paint_event(GUI::PaintEvent& event)
|
||||||
|
|
||||||
painter.clear_rect(event.rect(), Gfx::Color());
|
painter.clear_rect(event.rect(), Gfx::Color());
|
||||||
|
|
||||||
for (int i = 0; i < m_num_eyes; i++)
|
for (int i = 0; i < m_full_rows; i++) {
|
||||||
render_eyeball(i, painter);
|
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;
|
auto eye_width = width() / m_eyes_in_row;
|
||||||
Gfx::IntRect bounds { index * eye_width, 0, eye_width, height() };
|
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 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);
|
bounds.shrink(int(eye_width / 12.5), 0);
|
||||||
painter.fill_ellipse(bounds, palette().base_text());
|
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;
|
double max_distance_along_this_direction;
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
if (dx != 0 && abs(dx) >= abs(dy)) {
|
if (dx != 0 && abs(dx) >= abs(dy)) {
|
||||||
double slope = dy / dx;
|
double slope = dy / dx;
|
||||||
double slope_squared = slope * slope;
|
double slope_squared = slope * slope;
|
||||||
|
@ -120,6 +126,7 @@ Gfx::IntPoint EyesWidget::pupil_center(Gfx::IntRect& eyeball_bounds) const
|
||||||
} else {
|
} else {
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
double scale = min(1.0, max_distance_along_this_direction / mouse_distance);
|
double scale = min(1.0, max_distance_along_this_direction / mouse_distance);
|
||||||
|
|
||||||
|
|
|
@ -37,17 +37,23 @@ public:
|
||||||
void track_cursor_globally();
|
void track_cursor_globally();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EyesWidget(int num_eyes)
|
EyesWidget(int num_eyes, int full_rows, int extra)
|
||||||
: m_num_eyes(num_eyes)
|
: 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 mousemove_event(GUI::MouseEvent&) override;
|
||||||
virtual void paint_event(GUI::PaintEvent&) 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 pupil_center(Gfx::IntRect& eyeball_bounds) const;
|
||||||
|
|
||||||
Gfx::IntPoint m_mouse_position;
|
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 };
|
||||||
};
|
};
|
||||||
|
|
|
@ -32,9 +32,17 @@
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
int num_eyes = 2;
|
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;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.add_option(num_eyes, "Number of eyes", "num-eyes", 'n', "number");
|
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);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
if (pledge("stdio shared_buffer accept rpath unix cpath wpath fattr thread", nullptr) < 0) {
|
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;
|
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();
|
auto window = GUI::Window::construct();
|
||||||
window->set_title("Eyes");
|
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);
|
window->set_has_alpha_channel(true);
|
||||||
|
|
||||||
auto& eyes = window->set_main_widget<EyesWidget>(num_eyes);
|
auto& eyes = window->set_main_widget<EyesWidget>(num_eyes, full_rows, extra_columns);
|
||||||
window->show();
|
window->show();
|
||||||
eyes.track_cursor_globally();
|
eyes.track_cursor_globally();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue