Exemplo n.º 1
0
    def updateContentTransform(self):
        clamp = lambda x, minV, maxV: minV if x < minV else maxV if x > maxV else x

        if self.minimumZoomScale < 0.1:
            self.minimumZoomScale = 0.1
        if self.maximumZoomScale < self.minimumZoomScale:
            self.maximumZoomScale = self.minimumZoomScale

        self.__zoomScale = clamp(self.__zoomScale, self.minimumZoomScale,
                                 self.maximumZoomScale)

        bounds = self.contentBounds()
        scaleX = bounds.width / self.__zoomScale
        scaleY = bounds.height / self.__zoomScale

        maxX = max(self.__contentSize.width - scaleX, 0)
        maxY = max(self.__contentSize.height - scaleY, 0)

        self.__contentOffset.x = clamp(self.__contentOffset.x, 0, maxX)
        self.__contentOffset.y = clamp(self.__contentOffset.y, 0, maxY)

        scale = core.LinearTransform2()
        scale.scale(self.__zoomScale)
        transform = core.AffineTransform2()
        transform.translate(-self.__contentOffset.x, -self.__contentOffset.y)
        transform.multiply(scale)
        transform.translate(bounds.x, bounds.y)
        tm = transform.matrix3()
        if self.contentTransform != tm:
            self.contentTransform = tm
            self._updateScrollSliderRect()
            self.redraw()
        self.__updateContentTransform = False
Exemplo n.º 2
0
    def update(self, delta, tick):
        if self.hidden or self.state == Sprite.STATE_DISABLED:
            self._mouseId = None

        if self.paused:
            return

        finished = []
        # update animations
        for attr, anim in self._animators.items():
            anim.update(delta)
            self.__dict__[attr] = anim.value
            if anim.finished:
                finished.append((attr, anim))
                print('anim finished attr:{}'.format(attr))

        # calculate transform
        trans = core.AffineTransform2()
        trans.translate(self.size[0] * -0.5, self.size[1] * -0.5)
        linear = core.LinearTransform2()
        linear.scale(self.scale[0], self.scale[1])
        linear.rotate(self.rotate[0])
        trans.multiply(linear)
        trans.translate(self.offset[0] + self.center[0],
                        self.offset[1] + self.center[1])

        self.transform = trans.matrix3()
        trans.inverse()
        self.transformInverse = trans.matrix3()

        # update children
        for c in self.children[:]:
            c.update(delta, tick)

        # remove finished animation, invoke finish callback
        for attr, anim in finished:
            del self._animators[attr]
            if anim.callback:
                anim.callback(anim)
Exemplo n.º 3
0
    def updateScroll(self):
        bounds = self.contentBounds()
        if self.font:
            invScale = 1.0 / self.scaleFactor
            charWidth = self.font.width * invScale

            if bounds.width > charWidth * 2:  # at least 2 characters should be displayed.
                maxX = bounds.width * 0.9
                minX = bounds.width * 0.1
                text = self.__text + self.composingText
                textLen = self.font.lineWidth(text) * invScale
                if textLen > maxX:
                    text = self.__text[0:self.__caretPos] + self.composingText
                    textLen = self.font.lineWidth(text) * invScale

                    transform = core.AffineTransform2(
                        core.Matrix3(self.contentTransform))
                    textLen += transform.translation[0]

                    indent = min(bounds.width * 0.1, charWidth)
                    offset = 0

                    while textLen < minX:
                        textLen += indent
                        offset += indent

                    while textLen > maxX:
                        textLen -= indent
                        offset -= indent

                    if offset != 0:
                        transform.translate(core.Vector2(offset, 0))
                        pos = transform.translation
                        if pos[0] > 0:
                            transform.translation = 0, pos[1]
                        self.contentTransform = transform.matrix3()
                    return
        self.contentTransform = core.Matrix3()
