~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

« back to all changes in this revision

Viewing changes to glchess/src/lib/scene/human.py

  • Committer: Package Import Robot
  • Author(s): Rodrigo Moya
  • Date: 2011-05-30 13:32:04 UTC
  • mfrom: (1.3.4)
  • mto: (163.1.3 precise)
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: package-import@ubuntu.com-20110530133204-celaq1v1dsxc48q1
Tags: upstream-3.0.2
ImportĀ upstreamĀ versionĀ 3.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
"""
3
 
"""
4
 
 
5
 
__author__ = 'Robert Ancell <bob27@users.sourceforge.net>'
6
 
__license__ = 'GNU General Public License Version 2'
7
 
__copyright__ = 'Copyright 2005-2006  Robert Ancell'
8
 
 
9
 
import glchess.scene
10
 
 
11
 
class SceneHumanInput:
12
 
    """
13
 
    """
14
 
    
15
 
    def __init__(self):
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
20
 
    
21
 
    # Methods to extend
22
 
    
23
 
    def onRedraw(self):
24
 
        """This method is called when the scene needs redrawing"""
25
 
        pass
26
 
        
27
 
    def playerIsHuman(self):
28
 
        """Check if the current player is a human.
29
 
        
30
 
        Return True the current player is human else False.
31
 
        """
32
 
        return False
33
 
    
34
 
    def getSquare(self, x, y):
35
 
        """Find the chess square at a given 2D location.
36
 
        
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.
39
 
        
40
 
        Return the co-ordinate as a tuple in the form (file,rank) or None if
41
 
        no square at this point.
42
 
        """
43
 
        pass
44
 
 
45
 
    def squareIsFriendly(self, coord):
46
 
        """Check if a given square contains a friendly piece.
47
 
        
48
 
        Return True if this square contains a friendly piece.
49
 
        """
50
 
        return False
51
 
    
52
 
    def canMove(self, start, end):
53
 
        """Check if a move is valid.
54
 
        
55
 
        'start' is the location to move from in LAN format (string).
56
 
        'end' is the location to move from in LAN format (string).
57
 
        """
58
 
        return False
59
 
    
60
 
    def moveHuman(self, start, end):
61
 
        """Called when a human player moves.
62
 
        
63
 
        'start' is the location to move from in LAN format (string).
64
 
        'end' is the location to move from in LAN format (string).
65
 
        """
66
 
        pass
67
 
    
68
 
    # Public methods
69
 
    
70
 
    def enableHumanInput(self, inputEnabled):
71
 
        """Enable/disable human input.
72
 
        
73
 
        'inputEnabled' is a flag to show if human input is enabled (True) or disabled (False).
74
 
        """
75
 
        if inputEnabled is False:
76
 
            self.__selectSquare(None)
77
 
        self.__inputEnabled = inputEnabled
78
 
        
79
 
    def select(self, x, y):
80
 
        """
81
 
        """
82
 
        if self.__inputEnabled is False:
83
 
            return
84
 
        
85
 
        # Only bother if the current player is human
86
 
        if self.playerIsHuman() is False:
87
 
            return
88
 
        
89
 
        # Get the selected square
90
 
        coord = self.getSquare(x, y)
91
 
        if coord is None:
92
 
            return
93
 
        
94
 
        # Deselect when clicking on piece a second time
95
 
        if self.__startSquare == coord:
96
 
            self.__selectSquare(None)
97
 
            return
98
 
 
99
 
        # If this is a friendly piece then select it
100
 
        if self.squareIsFriendly(coord):
101
 
            self.__selectSquare(coord)
102
 
 
103
 
        else:
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)
108
 
 
109
 
        # Redraw the scene
110
 
        self.onRedraw()
111
 
        
112
 
        return coord
113
 
 
114
 
    def deselect(self, x, y):
115
 
        """
116
 
        """
117
 
        if self.__inputEnabled is False:
118
 
            return
119
 
        
120
 
        # Only bother if the current player is human
121
 
        if self.playerIsHuman() is False:
122
 
            return
123
 
        
124
 
        # Get the selected square
125
 
        coord = self.getSquare(x, y)
126
 
        if coord is None:
127
 
            return
128
 
        
129
 
        # Attempt to move here
130
 
        if self.__startSquare is not None and self.__startSquare != coord:
131
 
            self.__move(self.__startSquare, coord)
132
 
        
133
 
        # Redraw the scene
134
 
        self.onRedraw()
135
 
        
136
 
        return coord
137
 
    
138
 
    # Private methods
139
 
    
140
 
    def __selectSquare(self, coord):
141
 
        if self.__startSquare == coord:
142
 
            return
143
 
        self.__startSquare = coord
144
 
        self.selectSquare(coord)
145
 
    
146
 
    def __move(self, start, end):
147
 
        """Attempt to make a move.
148
 
        
149
 
        ...
150
 
        """
151
 
        if self.canMove(start, end) is False:
152
 
            return
153
 
        self.__selectSquare(None)
154
 
        self.moveHuman(start, end)