~skykooler/swift-swf/quickly_trunk

« back to all changes in this revision

Viewing changes to bin/swift-swf

  • Committer: Skyler Lehmkuhl
  • Date: 2011-03-29 05:34:23 UTC
  • Revision ID: skykooler@yahoo.com-20110329053423-f9d3d3jdupeeoom0
quickly saved

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import gobject
26
26
import copy
27
27
import urllib
 
28
import time
 
29
import imp
 
30
from PIL import Image
28
31
 
29
32
# optional Launchpad integration
30
33
# this shouldn't crash if not found as it is simply used for bug reporting
37
40
import gio
38
41
import gobject
39
42
import cairo
 
43
import pango
40
44
import math
41
45
import subprocess
42
46
 
108
112
        avbox.show()                                                                                    # make the vbox visible
109
113
        alert.show()                                                                                    # make the alert itself visible
110
114
 
 
115
def alert_list(title, list):
 
116
        #Launches an alert window with a given text.
 
117
        global retval
 
118
        def abutton_press_event(widget, event, radlist):
 
119
                #Close when "Ok" is pressed
 
120
                global retval
 
121
                for i in radlist:
 
122
                        if i.get_active():
 
123
                                retval = radlist.index(i)
 
124
                alert.destroy()
 
125
        alert = gtk.Dialog()                                                                    # make a new window for the alert
 
126
        alert.set_position(gtk.WIN_POS_CENTER)                                  # put it in the center of the screen
 
127
        alert.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)    # tell the WM that it is a dialog
 
128
        alert.set_destroy_with_parent(True)                                             # if someone closes SWIFT, we want the alert to close too
 
129
        alert.set_modal(True)                                                                   # alert should block input to SWIFT until acknowledged
 
130
        alert.set_title(title)                                                                  # call it what we want to
 
131
        #alert.set_size_request(250, 100)                                               # make it 250x100
 
132
        #avbox = vbox = gtk.VBox(False, 0)                                              # create a vertical box container
 
133
        #alert.add(avbox)                                                                               # add said container to the alert
 
134
        radio = None
 
135
        radlist = []
 
136
        for i in list:
 
137
                radiobutton = gtk.RadioButton(radio, str(i))
 
138
                radio = radiobutton
 
139
                radlist.append(radiobutton)
 
140
                alert.vbox.pack_start(radiobutton, True, False, 0)
 
141
                radiobutton.show()
 
142
        
 
143
        abutton = gtk.Button("OK")                                                              # new button with text "OK"
 
144
        abutton.set_use_stock(True)                                                             # it is a stock button provided by GTK
 
145
        abutton.set_size_request(50, -1)                                                # we don't want it as wide as the whole alert
 
146
        abutton.set_events(gtk.gdk.ALL_EVENTS_MASK);                    # capture clicks, keyboard, anything
 
147
        abutton.connect("button_press_event", abutton_press_event, radlist)     # but only listen to the clicks
 
148
        abutton.show()                                                                                  # make button visible
 
149
        ahbox = gtk.HBox(False, 10)                                                             # make a new hbox
 
150
        ahbox.pack_start(abutton, True, False, 0)                               # put the button in it, but don't make it expand
 
151
        alert.vbox.pack_start(ahbox, True, False, 0)                                    # put the hbox in the vbox
 
152
        ahbox.show()                                                                                    # make it visible
 
153
        #avbox.show()                                                                                   # make the vbox visible
 
154
        alert.show()                                                                                    # make the alert itself visible
 
155
        result = alert.run()
 
156
        return retval
 
157
 
111
158
def get_pixel_colour(i_x, i_y):
112
159
        o_gdk_pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
113
160
        o_gdk_pixbuf.get_from_drawable(gtk.gdk.get_default_root_window(), gtk.gdk.colormap_get_system(), i_x, i_y, 0, 0, 1, 1)
197
244
        drawable.draw_line(gc, x1, y1, x2, y2)
198
245
 
199
246
def drawBox(gc, drawable, x, y, width, height, filled=1, rotation=0):
200
 
        #TODO:Add gradient support!
201
 
        #TODO:Add image and texture support
202
247
        x0 = 0-width/2
203
248
        y0 = 0-height/2
204
249
        x1 = width/2
205
250
        y1 = height/2
206
251
        r = rotation
207
 
        
 
252
        print x, y, width, height
208
253
        xa = int(math.cos(r)*x0-math.sin(r)*y0)
209
254
        ya = int(math.cos(r)*y0+math.sin(r)*x0)
210
255
        xb = int(math.cos(r)*x1-math.sin(r)*y0)
269
314
        global currentfile
270
315
        global currentframe
271
316
        if type(fill)==type(()) or type(fill)==type([]):
272
 
                cr.set_source_rgba(fill[0], fill[1], fill[2], 1)
 
317
                cr.set_source_rgb(fill[0], fill[1], fill[2])
273
318
        elif fill.startswith("#"):
274
319
                rgbfill = hex2rgb(fill)
275
 
                cr.set_source_rgba(rgbfill[0], rgbfill[1], rgbfill[2], 1)
 
320
                cr.set_source_rgb(rgbfill[0], rgbfill[1], rgbfill[2])
276
321
        else:
277
322
                fill = currentfile["colors"]["textures"][fill]
278
323
                if fill["type"]=="color":
279
 
                        cr.set_source_rgba(fill["fill"][0], fill["fill"][1], fill["fill"][2], 1)
 
324
                        cr.set_source_rgb(fill["fill"][0], fill["fill"][1], fill["fill"][2])
280
325
                elif fill["type"]=="gradient":
281
326
                        #make this use the gradient
282
327
                        grad = gradient(fill["fill"])
301
346
        cr.restore()
302
347
        if inactive:
303
348
                cr.set_source_rgba(0.5,0.5,0.5,0.5)
304
 
                cr.fill_preserve()
 
349
                cr.fill()
 
350
        else:
 
351
                cr.fill()
305
352
def drawCurve(gc, drawable, points, filled=False, precision=20):
306
353
        npoints = []
307
354
        s=[]
308
355
        r = range(len(points)-2)
309
356
        for i in r:
310
357
                if i/2==int(math.ceil(i/2.0)):
311
 
                        s.append(i)
 
358
                        s.append(i)                                     #Very opaque function to fill s with all the even numbers in r
312
359
        for i in s:
313
360
                for j in range(precision):
314
361
                        k=1.0*(precision-j)
315
 
                        x=(ave(ave(points[i][0], points[i+1][0], k/precision), ave(points[i+1][0], points[i+2][0], k/precision), k/precision))
316
 
                        y=(ave(ave(points[i][1], points[i+1][1], k/precision), ave(points[i+1][1], points[i+2][1], k/precision), k/precision))
 
362
                        x=int(ave(ave(points[i][0], points[i+1][0], k/precision), ave(points[i+1][0], points[i+2][0], k/precision), k/precision))
 
363
                        y=int(ave(ave(points[i][1], points[i+1][1], k/precision), ave(points[i+1][1], points[i+2][1], k/precision), k/precision))
317
364
                        npoints.append((x, y))
318
365
        if filled==False:
319
366
                for i in range(len(npoints)-1):
327
374
        if filled==True:
328
375
                cr.fill_preserve()
329
376
        #cr.stroke_preserve()
 
377
def drawcrShape(cr, shapedata, filled=True):
 
378
        for i in shapedata:
 
379
                if i[0]=="M":
 
380
                        cr.move_to(i[1], i[2])
 
381
                elif i[0]=="L":
 
382
                        cr.line_to(i[1], i[2])
 
383
                elif i[0]=="C":
 
384
                        cr.curve_to(i[1], i[2], i[3], i[4], i[5], i[6])
 
385
        if filled:
 
386
                cr.fill()
 
387
        else:
 
388
                cr.stroke()
 
389
def drawShape(gc, drawable, shapedata, filled, precision=20):
 
390
        npoints = []
 
391
        for i in shapedata:
 
392
                if i[0]=="C":
 
393
                        for j in range(precision):
 
394
                                k=1.0*(precision-j)
 
395
                                x=int(ave(ave(i[1], i[3], k/precision), ave(i[3], i[5], k/precision), k/precision))
 
396
                                y=int(ave(ave(i[2], i[4], k/precision), ave(i[4], i[6], k/precision), k/precision))
 
397
                                npoints.append((x, y))
 
398
                elif i[0]=="L":
 
399
                        npoints.append((i[1], i[2]))
 
400
                elif i[0]=="M":
 
401
                        if filled==False:
 
402
                                for i in range(len(npoints)-1):
 
403
                                        drawLine(gc, drawable, npoints[i][0], npoints[i][1], npoints[i+1][0], npoints[i+1][1])
 
404
                        else:
 
405
                                print "M", npoints
 
406
                                drawable.draw_polygon(gc, 1, npoints)
 
407
                        npoints = []
 
408
        drawable.draw_polygon(gc, 1, npoints)
330
409
def on_key_press(widget, event):
331
410
        keyname = gtk.gdk.keyval_name(event.keyval)
332
411
        if keyname=="A":
353
432
        global objects
354
433
        global tnum
355
434
        tnum = tnum+1
356
 
        print tnum, event
357
435
        x,y,w,h = stage.allocation
358
436
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w,h)
359
437
        cr = cairo.Context(surface)
364
442
        cr.paint()
365
443
        cr.set_line_width(2)
366
444
        ccol = currentcolor
367
 
        global pixmap
368
 
        global colormap
369
 
        pixmap=gtk.gdk.Pixmap(None, w, h, 24)
370
 
        colormap=pixmap.get_colormap()
371
 
        biggest=0
372
 
        for i in objects:
373
 
                print objects
374
 
                if objects[i].type=="box" or objects[i].type=="ellipse" or objects[i].type=="button":
375
 
                        gc = gtk.gdk.GC(pixmap, colormap.alloc_color(selectid[selectors.index(i)], True, True), colormap.alloc_color("#000000", True, True))
376
 
                        objects[i].draw(cr, gc, pixmap)
 
445
        biggest = 0
 
446
        objects["root"].draw(cr, w, h, stage.get_toplevel().builder.get_object("drawingarea2"))
377
447
        if currentframe[level] in currentfile["frames"][0]:
378
448
                if not currentselect[level+1] == None:
379
449
                        if not currentfile["objects"][currentselect[level]].type=="button":
380
450
                                drawcrBox(cr, currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1][currentselect[level+1]]["x"]-1, currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1][currentselect[level+1]]["y"]-1, currentfile["objects"][currentselect[level+1]].width+2, currentfile["objects"][currentselect[level+1]].height+2, 0, [0.0,0.0, 1.0])
381
451
                                corners = [0, 0, currentfile["objects"][currentselect[level+1]].width+2, currentfile["objects"][currentselect[level+1]].height+2]
382
 
                        else:
 
452
                        #else:
383
453
                                #implement drawcrBox(cr, etc.)
384
454
                                #gc = gtk.gdk.GC(pixmap, colormap.alloc_color(selectid[selectors.index(j)], True, True), colormap.alloc_color("#000000", True, True))
385
 
                                '''x = currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1][j]["x"]
386
 
                                y = currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1][j]["y"]
387
 
                                q = currentfile["objects"][currentselect[level]].frames["idle"]
388
 
                                for i in q:
389
 
                                        if q[i][i].type=="box":
390
 
                                                try:
391
 
                                                        if q[i]["x"]<corners[0]:
392
 
                                                                corners[0]= q[i]["x"]
393
 
                                                        if q[i]["x"]+q[i][i].width>corners[1]:
394
 
                                                                corners[1]= q[i]["x"]+q[i][i].width
395
 
                                                        if q[i]["y"]<corners[2]:
396
 
                                                                corners[2] = q[i]["y"]
397
 
                                                        if q[i]["y"]+q[i][i].height>corners[3]:
398
 
                                                                corners[3] = q[i]["y"]+q[i][i].height
399
 
                                                except:
400
 
                                                        corners = [q[i]["x"], q[i]["x"]+q[i][i].width, q[i]["y"], q[i]["y"]+q[i][i].height]
401
 
                                drawcrBox(cr, corners[0]+x, corners[2]+y, corners[1]-corners[0], corners[3]-corners[2], 0, [0.0, 0.0, 1.0], 0)'''
