开发者

How to insert an image into a PolygonMorph?

开发者 https://www.devze.com 2022-12-16 12:12 出处:网络
I need to get a texture into a PolygonMorph, but these seem to require an InfiniteForm as color/ filling.

I need to get a texture into a PolygonMorph, but these seem to require an InfiniteForm as color/ filling.

The InfiniteForm is no solution as i need to rotate the PolygonMorph later on and moving the PolygonMorph around开发者_如何学C also has sideeffects on the displayed texture.

It would be very useful if it would be possible to scale the inserted texture as well.

How would you do this without replacing the existing PolygonMorph (or at least keeping the PolygonMorph's shape)?


Heres another idea for your problem. The solution includes 2 phases.

Phase 1: (drawTextures) We use BitBlt to fill a Form with our texture tiles. The form is stored in an instance variable called texturing. This form is clipped in phase 2

Phase 2: (clipTextures) Now we produce a form which is shaped like our polygon with polygon filledForm. Afterwards we substract this from a completley black form. Now we have a negative shape of the polygon. With this we clip the texturing. Now we can create an Image Morph and add it to the polygon or whatever we want to do with it.

Unfortunately the filledForm implementation cannot deal with convec shapes. So be careful how your polygon looks like.

This solution is pretty fast and can also be applied during run time. We are changing the shape of the polygon every 10msec and its rendering fine.

!PgiTexturedMorph methodsFor: 'graphics' stamp: 'pre 2/12/2011 13:30:15.156'!
    drawTexture
        | textureForm aTexture aBitBlt |

        textureForm := Form extent: (self shape extent) depth: 32.
        aTexture := self baseImage deepCopy .
        textureForm := Form extent: (self shape extent) depth: 32.
        (0 to: ((textureForm extent x) / (aTexture extent x))) do: [ :eachX | 
            (0 to: ((textureForm extent y) / (aTexture extent y))) do: [ :eachY |
                aBitBlt := BitBlt   destForm: textureForm 
                                sourceForm: aTexture 
                                fillColor: nil
                                combinationRule: 7 
                                destOrigin: (eachX * (aTexture extent x))@(eachY *(aTexture extent y)) 
                                sourceOrigin: 0@0 
                                extent: (aTexture extent) 
                                clipRect: (textureForm computeBoundingBox).
                aBitBlt copyBits.
            ]].

        self texturing: textureForm.! !

    !PgiTexturedMorph methodsFor: 'graphics' stamp: 'pre!
    clipTextures
        | clippingForm aBitBlt |

        clippingForm := (Form extent: (self shape extent + (1@0))) fillBlack.
        aBitBlt := BitBlt   destForm: clippingForm 
                        sourceForm: (self shape filledForm) 
                        fillColor: nil
                        combinationRule: 6 
                        destOrigin: 0@0
                        sourceOrigin: 0@0 
                        extent: (self shape extent) 
                        clipRect: (clippingForm computeBoundingBox).
        aBitBlt copyBits.
        aBitBlt := BitBlt   destForm: (self texturing) 
                        sourceForm: (clippingForm ) 
                        fillColor: nil
                        combinationRule: 17 
                        destOrigin: 0@0
                        sourceOrigin: 0@0 
                        extent: (clippingForm  extent) 
                        clipRect: (self texturing computeBoundingBox).
        aBitBlt copyBits.

        self texturePart image: (self texturing).
        self texturePart changed.! !
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号