mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-07 19:57:45 +03:00
Solitaire: Replace self-owned timer with Core::Object's timer
This is just a bit nicer than owning a separate timer in the Solitaire application because LibCore will prevent timer events from firing when e.g. the window is not visible. Therefore SolitaireWidget doesn't need need to check for such conditions.
This commit is contained in:
parent
cfc3a2ebac
commit
15f0ee1727
Notes:
sideshowbarker
2024-07-18 18:40:24 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/15f0ee1727c Pull-request: https://github.com/SerenityOS/serenity/pull/6880
@ -5,15 +5,14 @@
|
||||
*/
|
||||
|
||||
#include "SolitaireWidget.h"
|
||||
#include <LibCore/Timer.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <time.h>
|
||||
|
||||
static const Color s_background_color { Color::from_rgb(0x008000) };
|
||||
static constexpr uint8_t new_game_animation_delay = 5;
|
||||
static constexpr int s_timer_interval_ms = 1000 / 60;
|
||||
|
||||
SolitaireWidget::SolitaireWidget(GUI::Window& window, Function<void(uint32_t)>&& on_score_update)
|
||||
SolitaireWidget::SolitaireWidget(Function<void(uint32_t)>&& on_score_update)
|
||||
: m_on_score_update(move(on_score_update))
|
||||
{
|
||||
set_fill_with_background_color(false);
|
||||
@ -31,9 +30,6 @@ SolitaireWidget::SolitaireWidget(GUI::Window& window, Function<void(uint32_t)>&&
|
||||
m_stacks[Pile5] = CardStack({ 10 + 4 * Card::width + 40, 10 + Card::height + 10 }, CardStack::Type::Normal);
|
||||
m_stacks[Pile6] = CardStack({ 10 + 5 * Card::width + 50, 10 + Card::height + 10 }, CardStack::Type::Normal);
|
||||
m_stacks[Pile7] = CardStack({ 10 + 6 * Card::width + 60, 10 + Card::height + 10 }, CardStack::Type::Normal);
|
||||
|
||||
m_timer = Core::Timer::construct(1000 / 60, [&]() { tick(window); });
|
||||
m_timer->stop();
|
||||
}
|
||||
|
||||
SolitaireWidget::~SolitaireWidget()
|
||||
@ -45,11 +41,8 @@ static float rand_float()
|
||||
return rand() / static_cast<float>(RAND_MAX);
|
||||
}
|
||||
|
||||
void SolitaireWidget::tick(GUI::Window& window)
|
||||
void SolitaireWidget::timer_event(Core::TimerEvent&)
|
||||
{
|
||||
if (!is_visible() || !updates_enabled() || !window.is_visible_for_timer_purposes())
|
||||
return;
|
||||
|
||||
if (m_game_over_animation) {
|
||||
VERIFY(!m_animation.card().is_null());
|
||||
if (m_animation.card()->position().x() > SolitaireWidget::width || m_animation.card()->rect().right() < 0)
|
||||
@ -96,7 +89,7 @@ void SolitaireWidget::stop_game_over_animation()
|
||||
void SolitaireWidget::setup()
|
||||
{
|
||||
stop_game_over_animation();
|
||||
m_timer->stop();
|
||||
stop_timer();
|
||||
|
||||
for (auto& stack : m_stacks)
|
||||
stack.clear();
|
||||
@ -118,7 +111,7 @@ void SolitaireWidget::setup()
|
||||
m_new_deck.append(m_new_deck.take(rand() % m_new_deck.size()));
|
||||
|
||||
m_new_game_animation = true;
|
||||
m_timer->start();
|
||||
start_timer(s_timer_interval_ms);
|
||||
update();
|
||||
}
|
||||
|
||||
@ -353,11 +346,6 @@ void SolitaireWidget::paint_event(GUI::PaintEvent& event)
|
||||
GUI::Painter painter(*this);
|
||||
|
||||
if (m_repaint_all) {
|
||||
/* Only start the timer when update() got called from the
|
||||
window manager, or else we might end up with a blank screen */
|
||||
if (!m_timer->is_active())
|
||||
m_timer->start();
|
||||
|
||||
painter.fill_rect(event.rect(), s_background_color);
|
||||
|
||||
for (auto& stack : m_stacks)
|
||||
|
@ -20,7 +20,7 @@ public:
|
||||
void setup();
|
||||
|
||||
private:
|
||||
SolitaireWidget(GUI::Window&, Function<void(uint32_t)>&& on_score_update);
|
||||
SolitaireWidget(Function<void(uint32_t)>&& on_score_update);
|
||||
|
||||
class Animation {
|
||||
public:
|
||||
@ -85,7 +85,6 @@ private:
|
||||
void stop_game_over_animation();
|
||||
void create_new_animation_card();
|
||||
void check_for_game_over();
|
||||
void tick(GUI::Window&);
|
||||
|
||||
ALWAYS_INLINE CardStack& stack(StackLocation location)
|
||||
{
|
||||
@ -98,8 +97,8 @@ private:
|
||||
virtual void mousemove_event(GUI::MouseEvent&) override;
|
||||
virtual void doubleclick_event(GUI::MouseEvent&) override;
|
||||
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||
virtual void timer_event(Core::TimerEvent&) override;
|
||||
|
||||
RefPtr<Core::Timer> m_timer;
|
||||
NonnullRefPtrVector<Card> m_focused_cards;
|
||||
NonnullRefPtrVector<Card> m_new_deck;
|
||||
CardStack m_stacks[StackLocation::__Count];
|
||||
|
@ -39,7 +39,7 @@ int main(int argc, char** argv)
|
||||
window->set_resizable(false);
|
||||
window->resize(SolitaireWidget::width, SolitaireWidget::height);
|
||||
|
||||
auto widget = SolitaireWidget::construct(window, [&](uint32_t score) {
|
||||
auto widget = SolitaireWidget::construct([&](uint32_t score) {
|
||||
window->set_title(String::formatted("Score: {} - Solitaire", score));
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user