402
455
                cselname = stage.get_toplevel().builder.get_object("entry1").get_buffer()
403
456
                cselname.set_text(str(currentselect[level+1]), 25)
404
457
                if not currentselect[level+1]==None:
419
472
                                fac = facx(currentfile["frames"][0][biggest], currentfile["frames"][0][biggest+1], currentframe[level])
420
473
                                x = ave(f2["x"], f1["x"], fac)
421
474
                                y = ave(f2["y"], f1["y"], fac)
422
 
                                print i, f1, f2
423
475
                                r = ave(f2["rotate"], f1["rotate"], fac)
424
476
                        else:
425
477
                                f1 = currentfile["frames"][biggest+1][i]
473
525
                                        currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1][currentselect[level+1]]["y"]=startmouse[1]-fy
474
526
                                        global source_id
475
527
                                        if not source_id:
476
 
                                                #source_id = gobject.timeout_add(100, expostagevent, stage, "animation-event")
477
 
                                                #print "hoojiwhatsit"
478
528
                                                source_id = "k"
479
529
                elif mode == "s":
480
530
                        if currentframe[level] in currentfile["frames"][0]:
488
538
                                        currentfile["frames"][currentfile["frames"][0].index(currentframe)+1][currentselect[level]]["x"]=startmouse[0]-fx
489
539
                                        currentfile["frames"][currentfile["frames"][0].index(currentframe)+1][currentselect[level]]["y"]=startmouse[1]-fy
490
540
                                        #source_id = gobject.timeout_add(100, expostagevent, stage, "animation-event")
491
 
        if mode == "c":
492
 
                startmouse = stage.get_toplevel().builder.get_object("drawingarea1").get_pointer()
493
 
                x = startmouse[0]
494
 
                y = startmouse[1]
495
 
                cc = []
496
 
                for i in currentcurve:
497
 
                        cc.append(i)
498
 
                cc.append((x, y))
499
 
        #       drawCurve(gc, stage.window, cc)
500
 
                drawcrCurve(cr, cc)
501
 
        #stage.window.end_paint()
502
 
        
503
541
        cra.set_source_surface(surface)
504
542
        cra.paint()
505
543
        
567
605
                                        framewin.window.draw_pixbuf(None, frame_inactive_blank, 0, 0, i*16, 0, 16, -1)
568
606
        elif currentfile["objects"][currentselect[level]].type == "button":
569
607
                framewin.window.draw_pixbuf(None, keyframe_active_button, (instep%16)*64, 0, 0, 0, 64, -1)
570
 
        #print instep
571
608
        step = instep
572
609
        if event=="animation-event":
573
610
                source_id = gobject.timeout_add(100, exposeframevent, framewin, "animation-event", step+1)
575
612
        global step
576
613
        objpic = gtk.gdk.pixbuf_new_from_file("/usr/share/swift-swf/media/object_active.png")
577
614
        objwin.window.draw_pixbuf(None, objpic, (step%16)*128, 0, 0, 0, 128, -1)
578
 
        #print instep
579
615
        source_id = gobject.timeout_add(100, exposobjevent, objwin, "expose-event")
580
616
def exposegradevent(gradwin, event):
581
617
        gb0=gradwin.get_toplevel().builder.get_object("drawingarea4")
816
852
        return x;
817
853
 
818
854
        
 
855
def keyvent(stage, event=None, window=None, data=None):
 
856
        keyname = str(gtk.gdk.keyval_name(event.keyval))
 
857
        if keyname=="space":
 
858
                window.select_any(stage, data)
 
859
        elif keyname=="r":
 
860
                window.draw_rect(stage, data)
 
861
        elif keyname=="e":
 
862
                window.draw_ellipse(stage, data)
 
863
        elif keyname=="t":
 
864
                window.draw_text(stage, data)
 
865
        elif keyname=="b":
 
866
                window.paint_bucket(stage, data)
 
867
        elif keyname=="Return":
 
868
                window.play_frames(stage, data)
 
869
        elif keyname=="Delete":
 
870
                window.delete_item(stage, data)
 
871
        else:
 
872
                print keyname
 
873
 
819
874
class box:
820
875
        # Basic class for box. Will be deprecated as soon as replacement "shape" class is created.
821
876
        def __init__(self, name, width=100, height=100, parent="root", fill="none"):
827
882
                self.fill = fill
828
883
                self.level = objects[parent].level+1
829
884
                self.type = "box"
830
 
        def draw(self, cr, gc=None, pixmap=None):
 
885
        def draw(self, cr, gc=None, pixmap=None, stage=None):
831
886
                global currentfile
832
887
                global currentframe
833
888
                global level
834
889
                global objects
835
890
                # x, y, rotate, keyframe=False, 
836
891
                parent = objects[self.parent]
 
892
                if parent.type=="button":
 
893
                        if not currentframe[self.level-1] in parent.frames[0]:
 
894
                                currentframe[self.level-1]="idle"
837
895
                if currentframe[self.level-1] in parent.frames[0]:
838
896
                        if self.name in parent.frames[parent.frames[0].index(currentframe[self.level-1])+1]:
839
897
                                x = parent.frames[parent.frames[0].index(currentframe[self.level-1])+1][self.name]["x"]
880
938
                        inactive = False
881
939
                drawcrBox(cr, x, y, self.width, self.height, 1, self.fill, rotate, [0, 0, 0, 0], inactive)
882
940
                if keyframe:
883
 
                        drawBox(gc, pixmap, x, y, self.width, self.height, 1, rotate)
 
941
                        try:
 
942
                                drawBox(gc, pixmap, x, y, self.width, self.height, 1, rotate)
 
943
                        except:
 
944
                                print "Drawing to offscreen buffer failed. Not a problem if you were exporting an image."
884
945
 
885
946
class ellipse:
886
947
        # Basic class for ellipse. Will be deprecated as soon as replacement "shape" class is created.
890
951
                self.width = width
891
952
                self.height = height
892
953
                self.parent = parent
893
 
                self.fill = fill
 
954
                self.fill = copy.deepcopy(fill)
894
955
                self.level = objects[parent].level+1
895
956
                self.type = "ellipse"
896
 
        def draw(self, cr, gc=None, pixmap=None):
 
957
        def draw(self, cr, gc=None, pixmap=None, stage=None):
897
958
                global currentfile
898
959
                global currentframe
899
960
                global level
900
961
                global objects
901
 
                # x, y, rotate, keyframe=False, 
 
962
                # x, y, rotate, keyframe=False,
902
963
                parent = objects[self.parent]
 
964
                if parent.type=="button":
 
965
                        if not currentframe[self.level-1] in parent.frames[0]:
 
966
                                currentframe[self.level-1]="idle"
903
967
                if currentframe[self.level-1] in parent.frames[0]:
904
968
                        if self.name in parent.frames[parent.frames[0].index(currentframe[self.level-1])+1]:
905
969
                                x = parent.frames[parent.frames[0].index(currentframe[self.level-1])+1][self.name]["x"]
937
1001
                x = x+parent.x
938
1002
                y = y+parent.y
939
1003
                rotate = rotate+parent.rotate
940
 
                fill = self.fill
 
1004
                fill = copy.deepcopy(self.fill)
941
1005
                if type(fill)==type([]):
942
1006
                        fill = hex2rgb(fill)
943
1007
                if self.level<=level and not self.parent in currentselect:
946
1010
                        inactive = False
947
1011
                drawcrEllipse(cr, x, y, self.width, self.height, self.fill, rotate, [0, 0, 0, 0], inactive)
948
1012
                if keyframe:
949
 
                        drawEllipse(gc, pixmap, x, y, self.width, self.height, 1, rotate)
 
1013
                        try:
 
1014
                                drawEllipse(gc, pixmap, x, y, self.width, self.height, 1, rotate)
 
1015
                        except:
 
1016
                                print "Drawing to offscreen buffer failed. Not a problem if you were exporting an image."
 
1017
class shape:
 
1018
        def __init__(self, name, width=0, height=0, parent="root", fill="none"):
 
1019
                global objects
 
1020
                self.name = name
 
1021
                self.width = width
 
1022
                self.height = height
 
1023
                self.parent = parent
 
1024
                self.fill = copy.deepcopy(fill)
 
1025
                self.level = objects[parent].level+1
 
1026
                self.shapedata = []
 
1027
                self.type = "shape"
 
1028
        def draw(self, cr, gc=None, pixmap=None, stage=None):
 
1029
                #implement shape.draw()
 
1030
                global currentfile
 
1031
                global currentframe
 
1032
                global level
 
1033
                global objects
 
1034
                # x, y, rotate, keyframe=False,
 
1035
                parent = objects[self.parent]
 
1036
                if parent.type=="button":
 
1037
                        if not currentframe[self.level-1] in parent.frames[0]:
 
1038
                                currentframe[self.level-1]="idle"
 
1039
                if currentframe[self.level-1] in parent.frames[0]:
 
1040
                        if self.name in parent.frames[parent.frames[0].index(currentframe[self.level-1])+1]:
 
1041
                                x = parent.frames[parent.frames[0].index(currentframe[self.level-1])+1][self.name]["x"]
 
1042
                                y = parent.frames[parent.frames[0].index(currentframe[self.level-1])+1][self.name]["y"]
 
1043
                                rotate = parent.frames[parent.frames[0].index(currentframe[self.level-1])+1][self.name]["rotate"]
 
1044
                                keyframe = True
 
1045
                        else:
 
1046
                                return
 
1047
                else:
 
1048
                        keyframe = False
 
1049
                        biggest=0
 
1050
                        for i in range (0, currentframe[self.level-1]+1):
 
1051
                                if parent.frames[0][i]==currentframe[self.level-1]:
 
1052
                                        biggest = i
 
1053
                                        break
 
1054
                                elif parent.frames[0][i]<currentframe[self.level-1]:
 
1055
                                        biggest = i
 
1056
                                else:
 
1057
                                        break
 
1058
                        if self.name in parent.frames[biggest+1]:
 
1059
                                if len(parent.frames)>biggest+2 and self.name in parent.frames[biggest+2]["sort"]:
 
1060
                                        f1 = parent.frames[biggest+1][self.name]
 
1061
                                        f2 = parent.frames[biggest+2][self.name]
 
1062
                                        fac = facx(parent.frames[0][biggest], parent.frames[0][biggest+1], currentframe[self.level-1])
 
1063
                                        x = ave(f2["x"], f1["x"], fac)
 
1064
                                        y = ave(f2["y"], f1["y"], fac)
 
1065
                                        rotate = ave(f2["rotate"], f1["rotate"], fac)
 
1066
                                else:
 
1067
                                        f1 = parent.frames[biggest+1][self.name]
 
1068
                                        x = f1["x"]
 
1069
                                        y = f1["y"]
 
1070
                                        rotate = f1["rotate"]
 
1071
                        else:
 
1072
                                return
 
1073
                x = x+parent.x
 
1074
                y = y+parent.y
 
1075
                rotate = rotate+parent.rotate
 
1076
                fill = copy.deepcopy(self.fill)
 
1077
                if type(fill)==type([]):
 
1078
                        fill = hex2rgb(fill)
 
