Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/boardwidget.py
blob: a2b868dc31fc219f2082ee992a6419c48260be22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import logging
import cairo
import gobject
import gtk
import random

import game


logger = logging.getLogger('PlayGo.boardwidget')


class BoardWidget(gtk.EventBox):
    "Gtk widget for drawing the graphical board."""

    __gsignals__ =  {
            'insert-requested': (gobject.SIGNAL_RUN_FIRST, None, [int]),
        }

    def __init__( self, aBoard ):
        
        gtk.EventBox.__init__( self )

        self.output = gtk.DrawingArea()
        self.set_property('child', self.output)

        self.output.connect('expose-event', self.expose_cb)
        self.add_events(gtk.gdk.POINTER_MOTION_MASK)
        
        self.connect('button-release-event', self.__class__.button_release_cb)
        self.connect('motion-notify-event', self.__class__.motion_cb)

        self.drawCoords = 1
        self.columns = aBoard.size
        self.rows = aBoard.size
        self.lastUnit = 0
        self.lastDat = 0
        self.lastX = 0 
        self.lastY = 0
        self.lastCmap = None
        self.myBoard = aBoard
        self.myGame = None
        
        self.lastColor = 1 

        # get the bitmap for genuine simulated wooden board
#        input = open("./images/board.gif")
#        imagebuf = input.read()
#        pixbufloader = gtk.gdk.PixbufLoader()
#        pixbufloader.write(imagebuf)
#        pixbufloader.close()
#        self.pixBoard = pixbufloader.get_pixbuf()

        # get the bitmap for genuine simulated white stone
        input = open("./images/white.gif")
        imagebuf = input.read()
        pixbufloader = gtk.gdk.PixbufLoader()
        pixbufloader.write(imagebuf)
        pixbufloader.close()
        self.pixWhite = pixbufloader.get_pixbuf()
        
        # get the bitmap for genuine simulated black stone
        input = open("./images/black.gif")
        imagebuf = input.read()
        pixbufloader = gtk.gdk.PixbufLoader()
        pixbufloader.write(imagebuf)
        pixbufloader.close()
        self.pixBlack = pixbufloader.get_pixbuf()
        
        logger.debug( "baord widget starts" )
        
#        for x in range( 19 ):
#            for y in range( 19 ):
#                self.myBoard.setPointi(x, y, random.randint( 0, 3 ) ) 
                

            
    def check_coord (self, i, j):
        return i >= 0 and i < self.rows and j >= 0 and j < self.columns

    def insert(self, dat, value):
        """Return:
            None : no winner
            0, 1: player 0/1 wins the game
        """
        color = dat >> 16
        y = dat & 0xff
        x = ( dat >> 8 ) & 0xff
        
        logger.debug( 'stone event x=%d y=%d col=%d value=%d', x,y, dat, color )
        
        assert x < self.myBoard.size
        assert y < self.myBoard.size
        
        self.myBoard.setPointi( x, y, color )


        return None


    def remove(self, column, value):
        """Return:
            None : no winner
            0, 1: player 0/1 wins the game
        """
        y = column & 0xff
        x = column >> 8
        
        logger.debug( 'remove stone event x=%d y=%d col=%d value=%d', x,y,column, value )
        
        assert x < self.myBoard.size
        assert y < self.myBoard.size
        
        self.myBoard.setPointi( x, y, 0 )
        return None


    def draw_background(self, rect, unit, ctx):

        ct = gtk.gdk.CairoContext(ctx)
        ct.set_source_pixbuf(self.pixBoard,0,0)
        ctx.paint()
        ctx.stroke()
                

    def draw_lines(self, rect, unit, ctx):
        
        # single width balck lines
        ctx.set_line_width(1)
        ctx.set_source_rgba(0, 0, 0, 1)

        for i in xrange(self.rows + 1):
            ctx.move_to( unit, i * unit)
            ctx.line_to(self.columns * unit, i * unit )

        for i in xrange(self.columns + 1):
            ctx.move_to(i * unit, unit )
            ctx.line_to(i * unit, self.rows * unit)

        ctx.stroke()
        
        # star point coords per board size
        if self.columns == 19 :
            seq = [ 4, 10, 16 ]
        elif self.columns == 13 :
            seq = [ 4, 7, 10 ]
        elif self.columns == 9 :
            seq = [ 3, 7 ]
            # set the middle singleton
            ctx.arc( unit * 5, unit * 5, 3, 0, -1e-10)
            ctx.fill_preserve()
            ctx.stroke()
        
        # stroke in the star points
        #TODO: adjust size for teeny boards
        for x in seq :
            for y in seq :
                ctx.arc( unit * x, unit * y, 3, 0, -1e-10)
                ctx.fill_preserve()
                ctx.stroke()     

    def draw_stone(self, x, y, color, unit, ctx):
        
        x = x + 1
        y = y + 1
        ct = gtk.gdk.CairoContext(ctx)
        if  color == 0 : 
            ct.set_source_pixbuf(self.pixBlackSized, unit*x - unit/2, unit*y - unit/2, )
        else :
            ct.set_source_pixbuf(self.pixWhiteSized, unit*x - unit/2, unit*y - unit/2, )
            
        ctx.paint()
        

    def draw_stones( self, ctx ):
            
        for x in xrange(self.rows):
            for y in xrange(self.columns):
                
                point =  self.myBoard.getPoint( x, y )
                
                if ( point == 1 ) :
                    self.draw_stone( x, y, 1, self.lastUnit, ctx )
                elif ( point == 2 ) :
                    self.draw_stone( x, y, 0, self.lastUnit, ctx )
                    
        ctx.stroke()

    def get_mouse_event_col(self, event):
        
        unit, x0, y0 = self.get_coordinates(self.get_allocation())
        col = ( ( event.x - x0 ) / unit ) - 0.5
        row = ( ( event.y - y0 ) / unit ) - 0.5
        return int(row), int(col)

    def motion_cb(self, event):

        y, x = self.get_mouse_event_col(event)
        dat = ( y << 8 ) + x
        
        if dat == self.lastDat :
            return
        
        ctx = self.myWidget.window.cairo_create()
        ctx.save()
        
        unit, x0, y0 = self.get_coordinates(self.get_allocation())
        ctx.translate( x0, y0 )

        
        #ctx.set_source_rgba(0, 0, 0, 0 )
        #ctx.arc( self.lastX, self.lastY, 16, 0, -1e-10)
        #self.myWidget.window.clear_area( self.lastX + ( unit ), self.lastY, unit, unit )
        #if self.lastCmap :
        #    self.myWidget.window.draw_image( ctx, self.lastCmap, 0, 0, self.lastX, self.lastY, unit, unit )
        
        #self.myWidget.window.clear_area( int(self.lastX), int(self.lastY), int(unit), int(unit) )
        
        
        ctx.set_source_rgba(0, 0, 0, .5 )
        self.lastX =unit * x 
        self.lastY =unit * y 
        #ctx.arc( self.lastX, self.lastY, 16, 0, -1e-10)
        #ctx.fill_preserve()
        #ctx.stroke()
        ctx.restore()

    def button_release_cb(self, event):

        x, y = self.get_mouse_event_col(event)
        dat = ( y << 8 ) + x
        
        if self.myGame is None :
            
            if event.button != 3 :
                if self.lastColor is 1:
                    dat = dat | 0x10000
                    self.lastColor = 2;
                else :
                    dat = dat | 0x20000
                    self.lastColor = 1;
                
            self.insert( dat, 1 )
            
        else:
            
            if event.button != 3 :
                if self.myGame.is_initiator : 
                    dat = dat | 0x10000
                else :
                    dat = dat | 0x20000
                
            self.emit('insert-requested', dat )

        logger.debug( 'mouse up button event x=%d   y=%d     row=%d col=%d   value=%x', event.x, event.y, x, y, dat )
        
        