Exemplo n.º 4
0
    def onUpdate(self, delta, tick, date):
        parent = self.parent()
        scale = parent.contentScale
        lin = core.LinearTransform2()
        lin.scale(scale)
        self.transform = core.AffineTransform2(lin).matrix3()

        a = self.backgroundColor.a
        if round(a * 255) < round(self.alpha * 255):
            if self.delay > 0:
                offset = self.alpha * delta / self.delay
                a += offset
            else:
                a = self.alpha

            if a > self.alpha:
                a = self.alpha

            self.backgroundColor.a = a
            self.redraw()

        if len(self.children()) == 0:
            self.screen().postOperation(self.removeFromParent, ())
Exemplo n.º 5
0
Arquivo: view.py Projeto: DKGL/DKDemo
    def setFrame(self, frame):
        assert isinstance(frame, core.Rect)
        #assert frame.width > 0
        #assert frame.height > 0
        assert self.minimumViewWidth > 0
        assert self.minimumViewHeight > 0
        width = max(frame.width, self.minimumViewWidth)
        height = max(frame.height, self.minimumViewHeight)

        frame = core.Rect(frame.origin + (width, height))

        try:
            f = self.__frame
            if f == frame:
                return
        except AttributeError:
            pass

        self.__frame = frame
        self.contentScale = width, height

        linear = core.LinearTransform2()
        linear.scale(width, height)
        self.transform = core.AffineTransform2(linear, frame.origin).matrix3()
Exemplo n.º 6
0
    def createFromPlist(cls, source, pool):
        assert isinstance(pool, ResourcePool)

        if isinstance(source, core.Data):
            rootObject = plistlib.readPlistFromBytes(source)
        else:
            rootObject = plistlib.readPlist(source)

        if rootObject:
            metadata = rootObject.get('metadata')
            if not metadata:
                raise KeyError('metadata key is missing.')
            filename = metadata.get('realTextureFileName')
            if not filename:
                filename = metadata.get('textureFileName')
            if not filename:
                raise KeyError(
                    'metadata.realTextureFileName or metadata.textureFileName key is missing.'
                )
            texture = pool.loadResource(filename)

            if texture:
                resolution = _ParseStringToNumbers(metadata.get('size'))
                resolution = core.Size(*resolution)

                frames = {}
                for key, value in rootObject.get('frames').items():
                    frame = _ParseStringToNumbers(value.get('frame'))
                    srcRect = _ParseStringToNumbers(
                        value.get('sourceColorRect'))
                    srcSize = _ParseStringToNumbers(value.get('sourceSize'))
                    rotated = value.get('rotated')

                    frame = core.Rect(*frame)
                    srcRect = core.Rect(*srcRect)
                    srcSize = core.Size(*srcSize)

                    offsetX = srcRect.x / srcSize.width
                    offsetY = 1.0 - (srcRect.y +
                                     srcRect.height) / srcSize.height
                    scaleX = srcRect.width / srcSize.width
                    scaleY = srcRect.height / srcSize.height

                    texTM = core.AffineTransform2()
                    if rotated:
                        linear = core.LinearTransform2()
                        linear.scale(frame.width, frame.height)
                        linear.rotate(-math.pi * 0.5)
                        texTM.multiply(linear)
                        texTM.translate(frame.x, resolution.height - frame.y)
                    else:
                        linear = core.LinearTransform2()
                        linear.scale(frame.width, frame.height)
                        texTM.multiply(linear)
                        texTM.translate(
                            frame.x,
                            resolution.height - frame.y - frame.height)

                    linear = core.LinearTransform2()
                    linear.scale(1.0 / resolution.width,
                                 1.0 / resolution.height)
                    texTM.multiply(linear)

                    frames[key] = Frame(texTM.matrix3(),
                                        core.Point(offsetX, offsetY),
                                        core.Size(scaleX, scaleY), srcSize)

                print('texturepack:{} frames loaded.'.format(len(frames)))
                return cls(texture, filename, resolution, frames)
            else:
                raise FileNotFoundError('file:{} not found'.format(filename))