1079
                if self.level<=level and not self.parent in currentselect:
 
1080
                        inactive = True
 
1081
                else:
 
1082
                        inactive = False
 
1083
                if type(fill)==type(()) or type(fill)==type([]):
 
1084
                        cr.set_source_rgb(fill[0], fill[1], fill[2])
 
1085
                elif fill.startswith("#"):
 
1086
                        rgbfill = hex2rgb(fill)
 
1087
                        cr.set_source_rgb(rgbfill[0], rgbfill[1], rgbfill[2])
 
1088
                else:
 
1089
                        fill = currentfile["colors"]["textures"][fill]
 
1090
                        if fill["type"]=="color":
 
1091
                                cr.set_source_rgb(fill["fill"][0], fill["fill"][1], fill["fill"][2])
 
1092
                        elif fill["type"]=="gradient":
 
1093
                                #make this use the gradient
 
1094
                                grad = gradient(fill["fill"])
 
1095
                                g = cairo.LinearGradient(x, y, x+width, y+height)
 
1096
                                for i in range(len(grad)):
 
1097
                                        g.add_color_stop_rgba(grad[i][0], grad[i][1], grad[i][2], grad[i][3], grad[i][4])
 
1098
                                cr.set_source(g)
 
1099
                        else:
 
1100
                                #use a texture                  
 
1101
                                image = currentfile["colors"]["images"][fill["fill"]]
 
1102
                                surface = cairo.ImageSurface.create_from_png(image)
 
1103
                                pat = cairo.SurfacePattern(surface)
 
1104
                                matrix = cairo.Matrix(x0=-x, y0=-y)
 
1105
                                pat.set_matrix(matrix)
 
1106
                                cr.set_source(pat)
 
1107
                drawcrShape(cr, self.shapedata, True)
 
1108
                if keyframe:
 
1109
                        try:
 
1110
                                print "Drawing box..."
 
1111
                                drawShape(gc, pixmap, self.shapedata, 1)
 
1112
                                #drawEllipse(gc, pixmap, 20, 20, 300, 200, 1)
 
1113
                        except:
 
1114
                                print "Drawing to offscreen buffer failed. Not a problem if you were exporting an image."
 
1115
                test=False
 
1116
                if test:
 
1117
                        stage.window.draw_drawable(gc, pixmap, 0, 0, 0, 0, 500, 500)
950
1118
class button:
951
1119
        # Basic class for buttons.
952
 
        def __init__(self, name, width=100, height=100, parent="root"):
 
1120
        def __init__(self, name, width=100, height=100, parent="root", frames=[["idle"],{}]):
953
1121
                self.width = width
954
1122
                self.height = height
955
1123
                self.name = name
956
1124
                self.type = "button"
957
1125
                self.parent = parent
958
1126
                self.level = objects[parent].level+1
959
 
                self.frames = [["idle"],{}]
960
 
        def draw(self, cr, gc=None, pixmap=None):
 
1127
                self.frames = copy.deepcopy(frames)
 
1128
        def draw(self, cr, gc=None, pixmap=None, stage=None):
961
1129
                global currentfile
962
1130
                global currentframe
963
1131
                global level
1000
1168
                for i in self.frames[self.frames[0].index(frame)+1]:
1001
1169
                        self.frames[self.frames[0].index(frame)+1][i][i].draw(cr, gc, pixmap)
1002
1170
                        
 
1171
        def toString(self):
 
1172
                return "{width = "+str(self.width)+", height = "+str(self.height)+", name = '"+str(self.name)+"', parent = '"+str(self.parent)+"', level = "+str(self.level)+", frames = "+str(self.frames)+"}"
1003
1173
 
1004
1174
class root:
1005
1175
        # Class for root. Only ever one instance I think.
1009
1179
                self.x=0
1010
1180
                self.y=0
1011
1181
                self.rotate=0
 
1182
        def draw(self, cr, w, h, stage):
 
1183
                global currentframe
 
1184
                if currentframe[0] in self.frames[0]:
 
1185
                        global pixmap
 
1186
                        global colormap
 
1187
                        pixmap=gtk.gdk.Pixmap(None, w, h, 24)
 
1188
                        colormap=pixmap.get_colormap()
 
1189
                        for i in self.frames[self.frames[0].index(currentframe[0])+1]["sort"]:
 
1190
                                gc = gtk.gdk.GC(pixmap, colormap.alloc_color(selectid[selectors.index(i)], True, True), colormap.alloc_color("#000000", True, True))
 
1191
                                currentfile["objects"][i].draw(cr, gc, pixmap, stage)
 
1192
                else:
 
1193
                        for i in range (0, currentframe[0]+1):
 
1194
                                if self.frames[0][i]==currentframe[0]:
 
1195
                                        biggest = i
 
1196
                                        break
 
1197
                                elif self.frames[0][i]<currentframe[0]:
 
1198
                                        biggest = i
 
1199
                                else:
 
1200
                                        break;
 
1201
                        for i in self.frames[self.frames[0].index(biggest)+1]["sort"]:
 
1202
                                currentfile["objects"][i].draw(cr, None, None)
 
1203
        def toString(self):
 
1204
                return "{x = "+str(self.x)+", y = "+str(self.y)+"', rotate = '"+str(self.rotate)+"', level = "+str(self.level)+", frames = "+str(self.frames)+"}"
 
1205
 
1012
1206
class SwiftSwfWindow(gtk.Window):
1013
1207
        __gtype_name__ = "SwiftSwfWindow"
1014
 
 
1015
1208
        
1016
1209
 
1017
1210
        def AddLibItem(self, item):
1095
1288
                if len(sys.argv)==2:
1096
1289
                        print "looks good..."
1097
1290
                for arg in sys.argv:
1098
 
                        print "___________________ARGS____________________"
1099
1291
                        if not arg=="swift-swf" or arg=="./swift-swf":
1100
1292
                                print arg
1101
1293
                
1125
1317
                #Delete any temp files so swift won't give an error on next boot
1126
1318
                os.system("rm /tmp/currentfile")
1127
1319
                gtk.main_quit()
1128
 
        def save_dialog(self):
 
1320
        def save_dialog(self, widget, data=None):
1129
1321
                global path
1130
1322
                global title
1131
1323
                global currentfile
1159
1351
                        for i in actions:
1160
1352
                                frame_actions.append("\t\t"+i)
1161
1353
                        frame_actions.append("\t.end")
1162
 
                        print frame_actions
1163
1354
                        buff = self.builder.get_object("textview1").get_buffer()
1164
1355
                        start_iter = buff.get_start_iter()
1165
1356
                        end_iter = buff.get_end_iter()
1175
1366
                #record_type = "http://wiki.ubuntu.com/Quickly/JottyDoc"
1176
1367
                #results = self.database.get_records(record_type = record_type, 
1177
1368
                #                                                                       create_view = True)
1178
 
                print currentfile
1179
1369
                fc = open(path+title+".swift", "w")
1180
1370
                fc.write(savestring)
1181
1371
                fc.flush()
1182
1372
                fc.close()
1183
 
                print "HEY THERE!"
1184
1373
                self.set_title("Swift - "+title+".swift")
1185
1374
                '''
 
1375
                self.save_file(widget, True)
1186
1376
                if result != gtk.RESPONSE_OK:
1187
1377
                        return
1188
1378
 
1189
1379
        def save_file_as(self, widget, data=None):
1190
 
                self.save_dialog()
 
1380
                self.save_dialog(widget, data)
1191
1381
 
1192
1382
        def save_file(self, widget, data=None):
1193
1383
                global path
1197
1387
                b = ""
1198
1388
                for i in a:
1199
1389
                        b = b + i
1200
 
                print path+title+".swift"
1201
 
                if b == "":
1202
 
                        print "new file"
 
1390
                if data==True:
 
1391
                        
1203
1392
                        tar = tarfile.open(path+title+".swift", "w:gz")
1204
 
                        for i in currentfile["images"]:
1205
 
                                tar.add(currentfile["images"][i], i, False)
1206
 
                        a.write(str(currentfile))
 
1393
                        for i in currentfile["colors"]["images"]:
 
1394
                                tar.add(currentfile["colors"]["images"][i], currentfile["colors"]["images"][i].split("/")[-1], False)
 
1395
                        currentfilo = copy.deepcopy(currentfile)
 
1396
                        for i in currentfilo["colors"]["images"]:
 
1397
                                currentfilo["colors"]["images"][i] = currentfilo["colors"]["images"][i].split("/")[-1]
 
1398
                        currentfilo["objects"] = {}
 
1399
                        a.write(str(currentfilo))
 
1400
                        for i in currentfile["objects"]:
 
1401
                                j = currentfile["objects"][i]
 
1402
                                if j.type=="box" or j.type=="ellipse":
 
1403
                                        a.write("\n"+j.type+"(\""+j.name+"\","+str(j.width)+","+str(j.height)+",\""+str(j.parent)+"\",\""+str(j.fill)+"\")")
 
1404
                                elif j.type=="button":
 
1405
                                        a.write("\n"+j.type+"(\""+j.name+"\","+str(j.width)+","+str(j.height)+",\""+str(j.parent)+"\","+str(j.frames)+")")
1207
1406
                        a.close()
1208
1407
                        tar.add(path+"currentfile", "currentfile", False)
1209
1408
                        tar.close()
1210
1409
                        os.system("rm "+path+"currentfile")
1211
 
                        self.save_dialog()
 
1410
                        
 
1411
                elif b == "":
 
1412
                        self.save_dialog(widget)
1212
1413
                elif title == "Untitled":
1213
 
                        print "new file"
1214
1414
                        a.close()
1215
 
                        self.save_file_as()
 
1415
                        self.save_file_as(widget, data)
1216
1416
                else:
1217
 
                        print "been there, done that."
1218
 
                        '''
1219
 
                        buff = self.builder.get_object("textview1").get_buffer()
1220
 
                        currentfile = str(buff).split("\n")
1221
 
                        currentfile[0] = ".flash filename=\""+title+".swf\""
1222
 
                        savestring = ""
1223
 
                        for i in currentfile:
1224
 
                          savestring = savestring+str(i)+"\n"
1225
 
                        a.write(savestring)
