Can now use images on OSX

This commit is contained in:
Skyler Lehmkuhl
2013-01-04 12:19:07 -05:00
parent b0368e7c73
commit ec6a5ec494
5 changed files with 52 additions and 11 deletions
+30 -3
View File
@@ -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):
+8 -3
View File
@@ -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()
+5 -3
View File
@@ -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)