1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:17:35 +00:00

LibChess: Change cursor style when hovering or dragging valid piece

This commit is contained in:
BrandonKi 2021-05-31 14:19:56 -04:00 committed by Andreas Kling
parent cdb070cdfb
commit ee38f5241d

View file

@ -169,6 +169,7 @@ void ChessWidget::mousedown_event(GUI::MouseEvent& event)
if (event.button() == GUI::MouseButton::Right) { if (event.button() == GUI::MouseButton::Right) {
if (m_dragging_piece) { if (m_dragging_piece) {
m_dragging_piece = false; m_dragging_piece = false;
set_override_cursor(Gfx::StandardCursor::None);
m_available_moves.clear(); m_available_moves.clear();
} else { } else {
m_current_marking.from = mouse_to_square(event); m_current_marking.from = mouse_to_square(event);
@ -181,6 +182,7 @@ void ChessWidget::mousedown_event(GUI::MouseEvent& event)
auto piece = board().get_piece(square); auto piece = board().get_piece(square);
if (drag_enabled() && piece.color == board().turn() && !m_playback) { if (drag_enabled() && piece.color == board().turn() && !m_playback) {
m_dragging_piece = true; m_dragging_piece = true;
set_override_cursor(Gfx::StandardCursor::Drag);
m_drag_point = event.position(); m_drag_point = event.position();
m_moving_square = square; m_moving_square = square;
@ -219,6 +221,7 @@ void ChessWidget::mouseup_event(GUI::MouseEvent& event)
return; return;
m_dragging_piece = false; m_dragging_piece = false;
set_override_cursor(Gfx::StandardCursor::Hand);
m_available_moves.clear(); m_available_moves.clear();
auto target_square = mouse_to_square(event); auto target_square = mouse_to_square(event);
@ -282,6 +285,7 @@ void ChessWidget::mouseup_event(GUI::MouseEvent& event)
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
if (over) { if (over) {
set_override_cursor(Gfx::StandardCursor::None);
set_drag_enabled(false); set_drag_enabled(false);
update(); update();
GUI::MessageBox::show(window(), msg, "Game Over", GUI::MessageBox::Type::Information); GUI::MessageBox::show(window(), msg, "Game Over", GUI::MessageBox::Type::Information);
@ -299,15 +303,28 @@ void ChessWidget::mousemove_event(GUI::MouseEvent& event)
if (!frame_inner_rect().contains(event.position())) if (!frame_inner_rect().contains(event.position()))
return; return;
if (!m_dragging_piece) if (m_engine && board().turn() != side())
return; return;
if (!m_dragging_piece) {
auto square = mouse_to_square(event);
if (!square.in_bounds())
return;
auto piece = board().get_piece(square);
if (piece.color == board().turn())
set_override_cursor(Gfx::StandardCursor::Hand);
else
set_override_cursor(Gfx::StandardCursor::None);
return;
}
m_drag_point = event.position(); m_drag_point = event.position();
update(); update();
} }
void ChessWidget::keydown_event(GUI::KeyEvent& event) void ChessWidget::keydown_event(GUI::KeyEvent& event)
{ {
set_override_cursor(Gfx::StandardCursor::None);
switch (event.key()) { switch (event.key()) {
case KeyCode::Key_Left: case KeyCode::Key_Left:
playback_move(PlaybackDirection::Backward); playback_move(PlaybackDirection::Backward);