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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
import pygtk
pygtk.require( '2.0' )
import gtk
import common.Config as Config
from gettext import gettext as _
import common.Util.InstrumentDB as InstrumentDB
from Jam import Block
from Jam import Popup
class Desktop( gtk.EventBox ):
def __init__( self, owner ):
gtk.EventBox.__init__( self )
self.instrumentDB = InstrumentDB.getRef()
self.owner = owner
self.drawingArea = gtk.DrawingArea()
self.add( self.drawingArea )
# take drawing setup from owner
self.gc = owner.gc
self.colors = owner.colors
self.blockMask = owner.blockMask
self.noteDB = owner.noteDB
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.loops = {} # dict of playing loops by loop root
self.drums = [] # list of active drums
self.add_events(gtk.gdk.POINTER_MOTION_MASK|gtk.gdk.POINTER_MOTION_HINT_MASK)
self.connect( "size-allocate", self.on_size_allocate )
self.connect( "button-press-event", self.on_button_press )
self.connect( "button-release-event", self.on_button_release )
self.connect( "motion-notify-event", self.on_motion_notify )
self.drawingArea.connect( "expose-event", self.on_expose )
self.clickedBlock = None
self.possibleParent = None
self.possibleSubstitute = None
self.dragging = False
self.possibleDelete = False
self.overKey = False
#-- Popups --------------------------------------------
self.rightClicked = False
self.popup = {}
self.popup[Popup.Instrument] = Popup.Instrument( _("Instrument Properties"), self.owner )
self.popup[Popup.Drum] = Popup.Drum( _("Drum Kit Properties"), self.owner )
self.popup[Popup.Loop] = Popup.Loop( _("Loop Properties"), self.owner )
self.popup[Popup.Shortcut] = Popup.Shortcut( _("Assign Key"), self.owner )
def dumpToStream( self, ostream ):
for b in self.blocks:
b.dumpToStream( ostream )
def on_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
def getLoopIds( self ):
return [ self.loops[loop] for loop in self.loops ]
#==========================================================
# Blocks
def addBlock( self, blockClass, blockData, loc = (-1,-1), drag = False ):
block = blockClass( self, 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, int(self.absoluteLoc[0] + x), int(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:
pass
elif blockClass == Block.Drum:
pass
elif blockClass == Block.Loop:
pass
return block
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.isActive():
self.deactivateDrum( block )
elif block.type == Block.Loop:
if block.isActive():
self.deactivateLoop( block )
if block in self.blocks:
block.invalidate_rect( True )
self.blocks.remove( block )
block.destroy()
def _clearDesktop( self ):
for i in range( len(self.blocks)-1, -1, -1 ):
self.deleteBlock( self.blocks[i] )
def mapKey( self, key, block, oldKey = None ):
self.owner.mapKey( key, block, oldKey )
def getInstrumentImage( self, id, active = False ):
return self.owner.getInstrumentImage( id, active )
def getKeyImage( self, key, active = False ):
return self.owner.getKeyImage( key, active )
def getLoopImage( self, id, active = False ):
return self.owner.getLoopImage( id, active )
def updateLoopImage( self, id ):
self.owner.updateLoopImage( id )
#==========================================================
# State
def activateInstrument( self, block ):
if self.activeInstrument:
self.activeInstrument.setActive( False )
block.setActive( True )
self.activeInstrument = block
self.updateInstrument( block )
def updateInstrument( self, block ):
data = block.data
self.owner._updateInstrument( data["id"], data["volume"], data["pan"], data["reverb"] )
def activateDrum( self, block ):
for drum in self.drums:
self.deactivateDrum( drum )
block.setActive( True )
self.drums.append( block )
self.updateDrum( block, True )
def deactivateDrum( self, block ):
self.owner._stopDrum( self.loops[block] )
del self.loops[block]
block.setActive( False )
self.drums.remove( block )
def updateDrum( self, block, firstTime = False ):
data = block.data
if firstTime:
loopId = None
else:
loopId = self.loops[block]
self.loops[block] = self.owner._playDrum( data["id"], data["page"], data["volume"], data["reverb"], data["beats"], data["regularity"], loopId )
def activateLoop( self, block ):
block.setActive( True )
self.updateLoop( block, True )
def deactivateLoop( self, block ):
block.setActive( False )
self.owner._stopLoop( self.loops[block] )
del self.loops[block]
def updateLoop( self, block, firstTime = False ):
inst = block.parent.data
tune = []
itr = block
while itr != None:
tune.append( itr.data["id"] )
itr = itr.child
if firstTime:
loopId = None
else:
loopId = self.loops[block]
self.loops[block] = self.owner._playLoop( inst["id"], inst["volume"], inst["reverb"], tune, loopId )
#==========================================================
# Mouse
def on_button_press( self, widget, event ):
if event.button == 3:
self.rightClicked = True
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 on_button_release( self, widget, event ):
if event.button == 3: # Right Click
if self.clickedBlock:
if self.clickedBlock.type == Block.Instrument:
self.popup[Popup.Instrument].setBlock( self.clickedBlock )
elif self.clickedBlock.type == Block.Drum:
self.popup[Popup.Drum].setBlock( self.clickedBlock )
elif self.clickedBlock.type == Block.Loop:
self.popup[Popup.Loop].setBlock( self.clickedBlock )
self.clickedBlock = None
self.rightClicked = False
self.overKey = False # just in case
return
if self.overKey:
self.popup[Popup.Shortcut].setBlock( self.clickedBlock )
self.overKey.invalidate_rect( False )
self.overKey = False
self.clickedBlock = None
return
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():
if self.clickedBlock.resetLoc():
self.blocks.append( self.clickedBlock )
else:
self.deleteBlock( self.clickedBlock )
self.clickedBlock = None
if self.possibleSubstitute.type == Block.Instrument:
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 on_motion_notify( self, widget, event ):
if self.rightClicked:
return
if self.clickedBlock and self.overKey:
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
blockCount = len(self.blocks)
if not self.clickedBlock:
if self.popup[Popup.Shortcut].is_up():
return
for i in range(blockCount-1, -1, -1):
over = self.blocks[i].testMouseOver( event )
if over == -1:
break # over block but no hotspot
if over:
if self.overKey != self.blocks[i]:
if self.overKey:
self.overKey.invalidate_rect( False )
self.overKey = over
self.overKey.invalidate_rect( False )
return
if self.overKey:
self.overKey.invalidate_rect( False )
self.overKey = False
return
self.dragging = True
if self.clickedBlock.motion_notify( event ): # first drag of root block, remove from self.blocks
self.blocks.remove( self.clickedBlock )
blockCount = len(self.blocks)
if event.y < 0 or event.y > self.alloc.height:
self.possibleDelete = True
return
else:
self.possibleDelete = False
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.blockMask )
for block in self.blocks:
block.draw( startX, startY, stopX, stopY, self.screenBuf )
self.screenBufDirty = False
def on_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.blockMask )
# 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 )
# draw key highlight
if self.overKey:
self.overKey.drawKeyHighlight( 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
|