1226
 
                        '''
1227
 
                        a.flush()
 
1417
                        tar = tarfile.open(path+title+".swift", "w:gz")
 
1418
                        for i in currentfile["colors"]["images"]:
 
1419
                                tar.add(currentfile["colors"]["images"][i], currentfile["colors"]["images"][i].split("/")[-1], False)
 
1420
                        currentfilo = copy.deepcopy(currentfile)
 
1421
                        for i in currentfilo["colors"]["images"]:
 
1422
                                currentfilo["colors"]["images"][i] = "\""+currentfilo["colors"]["images"][i].split("/")[-1]+"\""
 
1423
                        currentfilo["objects"] = {}
 
1424
                        #for i in currentfilo["colors"]["textures"]
 
1425
                        a.write(str(currentfilo))
 
1426
                        for i in currentfile["objects"]:
 
1427
                                j = currentfile["objects"][i]
 
1428
                                if j.type=="box" or j.type=="ellipse":
 
1429
                                        a.write("\n"+j.type+"(\""+j.name+"\","+str(j.width)+","+str(j.height)+",\""+str(j.parent)+"\",\""+str(j.fill)+"\")")
 
1430
                                elif j.type=="button":
 
1431
                                        a.write("\n"+j.type+"(\""+j.name+"\","+str(j.width)+","+str(j.height)+",\""+str(j.parent)+"\","+str(j.frames)+")")
1228
1432
                        a.close()
 
1433
                        tar.add(path+"currentfile", "currentfile", False)
 
1434
                        tar.close()
 
1435
                        os.system("rm "+path+"currentfile")
 
1436
                        #self.save_dialog(widget, data)
1229
1437
 
1230
1438
        def open_file(self, widget, data=None):
1231
1439
                global library
1232
1440
                global title
1233
1441
                global path
1234
1442
                global currentfile
 
1443
                global currentselect
1235
1444
                global selectors
1236
1445
                global selectid
 
1446
                global objects
 
1447
                global level
1237
1448
                #run the open dialog
1238
1449
                opener = OpenDialog.NewOpenDialog()
1239
1450
                result = opener.run()
1252
1463
                        for item in tar:
1253
1464
                                tar.extract(item, "/tmp/")
1254
1465
                        os.system ("rm "+ren)
1255
 
                        print 'Done.'
1256
1466
                except:
1257
1467
                        print "Error opening file."
1258
1468
                        alert("Error opening file!")
1263
1473
                for i in fc:
1264
1474
                        currentfilebase.append(i)
1265
1475
                currentfile = eval (currentfilebase[0])
 
1476
                objects = currentfile["objects"]
 
1477
                objects["root"] = root()
 
1478
                objects["root"].frames = currentfile["frames"]
 
1479
                for i in range(1, len(currentfilebase)):
 
1480
                        obtemp = eval (currentfilebase[i])
 
1481
                        currentfile["objects"][obtemp.name] = obtemp
 
1482
                currentselect = ["root", None]
 
1483
                for i in currentfile["colors"]["images"]:
 
1484
                        currentfile["colors"]["images"][i] = "/tmp/"+currentfile["colors"]["images"][i]
 
1485
                level = 0
1266
1486
                fc.close()
1267
1487
                self.set_title("Swift - "+title+".swift")
1268
1488
                selectid=["#000000"]
1269
1489
                selectors=[None]
1270
1490
                for i in currentfile["objects"]:
1271
1491
                        self.AddLibItem([None, i, 1])
1272
 
                        if currentfile["objects"][i]["type"]=="box":
 
1492
                        if currentfile["objects"][i].type=="box":
1273
1493
                                #Ugly expression meaning add 1 to highest hex value
1274
1494
                                selectid.append("#01"+("%X" % (int(selectid[-1][3:7], 16)+1)).zfill(4))
1275
1495
                                selectors.append(i)
1276
 
                        elif currentfile["objects"][i]["type"]=="ellipse":
 
1496
                        elif currentfile["objects"][i].type=="ellipse":
1277
1497
                                selectid.append("#02"+("%X" % (int(selectid[-1][3:7], 16)+1)).zfill(4))
1278
1498
                                selectors.append(i)
1279
 
                        elif currentfile["objects"][i]["type"]=="curve":
 
1499
                        elif currentfile["objects"][i].type=="curve":
1280
1500
                                selectid.append("#03"+("%X" % (int(selectid[-1][3:7], 16)+1)).zfill(4))
1281
1501
                                selectors.append(i)
1282
1502
                                
1299
1519
 
1300
1520
 
1301
1521
                '''
1302
 
                #TODO:Finish open .sc script
1303
 
                global library
1304
 
                global title
1305
 
                global path
1306
 
                #run the open dialog
1307
 
                opener = OpenscDialog.NewOpenscDialog()
1308
 
                result = opener.run()
1309
 
 
1310
 
                filename = opener.get_filename()
1311
 
 
1312
 
                patharray = filename.split("/")
1313
 
                title = patharray.pop()
1314
 
                titlearray = title.split(".")
1315
 
                title = titlearray[0]
1316
 
                path = ""
1317
 
                for i in patharray:
1318
 
                        path = path+i+"/"
1319
 
                
1320
 
                fc = open(filename, "r")
1321
 
                global currentfile
1322
 
                global currentfilo
1323
 
                currentfilo = []
1324
 
                currentfile = {"fileinfo":{}, "colors":{"gradients":{}, "textures":{}, "images":{}}, "objects":{}, "sprites":{}, "buttons":{}, "frames":[[]]}
1325
 
                for i in fc:
1326
 
                        currentfilo.append(i)
1327
 
                currenttext = ""
1328
 
                library={}
1329
 
 
1330
 
                def convtext(a):
1331
 
                        b=a.split()
1332
 
                        return (conv (splitlib (b, 0)[0]))
1333
 
 
1334
 
                def splitlib(a, j):
1335
 
                        q=[]
1336
 
                        f=[]
1337
 
                        fflag=False
1338
 
                        for i in range (j, len(a)):
1339
 
                                global framelist
1340
 
                                try:
1341
 
                                        c
1342
 
                                except NameError:
1343
 
                                        c = None
1344
 
                                if c == None or not c[1]>i:
1345
 
                                        if not a[i] == ".end":
1346
 
                                                if a[i]==".gradient" or a[i]==".outline" or a[i]==".sprite" or a[i]==".startclip" or a[i]==".button" or a[i]==".action:":
1347
 
                                                        c=splitlib(a, i+1)
1348
 
                                                        q.append(a[i])
1349
 
                                                        q.append(c[0])
1350
 
                                                elif a[i]==".frame":
1351
 
                                                        if fflag==True:
1352
 
                                                                for k in range(int(a[i+1])+1):
1353
 
                                                                        framelist.append([])
1354
 
                                                                framelist[int(a[i+1])]=f
1355
 
                                                        f=[]
1356
 
                                                        fflag=True
1357
 
                                                elif fflag==True:
1358
 
                                                        f.append(a[i])
1359
 
                                                else:
1360
 
                                                        q.append(a[i])
1361
 
                                        else:
1362
 
                                                q.append(".end")
1363
 
                                                return q, i+1
1364
 
 
1365
 
                def conv(b):
1366
 
                        for i in b:
1367
 
                                print type(i).__name__+"  "+str(i)
1368
 
                        print "______________________________________________________________________"
1369
 
                        print ""
1370
 
                        j=-1
1371
 
                        c={}
1372
 
                        q=[]
1373
 
                        buf=[]
1374
 
                        for i in range (len(b)):
1375
 
                                        print b[i]
1376
 
                                        if not type(b[i]).__name__=="list" and b[i].startswith("."):
1377
 
                                                if b[i] == ".box":
1378
 
                                                        if j>-1:
1379
 
                                                                c[q[j]]=buf
1380
 
                                                        q.append(b[i+1])
1381
 
                                                        j+=1
1382
 
                                                        print b[i]
1383
 
                                                        buf=[".box"]
1384
 
                                                else:
1385
 
                                                        if j>-1:
1386
 
                                                                c[q[j]]=buf
1387
 
                                                        q.append(b[i])
1388
 
                                                        j+=1
1389
 
                                                        print b[i]
1390
 
                                                        buf=[]
1391
 
                                        else:
1392
 
                                                        print b[i]
1393
 
                                                        buf.append(b[i])
1394
 
                        #print "buf="+str(buf)
1395
 
                        print "c="+str(c)
1396
 
                        '''
1397
 
                '''
1398
1522
                        if j>-1:
1399
1523
                                c[q[j]]=buf
1400
1524
                        q.append(i)
1401
1525
                        j+=1
1402
1526
                        buf=[]
1403
1527
                        '''
1404
 
                '''
1405
 
                        return c
1406
 
 
1407
 
                currentfilo=convtext(join(currentfile))
1408
 
                #print currentfile
1409
 
                print currentfilo
1410
 
                stage = self.builder.get_object("drawingarea1")
1411
 
                stage.show()
1412
 
                stage.set_size_request(512, 512)
1413
 
                colormap = stage.get_colormap()
1414
 
                red = colormap.alloc_color('red', True, True)
1415
 
                stagedraw = stage.window
1416
 
                gc = stagedraw.new_gc(red)
1417
 
                def exposevent(stage, event):
1418
 
                        for i in currentfilo:
1419
 
                                if currentfilo[i][0] == ".box":
1420
 
                                        nx = 0.0
1421
 
                                        ny = 0.0
1422
 
                                        for j in currentfilo:
1423
 
                                                if j == ".put" and currentfilo[j][0] == i:
1424
 
                                                        print currentfilo[j]
1425
 
                                                        nx = int(currentfilo[j][2].split("=")[1])
1426
 
                                                        ny = int(currentfilo[j][3].split("=")[1])
1427
 
                                                        break
1428
 
                                        colo = colormap.alloc_color(currentfilo[i][4].split("=")[1], True, True)
1429
 
                                        gc.set_foreground(colo)
1430
 
                                        drawBox(gc, stagedraw, nx, ny, int(currentfilo[i][2]), int(currentfilo[i][3]))
1431
 
                stage.connect("expose-event", exposevent)
1432
 
                #objname=self.builder.get_object("textview3").get_buffer()
1433
 
                for i in currentfilo:
1434
 
                        if currentfilo[i][0] == ".box":
1435
 
                                entry=i
1436
 
                                image = gtk.Image()
1437
 
                                image.set_from_file("../media/icon.png")
1438
 
                                image.show()
1439
 
                                self.AddLibItem([image,  entry, "1"])
1440
 
                                #objname.set_text(entry)
1441
 
                buff = self.builder.get_object("textview1").get_buffer()
1442
 
                buff.set_text(str(currentfile))
1443
 
                fc.close()
1444
 
                self.set_title("Swift - "+title+".swift")
1445
 
                #close the dialog, and check whether to proceed
1446
 
                opener.destroy()
1447
 
                if result != gtk.RESPONSE_OK:
1448
 
                        return
1449
 
 
1450
 
                #get the record from CouchDB and extract the text
1451
 
                #if rec_id == None:
1452
 
                #       return
1453
 
                #record = self.database.get_record(rec_id)
1454
 
                #text = record["text"]