#        self.myBoard.setPointi( col, row, self.lastColor )
#        if  self.lastColor == 1:
#            self.lastColor = 2
#        else :
#            self.lastColor = 1
#            
        self.window.invalidate_rect(self.get_allocation(), True)

    def queue_draw(self):
        self.output.queue_draw()

    def get_coordinates(self, rect):
        """Returns tuple (unit size, origin x, origin y) suitable for drawing
        a grid within @rect."""

        rect = self.get_allocation()
        
        if rect.height / float(self.rows) < rect.width / float(self.columns):
            # wide
            unit = rect.height / float(self.rows)
            x0 = rect.x + (rect.width - self.columns * unit) / 2.0
            y0 = rect.y
        else:
            # narrow
            unit = rect.width / float(self.columns)
            x0 = rect.x
            y0 = rect.y + (rect.height - self.rows * unit) / 2.0
         
        # now shrink the size for a 1 unit border   
        unit = unit - unit / self.rows

        #return unit, x0, y0
        return unit, 0, 0
    
    def draw(self, rect, ctx, win):
        """Draw a board using the cairo context @ctx within the rectangle
        @rect."""

        #ctx.save()
        ctx.set_line_cap(cairo.LINE_CAP_ROUND)
        unit, x0, y0 = self.get_coordinates(rect)
        
        # I could not find the resize event so...
        if self.lastUnit != unit :
            
            pixbuf = gtk.gdk.pixbuf_new_from_file_at_size("./images/board.gif", rect.width, rect.height )
            pixmap, mask = pixbuf.render_pixmap_and_mask()
            del pixbuf
            
            pctx = pixmap.cairo_create()
            self.draw_lines( rect, unit, pctx )
            win.set_back_pixmap(pixmap, False )
            del pixmap
            del pctx
            
            # now resize the stones
            self.pixBlackSized = self.pixBlack.scale_simple( int(unit), int(unit), gtk.gdk.INTERP_BILINEAR )
            self.pixWhiteSized = self.pixWhite.scale_simple( int(unit), int(unit), gtk.gdk.INTERP_BILINEAR )
            self.lastUnit = unit

            # redraw it all
            win.invalidate_rect( rect, False )
#            bx, by = self.pixBoard.get_size()
#            if rect.height > by :
#                self.pixBoard = self.pixBoard.scaleSimple( bx, by, gtk.gdk.INTERP_BILINEAR ) 
        
        ctx.translate( x0, y0 )
        #self.draw_background( rect, unit, ctx )
        #self.draw_lines( rect, unit, ctx )
        self.draw_stones( ctx )       
        
        self.myWidget.window.set_cursor( gtk.gdk.Cursor(gtk.gdk.CIRCLE) ) 
        #ctx.restore()


    def expose_cb(self, widget, event):

        rect = self.get_allocation()
        if rect.height != rect.width :
            if rect.height > rect.width :
                widget.window.resize( rect.width, rect.width )
            else :
                widget.window.resize( rect.height, rect.height )
#                
        self.myWidget = widget
        ctx = widget.window.cairo_create()
        ctx.save()
        rect = self.get_allocation()
        self.draw(rect, ctx, widget.window )
        ctx.restore()