2022-08-20 14:14:48 +01:00
|
|
|
/*
|
2023-02-01 13:50:10 +00:00
|
|
|
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
|
2022-08-20 14:14:48 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CardSettingsWidget.h"
|
|
|
|
#include <Applications/GamesSettings/CardSettingsWidgetGML.h>
|
2023-01-12 15:07:47 -05:00
|
|
|
#include <LibCards/Card.h>
|
|
|
|
#include <LibCards/CardGame.h>
|
2022-08-21 16:07:46 +01:00
|
|
|
#include <LibCards/CardPainter.h>
|
2023-01-12 15:07:47 -05:00
|
|
|
#include <LibCards/CardStack.h>
|
2022-08-20 14:14:48 +01:00
|
|
|
#include <LibConfig/Client.h>
|
2022-08-21 16:02:56 +01:00
|
|
|
#include <LibGUI/FileSystemModel.h>
|
2022-08-21 16:07:46 +01:00
|
|
|
#include <LibGfx/Palette.h>
|
2022-08-21 16:02:56 +01:00
|
|
|
|
2023-01-12 14:45:11 -05:00
|
|
|
namespace GamesSettings {
|
|
|
|
|
2023-05-05 01:04:46 +01:00
|
|
|
static constexpr StringView default_card_back_image_path = "/res/graphics/cards/backs/buggie-deck.png"sv;
|
2022-08-20 14:14:48 +01:00
|
|
|
|
2023-02-01 13:29:07 +00:00
|
|
|
class CardGamePreview final : public Cards::CardGame {
|
|
|
|
C_OBJECT_ABSTRACT(CardGamePreview)
|
2023-01-12 15:07:47 -05:00
|
|
|
|
|
|
|
public:
|
2023-02-01 13:29:07 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<CardGamePreview>> try_create()
|
2023-01-12 15:07:47 -05:00
|
|
|
{
|
2023-02-01 13:29:07 +00:00
|
|
|
auto preview = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) CardGamePreview()));
|
2023-01-12 15:07:47 -05:00
|
|
|
|
2023-01-12 15:15:45 -05:00
|
|
|
Gfx::IntPoint point { 25, 30 };
|
2023-01-12 15:07:47 -05:00
|
|
|
TRY(preview->add_stack(point, Cards::CardStack::Type::Stock));
|
|
|
|
|
|
|
|
point.translate_by(Cards::Card::width + 30, 0);
|
|
|
|
TRY(preview->add_stack(point, Cards::CardStack::Type::Normal));
|
|
|
|
|
|
|
|
point.translate_by(Cards::Card::width + 30, 0);
|
|
|
|
TRY(preview->add_stack(point, Cards::CardStack::Type::Normal));
|
|
|
|
|
2023-01-12 15:15:45 -05:00
|
|
|
point.translate_by(20, 10);
|
|
|
|
TRY(preview->add_stack(point, Cards::CardStack::Type::Normal));
|
|
|
|
|
2023-01-12 15:07:47 -05:00
|
|
|
for (size_t i = 0; i < Cards::Card::card_count; ++i)
|
2023-01-20 12:45:02 +00:00
|
|
|
TRY(preview->stack_at_location(0).push(TRY(Cards::Card::try_create(Cards::Suit::Diamonds, static_cast<Cards::Rank>(i)))));
|
|
|
|
TRY(preview->stack_at_location(1).push(TRY(Cards::Card::try_create(Cards::Suit::Spades, Cards::Rank::Ace))));
|
|
|
|
TRY(preview->stack_at_location(2).push(TRY(Cards::Card::try_create(Cards::Suit::Hearts, Cards::Rank::Queen))));
|
|
|
|
TRY(preview->stack_at_location(3).push(TRY(Cards::Card::try_create(Cards::Suit::Clubs, Cards::Rank::Jack))));
|
2023-01-12 15:07:47 -05:00
|
|
|
|
|
|
|
preview->stack_at_location(0).peek().set_upside_down(true);
|
2023-01-12 15:15:45 -05:00
|
|
|
preview->stack_at_location(2).set_highlighted(true);
|
|
|
|
|
2023-01-12 15:07:47 -05:00
|
|
|
return preview;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-02-01 13:29:07 +00:00
|
|
|
CardGamePreview() = default;
|
2023-01-12 15:07:47 -05:00
|
|
|
|
|
|
|
virtual void paint_event(GUI::PaintEvent& event) override
|
|
|
|
{
|
|
|
|
Cards::CardGame::paint_event(event);
|
|
|
|
|
|
|
|
GUI::Painter painter(*this);
|
|
|
|
painter.add_clip_rect(frame_inner_rect());
|
|
|
|
painter.add_clip_rect(event.rect());
|
|
|
|
|
|
|
|
auto background_color = this->background_color();
|
|
|
|
for (auto& stack : stacks())
|
2023-03-06 14:17:01 +01:00
|
|
|
stack->paint(painter, background_color);
|
2023-01-12 15:07:47 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-01 13:50:10 +00:00
|
|
|
ErrorOr<NonnullRefPtr<CardSettingsWidget>> CardSettingsWidget::try_create()
|
2022-08-20 14:14:48 +01:00
|
|
|
{
|
2023-02-01 13:50:10 +00:00
|
|
|
auto card_settings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) CardSettingsWidget));
|
|
|
|
TRY(card_settings_widget->initialize());
|
|
|
|
return card_settings_widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> CardSettingsWidget::initialize()
|
|
|
|
{
|
|
|
|
TRY(load_from_gml(card_settings_widget_gml));
|
2022-08-20 14:14:48 +01:00
|
|
|
|
|
|
|
auto background_color = Gfx::Color::from_string(Config::read_string("Games"sv, "Cards"sv, "BackgroundColor"sv)).value_or(Gfx::Color::from_rgb(0x008000));
|
2022-08-21 16:07:46 +01:00
|
|
|
|
2023-02-01 13:29:07 +00:00
|
|
|
m_preview_frame = find_descendant_of_type_named<CardGamePreview>("cards_preview");
|
2023-01-12 15:07:47 -05:00
|
|
|
m_preview_frame->set_background_color(background_color);
|
2022-08-21 16:07:46 +01:00
|
|
|
|
|
|
|
m_background_color_input = find_descendant_of_type_named<GUI::ColorInput>("cards_background_color");
|
2022-08-20 14:14:48 +01:00
|
|
|
m_background_color_input->set_color(background_color, GUI::AllowCallback::No);
|
2022-08-21 16:07:46 +01:00
|
|
|
m_background_color_input->on_change = [&]() {
|
|
|
|
set_modified(true);
|
2023-01-12 15:07:47 -05:00
|
|
|
m_preview_frame->set_background_color(m_background_color_input->color());
|
2022-08-21 16:07:46 +01:00
|
|
|
};
|
2022-08-21 16:02:56 +01:00
|
|
|
|
|
|
|
m_card_back_image_view = find_descendant_of_type_named<GUI::IconView>("cards_back_image");
|
2023-05-05 01:04:46 +01:00
|
|
|
m_card_back_image_view->set_model(GUI::FileSystemModel::create("/res/graphics/cards/backs"));
|
2022-08-21 16:02:56 +01:00
|
|
|
m_card_back_image_view->set_model_column(GUI::FileSystemModel::Column::Name);
|
|
|
|
if (!set_card_back_image_path(Config::read_string("Games"sv, "Cards"sv, "CardBackImage"sv)))
|
|
|
|
set_card_back_image_path(default_card_back_image_path);
|
|
|
|
m_card_back_image_view->on_selection_change = [&]() {
|
2022-09-18 17:32:59 -04:00
|
|
|
auto& card_back_selection = m_card_back_image_view->selection();
|
|
|
|
if (card_back_selection.is_empty())
|
2022-08-21 16:02:56 +01:00
|
|
|
return;
|
2022-09-18 17:32:59 -04:00
|
|
|
m_last_selected_card_back = card_back_selection.first();
|
2022-08-21 16:02:56 +01:00
|
|
|
set_modified(true);
|
2022-08-21 16:07:46 +01:00
|
|
|
Cards::CardPainter::the().set_background_image_path(card_back_image_path());
|
2023-01-12 15:07:47 -05:00
|
|
|
m_preview_frame->update();
|
2022-08-21 16:02:56 +01:00
|
|
|
};
|
2022-09-18 17:32:59 -04:00
|
|
|
|
|
|
|
m_last_selected_card_back = m_card_back_image_view->selection().first();
|
2023-02-01 13:50:10 +00:00
|
|
|
|
|
|
|
return {};
|
2022-08-20 14:14:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CardSettingsWidget::apply_settings()
|
|
|
|
{
|
|
|
|
Config::write_string("Games"sv, "Cards"sv, "BackgroundColor"sv, m_background_color_input->text());
|
2022-08-21 16:02:56 +01:00
|
|
|
Config::write_string("Games"sv, "Cards"sv, "CardBackImage"sv, card_back_image_path());
|
2022-08-20 14:14:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CardSettingsWidget::reset_default_values()
|
|
|
|
{
|
|
|
|
m_background_color_input->set_color(Gfx::Color::from_rgb(0x008000));
|
2022-08-21 16:02:56 +01:00
|
|
|
set_card_back_image_path(default_card_back_image_path);
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
bool CardSettingsWidget::set_card_back_image_path(DeprecatedString const& path)
|
2022-08-21 16:02:56 +01:00
|
|
|
{
|
|
|
|
auto index = static_cast<GUI::FileSystemModel*>(m_card_back_image_view->model())->index(path, m_card_back_image_view->model_column());
|
|
|
|
if (index.is_valid()) {
|
|
|
|
m_card_back_image_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
|
2022-08-21 16:07:46 +01:00
|
|
|
Cards::CardPainter::the().set_background_image_path(path);
|
2023-01-12 15:07:47 -05:00
|
|
|
m_preview_frame->update();
|
2022-08-21 16:02:56 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString CardSettingsWidget::card_back_image_path() const
|
2022-08-21 16:02:56 +01:00
|
|
|
{
|
2022-09-18 17:32:59 -04:00
|
|
|
auto& card_back_selection = m_card_back_image_view->selection();
|
|
|
|
GUI::ModelIndex card_back_image_index = m_last_selected_card_back;
|
|
|
|
if (!card_back_selection.is_empty())
|
|
|
|
card_back_image_index = card_back_selection.first();
|
2022-08-21 16:02:56 +01:00
|
|
|
return static_cast<GUI::FileSystemModel const*>(m_card_back_image_view->model())->full_path(card_back_image_index);
|
2022-08-20 14:14:48 +01:00
|
|
|
}
|
2023-01-12 14:45:11 -05:00
|
|
|
|
|
|
|
}
|
2023-01-12 15:07:47 -05:00
|
|
|
|
2023-02-01 13:29:07 +00:00
|
|
|
REGISTER_WIDGET(GamesSettings, CardGamePreview);
|