1455
 
                '''
 
1528
 
1456
1529
 
1457
1530
        def new_file(self, widget=None, data=None):
1458
1531
                global currentfile
1488
1561
                buff.set_text(str(currentfile))
1489
1562
                libbuff = self.builder.get_object("libview")
1490
1563
                self.set_title("Swift - "+title+".swift")
1491
 
                print "New file"
1492
 
                
1493
1564
                currentcolor = [1, 0, 0]
1494
1565
                biter = 0
1495
1566
                eiter = 0
1521
1592
                
1522
1593
 
1523
1594
        def run_file(self, widget, data=None):
1524
 
                print outputwin
1525
1595
                global currentfile
1526
1596
                global path
1527
1597
                global title
1557
1627
                output = output+".flash filename=\""+title+".swf\" fps="+str(currentfile["fileinfo"]["fps"])+" bbox="+str(currentfile["fileinfo"]["bbox"][0])+"x"+str(currentfile["fileinfo"]["bbox"][1])+"x"+str(currentfile["fileinfo"]["bbox"][2])+"x"+str(currentfile["fileinfo"]["bbox"][1])+"\n"
1558
1628
                for i in currentfile["colors"]["images"]:
1559
1629
                        ex = currentfile["colors"]["images"][i].split(".")
1560
 
                        print ex
1561
1630
                        if ex[1]=="png":
1562
1631
                                output = output+".png "+i+" \""+currentfile["colors"]["images"][i]+"\"\n"
1563
1632
                        else:
1590
1659
                                        fill = str(ob.fill)
1591
1660
                                output = output+".filled "+i+" outline="+i+"_outline fill="+fill+"\n"
1592
1661
                        elif ob.type=="button":
1593
 
                                alert("GOT TO MAKE CAIRO RENDER THIS TO A TEXTURE!!!!!!!!!!!!!!!")
1594
1662
                                idlefile = open("/tmp/idlefile"+i+".png", "w")
1595
1663
                                hoverfile = open("/tmp/hoverfile"+i+".png", "w")
1596
1664
                                pressedfile = open("/tmp/pressedfile"+i+".png", "w")
1597
1665
                                hitfile = open("/tmp/hitfile"+i+".png", "w")
1598
 
                                idlesurf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 512, 512)
1599
 
                                hoversurf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 512, 512)
1600
 
                                pressedsurf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 512, 512)
1601
 
                                hitsurf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 512, 512)
 
1666
                                idlesurf = cairo.ImageSurface(cairo.FORMAT_ARGB32, ob.width, ob.height)
 
1667
                                hoversurf = cairo.ImageSurface(cairo.FORMAT_ARGB32, ob.width, ob.height)
 
1668
                                pressedsurf = cairo.ImageSurface(cairo.FORMAT_ARGB32, ob.width, ob.height)
 
1669
                                hitsurf = cairo.ImageSurface(cairo.FORMAT_ARGB32, ob.width, ob.height)
1602
1670
                                idlecr = cairo.Context(idlesurf)
1603
1671
                                hovercr = cairo.Context(hoversurf)
1604
1672
                                pressedcr = cairo.Context(pressedsurf)
1605
1673
                                hitcr = cairo.Context(hitsurf)
1606
 
                                idle = currentfile["objects"][i].frames["idle"]
1607
 
                                if not "hover" in currentfile["objects"][i].frames:
1608
 
                                        hover = currentfile["objects"][i].frames["idle"]
1609
 
                                else:
1610
 
                                        currentfile["objects"][i].frames["hover"]
1611
 
                                if not "pressed" in currentfile["objects"][i].frames:
1612
 
                                        pressed = currentfile["objects"][i].frames["idle"]
1613
 
                                else:
1614
 
                                        pressed = currentfile["objects"][i].frames["pressed"]
1615
 
                                if not "hit" in currentfile["objects"][i].frames:
1616
 
                                        hit = currentfile["objects"][i].frames["idle"]
1617
 
                                else:
1618
 
                                        hit = currentfile["objects"][i].frames["hit"]
 
1674
                                idlecr.translate(-ob.x, -ob.y)
 
1675
                                hovercr.translate(-ob.x, -ob.y)
 
1676
                                pressedcr.translate(-ob.x, -ob.y)
 
1677
                                hitcr.translate(-ob.x, -ob.y)
 
1678
                                idle = ob.frames[ob.frames[0].index("idle")+1]
 
1679
                                if not "hover" in ob.frames[0]:
 
1680
                                        hover = ob.frames[ob.frames[0].index("idle")+1]
 
1681
                                else:
 
1682
                                        hover = ob.frames[ob.frames[0].index("hover")+1]
 
1683
                                if not "pressed" in ob.frames[0]:
 
1684
                                        pressed = ob.frames[ob.frames[0].index("idle")+1]
 
1685
                                else:
 
1686
                                        pressed = ob.frames[ob.frames[0].index("pressed")+1]
 
1687
                                if not "hit" in ob.frames[0]:
 
1688
                                        hit = ob.frames[ob.frames[0].index("idle")+1]
 
1689
                                else:
 
1690
                                        hit = ob.frames[ob.frames[0].index("hit")+1]
1619
1691
                                for j in idle:
1620
 
                                        print idle[j]
1621
 
                                        idle[j][j].draw(idle[j]["x"], idle[j]["y"], idle[j]["rotate"], idlecr)
 
1692
                                        idle[j][j].draw(idlecr, None, None)
1622
1693
                                for j in hover:
1623
 
                                        hover[j][j].draw(hover[j]["x"], hover[j]["y"], hover[j]["rotate"], hovercr)
 
1694
                                        hover[j][j].draw(hovercr, None, None)
1624
1695
                                for j in pressed:
1625
 
                                        pressed[j][j].draw(pressed[j]["x"], pressed[j]["y"], pressed[j]["rotate"], pressedcr)
 
1696
                                        pressed[j][j].draw(pressedcr, None, None)
1626
1697
                                for j in hit:
1627
 
                                        hit[j][j].draw(hit[j]["x"], hit[j]["y"], hit[j]["rotate"], hitcr)
 
1698
                                        hit[j][j].draw(hitcr, None, None)
1628
1699
                                
1629
1700
                                idlesurf.write_to_png(idlefile)
1630
1701
                                hoversurf.write_to_png(hoverfile)
1686
1757
                def on_output_close(dummy, dummy1):
1687
1758
                        global outputwin
1688
1759
                        outputwin = False
1689
 
                        print "outputwin = "+str(outputwin)
1690
1760
                def read_output(view, buffer, command):
1691
1761
                        global err
1692
1762
                        print "err: "+ err
1693
1763
                        #stdin, stdouterr = os.popen4(command)
1694
1764
                        p = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
1695
1765
                        (stdin, stdouterr) = (p.stdin, p.stdout)
 
1766
                        tracing = False
1696
1767
                        while 1:
1697
1768
                                line = stdouterr.readline()
1698
1769
                                if not line:
1701
1772
                                        line = ""
1702
1773
                                else:
1703
1774
                                        line = line+"%"
1704
 
                                        line = line[25:-1]
 
1775
                                        line = line[0:-1]
 
1776
                                        if "TRACE: " in line:
 
1777
                                                line = line[line.index("TRACE: ")+7:-1]+"\n"
 
1778
                                                if tracing==False:
 
1779
                                                        line = "\n\n\n ----- BEGIN TRACE ----- \n\n\n"+line
 
1780
                                                        tracing=True
 
1781
                                        elif "SECURITY: " in line:
 
1782
                                                line = line[line.index("SECURITY: ")+10:-1]+"\n\n"
 
1783
                                        elif "UNIMPLEMENTED: " in line:
 
1784
                                                line = line[line.index("UNIMPLEMENTED: "):-1]
1705
1785
                                if not err == "":
1706
1786
                                        line = err+"\n"+line
1707
1787
                                        err = ""
1718
1798
                        outbox = gtk.TextView()
1719
1799
                        outbox.set_editable(False)
1720
1800
                        outbox.set_cursor_visible(False)
 
1801
                        outbox.set_wrap_mode(gtk.WRAP_WORD_CHAR)
1721
1802
                        outbuffer = outbox.get_buffer()
1722
1803
                        sw.add(outbox)
1723
1804
                        win = gtk.Window()
1735
1816
                thr = threading.Thread(target= read_output, args=(outbox, outbuffer, "gnash -v "+path+title+".swf"))
1736
1817
                thr.start()
1737
1818
 
 
1819
        def open_sc_file(self, widget, data=None):
 
1820
                alert("Not implemented yet.")
1738
1821
 
1739
1822
        def import_file(self, widget, data=None):
1740
1823
                global title
1741
1824
                global path
1742
1825
                global currentfile
 
1826
                global objects
1743
1827
                #run the open dialog
1744
1828
                importer = ImportDialog.NewImportDialog()
1745
1829
                result = importer.run()
1753
1837
                image.set_from_file("../media/icon.png")
1754
1838
                image.show()
1755
1839
                self.AddLibItem([image,  entry, "1"])
1756
 
                print filename
1757
 
                print currentfile
1758
1840
                importer.destroy()
 
1841
                return filename
1759
1842
        def import_file_to_stage(self, widget, data=None):
1760
 
                self.import_file(widget, data)
 
1843
                stage = self.builder.get_object("drawingarea1")
 
1844
                filename = self.import_file(widget, data)
 
1845
                try:
 
1846
                        im = Image.open(filename)
 
1847
                except IOError:
 
1848
                        alert(filename+" is not a valid image file!")
 
1849
                else:
 
1850
                        size = im.size
 
1851
                        newname = filename.split("/")[-1].split(".")[0]
 
1852
                        if not newname in objects:
 
1853
                                # CHANGE THE PARENT TO BE THE APPROPRIATE OBJECT
 
1854
                                objects[newname] = box(newname, size[0], size[1], "root", newname)
 
1855
                                for i in range(1, len(objects[objects[newname].parent].frames)):
 
1856
                                        objects[objects[newname].parent].frames[i]["sort"].append(newname)
 
1857
                                        objects[objects[newname].parent].frames[i][newname] = {"x":0, "y":0, "rotate":0}
 
1858
                                        if i==1:
 
1859
                                                objects[objects[newname].parent].frames[i][newname]["put"] = True
 
1860
                                selectid.append("#01"+("%X" % (int(selectid[-1][3:7], 16)+1)).zfill(4))
 
1861
                                selectors.append(newname)
 
1862
                                expostagevent(stage, "expose-event")
 
1863
                        else:
 
1864
                                alert("There is already an object called "+newname+" in the current project. I will just abandon this attempt because the alert window only has one button.")
1761
1865
 
1762
1866
        def draw_rect(self, widget, data=None):
1763
1867
                global mode
1789
1893
                
1790
1894
        def stage_action(self, widget, data=None):
1791
1895
                global clicked
1792
 
                global drawitems
1793
 
                global startmouse
1794
1896
                global mode
1795
1897
                if clicked == True or mode=="c":
1796
1898
                        stage = self.builder.get_object("drawingarea1")
1797
 
                        #stage.show()
1798
 
                        #stage.set_size_request(512, 512)
1799
 
                        #colormap = stage.get_colormap()
1800
 
                        #red = colormap.alloc_color('red', True, True)
1801
 
                        #stagedraw = stage.window
1802
 
                        #gc = stagedraw.new_gc(red)
1803
 
                        #ptr = stage.get_pointer()
1804
 
                        #stage.connect("expose-event", expostagevent)
1805
1899
                        expostagevent(stage, "expose-event")
1806
1900
 
1807
1901
        def stage_click(self, widget, data=None):
1822
1916
                        clicked=True
1823
1917
                        first=True
1824
1918
                        startmouse = self.builder.get_object("drawingarea1").get_pointer()
1825
 
                        print "setting"
1826
1919
                        undostack.append(copy.deepcopy(currentfile))
1827
1920
                        if mode=="c":
1828
1921
                                cc=(startmouse[0], startmouse[1])
1829
1922
                                currentcurve.append(cc)
1830
1923
                                expostagevent(self.builder.get_object("drawingarea1"), "exposevent")
1831
1924
                        elif mode==" ":
1832
 
                                print startmouse
1833
1925
                                checktest = False
1834
1926
                                '''for i in currentfile["frames"][currentfile["frames"][0].index(currentframe)+1]["sort"]:
1835
1927
                                        if currentfile["objects"][i]["type"]=="box" or currentfile["objects"][i]["type"]=="ellipse":
1845
1937
                                        return tuple(o_gdk_pixbuf.get_pixels_array().tolist()[0][0])
1846
1938
                                pixcol = get_pixel_color(startmouse[0], startmouse[1])
1847
1939
                                rgbpixcol = ("#"+str(pixcol[0]).zfill(2)+str(pixcol[1]).zfill(2)+str(pixcol[2]).zfill(2))
 
1940
                                print rgbpixcol
1848
1941
                                currentselect[level+1] = selectors[selectid.index(rgbpixcol)]
1849
1942
                                if doubleclick==True:
1850
1943
                                        if not currentselect[level+1]==None:
1851
1944
                                                if currentselect[level+1] in currentfile["objects"] and currentfile["objects"][currentselect[level]+1].type == "button":
1852
 
                                                        print "__________________DBLCLK_____________________"
1853
1945
                                                        level+=1
1854
1946
                                                        if len(currentselect)-1<level:
1855
1947
                                                                currentselect.append(None)
1856
1948
                                                        else:
1857
1949
                                                                currentselect[level+1]=None
1858
 
                                                        print "CURRENTSELECT", currentselect
1859
1950
                                                        currentframe.append(1)
1860
 
                                                        print "Double-clicked!"
1861
1951
                                expostagevent(self.builder.get_object("drawingarea1"), "exposevent")
 
