1
# -*- coding: utf-8 -*-
5
__author__ = 'Robert Ancell <bob27@users.sourceforge.net>'
6
__license__ = 'GNU General Public License Version 2'
7
__copyright__ = 'Copyright 2005-2006 Robert Ancell'
11
class SceneHumanInput:
16
"""Constructor for a scene with human selectable components"""
17
self.__inputEnabled = True # Flag to control if human input is enabled
18
self.__startSquare = None # The selected square to move from
19
self.__showHints = True
24
"""This method is called when the scene needs redrawing"""
27
def playerIsHuman(self):
28
"""Check if the current player is a human.
30
Return True the current player is human else False.
34
def getSquare(self, x, y):
35
"""Find the chess square at a given 2D location.
37
'x' is the number of pixels from the left of the scene to select.
38
'y' is the number of pixels from the bottom of the scene to select.
40
Return the co-ordinate as a tuple in the form (file,rank) or None if
41
no square at this point.
45
def squareIsFriendly(self, coord):
46
"""Check if a given square contains a friendly piece.
48
Return True if this square contains a friendly piece.
52
def canMove(self, start, end):
53
"""Check if a move is valid.
55
'start' is the location to move from in LAN format (string).
56
'end' is the location to move from in LAN format (string).
60
def moveHuman(self, start, end):
61
"""Called when a human player moves.
63
'start' is the location to move from in LAN format (string).
64
'end' is the location to move from in LAN format (string).
70
def enableHumanInput(self, inputEnabled):
71
"""Enable/disable human input.
73
'inputEnabled' is a flag to show if human input is enabled (True) or disabled (False).
75
if inputEnabled is False:
76
self.__selectSquare(None)
77
self.__inputEnabled = inputEnabled
79
def select(self, x, y):
82
if self.__inputEnabled is False:
85
# Only bother if the current player is human
86
if self.playerIsHuman() is False:
89
# Get the selected square
90
coord = self.getSquare(x, y)
94
# Deselect when clicking on piece a second time
95
if self.__startSquare == coord:
96
self.__selectSquare(None)
99
# If this is a friendly piece then select it
100
if self.squareIsFriendly(coord):
101
self.__selectSquare(coord)
104
# If we have already selected a start move try
105
# and move to this square
106
if self.__startSquare is not None:
107
self.__move(self.__startSquare, coord)
114
def deselect(self, x, y):
117
if self.__inputEnabled is False:
120
# Only bother if the current player is human
121
if self.playerIsHuman() is False:
124
# Get the selected square
125
coord = self.getSquare(x, y)
129
# Attempt to move here
130
if self.__startSquare is not None and self.__startSquare != coord:
131
self.__move(self.__startSquare, coord)
140
def __selectSquare(self, coord):
141
if self.__startSquare == coord:
143
self.__startSquare = coord
144
self.selectSquare(coord)
146
def __move(self, start, end):
147
"""Attempt to make a move.
151
if self.canMove(start, end) is False:
153
self.__selectSquare(None)
154
self.moveHuman(start, end)