mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-29 06:02:07 +03:00
Conway: Add interactivity
This commit is contained in:
parent
cffafd90de
commit
3179065466
Notes:
sideshowbarker
2024-07-18 22:59:32 +09:00
Author: https://github.com/BenWiederhake Commit: https://github.com/SerenityOS/serenity/commit/31790654666 Pull-request: https://github.com/SerenityOS/serenity/pull/5053
@ -95,25 +95,88 @@ void Game::timer_event(Core::TimerEvent&)
|
|||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Gfx::IntRect Game::first_cell_rect() const
|
||||||
|
{
|
||||||
|
auto game_rect = rect();
|
||||||
|
auto cell_size = Gfx::IntSize(game_rect.width() / m_columns, game_rect.height() / m_rows);
|
||||||
|
auto x_margin = (game_rect.width() - (cell_size.width() * m_columns)) / 2;
|
||||||
|
auto y_margin = (game_rect.height() - (cell_size.height() * m_rows)) / 2;
|
||||||
|
return { x_margin, y_margin, cell_size.width(), cell_size.height() };
|
||||||
|
}
|
||||||
|
|
||||||
void Game::paint_event(GUI::PaintEvent& event)
|
void Game::paint_event(GUI::PaintEvent& event)
|
||||||
{
|
{
|
||||||
GUI::Painter painter(*this);
|
GUI::Painter painter(*this);
|
||||||
painter.add_clip_rect(event.rect());
|
painter.add_clip_rect(event.rect());
|
||||||
painter.fill_rect(event.rect(), m_dead_color);
|
painter.fill_rect(event.rect(), m_dead_color);
|
||||||
auto game_rect = rect();
|
auto first_rect = first_cell_rect();
|
||||||
auto cell_size = Gfx::IntSize(game_rect.width() / m_columns, game_rect.height() / m_rows);
|
|
||||||
auto x_margin = (game_rect.width() - (cell_size.width() * m_columns)) / 2;
|
|
||||||
auto y_margin = (game_rect.height() - (cell_size.height() * m_rows)) / 2;
|
|
||||||
|
|
||||||
for (int y = 0; y < m_rows; y++) {
|
for (int y = 0; y < m_rows; y++) {
|
||||||
for (int x = 0; x < m_columns; x++) {
|
for (int x = 0; x < m_columns; x++) {
|
||||||
Gfx::IntRect rect {
|
Gfx::IntRect rect {
|
||||||
x * cell_size.width() + x_margin,
|
x * first_rect.width() + first_rect.left(),
|
||||||
y * cell_size.height() + y_margin,
|
y * first_rect.height() + first_rect.top(),
|
||||||
cell_size.width(),
|
first_rect.width(),
|
||||||
cell_size.height()
|
first_rect.height()
|
||||||
};
|
};
|
||||||
painter.fill_rect(rect, m_universe[y][x] ? m_alive_color : m_dead_color);
|
painter.fill_rect(rect, m_universe[y][x] ? m_alive_color : m_dead_color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Game::mousedown_event(GUI::MouseEvent& event)
|
||||||
|
{
|
||||||
|
switch (event.button()) {
|
||||||
|
case GUI::MouseButton::Left:
|
||||||
|
case GUI::MouseButton::Right:
|
||||||
|
m_last_button = event.button();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
interact_at(event.position());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::mouseup_event(GUI::MouseEvent& event)
|
||||||
|
{
|
||||||
|
if (event.button() == m_last_button)
|
||||||
|
m_last_button = GUI::MouseButton::None;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::mousemove_event(GUI::MouseEvent& event)
|
||||||
|
{
|
||||||
|
interact_at(event.position());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::interact_at(const Gfx::IntPoint& point)
|
||||||
|
{
|
||||||
|
if (m_last_button == GUI::MouseButton::None)
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto first_rect = first_cell_rect();
|
||||||
|
// Too tiny window, we don't actually display anything.
|
||||||
|
if (first_rect.width() == 0 || first_rect.height() == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Too far left/up.
|
||||||
|
if (point.x() < first_rect.left() || point.y() < first_rect.top())
|
||||||
|
return;
|
||||||
|
|
||||||
|
int cell_x = (point.x() - first_rect.left()) / first_rect.width();
|
||||||
|
int cell_y = (point.y() - first_rect.top()) / first_rect.height();
|
||||||
|
|
||||||
|
// Too far right/down.
|
||||||
|
if (cell_x >= m_columns || cell_y >= m_rows)
|
||||||
|
return;
|
||||||
|
|
||||||
|
switch (m_last_button) {
|
||||||
|
case GUI::MouseButton::Left:
|
||||||
|
m_universe[cell_y][cell_x] = true;
|
||||||
|
break;
|
||||||
|
case GUI::MouseButton::Right:
|
||||||
|
m_universe[cell_y][cell_x] = false;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -38,15 +38,21 @@ private:
|
|||||||
Game();
|
Game();
|
||||||
virtual void paint_event(GUI::PaintEvent&) override;
|
virtual void paint_event(GUI::PaintEvent&) override;
|
||||||
virtual void timer_event(Core::TimerEvent&) override;
|
virtual void timer_event(Core::TimerEvent&) override;
|
||||||
|
virtual void mousedown_event(GUI::MouseEvent&) override;
|
||||||
|
virtual void mouseup_event(GUI::MouseEvent&) override;
|
||||||
|
virtual void mousemove_event(GUI::MouseEvent&) override;
|
||||||
|
|
||||||
|
Gfx::IntRect first_cell_rect() const;
|
||||||
void seed_universe();
|
void seed_universe();
|
||||||
void update_universe();
|
void update_universe();
|
||||||
|
void interact_at(const Gfx::IntPoint&);
|
||||||
|
|
||||||
Gfx::Color m_alive_color { Color::Green };
|
const Gfx::Color m_alive_color { Color::Green };
|
||||||
Gfx::Color m_dead_color { Color::Black };
|
const Gfx::Color m_dead_color { Color::Black };
|
||||||
int m_rows { 200 };
|
const int m_rows { 200 };
|
||||||
int m_columns { 200 };
|
const int m_columns { 200 };
|
||||||
int m_sleep { 100 };
|
const int m_sleep { 100 };
|
||||||
|
GUI::MouseButton m_last_button { GUI::MouseButton::None };
|
||||||
|
|
||||||
bool m_universe[200][200];
|
bool m_universe[200][200];
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user