1952
                                self.builder.get_object("drawingarea1").grab_focus()
1862
1953
                        
1863
1954
        def stage_unclick(self, widget, data=None):
1864
1955
                global clicked
1890
1981
                                if type(currentcolor)==type([]):
1891
1982
                                        newcolor = "rgb"+rgb2hex(currentcolor[0], currentcolor[1], currentcolor[2])[1:]
1892
1983
                                        currentfile["colors"]["textures"][newcolor]={"type":"color", "fill":[currentcolor[0], currentcolor[1], currentcolor[2]]}
1893
 
                                        print currentfile["colors"]["textures"][newcolor]
1894
1984
                                        currentfile["objects"]["b"+str(biter)]=box("b"+str(biter), w, h, currentselect[level], newcolor)
1895
1985
                                else:
1896
1986
                                        currentfile["objects"]["b"+str(biter)]=box("b"+str(biter), w, h, currentselect[level], currentcolor)
1897
1987
                                selectid.append("#01"+("%X" % (int(selectid[-1][3:7], 16)+1)).zfill(4))
1898
1988
                                selectors.append("b"+str(biter))
1899
 
                                print selectors
1900
1989
                                for i in range(currentfile["frames"][0].index(currentframe[level]), len(currentfile["frames"][0])):
1901
1990
                                        currentfile["frames"][i+1]["b"+str(biter)]={"put":True, "x":x, "y":y, "rotate":0}
1902
1991
                                        currentfile["frames"][i+1]["sort"].append("b"+str(biter))
1915
2004
                                        y = stage.get_pointer()[1]
1916
2005
                                        h = startmouse[1]-y
1917
2006
                                if type(currentcolor)==type([]):
1918
 
                                        currentfile["objects"]["e"+str(eiter)]={"type":"ellipse", "width":w, "height":h, "fill":rgb2hex(currentcolor[0], currentcolor[1], currentcolor[2])}
 
2007
                                        currentfile["objects"]["e"+str(eiter)]=ellipse("e"+str(eiter), w, h, currentselect[level], rgb2hex(currentcolor[0], currentcolor[1], currentcolor[2]))
1919
2008
                                else:
1920
 
                                        currentfile["objects"]["e"+str(eiter)]={"type":"ellipse", "width":w, "height":h, "fill":currentcolor}
 
2009
                                        currentfile["objects"]["e"+str(eiter)]=ellipse("e"+str(eiter), w, h, currentselect[level], currentcolor)
1921
2010
                                
1922
2011
                                selectid.append("#02"+("%X" % (int(selectid[-1][3:7], 16)+1)).zfill(4))
1923
2012
                                selectors.append("e"+str(eiter))
2013
2102
                global currentframe
2014
2103
                global currentselect
2015
2104
                global level
2016
 
                if currentframe[level] in currentfile["frames"][0]:
2017
 
                        if currentselect[level] in currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1]:
2018
 
                                del currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1][currentselect[level]]
2019
 
                                del currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1]["sort"][currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1]["sort"].index(currentselect)]
2020
 
                                currentselect[level]=None
 
2105
                if currentframe[level+1] in currentfile["frames"][0]:
 
2106
                        if currentselect[level+1] in currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1]:
 
2107
                                del currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1][currentselect[level+1]]
 
2108
                                del currentfile["frames"][currentfile["frames"][0].index(currentframe[level])+1]["sort"][currentfile["frames"][currentfile["frames"][0].index(currentframe[level+1])+1]["sort"].index(currentselect[level+1])]
 
2109
                                currentselect[level+1]=None
2021
2110
                                expostagevent(self.builder.get_object("drawingarea1"), "expose-event")
2022
2111
        def send_to_back(self, widget, data=None):
2023
2112
                global currentselect
2026
2115
                if not currentselect[level]==None:
2027
2116
                        for j in range (len(currentfile["frames"])-1):
2028
2117
                                s = currentfile["frames"][j+1]["sort"]
2029
 
                                i = currentselect[level]
 
2118
                                i = currentselect[level+1]
2030
2119
                                del s[s.index(i)]
2031
2120
                                prePend(s, i)
2032
2121
                        expostagevent(self.builder.get_object("drawingarea1"), "expose-event")
2038
2127
                if not currentselect[level]==None:
2039
2128
                        for j in range (len(currentfile["frames"])-1):
2040
2129
                                s = currentfile["frames"][j+1]["sort"]
2041
 
                                i = currentselect[level]
 
2130
                                i = currentselect[level+1]
2042
2131
                                k = s.index(i)
2043
2132
                                del s[k]
2044
2133
                                if k<1:
2053
2142
                if not currentselect[level]==None:
2054
2143
                        for j in range (len(currentfile["frames"])-1):
2055
2144
                                s = currentfile["frames"][j+1]["sort"]
2056
 
                                i = currentselect[level]
 
2145
                                i = currentselect[level+1]
2057
2146
                                k = s.index(i)
2058
2147
                                del s[k]
2059
2148
                                if k>len(s):
2067
2156
                if not currentselect[level]==None:
2068
2157
                        for j in range (len(currentfile["frames"])-1):
2069
2158
                                s = currentfile["frames"][j+1]["sort"]
2070
 
                                i = currentselect[level]
 
2159
                                i = currentselect[level+1]
2071
2160
                                del s[s.index(i)]
2072
2161
                                s.append(i)
2073
2162
                        expostagevent(self.builder.get_object("drawingarea1"), "expose-event")
2084
2173
                        cr.set_source_rgb(0,0,0)
2085
2174
                        cr.paint()
2086
2175
                        carray = colors.colorArray()
2087
 
                        print carray
2088
2176
                        for i in range(len(carray)):
2089
2177
                                for j in range(len(carray[i])):
2090
2178
                                        drawcrBox(cr, i*15, j*15, 14, 14, 1, carray[i][j], 0, [1, 1, 1, 0])
2096
2184
                                        cr.translate(q*15, 180)
2097
2185
                                        cr.scale(15.0/surface.get_width(), 15.0/surface.get_height())
2098
2186
                                        q=q+1
2099
 
                                        print "q="+str(q)
2100
2187
                                        pattern = cairo.SurfacePattern(surface)
2101
2188
                                        cr.set_source(pattern)
2102
2189
                                        cr.paint()
2103
2190
                                        cr.restore()
2104
 
                        print "Select a color"
2105
 
                def button_press_event(widget, event):
 
2191
                def button_release_event(widget, event):
2106
2192
                        global currentcolor
2107
2193
                        mouse = drawing_area.get_pointer()
2108
2194
                        carray = colors.colorArray()
2109
 
                        print "A button was clicked at "+str(mouse[1]/15)
2110
 
                        #print "The color there is "+str(carray[int(mouse[0]/15)][int(mouse[1]/15)])
2111
2195
                        if int(mouse[1]/15)<12:
2112
2196
                                currentcolor = carray[int(mouse[0]/15)][int(mouse[1]/15)]
2113
2197
                        else:
2122
2206
                                                        z+=1
2123
2207
                        color=self.builder.get_object("fillcolor")
2124
2208
                        exposecolorevent(color, "expose-event")
2125
 
                        print currentcolor
2126
2209
                        cwindow.destroy()
2127
2210
                        return True
2128
2211
                cwindow.set_events(gtk.gdk.ALL_EVENTS_MASK);
2129
2212
                cwindow.connect("focus_out_event", focus_out_event)
2130
 
                cwindow.connect("button_press_event", button_press_event)
 
2213
                cwindow.connect("button_release_event", button_release_event)
2131
2214
                cwindow.set_decorated(False)
 
2215
                cwindow.grab_focus()
2132
2216
                cwindow.set_position(gtk.WIN_POS_MOUSE)
2133
2217
                cwindow.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_MENU)
2134
2218
                cwindow.set_resizable(False)
2135
2219
                cwindow.set_deletable(False)
 
2220
                cwindow.set_modal(True)
2136
2221
                cwindow.set_property('skip-taskbar-hint',True)
2137
2222
                cwindow.set_default_size(315, 230)
2138
2223
                cwindow.set_size_request(315, 230)
2152
2237
                drawing_area.set_size_request(315, 150)
2153
2238
                vbox.pack_start(drawing_area, True, True, 0)
2154
2239
                drawing_area.show()
 
2240
                drawing_area.grab_focus()
2155
2241
                drawing_area.connect("expose_event", expose_event)
