class ImageView(view.View): textureTransform = core.Matrix3() def __init__(self, image=None, *args, **kwargs): super().__init__(*args, **kwargs) if image: assert isinstance(image, core.Texture2D) self.textureImage = image self.textureBlend = blendstate.defaultOpaque self.textureSampler = None def onUnload(self): self.textureImage = None self.textureSampler = None return super().onUnload() def onRender(self, renderer): super().onRender(renderer) if self.textureImage: with renderer.contextForTexturedRects(self.textureImage, sampler=self.textureSampler, color=self.backgroundColor, blend=self.textureBlend) as r: r.add(self.contentDisplayBounds(), self.textureTransform)
def render(self, renderer): rc = renderer.contextForTexturedRects(texture=None, blend=blendstate.defaultAlpha) self._render(rc, core.Matrix3(), core.Color(1, 1, 1, 1)) if rc.texture: # draw rectangles with rc: pass
def onRender(self, renderer): c = self.backgroundColor if self.enabled: if self.__mouseTrackInfo: self.backgroundColor = self.outerBoundColorActivated textColor = self.titleTextColorActivated outlineColor = self.titleOutlineColorActivated elif self.__mouseHover: self.backgroundColor = self.outerBoundColorHighlighted textColor = self.titleTextColorHighlighted outlineColor = self.titleOutlineColorHighlighted else: self.backgroundColor = self.outerBoundColor textColor = self.titleTextColor outlineColor = self.titleOutlineColor else: self.backgroundColor = self.outerBoundColorDisabled textColor = self.titleTextColorDisabled outlineColor = self.titleOutlineColorDisabled super().onRender(renderer) self.backgroundColor = c _tm = renderer.transform renderer.transform = core.Matrix3() titleRect = self.titleFrame() titleRect.x += self.captionLeftMargin titleRect.width -= self.captionLeftMargin + self.captionRightMargin titleRect.y += self.captionBottomMargin titleRect.height -= self.captionTopMargin + self.captionBottomMargin font.drawText(renderer, titleRect, self.caption, self.font, textColor, outlineColor, align=font.ALIGN_BOTTOM) bounds = TitledView.contentBounds(self) border = round(self.contentBorder) if border > 0: rc = core.Rect(bounds) rc.x -= border rc.width += border * 2 rc.y -= border rc.height += border * 2 with renderer.contextForSolidRects( self.borderColor, blend=blendstate.defaultOpaque) as r: r.add(rc) with renderer.contextForSolidRects( self.backgroundColor, blend=blendstate.defaultOpaque) as r: r.add(bounds) pixelBounds = self.convertLocalToPixel(self.unprojectLocalRect(bounds)) renderer.viewport = pixelBounds.tuple renderer.bounds = bounds.tuple renderer.transform = _tm
def __init__(self, center=(0, 0), size=(1, 1), texturePack=None, name=''): self.name = name self.state = Sprite.STATE_NORMAL self.hidden = False self.transform = core.Matrix3() self.transformInverse = core.Matrix3() self._parent = None self.children = [] self.paused = False self.texturePack = texturePack self._textureIdsForState = { Sprite.STATE_NORMAL: (), Sprite.STATE_HIGHLIGHTED: (), Sprite.STATE_DISABLED: () } # callback functions with argument Sprite(self) self.highlightCallback = None self.buttonCallback = None # animated values # three values tuple self.diffuse = (1.0, 1.0, 1.0) # two values tuple self.center = center # Sprite's center of parent space self.size = size # Sprite's size of parent space self.offset = (0.0, 0.0) # Sprite's position offset of parent space self.scale = (1.0, 1.0) # Sprite's scale of parent space # single value tuple self.rotate = (0.0, ) self.alpha = (1.0, ) self.textureIndex = (0.0, ) self._animators = {} self._mouseId = None self.update(0, 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()
class Label(view.View): fontAttributes = font.attributes(12) textColor = core.Color(0.0, 0.0, 0.0) outlineColor = None textTransform = core.Matrix3() scaleToFit = False leftMargin = 0 topMargin = 0 rightMargin = 0 bottomMargin = 0 def __init__(self, text='', *args, **kwargs): super().__init__(*args, **kwargs) self.text = str(text) self.align = ALIGN_CENTER self.linebreak = LINE_BREAK_TRUNCATING_TAIL def onLoaded(self): super().onLoaded() if not self.font: self.font = font.loadUIFont(self.fontAttributes, self.scaleFactor) def onUnload(self): self.font = None super().onUnload() def onRender(self, renderer): super().onRender(renderer) if self.font: bounds = self.contentBounds() bounds.x += self.leftMargin bounds.y += self.bottomMargin bounds.width -= self.leftMargin + self.rightMargin bounds.height -= self.topMargin + self.bottomMargin font.drawText(renderer, bounds, self.text, self.font, self.textColor, self.outlineColor, scaleToFit=self.scaleToFit, align=self.align, linebreak=self.linebreak, blend=blendstate.defaultAlpha)
class ItemStyle: indentationLevelWidth = 15 expandIconSize = 12 padding = 2 topMargin = 1 leftMargin = 4 rightMargin = 1 bottomMargin = 1 fontAttributes = font.attributes(14, embolden=0) backgroundColor = None backgroundColorHighlighted = core.Color(1, 1, 0) backgroundColorActivated = core.Color(0.25, 0.25, 1.0) backgroundColorDisabled = core.Color(0.9, 0.9, 0.9) textColor = core.Color(0, 0, 0) textColorHighlighted = core.Color(0, 0, 0) textColorActivated = core.Color(1, 1, 1) textColorDisabled = core.Color(0.5, 0.5, 0.5) outlineColor = None outlineColorHighlighted = None outlineColorActivated = core.Color(0, 0, 0, 1) outlineColorDisabled = None backgroundColorBlend = blendstate.defaultOpaque arrowColor = core.Color(0.5, 0.5, 0.5) arrowColorActivated = core.Color(0.25, 0.25, 1.0) iconTransform = core.Matrix3() scaleFactor = view.DEFAULT_UI_SCALE def __init__(self): self.font = None
def onRender(self, renderer): border = round(self.borderWidth) if border > 0: renderer.clear(self.borderColor) bounds = View.contentBounds(self) pixelBounds = self.convertLocalToPixel( self.unprojectLocalRect(bounds)) renderer.viewport = pixelBounds.tuple renderer.bounds = bounds.tuple _tm = renderer.transform renderer.transform = core.Matrix3() with renderer.contextForSolidRects( self.backgroundColor, blend=blendstate.defaultOpaque) as r: r.add(bounds) renderer.transform = _tm else: renderer.clear(self.backgroundColor)
import _dk_core as core from ..renderer import Renderer from .. import blendstate from .. import rendertarget from . import view import math _identityMatrix3 = core.Matrix3() class _AnimatedProgressValue: begin = 0.0 end = 0.0 length = 0.0 elapsed = 0.0 class AnimatedProgressView(view.View): progressAnimation = 0.5 borderWidth = 1 borderColor = core.Color(0.0, 0.0, 0.0) backgroundColor = core.Color(0.5, 0.5, 0.5) progressColor = core.Color(1.0, 1.0, 1.0) minimumViewHeight = borderWidth * 2 + 1 minimumViewWidth = borderWidth * 2 + 1 def __init__(self,
def add(self, rect, transform=core.Matrix3()): if rect.width > 0 and rect.height > 0: self.transforms.extend(transform.tuple) return super().add(rect.tuple, 1)
def add(self, rect, transform, tex_rect=core.Rect(0,0,1,1), tex_trans=core.Matrix3()): if rect.width > 0 and rect.height > 0: self.transforms.extend(transform.tuple) self.tex_rects.extend(tex_rect.tuple) self.tex_trans.extend(tex_trans.tuple) return super().add(rect.tuple, 1)
def drawRect(self, renderer, rect, state, layout): super().drawRect(renderer, rect, state, layout) style = self.style if style.font: if not self.selectable() or state == _ITEM_STATE_DISABLED: textColor = style.textColorDisabled outlineColor = style.outlineColorDisabled checkmarkColor = style.checkmarkColorDisabled arrowColor = style.arrowColorDisabled elif state == _ITEM_STATE_HIGHLIGHTED: textColor = style.textColorHighlighted outlineColor = style.outlineColorHighlighted checkmarkColor = style.checkmarkColorHighlighted arrowColor = style.arrowColorHighlighted else: textColor = style.textColor outlineColor = style.outlineColor checkmarkColor = style.checkmarkColor arrowColor = style.arrowColor rc = core.Rect(rect) rc.x += style.leftMargin rc.y += style.bottomMargin rc.width -= style.textMargin + style.arrowSize + style.leftMargin + style.rightMargin rc.height -= style.topMargin + style.bottomMargin if self.__checked: # draw check mark y = (rc.height - style.checkmarkSize) * 0.5 cbRect = core.Rect(rc.x, rc.y + y, style.checkmarkSize, style.checkmarkSize) p0 = core.Point(0.15, 0.6) p1 = core.Point(0.05, 0.5) p2 = core.Point(0.4, 0.35) p3 = core.Point(0.4, 0.15) p4 = core.Point(0.85, 0.8) p5 = core.Point(0.95, 0.7) for p in (p0, p1, p2, p3, p4, p5): p.x = p.x * cbRect.width + cbRect.x p.y = p.y * cbRect.height + cbRect.y with renderer.contextForSolidTriangleStrip( checkmarkColor, blend=blendstate.defaultOpaque) as r: r.addTriangle(p0, p1, p2) r.add(p3) r.add(p4) r.add(p5) offset = style.checkmarkSize + style.padding rc.x += offset rc.width -= offset if self.__image: y = (rc.height - style.imageSize) * 0.5 rc2 = core.Rect(rc.x, rc.y + y, style.imageSize, style.imageSize) with renderer.contextForTexturedRects( self.__image, blend=blendstate.defaultAlpha) as r: r.add(rc2, core.Matrix3()) offset = style.imageSize + style.padding rc.x += offset rc.width -= offset font.drawText(renderer, rc, self.__text, style.font, textColor, outlineColor, align=font.ALIGN_BOTTOM_LEFT, linebreak=font.LINE_BREAK_TRUNCATING_TAIL) #draw arrow! if self.subMenu: if layout == _LAYOUT_HORIZONTAL: arRect = core.Rect( rect.x + rect.width - style.arrowSize - style.rightMargin, rect.y + style.bottomMargin, style.arrowSize, style.arrowSize) x1 = arRect.x x2 = arRect.x + arRect.width * 0.5 x3 = arRect.x + arRect.width y1 = arRect.y y2 = arRect.y + arRect.height * 0.5 pos = (core.Point(x1, y2), core.Point(x2, y1), core.Point(x3, y2)) else: y = (rc.height - style.arrowSize) * 0.5 arRect = core.Rect( rect.x + rect.width - style.arrowSize - style.rightMargin, rc.y + y, style.arrowSize, style.arrowSize) # x1 = arRect.x + arRect.width * 0.5 # x2 = arRect.x + arRect.width w = arRect.height * 0.8660254037844388 x1 = arRect.x + (arRect.width - w) * 0.5 x2 = x1 + w y1 = arRect.y y2 = arRect.y + arRect.height * 0.5 y3 = arRect.y + arRect.height pos = (core.Point(x1, y3), core.Point(x1, y1), core.Point(x2, y2)) if arrowColor: with renderer.contextForSolidTriangles( arrowColor, blend=blendstate.defaultOpaque) as r: r.add(*pos) else: print('drawRect textFont is missing!!')
class Button(label.Label, control.Control, imageview.ImageView): fontAttributes = font.attributes(12) backgroundColor = core.Color(0.85, 0.85, 0.85) backgroundColorHighlighted = core.Color(0.9, 0.9, 1.0) backgroundColorActivated = core.Color(0.5, 0.5, 0.5) backgroundColorDisabled = core.Color(0.4, 0.4, 0.4) textColor = core.Color(0.0, 0.0, 0.0) textColorHighlighted = core.Color(0.0, 0.0, 0.0) textColorActivated = core.Color(1.0, 1.0, 1.0) textColorDisabled = core.Color(0.3, 0.3, 0.3) outlineColor = None outlineColorHighlighted = core.Color(1.0, 1.0, 1.0, 1.0) outlineColorActivated = core.Color(0.0, 0.0, 0.0, 1.0) outlineColorDisabled = None backgroundImage = None backgroundImageHighlighted = None backgroundImageActivated = None backgroundImageDisabled = None backgroundImageTransform = core.Matrix3() borderColor = core.Color(0.0, 0.0, 0.0, 1.0) borderWidth = 1 def __init__(self, text='Button', *args, **kwargs): super().__init__(text=text, *args, **kwargs) self.buttonPressed = False self.__mouseHover = False self.__capturedMouseId = None if self.textureImage: self.backgroundImage = self.textureImage def setTextColor(self, color, state=control.Control.STATE_ALL): assert isinstance(color, core.Color) if state in (self.STATE_ALL, self.STATE_NORMAL): self.textColor = color if state in (self.STATE_ALL, self.STATE_HIGHLIGHTED): self.textColorHighlighted = color if state in (self.STATE_ALL, self.STATE_ACTIVATED): self.textColorActivated = color if state in (self.STATE_ALL, self.STATE_DISABLED): self.textColorDisabled = color def setOutlineColor(self, color, state=control.Control.STATE_ALL): assert isinstance(color, core.Color) if state in (self.STATE_ALL, self.STATE_NORMAL): self.outlineColor = color if state in (self.STATE_ALL, self.STATE_HIGHLIGHTED): self.outlineColorHighlighted = color if state in (self.STATE_ALL, self.STATE_ACTIVATED): self.outlineColorActivated = color if state in (self.STATE_ALL, self.STATE_DISABLED): self.outlineColorDisabled = color def onLoaded(self): super().onLoaded() def onUnload(self): self.backgroundImage = None self.backgroundImageHighlighted = None self.backgroundImageActivated = None self.backgroundImageDisabled = None return super().onUnload() def onRender(self, r): state = self.STATE_DISABLED if not self.enabled else \ self.STATE_ACTIVATED if self.buttonPressed else \ self.STATE_HIGHLIGHTED if self.__mouseHover else \ self.STATE_NORMAL textColors = (self.textColor, self.textColorHighlighted, self.textColorActivated, self.textColorDisabled) outlineColors = (self.outlineColor, self.outlineColorHighlighted, self.outlineColorActivated, self.outlineColorDisabled) bgColors = (self.backgroundColor, self.backgroundColorHighlighted, self.backgroundColorActivated, self.backgroundColorDisabled) bgImages = (self.backgroundImage, self.backgroundImageHighlighted, self.backgroundImageActivated, self.backgroundImageDisabled) self.textColor = textColors[state] self.outlineColor = outlineColors[state] self.backgroundColor = bgColors[state] self.textureImage = bgImages[state] if not self.textureImage: self.textureImage = self.backgroundImage super().onRender(r) self.backgroundColor = bgColors[0] self.textColor = textColors[0] self.outlineColor = outlineColors[0] def discardAction(self): if self.__capturedMouseId: if self.isMouseCapturedBySelf(self.__capturedMouseId[0]): self.releaseMouse(self.__capturedMouseId[0]) self.__capturedMouseId = None self.buttonPressed = False self.redraw() def onMouseDown(self, deviceId, buttonId, pos): super().onMouseDown(deviceId, buttonId, pos) acceptMouse = True if self.__capturedMouseId: if not self.isMouseCapturedBySelf(self.__capturedMouseId[0]): self.__capturedMouseId = None if self.__capturedMouseId is None: self.captureMouse(deviceId) self.__capturedMouseId = (deviceId, buttonId) self.buttonPressed = True self.redraw() def onMouseUp(self, deviceId, buttonId, pos): super().onMouseUp(deviceId, buttonId, pos) if self.__capturedMouseId and self.__capturedMouseId == (deviceId, buttonId): self.releaseMouse(deviceId) if self.bounds().isInside(pos): self.screen().postOperation(self.postEvent, ()) self.__capturedMouseId = None self.buttonPressed = False self.redraw() def onMouseMove(self, deviceId, pos, delta): super().onMouseMove(deviceId, pos, delta) if self.__capturedMouseId and self.__capturedMouseId[0] == deviceId: btnPressed = self.buttonPressed if self.isMouseCapturedBySelf(deviceId): self.buttonPressed = self.bounds().isInside(pos) else: self.__capturedMouseId = None self.buttonPressed = False if btnPressed != self.buttonPressed: self.redraw() elif deviceId == 0: if not self.__mouseHover: self.__mouseHover = True if not self.buttonPressed: self.redraw() def onMouseLeave(self, deviceId): super().onMouseLeave(deviceId) if deviceId == 0 and self.__mouseHover: self.__mouseHover = False if not self.buttonPressed: self.redraw() def postEvent(self): super().invokeAllTargets(self)
def _draw(self, renderer, width): try: self._expandIconRect self._labelIconRect self._labelTextRect self._rowRect self._groupRect except AttributeError: pass else: self._rowRect.width = width self._groupRect.width = width style = self.style state = self.state textColor = (style.textColor, style.textColorHighlighted, style.textColorActivated, style.textColorDisabled)[state] outlineColor = (style.outlineColor, style.outlineColorHighlighted, style.outlineColorActivated, style.outlineColorDisabled)[state] backgroundColor = (style.backgroundColor, style.backgroundColorHighlighted, style.backgroundColorActivated, style.backgroundColorDisabled)[state] if backgroundColor: with renderer.contextForSolidRects( backgroundColor, blend=style.backgroundColorBlend) as r: r.add(self._rowRect) if len(self.children) > 0: if self.expanded: w = self._expandIconRect.width * 0.8660254037844388 x1 = self._expandIconRect.x x2 = self._expandIconRect.x + self._expandIconRect.width * 0.5 x3 = self._expandIconRect.x + self._expandIconRect.width y1 = self._expandIconRect.y + ( self._expandIconRect.height - w) * 0.5 y2 = y1 + w pos = (core.Point(x1, y2), core.Point(x2, y1), core.Point(x3, y2)) else: w = self._expandIconRect.height * 0.8660254037844388 x1 = self._expandIconRect.x + (self._expandIconRect.width - w) * 0.5 x2 = x1 + w y1 = self._expandIconRect.y y2 = self._expandIconRect.y + self._expandIconRect.height * 0.5 y3 = self._expandIconRect.y + self._expandIconRect.height pos = (core.Point(x1, y3), core.Point(x1, y1), core.Point(x2, y2)) arrowColor = style.arrowColor if self.state != Item.STATE_ACTIVATED and self.hasSelectedDescendant( ): arrowColor = style.arrowColorActivated if arrowColor: with renderer.contextForSolidTriangles( arrowColor, blend=blendstate.defaultOpaque) as r: r.add(*pos) if self.labelImage: with renderer.contextForTexturedRects(self.labelImage) as r: r.add(self._labelIconRect, core.Matrix3()) font.drawText(renderer, self._labelTextRect, self.labelText, self.style.font, textColor, outlineColor, align=font.ALIGN_BOTTOM_LEFT, linebreak=font.LINE_BREAK_TRUNCATING_TAIL) if self.expanded: for item in self.children: item._draw(renderer, width)