Chess: Make the main widget a GUI::Frame for a nicer look :^)

This commit is contained in:
Andreas Kling 2021-05-17 12:02:38 +02:00
parent 2083d1a3d6
commit 67ed580532
Notes: sideshowbarker 2024-07-18 17:58:07 +09:00
3 changed files with 23 additions and 21 deletions

View file

@ -17,14 +17,9 @@
#include <LibGfx/Path.h>
#include <unistd.h>
ChessWidget::ChessWidget(const StringView& set)
{
set_piece_set(set);
}
ChessWidget::ChessWidget()
: ChessWidget("stelar7")
{
set_piece_set("stelar7");
}
ChessWidget::~ChessWidget()
@ -33,13 +28,15 @@ ChessWidget::~ChessWidget()
void ChessWidget::paint_event(GUI::PaintEvent& event)
{
GUI::Widget::paint_event(event);
GUI::Frame::paint_event(event);
GUI::Painter painter(*this);
painter.add_clip_rect(event.rect());
size_t tile_width = width() / 8;
size_t tile_height = height() / 8;
painter.translate(frame_thickness(), frame_thickness());
size_t tile_width = frame_inner_rect().width() / 8;
size_t tile_height = frame_inner_rect().height() / 8;
unsigned coord_rank_file = (side() == Chess::Color::White) ? 0 : 7;
Chess::Board& active_board = (m_playback ? board_playback() : board());
@ -164,7 +161,8 @@ void ChessWidget::paint_event(GUI::PaintEvent& event)
void ChessWidget::mousedown_event(GUI::MouseEvent& event)
{
GUI::Widget::mousedown_event(event);
if (!frame_inner_rect().contains(event.position()))
return;
if (event.button() == GUI::MouseButton::Right) {
if (m_dragging_piece) {
@ -197,7 +195,8 @@ void ChessWidget::mousedown_event(GUI::MouseEvent& event)
void ChessWidget::mouseup_event(GUI::MouseEvent& event)
{
GUI::Widget::mouseup_event(event);
if (!frame_inner_rect().contains(event.position()))
return;
if (event.button() == GUI::MouseButton::Right) {
m_current_marking.secondary_color = event.shift();
@ -295,7 +294,9 @@ void ChessWidget::mouseup_event(GUI::MouseEvent& event)
void ChessWidget::mousemove_event(GUI::MouseEvent& event)
{
GUI::Widget::mousemove_event(event);
if (!frame_inner_rect().contains(event.position()))
return;
if (!m_dragging_piece)
return;
@ -361,8 +362,8 @@ void ChessWidget::set_piece_set(const StringView& set)
Chess::Square ChessWidget::mouse_to_square(GUI::MouseEvent& event) const
{
unsigned tile_width = width() / 8;
unsigned tile_height = height() / 8;
unsigned tile_width = frame_inner_rect().width() / 8;
unsigned tile_height = frame_inner_rect().height() / 8;
if (side() == Chess::Color::White) {
return { (unsigned)(7 - (event.y() / tile_height)), (unsigned)(event.x() / tile_width) };

View file

@ -10,16 +10,14 @@
#include <AK/HashMap.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/StringView.h>
#include <LibChess/Chess.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Frame.h>
#include <LibGfx/Bitmap.h>
class ChessWidget final : public GUI::Widget {
C_OBJECT(ChessWidget)
class ChessWidget final : public GUI::Frame {
C_OBJECT(ChessWidget);
public:
ChessWidget();
ChessWidget(const StringView& set);
virtual ~ChessWidget() override;
virtual void paint_event(GUI::PaintEvent&) override;
@ -107,6 +105,8 @@ public:
};
private:
ChessWidget();
Chess::Board m_board;
Chess::Board m_board_playback;
bool m_playback { false };

View file

@ -65,9 +65,10 @@ int main(int argc, char** argv)
auto size = config->read_num_entry("Display", "size", 512);
window->set_title("Chess");
window->resize(size, size);
window->set_base_size({ 4, 4 });
window->set_size_increment({ 8, 8 });
window->set_resize_aspect_ratio(1, 1);
window->resize(size - 4, size - 4);
window->set_icon(app_icon.bitmap_for_size(16));