2156
2242
                drawing_area.set_events(gtk.gdk.EXPOSURE_MASK
2157
2243
                                                        | gtk.gdk.LEAVE_NOTIFY_MASK
2166
2252
                ftextbuf = textview.get_buffer()
2167
2253
                undostack.append(copy.deepcopy(currentfile))
2168
2254
                if not (currentframe[level] in currentfile["frames"][0]):
2169
 
                        print "Gonna add a frame here."
2170
2255
                        framenum=0
2171
2256
                        for i in currentfile["frames"][0]:
2172
2257
                                if i<currentframe[level]:
2174
2259
                        currentfile["frames"][0].insert(framenum, currentframe[level])
2175
2260
                        currentfile["frames"].insert(framenum+1, {})
2176
2261
                        currentfile["actions"].insert(framenum, "")
2177
 
                        print framenum
2178
2262
                        for i in currentfile["frames"][framenum]:
2179
2263
                                q=eval(str(currentfile["frames"][framenum][i]))
2180
2264
                                currentfile["frames"][framenum+1][i]=q
2181
2265
                                q=False
2182
2266
                        for i in currentfile["frames"][framenum+1]["sort"]:
2183
2267
                                currentfile["frames"][framenum+1][i]["put"]=False
2184
 
                        print currentfile["frames"]
2185
2268
                ftextbuf.set_text(currentfile["actions"][currentfile["frames"][0].index(currentframe[level])])
2186
2269
                textview.set_editable(True)
2187
2270
                textview.set_cursor_visible(True)
2204
2287
                                if not name in objects:
2205
2288
                                        x = objects["root"].frames[objects["root"].frames[0].index(currentframe[level])+1][currentselect[level+1]]["x"]
2206
2289
                                        y = objects["root"].frames[objects["root"].frames[0].index(currentframe[level])+1][currentselect[level+1]]["y"]
 
2290
                                        rotate = objects["root"].frames[objects["root"].frames[0].index(currentframe[level])+1][currentselect[level+1]]["rotate"]
2207
2291
                                        currentfile["objects"][name]=button(name)
2208
2292
                                        objects[name].frames[objects[name].frames[0].index("idle")+1] = {currentselect[level+1]:{currentselect[level+1]:currentfile["objects"][currentselect[level+1]], "x":0, "y":0, "rotate":0}}
2209
2293
                                        objects[currentselect[level+1]].level=objects[currentselect[level+1]].level+1
2210
2294
                                        objects[currentselect[level+1]].parent=name
 
2295
                                        objects[name].width = objects[currentselect[level+1]].width
 
2296
                                        objects[name].height = objects[currentselect[level+1]].height
 
2297
                                        objects[name].x = x
 
2298
                                        objects[name].y = y
 
2299
                                        objects[name].rotate = rotate
2211
2300
                                        for i in range(len(currentfile["frames"][0])):
2212
2301
                                                if currentselect[level+1] in currentfile["frames"][i+1]:
2213
2302
                                                        del currentfile["frames"][i+1][currentselect[level+1]]
2217
2306
                                        selectid.append("#05"+("%X" % (int(selectid[-1][3:7], 16)+1)).zfill(4))
2218
2307
                                        selectors.append(name)
2219
2308
                                        dialog.destroy()
2220
 
                                        currentselect[level]=name
 
2309
                                        currentselect[level+1]=name
2221
2310
                                        expostagevent(self.builder.get_object("drawingarea1"), "expose-event")
2222
2311
                                        image = gtk.Image()
2223
2312
                                        image.set_from_file("../media/icon.png")
2224
2313
                                        image.show()
 
2314
                                        libarea = self.builder.get_object("libarea")
 
2315
                                        expostagevent(libarea, "expose-event")
2225
2316
                                        self.AddLibItem([image, name, "1"])
2226
2317
                                else:
2227
2318
                                        alert("The name "+name+" is already taken. Please use a different name.")
2295
2386
                except:
2296
2387
                        alert("Nothing to redo!")
2297
2388
        def export_swf(self, widget, data=None):
2298
 
                #alert("Not implemented yet!")
2299
 
                
2300
 
                print outputwin
2301
2389
                global output
2302
2390
                global currentfile
2303
2391
                global path
2311
2399
                output = output+".flash filename=\""+title+".swf\" fps="+str(currentfile["fileinfo"]["fps"])+" bbox="+str(currentfile["fileinfo"]["bbox"][0])+"x"+str(currentfile["fileinfo"]["bbox"][1])+"x"+str(currentfile["fileinfo"]["bbox"][2])+"x"+str(currentfile["fileinfo"]["bbox"][1])+"\n"
2312
2400
                for i in currentfile["colors"]["images"]:
2313
2401
                        ex = currentfile["colors"]["images"][i].split(".")
2314
 
                        print ex
2315
2402
                        if ex[1]=="png":
2316
2403
                                output = output+".png "+i+" \""+currentfile["colors"]["images"][i]+"\"\n"
2317
2404
                        else:
2458
2545
                                if currentfile["colors"]["textures"][i]["type"]=="image":
2459
2546
                                        bar.set_text("Packing "+currentfile["colors"]["images"][currentfile["colors"]["textures"][i]["fill"]])
2460
2547
                                        imgtype = currentfile["colors"]["images"][currentfile["colors"]["textures"][i]["fill"]][-3:]
2461
 
                                        print imgtype
2462
2548
                                        html5=html5+"var "+i+"_img = new Image();\n"+i+"_img.src=\""+DataURI.img2data(currentfile["colors"]["images"][currentfile["colors"]["textures"][i]["fill"]], imgtype)+"\"\n"
2463
2549
                                        #html5=html5+currentfile["colors"]["images"][currentfile["colors"]["textures"][i]["fill"]].split("/")[-1]+" = "+i+"_src;\nvar "+i+" = ctx.createPattern("+i+"_img, 'repeat');\n"
2464
2550
                                        imcount=imcount+1
2489
2575
                fc.flush()
2490
2576
                fc.close()
2491
2577
        def export_image(self, widget, data=None):
2492
 
                alert("Image export is not implemented yet. Wait for the beta!")
 
2578
                global currentframe
 
2579
                global currentfile
 
2580
                global level
 
2581
                savedial = SaveDialog.NewSaveDialog()
 
2582
                result = savedial.run()
 
2583
                filename = savedial.get_filename()
 
2584
                savedial.destroy()
 
2585
                whatodo = alert_list("What do do want to do?", ["Export current frame as PNG", "Pi!", "Export all frames as one PNG", "Export all frames as PNG sequence"])
 
2586
                lastcf = copy.deepcopy(currentframe)
 
2587
                lastlevel = level
 
2588
                level = 0
 
2589
                if filename.split(".")[-1]=="png":
 
2590
                        if whatodo==0:
 
2591
                                png = open(filename, "w")
 
2592
                                surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 512, 512)
 
2593
                                cr = cairo.Context(surf)
 
2594
                                currentfile["objects"]["root"].draw(cr, 512, 512)
 
2595
                                surf.write_to_png(png)
 
2596
                                png.flush()
 
2597
                                png.close()
 
2598
                        elif whatodo==2:
 
2599
                                png = open(filename, "w")
 
2600
                                surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 512, 512)
 
2601
                                cr = cairo.Context(surf)
 
2602
                                for i in range(currentfile["objects"]["root"].frames[0][-1]):
 
2603
                                        currentframe[0] = i
 
2604
                                        currentfile["objects"]["root"].draw(cr, 512, 512)
 
2605
                                surf.write_to_png(png)
 
2606
                                png.flush()
 
2607
                                png.close()
 
2608
                        elif whatodo == 3:
 
2609
                                for i in range(currentfile["objects"]["root"].frames[0][-1]):
 
2610
                                        filename1 = filename.split(".")
 
2611
                                        filename1 = filename1[0]+str(i).zfill(3)+filename1[1]
 
2612
                                        png = open(filename1, "w")
 
2613
                                        surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 512, 512)
 
2614
                                        cr = cairo.Context(surf)
 
2615
                                        currentframe[0] = i
 
2616
                                        currentfile["objects"]["root"].draw(cr, 512, 512)
 
2617
                                        surf.write_to_png(png)
 
2618
                                        png.flush()
 
2619
                                        png.close()
 
2620
                level = lastlevel
 
2621
                currentframe = lastcf
2493
2622
        def export_video(self, widget, data=None):
2494
2623
                alert("Video export is not implemented yet. Wait for the beta!")
2495
2624
        def export_pdf(self, widget, data=None):
2549
2678
        def export_gif(self, widget, data=None):
2550
2679
                alert("GIF export is not implemented yet. Wait for the beta!")
2551
2680
        def export_sc(self, widget, data=None):
2552
 
                print outputwin
2553
2681
                global output
2554
2682
                global currentfile
2555
2683
                global path
2578
2706
                output = output+".flash filename=\""+title+".swf\" fps="+str(currentfile["fileinfo"]["fps"])+" bbox="+str(currentfile["fileinfo"]["bbox"][0])+"x"+str(currentfile["fileinfo"]["bbox"][1])+"x"+str(currentfile["fileinfo"]["bbox"][2])+"x"+str(currentfile["fileinfo"]["bbox"][1])+"\n"
2579
2707
                for i in currentfile["colors"]["images"]:
2580
2708
                        ex = currentfile["colors"]["images"][i].split(".")
2581
 
                        print ex
2582
2709
                        if ex[1]=="png":
2583
2710
                                output = output+".png "+i+" \""+currentfile["colors"]["images"][i]+"\"\n"
2584
2711
                        else:
2647
2774
                alert("Not complete")
2648
2775
                dialog = DocumentDialog.NewDocumentDialog()
2649
2776
                result = dialog.run()
 
2777
        def console(self, widget, data="None"):
 
2778
                Credits = "Everything © 2011 Skyler Lehmkuhl."
 
2779
                global SWIFT_ROOT
 
2780
                global consoledir
 
2781
                for i in consoledir:
 
2782
                        locals()[i] = consoledir[i]
 
2783
                def load(b):
 
2784
                        a = b.split("/")[-1].split(".")[0]
 
2785
                        if a==b:
 
2786
                                globals()[a] = __import__(b)
 
2787
                        else:
 
2788
                                globals()[a] = imp.load_source(a, b)
 
2789
                        return "Imported "+a
 
2790
                class sw:
 
2791
                        def __init__(self):
 
2792
                                global currentfile
 
2793
                                global selectid
 
2794
                                global currentcolor
 
2795
                                global currentselect
 
2796
                                self.__cf = currentfile
 
2797
                                self.__id = selectid
 
2798
                                self.__cc = currentcolor
 
2799
                                self.__cs = currentselect
 
2800
                        def addBox(self, x, y, w, h):
 
2801
                                global currentframe
 
2802
                                global biter
 
2803
                                global selectors
 
2804
                                if type(currentcolor)==type([]):
 
2805
                                        newcolor = "rgb"+rgb2hex(self.__cc[0], self.__cc[1], self.__cc[2])[1:]
 
2806
                                        self.__cf["colors"]["textures"][newcolor]={"type":"color", "fill":[self.__cc[0], self.__cc[1], self.__cc[2]]}
 
2807
                                        self.__cf["objects"]["b"+str(biter)]=box("b"+str(biter), w, h, self.__cs[level], newcolor)
 
2808
                                else:
 
2809
                                        self.__cf["objects"]["b"+str(biter)]=box("b"+str(biter), w, h, self.__cs[level], self.__cc)
 
2810
                                self.__id.append("#01"+("%X" % (int(self.__id[-1][3:7], 16)+1)).zfill(4))
 
2811
                                selectors.append("b"+str(biter))
 
2812
                                for i in range(self.__cf["frames"][0].index(currentframe[level]), len(self.__cf["frames"][0])):
 
2813
                                        self.__cf["frames"][i+1]["b"+str(biter)]={"put":True, "x":x, "y":y, "rotate":0}
 
2814
                                        self.__cf["frames"][i+1]["sort"].append("b"+str(biter))
 
2815
                                biter += 1
 
2816
                                return "Added new box."
 
2817
                        def addEllipse(self, x, y, w, h):
 
2818
                                global currentframe
 
2819
                                global eiter
 
2820
                                global selectors
 
2821
                                if type(currentcolor)==type([]):
 
2822
                                        newcolor = "rgb"+rgb2hex(self.__cc[0], self.__cc[1], self.__cc[2])[1:]
 
2823
                                        self.__cf["colors"]["textures"][newcolor]={"type":"color", "fill":[self.__cc[0], self.__cc[1], self.__cc[2]]}
 
2824
                                        self.__cf["objects"]["e"+str(eiter)]=ellipse("e"+str(eiter), w, h, self.__cs[level], newcolor)
 
2825
                                else:
 
2826
                                        self.__cf["objects"]["e"+str(eiter)]=ellipse("e"+str(eiter), w, h, self.__cs[level], self.__cc)
 
2827
                                self.__id.append("#01"+("%X" % (int(self.__id[-1][3:7], 16)+1)).zfill(4))
 
2828
                                selectors.append("e"+str(eiter))
 
2829
                                for i in range(self.__cf["frames"][0].index(currentframe[level]), len(self.__cf["frames"][0])):
 
2830
                                        self.__cf["frames"][i+1]["e"+str(eiter)]={"put":True, "x":x, "y":y, "rotate":0}
 
2831
                                        self.__cf["frames"][i+1]["sort"].append("e"+str(eiter))
 
2832
                                eiter += 1
 
2833
                                return "Added new ellipse."
 
2834
                        def redraw(self):
 
2835
                                expostagevent(window.builder.get_object("drawingarea1"), "expose-event")
 
2836
                                return "Redrawing stage."
 
2837
                if SWIFT_ROOT:
 
2838
                        global currentfile
 
2839
                        global currentframe
 
2840
                        global path
 
2841
                        global title
 
2842
                        global mode
 
2843
                        global clicked
 
2844
                        global drawitems
 
2845
                        global biter
 
2846
                        global eiter
 
2847
                        global citer
 
2848
                        global fiter
 
2849
                        global step
 
2850
                        global currentcurve
 
2851
                        global currentselect
 
2852
                        global currentcolor
 
2853
                        global selectid
 
2854
                        global source_id
 
2855
                        global selectors
 
2856
                        global undostack
 
2857
                        global redostack
 
2858
                        global doubleclick
 
2859
                        global outputwin
 
2860
                        global level
 
2861
                        global outbox
 
2862
                        global outbuffer
 
2863
                        global objects
 
2864
                        global tnum
 
2865
                        global SWIFT_VERSION
 
2866
                        global SWIFT_COMPAT
 
2867
                        
 
2868
                        locals()["currentfile"] = currentfile
 
2869
                        locals()["currentframe"] = currentframe
 
2870
                        locals()["path"] = path
 
2871
                        locals()["title"] = title
 
2872
                        locals()["mode"] = mode
 
2873
                        locals()["clicked"] = clicked
 
2874
                        locals()["drawitems"] = drawitems
 
2875
                        locals()["biter"] = biter
 
2876
                        locals()["eiter"] = eiter
 
2877
                        locals()["citer"] = citer
 
2878
                        locals()["fiter"] = fiter
 
2879
                        locals()["step"] = step
 
2880
                        locals()["currentcurve"] = currentcurve
 
2881
                        locals()["currentselect"] = currentselect
 
2882
                        locals()["currentcolor"] = currentcolor
 
2883
                        locals()["selectid"] = selectid
 
2884
                        locals()["source_id"] = source_id
 
2885
                        locals()["selectors"] = selectors
 
2886
                        locals()["undostack"] = undostack
 
2887
                        locals()["redostack"] = redostack
 
2888
                        locals()["doubleclick"] = doubleclick
 
2889
                        locals()["outputwin"] = outputwin
 
2890
                        locals()["level"] = level
 
2891
                        locals()["outbox"] = outbox
 
2892
                        locals()["outbuffer"] = outbuffer
 
2893
                        locals()["objects"] = objects
 
2894
                        locals()["tnum"] = tnum
 
2895
                        locals()["SWIFT_VERSION"] = SWIFT_VERSION
 
2896
                        locals()["SWIFT_COMPAT"] = SWIFT_COMPAT
 
2897
                        window.builder.get_object("checkbutton1").set_visible(True)
 
2898
                        print "ROOTROOTROOTROOT"
 
2899
                        class STDOUTWRITER:
 
2900
                                        def __init__(self, window):
 
2901
                                                self.terminal = sys.stdout
 
2902
                                                self.buffer = window.builder.get_object("textview3").get_buffer()
 
2903
                                                self.type = "writer"
 
2904
                                        def write(self, message):
 
2905
                                                self.terminal.write(message)
 
2906
                                                self.buffer.set_text(self.buffer.get_text(self.buffer.get_start_iter(), self.buffer.get_end_iter())+message)
 
2907
                def help(func=None):
 
2908
                        if func==None:
 
2909
                                return "Current commands are: \"help()\", \"Credits\", \"set()\", \"load()\" and if you know what you are doing, \"swiftroot()\"\nOr type help(\"function\") to get information about that function."
 
2910
                        elif func=="Credits" or func==Credits:
 
2911
                                return "Credits is not a function. Just type Credits (no parentheses)."
 
2912
                        elif func=="set" or func=="set()":
 
2913
                                return "set() sets a variable to a value. Use it instead of an equals sign.\nYou chould enclose the variable name in quotes, i.e. set(\"foo\", 5)."
 
2914
                        elif func=="sw" or func=="sw()":
 
2915
                                return "sw is the class used to access Swift. Its usage is:\n\tsw().function(arguments)\n\nHere is a list of functions:\n\taddBox(x, y, width, height)\n\taddEllipse(x, y, width, height)\n\tredraw()"
 
2916
                        elif func=="load" or func=="load()":
 
2917
                                return "load() is the function used to load custom modules. This allows you to write scripts to do useful things in SWIFT, or just cool things like fractals.\nUseage:\n\tload(\"/path/to/module.py\")"
 
2918
                        elif func=="function":
 
2919
                                return "No, I meant you type a quote, then the name of the function, then another quote. Like this:\n\thelp(\"set\")"
 
2920
                def set(arg1, arg2):
 
2921
                        if not arg1:
 
2922
                                return "You must put quotes around the first argument!"
 
2923
                        else:
 
2924
                                if SWIFT_ROOT:
 
2925
                                        arg1 = arg2
 
2926
                                else:
 
2927
                                        consoledir[arg1] = arg2
 
2928
                                return arg2
 
2929
                def swiftroot():
 
2930
                        global SWIFT_ROOT
 
2931
                        SWIFT_ROOT = True
 
2932
                        return "Be warned, this will give you complete access to all internal variables in SWIFT. DO NOT do this unless you have read the source code (or seriously don't care about the current file!)!!!\nIf you didn't want to become root, type set(SWIFT_ROOT, False)"
 
2933
                print widget, data
 
2934
                if window.builder.get_object("checkbutton1").get_active():
 
2935
                        try:
 
2936
                                stype = sys.stdout.type
 
2937
                        except:
 
2938
                                sys.stdout = STDOUTWRITER(window)
 
2939
                        try:
 
2940
                                stype = sys.stderr.type
 
2941
                        except:
 
2942
                                sys.stderr = STDOUTWRITER(window)
 
2943
                        exec widget.get_text() in globals(), locals()
 
2944
                else:
 
2945
                        try:
 
2946
                                window.builder.get_object("textview3").get_buffer().set_text(window.builder.get_object("textview3").get_buffer().get_text(window.builder.get_object("textview3").get_buffer().get_start_iter(), window.builder.get_object("textview3").get_buffer().get_end_iter())+"\n"+str(eval(widget.get_text())))
 
2947
                        except NameError as n:
 
2948
                                for i in n:
 
2949
                                        consoledir[i[6:-16]]=None
 
2950
                                try:
 
2951
                                        window.builder.get_object("textview3").get_buffer().set_text(window.builder.get_object("textview3").get_buffer().get_text(window.builder.get_object("textview3").get_buffer().get_start_iter(), window.builder.get_object("textview3").get_buffer().get_end_iter())+"\n"+str(eval(widget.get_text())))
 
2952
                                except Exception as e:
 
2953
                                        window.builder.get_object("textview3").get_buffer().set_text(window.builder.get_object("textview3").get_buffer().get_text(window.builder.get_object("textview3").get_buffer().get_start_iter(), window.builder.get_object("textview3").get_buffer().get_end_iter())+"\n"+str(e))
 
2954
                window.builder.get_object("textview3").set_wrap_mode(gtk.WRAP_CHAR)
 
2955
                window.builder.get_object("textview3").scroll_mark_onscreen(window.builder.get_object("textview3").get_buffer().get_insert())
 
2956
                widget.set_text("")
 
2957
                
2650
2958
        
2651
2959
 
2652
2960
def NewSwiftSwfWindow():
2687
2995
        global outbuffer
2688
2996
        global objects
2689
2997
        global tnum
 
2998
        global consoledir
 
2999
        global SWIFT_VERSION
 
3000
        global SWIFT_COMPAT
 
3001
        global SWIFT_ROOT
 
3002
        #specify the current version and what version files it can still open
 
3003
        SWIFT_VERSION = "1.0-alpha4"
 
3004
        SWIFT_COMPAT = ["1.0-alpha4"]
 
3005
        #setting this to True gives you "root" access in the console
 
3006
        SWIFT_ROOT = False
 
3007
        #the list of variables in the console
 
3008
        consoledir = {}
2690
3009
        tnum = 0
2691
3010
        source_id = None
2692
3011
        #level: Depth of editing stack (0 is working at toplevel, i.e. main timeline)
2705
3024
        objects = {"root":theoneroot}
2706
3025
        b0 = box("b0", 256, 256, "root", "t2")
2707
3026
        e0 = ellipse("e0", 300, 100, "root", "turquoise")
2708
 
        currentfile = {"fileinfo":{"fps":25, "bbox":[500, 500, 0, 0]}, "colors":{"gradients":{"gr1":[[0.0, 0.0, 0.0, 1.0, 1], [0.5, 0.0, 1.0, 0.0, 1], [1.0, 1.0, 0.0, 0.0, 1]]}, "textures":{"t1":{"type":"gradient", "fill":"gr1"}, "t2":{"type":"image", "fill":"i1"}, "turquoise":{"type":"color", "fill":[0.0, 1.0, 1.0]}}, "images":{"i1":"/usr/share/swift-swf/media/icon.png"}}, "objects":{"b0":b0, "e0":e0}, "sprites":{}, "buttons":{}, "actions":[""], "frames":[[0], {"sort":["b0", "e0"], "b0":{"put":True, "x":100, "y":100, "rotate":0}, "e0":{"put":True, "x":200, "y":300, "rotate":-0.5}}]}
 
3027
        s0 = shape("s0", 280, 280, "root", "turquoise")
 
3028
        s0.shapedata = [["M", 20, 20], ["L", 300, 20], ["L", 300, 300], ["C", 20, 300, 300, 20, 20, 20]]
 
3029
        currentfile = {"fileinfo":{"fps":25, "bbox":[500, 500, 0, 0]}, "colors":{"gradients":{"gr1":[[0.0, 0.0, 0.0, 1.0, 1], [0.5, 0.0, 1.0, 0.0, 1], [1.0, 1.0, 0.0, 0.0, 1]]}, "textures":{"t1":{"type":"gradient", "fill":"gr1"}, "t2":{"type":"image", "fill":"i1"}, "turquoise":{"type":"color", "fill":[0.0, 1.0, 1.0]}}, "images":{"i1":"/usr/share/swift-swf/media/icon.png"}}, "objects":{"b0":b0, "e0":e0, "s0":s0}, "sprites":{}, "buttons":{}, "actions":[""], "frames":[[0], {"sort":["s0", "b0", "e0"], "s0":{"put":True, "x":20, "y":20, "rotate":0}, "b0":{"put":True, "x":100, "y":100, "rotate":0}, "e0":{"put":True, "x":200, "y":300, "rotate":-0.5}}]}
2709
3030
        objects = currentfile["objects"]
2710
3031
        objects["root"] = theoneroot
2711
3032
        objects["root"].frames = currentfile["frames"]
2715
3036
        clicked = False
2716
3037
        doubleclick = False
2717
3038
        selectid=["#000000", "#010101", "#020102", "#030103"]
2718
 
        selectors=[None, "b0", "e0", "c0"]
 
3039
        selectors=[None, "b0", "e0", "s0"]
2719
3040
        undostack=[]
2720
3041
        redostack=[]
2721
3042
        outputwin=False
 
3043
        outbox = None
 
3044
        outbuffer = None
 
3045
        
2722
3046
        
2723
3047
        builder = gtk.Builder()
2724
3048
        builder.add_from_file(ui_filename)
2760
3084
                stage.set_size_request(512, 512)
2761
3085
                stage.connect('drag_data_received', on_drag_data_received)
2762
3086
                stage.drag_dest_set( gtk.DEST_DEFAULT_MOTION | gtk.DEST_DEFAULT_HIGHLIGHT | gtk.DEST_DEFAULT_DROP, dnd_list, gtk.gdk.ACTION_COPY)
 
3087
                
 
3088
                stage.set_can_focus(True)
 
3089
                stage.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.KEY_PRESS_MASK)
 
3090
                stage.connect('key_press_event', keyvent, window)
 
3091
                stage.grab_focus()
 
3092
                
2763
3093
                frameframe = window.builder.get_object("drawingarea2")
2764
3094
                objwin = window.builder.get_object("drawingarea3")
2765
3095
                gradwin = window.builder.get_object("drawingarea4")
2784
3114
                exposeframevent(frameframe, "animation-event")
2785
3115
                exposegradevent(gradwin, "init-event")
2786
3116
                exposecolorevent(cwin, "init-event")
 
3117
                console = window.builder.get_object("textview3")
 
3118
                # create a font description object
 
3119
                fontdesc = pango.FontDescription("Courier 10")
 
3120
                # and pass it on as parameter to the method:
 
3121
                console.modify_font(fontdesc)
 
3122
                consolebuffer = console.get_buffer()
 
3123
                consolebuffer.set_text("SWIFT Internal Python Console, v1.0.0\nType \"help()\" for instructions,\nor \"Credits\" to see all the people who have worked on SWIFT.")
 
3124
                consolentry = window.builder.get_object("entry6")
 
3125
                consolentry.connect("activate", window.console, gtk.RESPONSE_OK)
 
3126
                
2787
3127
        else:
2788
3128
                alert("Swift is already running!", True)
2789
3129
                print "Swift is already running!"