mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:47:46 +00:00
Solitaire: Convert Solitaire window to be created from GML
No functionial change here, but this more easily allows for adding GUI elements to the Solitaire window. This nests the SolitaireWidget as a child of the main window's widget so that the SolitaireWidget does not color the entire window green when it paints its background.
This commit is contained in:
parent
e1492e9a62
commit
0ff4eec8a8
5 changed files with 33 additions and 12 deletions
|
@ -1,8 +1,11 @@
|
|||
compile_gml(Solitaire.gml SolitaireGML.h solitaire_gml)
|
||||
|
||||
set(SOURCES
|
||||
Card.cpp
|
||||
CardStack.cpp
|
||||
Game.cpp
|
||||
main.cpp
|
||||
SolitaireGML.h
|
||||
)
|
||||
|
||||
serenity_app(Solitaire ICON app-solitaire)
|
||||
|
|
|
@ -8,17 +8,16 @@
|
|||
#include <LibGUI/Painter.h>
|
||||
#include <time.h>
|
||||
|
||||
REGISTER_WIDGET(Solitaire, Game);
|
||||
|
||||
namespace Solitaire {
|
||||
|
||||
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;
|
||||
|
||||
Game::Game(Function<void(uint32_t)>&& on_score_update)
|
||||
: m_on_score_update(move(on_score_update))
|
||||
Game::Game()
|
||||
{
|
||||
set_fill_with_background_color(false);
|
||||
|
||||
m_stacks[Stock] = CardStack({ 10, 10 }, CardStack::Type::Stock);
|
||||
m_stacks[Waste] = CardStack({ 10 + Card::width + 10, 10 }, CardStack::Type::Waste);
|
||||
m_stacks[Foundation4] = CardStack({ Game::width - Card::width - 10, 10 }, CardStack::Type::Foundation);
|
||||
|
@ -120,7 +119,9 @@ void Game::setup()
|
|||
void Game::update_score(int to_add)
|
||||
{
|
||||
m_score = max(static_cast<int>(m_score) + to_add, 0);
|
||||
m_on_score_update(m_score);
|
||||
|
||||
if (on_score_update)
|
||||
on_score_update(m_score);
|
||||
}
|
||||
|
||||
void Game::keydown_event(GUI::KeyEvent& event)
|
||||
|
|
|
@ -21,8 +21,10 @@ public:
|
|||
virtual ~Game() override;
|
||||
void setup();
|
||||
|
||||
Function<void(uint32_t)> on_score_update;
|
||||
|
||||
private:
|
||||
Game(Function<void(uint32_t)>&& on_score_update);
|
||||
Game();
|
||||
|
||||
class Animation {
|
||||
public:
|
||||
|
@ -119,7 +121,6 @@ private:
|
|||
uint8_t m_new_game_animation_delay { 0 };
|
||||
|
||||
uint32_t m_score { 0 };
|
||||
Function<void(uint32_t)> m_on_score_update;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
10
Userland/Games/Solitaire/Solitaire.gml
Normal file
10
Userland/Games/Solitaire/Solitaire.gml
Normal file
|
@ -0,0 +1,10 @@
|
|||
@GUI::Widget {
|
||||
fill_with_background_color: false
|
||||
|
||||
layout: @GUI::VerticalBoxLayout {
|
||||
}
|
||||
|
||||
@Solitaire::Game {
|
||||
name: "game"
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include "Game.h"
|
||||
#include <Games/Solitaire/SolitaireGML.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
|
@ -39,15 +40,21 @@ int main(int argc, char** argv)
|
|||
window->set_resizable(false);
|
||||
window->resize(Solitaire::Game::width, Solitaire::Game::height);
|
||||
|
||||
auto widget = Solitaire::Game::construct([&](uint32_t score) {
|
||||
auto& widget = window->set_main_widget<GUI::Widget>();
|
||||
widget.load_from_gml(solitaire_gml);
|
||||
|
||||
auto& game = *widget.find_descendant_of_type_named<Solitaire::Game>("game");
|
||||
game.set_focus(true);
|
||||
|
||||
game.on_score_update = [&](uint32_t score) {
|
||||
window->set_title(String::formatted("Score: {} - Solitaire", score));
|
||||
});
|
||||
};
|
||||
|
||||
auto menubar = GUI::Menubar::construct();
|
||||
auto& game_menu = menubar->add_menu("&Game");
|
||||
|
||||
game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
|
||||
widget->setup();
|
||||
game.setup();
|
||||
}));
|
||||
game_menu.add_separator();
|
||||
game_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
|
||||
|
@ -56,10 +63,9 @@ int main(int argc, char** argv)
|
|||
help_menu.add_action(GUI::CommonActions::make_about_action("Solitaire", app_icon, window));
|
||||
|
||||
window->set_menubar(move(menubar));
|
||||
window->set_main_widget(widget);
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
window->show();
|
||||
widget->setup();
|
||||
game.setup();
|
||||
|
||||
return app->exec();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue