From ec6a5ec4946c27ae53818d53ae9fe73ac035a2b4 Mon Sep 17 00:00:00 2001 From: Skyler Lehmkuhl Date: Fri, 4 Jan 2013 12:19:07 -0500 Subject: [PATCH] Can now use images on OSX --- GUI/Cocoa/Canvas.py | 33 ++++++++++++++++++++++++++++++--- GUI/Cocoa/Color.py | 11 ++++++++--- GUI/Cocoa/Image.py | 8 +++++--- lightningbeam.py | 4 +++- svlgui.py | 7 ++++++- 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/GUI/Cocoa/Canvas.py b/GUI/Cocoa/Canvas.py index e65d6b4..9e3b588 100644 --- a/GUI/Cocoa/Canvas.py +++ b/GUI/Cocoa/Canvas.py @@ -3,7 +3,7 @@ # from array import array -from Foundation import NSPoint, NSMakeRect, NSString +from Foundation import NSPoint, NSMakeRect, NSString, NSRect, NSZeroRect from AppKit import NSGraphicsContext, NSBezierPath, NSEvenOddWindingRule, \ NSFontAttributeName, NSForegroundColorAttributeName, \ NSCompositeCopy, NSCompositeSourceOver, NSAffineTransform @@ -11,6 +11,7 @@ from GUI import export from GUI.StdColors import black, white from GUI.GCanvases import Canvas as GCanvas import math +import sys class Canvas(GCanvas): @@ -67,6 +68,10 @@ class Canvas(GCanvas): def newpath(self): self._ns_path.removeAllPoints() + self.minx = sys.maxint + self.miny = sys.maxint + self.maxx = -sys.maxint/2 + self.maxy = -sys.maxint/2 #for i in range(len(self.transformstack)): #j = self.transformstack.pop() #transforms = {"translate":self.translate,"rotate":self.rotate,"scale":self.scale} @@ -74,6 +79,10 @@ class Canvas(GCanvas): def moveto(self, x, y): x, y = self._transform(x, y) + self.minx = min(self.minx, x) + self.miny = min(self.miny, y) + self.maxx = max(self.maxx, x) + self.maxy = max(self.maxy, y) self._ns_path.moveToPoint_((x, y)) def rmoveto(self, dx, dy): @@ -81,6 +90,10 @@ class Canvas(GCanvas): def lineto(self, x, y): x, y = self._transform(x, y) + self.minx = min(self.minx, x) + self.miny = min(self.miny, y) + self.maxx = max(self.maxx, x) + self.maxy = max(self.maxy, y) self._ns_path.lineToPoint_((x, y)) def rlineto(self, dx, dy): @@ -90,6 +103,10 @@ class Canvas(GCanvas): cp1 = self._transform(*cp1) cp2 = self._transform(*cp2) ep = self._transform(*ep) + self.minx = min(self.minx, cp1[0], cp2[0], ep[0]) + self.miny = min(self.miny, cp1[1], cp2[1], ep[1]) + self.maxx = max(self.maxx, cp1[0], cp2[0], ep[0]) + self.maxy = max(self.maxy, cp1[1], cp2[1], ep[1]) self._ns_path.curveToPoint_controlPoint1_controlPoint2_( ep, cp1, cp2) @@ -171,7 +188,12 @@ class Canvas(GCanvas): def fill(self): ns = self._ns_path - self._fillcolor._ns_color.set() + # self._fillcolor._ns_color.set() + if self._fillcolor.image: + ns.addClip() + self._fillcolor._ns_color.drawInRect_fromRect_operation_fraction_(NSRect((self.minx,self.miny),(self.maxx-self.minx,self.maxy-self.miny)),NSZeroRect,NSCompositeSourceOver,1.0) + else: + self._fillcolor._ns_color.setFill() ns.fill() def erase(self): @@ -186,7 +208,12 @@ class Canvas(GCanvas): ns = self._ns_path self._pencolor._ns_color.set() ns.stroke() - self._fillcolor._ns_color.set() + # self._fillcolor._ns_color.set() + if self._fillcolor.image: + ns.addClip() + self._fillcolor._ns_color.drawInRect_fromRect_operation_fraction_(NSRect((self.minx,self.miny),(self.maxx-self.minx,self.maxy-self.miny)),NSZeroRect,NSCompositeSourceOver,1.0) + else: + self._fillcolor._ns_color.setFill() ns.fill() def show_text(self, text): diff --git a/GUI/Cocoa/Color.py b/GUI/Cocoa/Color.py index a756d38..51ef2ab 100644 --- a/GUI/Cocoa/Color.py +++ b/GUI/Cocoa/Color.py @@ -18,9 +18,14 @@ class Color(GColor): _from_ns_color = classmethod(_from_ns_color) - def __init__(self, red, green, blue, alpha = 1.0): - self._ns_color = NSColor.colorWithCalibratedRed_green_blue_alpha_( - red, green, blue, alpha) + def __init__(self, red, green, blue, alpha = 1.0, image = False, im = ''): + self.image = image + if image: + # self._ns_color = NSColor.colorWithPatternImage_(im._ns_image) + self._ns_color = im._ns_image + else: + self._ns_color = NSColor.colorWithCalibratedRed_green_blue_alpha_( + red, green, blue, alpha) def get_red(self): return self._ns_color.redComponent() diff --git a/GUI/Cocoa/Image.py b/GUI/Cocoa/Image.py index a40ab1e..9c70010 100644 --- a/GUI/Cocoa/Image.py +++ b/GUI/Cocoa/Image.py @@ -20,12 +20,14 @@ class Image(GImage): if not ns_rep: raise ValueError("Unrecognised image file type: %s" % file) ns_rep.setSize_((ns_rep.pixelsWide(), ns_rep.pixelsHigh())) + self._ns_image = NSImage.alloc().initWithContentsOfFile_(file) + # self.ns_image.addRepresentation_(ns_rep) self._init_from_ns_rep(ns_rep) def _init_from_ns_rep(self, ns_rep): - ns_image = NSImage.alloc().init() - ns_image.addRepresentation_(ns_rep) + self._ns_image = NSImage.alloc().init() + self._ns_image.addRepresentation_(ns_rep) self._ns_bitmap_image_rep = ns_rep - self._init_with_ns_image(ns_image, flipped = True) + self._init_with_ns_image(self._ns_image, flipped = True) export(Image) diff --git a/lightningbeam.py b/lightningbeam.py index a26ffd2..5ef812c 100755 --- a/lightningbeam.py +++ b/lightningbeam.py @@ -574,7 +574,9 @@ def import_to_stage(widget=None): thefile = svlgui.file_dialog("open",None,["jpg","png","bmp","wav"]).path for i in ("jpg","png","bmp"): if thefile.endswith(i): - im = svlgui.Image(thefile) + # im = svlgui.Image(thefile) + im = box(100,100,200,200,svlgui.Color(thefile)) + print im.filled im.onMouseDown = onMouseDownObj im.onMouseMove = onMouseMoveObj im.onMouseDrag = onMouseDragObj diff --git a/svlgui.py b/svlgui.py index 6757364..52304ea 100644 --- a/svlgui.py +++ b/svlgui.py @@ -81,13 +81,15 @@ class Color (object): if type(val)==type([]): self.type = "RGB" self.val = val - elif type(val)==type(""): + elif isinstance(val, basestring): if val.startswith("#"): self.type = "RGB" self.val = hex2rgb(val) else: self.type = "Image" self.val = val + if SYSTEM=="osx": + self.image = GUI.Image(file=val) def _getcairo(self): if self.type=="RGB": return cairo.SolidPattern(*self.val) @@ -98,6 +100,9 @@ class Color (object): def _getpygui(self): if self.type=="RGB": return Colors.rgb(*self.val) + elif self.type=="Image": + a = Colors.rgb(0,0,0,1,image=True,im=self.image) + return a def _getrgb(self): if self.type=="RGB": return rgb2hex(*self.val)