[squeak-dev] PolygonMorph textures

Robin Schreiber The_robin_hood at gmx.net
Wed Dec 15 22:30:54 UTC 2010


Hi everyone,

What I would like to have, is a Polygon Morph that can be painted with a specified texture e.g. a certain image that repeats multiple times so that it covers up the PolygonMorph from inside with a pattern, but still preserve the Polygons outline. 
My first idea was, to create a simple polygonMorph, add Imagemorphs as Submorphs, containing he specified images, and then make use of the clipSubmorphs method. Unfortunately It turns out that the requried clipping bounds can only be specifed by a rectangel and not by  a polygonMorph or an array of vertices.
I then tried a different idea, basically creating a new class consisting out of a simple Morph with setheral Submorphs (textureParts) who each contain the texture Image (therefore ImageMorphs).

drawTexture
	"This method 'fills up' the PgiMorph with its specified texture image"
	| xStep yStep xUpper xLower yUpper yLower|
	xUpper := self bounds topRight x.
	xLower := self bounds topLeft x.
	yUpper := self bounds bottomRight y.
	yLower := self bounds topLeft y.
	xStep := self baseImage width.
	yStep := self baseImage height.
	self textureParts: Bag new.
	
	xLower to: xUpper by: xStep do: 
		[:ax | 
			yLower to: yUpper by: yStep do:
				[:ay |
					self textureParts add: (((ImageMorph new image: (self baseImage form copy: self baseImage form boundingBox)) position: ax at ay) openInWorld).].].
			
	self textureParts do:[:each | self addMorph:each].

 I then create a PolygonMorph specify the desired form factor and then iterate over every SubImageMorph, setting each Imagemoprhs Form Pixelvalue transparent where the Point is outside the PolygonMorph and leave it untouched where it is inside.

clipTextures
	self textureParts do:[:each | self clipTexture: each].

********************************

clipTexture: aTexturePart
	"this method clips the selected texturepart with respect to the maskMorph"
	| xUpper xLower yUpper yLower |
	xUpper := aTexturePart bounds topRight x. Transcript show: aTexturePart bounds topRight.
	xLower := aTexturePart bounds topLeft x.
	yUpper := aTexturePart bounds bottomRight y.
	yLower := aTexturePart bounds topLeft y.

	xLower to: xUpper do: 
		[ :ax |
			yLower to: yUpper do:
			 [ :ay |
				 (self maskMorph containsPoint: (ax at ay)) ifFalse: [
					aTexturePart form colorAt: (ax at ay) put: (Color transparent).].].].

It turns out however that I receive a very strangebe haviour of this new kind of Morph, meaning it does not seem to transparentize every Pixel of the Form that would be needed.

Anyway I can include the complete code segment if you wish, however it may be of course that I am on a completely wrong track with all of this, so if anyone of you knows of a better solution I'd be delighted to hear from you.

Robin




More information about the Squeak-dev mailing list