Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Jam/Desktop.py
blob: d09403950bf3fc57daf3874c113aed240ff41560 (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369

import pygtk
pygtk.require( '2.0' )
import gtk

import Config

import Jam.Block as Block

class Desktop( gtk.EventBox ):

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

        self.owner = owner

        self.drawingArea = gtk.DrawingArea()
        self.add( self.drawingArea )

        win = gtk.gdk.get_default_root_window()
        self.gc = gtk.gdk.GC( win )
        colormap = self.drawingArea.get_colormap()
        self.colors = { "bg":                 colormap.alloc_color( Config.BG_COLOR, True, True ), \
                        "Border_Active":      colormap.alloc_color( "#FF6000", True, True ), \
                        "Border_Inactive":    colormap.alloc_color( "#5D5D5D", True, True ), \
                        "Border_Highlight":   colormap.alloc_color( "#FFFFFF", True, True ), \
                        "Bg_Active":          colormap.alloc_color( "#9400BE", True, True ), \
                        "Bg_Inactive":        colormap.alloc_color( "#DBDBDB", True, True ), \
                        "tempWhite":     colormap.alloc_color( "#FFFFFF", True, True ), \
                        "tempBlock1":    colormap.alloc_color( "#227733", True, True ), \
                        "tempBlock2":    colormap.alloc_color( "#837399", True, True ), \
                        "tempBlock3":    colormap.alloc_color( "#111177", True, True ), \
                        "tempBlock4":    colormap.alloc_color( "#99AA22", True, True ), \
                        "tempBlock5":    colormap.alloc_color( "#449977", True, True ) }

        if True: # load clipmask
            pix = gtk.gdk.pixbuf_new_from_file(Config.IMAGE_ROOT+'jam-blockMask.png')
            pixels = pix.get_pixels()
            stride = pix.get_rowstride()
            channels = pix.get_n_channels()
            bitmap = ""
            byte = 0
            shift = 0
            for j in range(pix.get_height()):
                offset = stride*j
                for i in range(pix.get_width()):
                    r = pixels[i*channels+offset]
                    if r != "\0": byte += 1 << shift
                    shift += 1
                    if shift > 7:
                        bitmap += "%c" % byte
                        byte = 0
                        shift = 0
                if shift > 0:
                    bitmap += "%c" % byte
                    byte = 0
                    shift = 0
            self.clipMask = gtk.gdk.bitmap_create_from_data( None, bitmap, pix.get_width(), pix.get_height() )

        self.dirtyRectToAdd = gtk.gdk.Rectangle() # used by the invalidate_rect function
        self.screenBuf = None
        self.screenBufDirty = False
        self.screenBufDirtyRect = gtk.gdk.Rectangle()

        self.blocks = [] # items on the desktop
        self.activeInstrument = None
        self.activeDrum = None

        self.add_events(gtk.gdk.POINTER_MOTION_MASK|gtk.gdk.POINTER_MOTION_HINT_MASK)
        
        self.connect( "size-allocate", self.size_allocate )
        self.connect( "button-press-event", self.button_press )
        self.connect( "button-release-event", self.button_release )
        self.connect( "motion-notify-event", self.motion_notify )
        self.drawingArea.connect( "expose-event", self.expose )

        self.clickedBlock = None
        self.possibleParent = None
        self.possibleSubstitute = None
        self.dragging = False
        self.possibleDelete = False

 
    def size_allocate( self, widget, allocation ):
        if self.screenBuf == None or self.alloc.width != allocation.width or self.alloc.height != allocation.height:
            win = gtk.gdk.get_default_root_window()
            self.screenBuf = gtk.gdk.Pixmap( win, allocation.width, allocation.height )
            self.invalidate_rect( 0, 0, allocation.width, allocation.height )
        self.alloc = allocation
        self.absoluteLoc = [0,0]
        parent = self.get_parent()
        while parent:
            alloc = parent.get_allocation()
            self.absoluteLoc[0] += alloc.x
            self.absoluteLoc[1] += alloc.y
            parent = parent.get_parent()
        return False

    #==========================================================
    # Blocks

    def addBlock( self, blockClass, blockData, loc = (-1,-1), drag = False ):
        
        block = blockClass( self, self.gc, blockData )

        if loc[0] != -1: x = loc[0]
        else:            x = self.alloc.width//2 
        if loc[1] != -1: y = loc[1]
        elif drag:       y = self.alloc.height - block.height//2
        else:            y = self.alloc.height//2

        if drag:
            win = gtk.gdk.get_default_root_window()
            display = win.get_display()
            screen = display.get_default_screen()
            display.warp_pointer( screen, self.absoluteLoc[0] + x, self.absoluteLoc[1] + y )
            self._beginDrag( block )
            block.setLoc( x - block.width//2, y - block.height//2 )
        else:
            self.blocks.append( block )
            block.setLoc( x - block.width//2, y - block.height//2 )

        if blockClass == Block.Instrument:
            self.activateInstrument( block )
        elif blockClass == Block.Drum:
            pass
        elif blockClass == Block.Loop:
            pass

    def deleteBlock( self, block ):
        if block.type == Block.Instrument:
            if block == self.activeInstrument:
                self.activeInstrument = None
                for i in range(len(self.blocks)-1, -1, -1):
                    if self.blocks[i].type == Block.Instrument:
                        self.activateInstrument( self.blocks[i] )
                        break

        elif block.type == Block.Drum:
            if block == self.activeDrum:
                self.deactivateDrum()

        elif block.type == Block.Loop:
            pass

        if block in self.blocks:
            block.invalidate_rect( True )
            self.blocks.remove( block )

        block.destroy()
 

    #==========================================================
    # State

    def activateInstrument( self, block ):
        if self.activeInstrument:
            self.activeInstrument.setActive( False )

        block.setActive( True )
        self.activeInstrument = block
        data = block.data
        self.owner._updateInstrument( data["id"], data["volume"] )

    def activateDrum( self, block ):
        if self.activeDrum:
            self.activeDrum.setActive( False )
            self.owner._stopDrum()

        block.setActive( True )
        self.activeDrum = block

        self.updateDrum()

    def deactivateDrum( self ):
        if not self.activeDrum:
            return

        self.activeDrum.setActive( False )
        self.activeDrum = None
        self.owner._stopDrum()

    def updateDrum( self ):
        data = self.activeDrum.data
        self.owner._playDrum( data["id"], data["volume"], data["beats"], data["regularity"], data["seed"] ) 

    #==========================================================
    # Mouse

    def button_press( self, widget, event ):
        
        hit = False
        for i in range(len(self.blocks)-1, -1, -1):
            hit = self.blocks[i].button_press( event )
            if hit:
                self.clickedBlock = hit
                break

    def button_release( self, widget, event ):

        if self.possibleDelete:
            self.possibleDelete = False
            self.deleteBlock( self.clickedBlock )
            self.clickedBlock = None
            self.possibleParent = None
            self.possibleSubstitute = None
            self.dragging = False

        if self.dragging:
            self.dragging = False
            
            if self.possibleParent:
                self.possibleParent.addChild( self.clickedBlock )
                root = self.possibleParent.getRoot()
                self.blocks.remove(root)
                self.blocks.append(root)
                root.invalidateBranch( True )
                self.possibleParent = None
            elif self.possibleSubstitute:
                self.possibleSubstitute.substitute( self.clickedBlock )
                if self.clickedBlock.isPlaced():
                    self.clickedBlock.resetLoc()
                    self.blocks.append( self.clickedBlock )
                else:
                    self.deleteBlock( self.clickedBlock )
                    self.clickedBlock = None
                    self.activateInstrument( self.possibleSubstitute )
                self.possibleSubstitute = None
            else:
                self.blocks.append( self.clickedBlock )

        if self.clickedBlock:
            self.clickedBlock.button_release( event )
            self.clickedBlock = None
            

    def motion_notify( self, widget, event ):
        
        if not self.clickedBlock:
            return

        if event.is_hint or widget != self:
            x, y, state = self.window.get_pointer()
            event.x = float(x)
            event.y = float(y)
            event.state = state
        
        self.dragging = True

        if self.clickedBlock.motion_notify( event ): # first drag of root block, remove from self.blocks
            self.blocks.remove( self.clickedBlock )

        if event.y < 0 or event.y > self.alloc.height:
            self.possibleDelete = True
            return 
        else:
            self.possibleDelete = False

        blockCount = len(self.blocks)
        if self.clickedBlock.canChild and blockCount:
            for i in range(blockCount-1, -1, -1):
                handled = self.blocks[i].testChild( self.clickedBlock.getParentAnchor() )
                if handled:
                    if self.possibleParent != handled:
                        if self.possibleParent:
                            self.possibleParent.invalidate_rect( False )
                        self.possibleParent = handled
                        self.possibleParent.invalidate_rect( False )
                    break
            if not handled and self.possibleParent:
                self.possibleParent.invalidate_rect( False )
                self.possibleParent = None

        if self.clickedBlock.canSubstitute and blockCount:
            for i in range(blockCount-1, -1, -1):
                handled = self.blocks[i].testSubstitute( self.clickedBlock )
                if handled:
                    if self.possibleSubstitute != handled:
                        if self.possibleSubstitute:
                            self.possibleSubstitute.invalidate_rect( False )
                        self.possibleSubstitute = handled
                        self.possibleSubstitute.invalidate_rect( False )
                    break
            if not handled and self.possibleSubstitute:
                self.possibleSubstitute.invalidate_rect( False )
                self.possibleSubstitute = None

    def _beginDrag( self, block ):
        block._beginDrag()
        self.clickedBlock = block
        self.dragging = True

    #==========================================================
    # Drawing

    def draw( self ):

        startX = self.screenBufDirtyRect.x
        startY = self.screenBufDirtyRect.y
        stopX = startX + self.screenBufDirtyRect.width
        stopY = startY + self.screenBufDirtyRect.height

        self.gc.set_clip_rectangle( self.screenBufDirtyRect )

        # draw background
        self.gc.foreground = self.colors["bg"]
        self.screenBuf.draw_rectangle( self.gc, True, startX, startY, self.screenBufDirtyRect.width, self.screenBufDirtyRect.height )

        # draw blocks
        self.gc.set_clip_mask( self.clipMask )
        for block in self.blocks:
            block.draw( startX, startY, stopX, stopY, self.screenBuf )

        self.screenBufDirty = False

    def expose( self, DA, event ):

        if self.screenBufDirty:
            self.draw()

        self.drawingAreaDirty = False

        startX = event.area.x
        startY = event.area.y
        stopX = event.area.x + event.area.width
        stopY = event.area.y + event.area.height

        self.gc.set_clip_rectangle( event.area )

        # draw base
        DA.window.draw_drawable( self.gc, self.screenBuf, startX, startY, startX, startY, event.area.width, event.area.height )

        if self.possibleDelete:
            return

        self.gc.set_clip_mask( self.clipMask )

        # draw possible parent
        if self.possibleParent:
            self.possibleParent.drawHighlight( startX, startY, stopX, stopY, DA.window )

        # draw dragged objects
        if self.dragging:
            self.clickedBlock.draw( startX, startY, stopX, stopY, DA.window )

        # draw possible substitute
        if self.possibleSubstitute:
            self.possibleSubstitute.drawHighlight( startX, startY, stopX, stopY, DA.window )

    def invalidate_rect( self, x, y, width, height, base = True ):
        self.dirtyRectToAdd.x = x
        self.dirtyRectToAdd.y = y
        self.dirtyRectToAdd.width = width
        self.dirtyRectToAdd.height = height

        #print "dirty %d %d %d %d %d %d" % (x, y, width, height, x+width, y+height)
        if base: # the base image has been dirtied
            if not self.screenBufDirty:
                self.screenBufDirtyRect.x = x
                self.screenBufDirtyRect.y = y
                self.screenBufDirtyRect.width = width
                self.screenBufDirtyRect.height = height
            else:
                self.screenBufDirtyRect = self.screenBufDirtyRect.union( self.dirtyRectToAdd )
            self.screenBufDirty = True
        if self.drawingArea.window != None:
            self.drawingArea.window.invalidate_rect( self.dirtyRectToAdd, True )
        self.drawingAreaDirty = True