Exemplo n.º 1
0
Arquivo: kine.py Projeto: DKGL/DKDemo
    def onLoaded(self):
        super().onLoaded()
        self.backgroundColor = dk.Color(0.25, 0.25, 0.25)

        self.infoLabel = dk.ui.Label(' Left-Click: camera move, Right-Click: Shoot box (Second touch on mobile)')
        self.infoLabel.fontAttributes = dk.ui.font.attributes(14, outline=2)
        self.infoLabel.align = dk.ui.label.ALIGN_LEFT
        self.infoLabel.setBlendState(dk.blendstate.defaultAlpha)
        self.infoLabel.backgroundColor = dk.color.clear
        self.infoLabel.textColor = dk.color.white
        self.infoLabel.outlineColor = dk.color.black
        self.infoLabel.enabled = False
        self.addChild(self.infoLabel)

        # 슈팅 박스
        self.shootingShapes = (dk.BoxShape(0.85, 0.85, 0.85),
                               dk.BoxShape(1.0, 1.0, 1.0),
                               dk.BoxShape(2.0, 1.0, 2.0),
                               dk.CylinderShape(1.0, 1.0, 1.0),
                               dk.CylinderShape(1.5, 0.5, 1.5),
                               dk.SphereShape(1.0))

        self.resourcePool = dk.ResourcePool()
        self.resourcePool.addSearchPath(dk.appInstance().resourceDir + "/dao")

        # 씬 생성
        self.model = self.resourcePool.loadResource('dao.DKMODEL')
        animaiton = self.resourcePool.loadResource('dao.DKANIMATION')
        animControl = animaiton.createLoopController()
        animControl.play()
        self.model.setAnimation(animControl)

        self.scene = dk.DynamicsScene()
        self.scene.drawMode = dk.scene.DRAW_COLLISION_SHAPES | dk.scene.DRAW_MESHES
        self.scene.ambientColor = dk.Color(0.45, 0.45, 0.45)
        self.scene.lights.append(dk.light.directional(dk.Vector3(0, -1, 0), dk.Color(1, 1, 1)))
        self.scene.updateLights()
        self.scene.addObject(self.model)

        # 바닥 객체 생성
        groundShape = dk.StaticPlaneShape(dk.Vector3(0, 1, 0), 0)
        groundObject = createRigidBody(0, groundShape, dk.Vector3(0, 0, 0), 'groundObject')
        self.scene.addObject(groundObject)

        # 카메라 초기화.
        self.camera = dk.Camera()
        self.cameraInfo = CameraInfo(fov=math.radians(80),
                                     near=1.0,
                                     far=1000.0,
                                     aspect=1.0,
                                     target=dk.Vector3(0, 5, 0))
        pos = dk.Vector3(0, 10, -20)
        up = dk.Vector3(0, 1, 0)
        self.camera.setView(pos, self.cameraInfo.target - pos, up)
Exemplo n.º 2
0
    def onLoaded(self):
        super().onLoaded()
        items = [
            (kine.Frame, 'Kinematic Animation'),
            (sprite.Frame, '2D Sprites, Texture Atlas'),
            (ui.Frame, 'UI Widgets'),
            (vehicle.Frame, 'Vehicle'),
            (shadow.Frame, 'Shadow Mapping'),
            (anim.Frame, 'Character Animation'),
        ]

        btnFontAttr = dk.ui.font.attributes(16, outline=2)

        self.buttons = []
        btnY = 10
        for itemClass, itemText in items:
            btn = dk.ui.Button(itemText, frame=dk.Rect(0, 0, 160, 60))
            btn.demoClass = itemClass
            btn.addTarget(self, self.openDemo)
            btn.fontAttributes = btnFontAttr
            self.addChild(btn)
            self.buttons.append(btn)

        self.demoFrame = None

        self.infoButton = dk.ui.Button('Info', frame=dk.Rect(0, 0, 80, 40))
        self.infoButton.fontAttributes = btnFontAttr
        self.infoButton.setTextColor(dk.Color(1, 1, 1, 1))
        self.infoButton.setOutlineColor(dk.Color(0.25, 0, 0, 1))
        self.infoButton.backgroundColor = dk.Color(1, 0, 0, 0.75)
        self.infoButton.backgroundColorHighlighted = dk.Color(1, 0.5, 0.5)
        self.infoButton.backgroundColorActivated = dk.Color(0.8, 0, 0)
        self.infoButton.addTarget(self, self.showInfoView)
        self.addChild(self.infoButton)

        self.closeButton = dk.ui.Button('Close', frame=dk.Rect(0, 0, 80, 40))
        self.closeButton.fontAttributes = btnFontAttr
        self.closeButton.setTextColor(dk.Color(1, 1, 1, 1))
        self.closeButton.setOutlineColor(dk.Color(0.25, 0, 0, 1))
        self.closeButton.backgroundColor = dk.Color(1, 0, 0, 0.75)
        self.closeButton.backgroundColorHighlighted = dk.Color(1, 0.5, 0.5)
        self.closeButton.backgroundColorActivated = dk.Color(0.8, 0, 0)
        self.closeButton.addTarget(self, self.closeDemo)
        self.addChild(self.closeButton)

        self.screen().postOperation(self.runGC, ())
Exemplo n.º 3
0
    def onRender(self, renderer):
        super().onRender(renderer)
        renderer.polygonOffset = 1, 1

        def chassisColor(co, *args):
            if co is self.carChassis:
                return dk.Color(1.0, 1.0, 1.0), dk.Color(0.0, 0.0, 0.0)
            if co.mass() > 0:
                return dk.Color(1.0, 0.0, 0.0), dk.Color(0.0, 0.0, 0.0)
            return args

        renderer.renderScene(self.scene,
                             self.camera,
                             0,
                             objectColorCallback=chassisColor)
        with renderer.contextForCollisionShape(self.camera) as r:
            for trans in self.wheelTrans:
                r.add(self.wheelShape, trans, dk.Color(0.85, 0.85, 0.85),
                      dk.Color(0.0, 0.0, 0.0))
Exemplo n.º 4
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.backgroundColor = dk.Color(0.6, 0.8, 1.0)
     self.camera = dk.Camera()
     self.cameraInfo = CameraInfo(fov=math.radians(80),
                                  near=4.0,
                                  far=1000.0,
                                  aspect=1.0,
                                  target=dk.Vector3(0, 0, 0))
     pos = dk.Vector3(0, 20, -20)
     up = dk.Vector3(0, 1, 0)
     self.camera.setView(pos, self.cameraInfo.target - pos, up)
     self.steering = 0
Exemplo n.º 5
0
    def __init__(self, *args, **kw):
        super().__init__(*args, **kw)

        self.cameraInfo = CameraInfo()
        self.cameraInfo.target = dk.Vector3(0, 1000, 0)

        self.camera = dk.Camera()
        self.inspectCamera = dk.Camera()

        pos = dk.Vector3(0, 1000, -1700)
        self.camera.setView(pos, self.cameraInfo.target - pos, dk.Vector3(0, 1, 0))
        self.inspectCamera.setView(pos, self.cameraInfo.target - pos, dk.Vector3(0, 1, 0))

        self.backgroundColor = dk.Color(0.85, 0.85, 1.0)
        self.inspectMode = False
        self.shadowMapInfo = ShadowMapInfo()
        self.shadowMapInfo.type = SHADOW_MAP_LISPSM
Exemplo n.º 6
0
Arquivo: anim.py Projeto: DKGL/DKDemo
    def onLoaded(self):
        super().onLoaded()
        self.backgroundColor = dk.Color(0.22, 0.32, 1.0)

        charMenu = dk.ui.Menu()
        c1 = charMenu.addItem('Character 1')
        c1.filename = 'dil.DKMODEL'
        c1.callback = self.loadCharacter
        c2 = charMenu.addItem('Character 2')
        c2.filename = 'dih.DKMODEL'
        c2.callback = self.loadCharacter
        c3 = charMenu.addItem('Character 3')
        c3.filename = 'kon.DKMODEL'
        c3.callback = self.loadCharacter
        self.charMenuItems = [c1, c2, c3]

        animMenu = dk.ui.Menu()
        a1 = animMenu.addItem('Walk')
        a1.animIndex = 0
        a1.callback = self.changeAnimation
        a2 = animMenu.addItem('Run')
        a2.animIndex = 1
        a2.callback = self.changeAnimation
        a3 = animMenu.addItem('Attack')
        a3.animIndex = 2
        a3.callback = self.changeAnimation
        self.animMenuItems = [a1, a2, a3]

        speedMenu = dk.ui.Menu()
        s1 = speedMenu.addItem('0.5')
        s1.animSpeed = 0.5
        s1.callback = self.changeAnimationSpeed
        s2 = speedMenu.addItem('1.0')
        s2.animSpeed = 1.0
        s2.callback = self.changeAnimationSpeed
        s3 = speedMenu.addItem('2.0')
        s3.animSpeed = 2.0
        s3.callback = self.changeAnimationSpeed
        speedMenu.addSeparator()
        speedMenu.addItem('Reverse').enabled = False
        s4 = speedMenu.addItem('-0.5')
        s4.animSpeed = -0.5
        s4.callback = self.changeAnimationSpeed
        s5 = speedMenu.addItem('-1.0')
        s5.animSpeed = -1.0
        s5.callback = self.changeAnimationSpeed
        s6 = speedMenu.addItem('-2.0')
        s6.animSpeed = -2.0
        s6.callback = self.changeAnimationSpeed
        self.animSpeedItems = [s1, s2, s3, s4, s5, s6]

        ctrlMenu = dk.ui.Menu()
        animPlayStopMenuItem = ctrlMenu.addItem('Play')
        animPlayStopMenuItem.playing = False
        animPlayStopMenuItem.callback = self.togglePlayStop
        ctrlMenu.addSeparator()
        ctrlMenu.addItem('Speed').subMenu = speedMenu

        self.menuBar = dk.ui.Menu()
        self.menuBar.addItem('Models').subMenu = charMenu
        self.menuBar.addSeparator()
        self.menuBar.addItem('Animations').subMenu = animMenu
        self.menuBar.addSeparator()
        self.menuBar.addItem('Control').subMenu = ctrlMenu
        self.menuBar.addSeparator()

        self.addChild(self.menuBar)

        self.infoLabel = dk.ui.Label(
            ' left-click: camera, right-click: light (two fingers move on mobile)'
        )
        self.infoLabel.fontAttributes = dk.ui.font.attributes(14, outline=2)
        self.infoLabel.align = dk.ui.label.ALIGN_LEFT
        self.infoLabel.setBlendState(dk.blendstate.defaultAlpha)
        self.infoLabel.backgroundColor = dk.color.clear
        self.infoLabel.textColor = dk.color.white
        self.infoLabel.outlineColor = dk.color.black
        self.infoLabel.enabled = False
        self.addChild(self.infoLabel)

        self.resourcePool = dk.ResourcePool()
        self.resourcePool.addSearchPath(dk.appInstance().resourceDir +
                                        "/knights")

        # create scene
        self.scene = dk.Scene()
        self.character = None
        self.sword = self.resourcePool.loadResource('katana.DKMODEL')
        self.animations = [
            self.resourcePool.loadResource('walk.DKANIMATION'),
            self.resourcePool.loadResource('run.DKANIMATION'),
            self.resourcePool.loadResource('attack.DKANIMATION'),
        ]
        self.animControl = None

        self.scene.ambientColor = dk.Color(0.45, 0.45, 0.45)
        self.scene.lights.append(
            dk.light.directional(dk.Vector3(0, -1, 0), dk.Color(1, 1, 1)))
        self.scene.updateLights()

        # init camera
        self.camera = dk.Camera()
        self.cameraInfo = CameraInfo(fov=math.radians(80),
                                     near=10.0,
                                     far=1000.0,
                                     aspect=1.0,
                                     target=dk.Vector3(0, 80, 0))
        pos = dk.Vector3(0, 100, 120)
        up = dk.Vector3(0, 1, 0)
        self.camera.setView(pos, self.cameraInfo.target - pos, up)

        self.changeAnimation(self.animMenuItems[0])
        self.togglePlayStop(animPlayStopMenuItem)
        self.changeAnimationSpeed(self.animSpeedItems[1])
        self.screen().postOperation(self.loadCharacter,
                                    (self.charMenuItems[2], ))
Exemplo n.º 7
0
    def onLoaded(self):
        super().onLoaded()

        bounds = self.bounds()

        # slider for light direction
        sliderX = dk.ui.Slider(0, range=(-1.5, 1.5), frame=dk.Rect(0, 100, 200, 40))
        sliderZ = dk.ui.Slider(0, range=(-1.5, 1.5), frame=dk.Rect(0, 150, 40, 200))
        sliderZ.vertical = True
        sliderX.vectorIndex = 0
        sliderZ.vectorIndex = 2
        sliderX.addTarget(self, self.onSliderValueChanged)
        sliderZ.addTarget(self, self.onSliderValueChanged)
        self.addChild(sliderX)
        self.addChild(sliderZ)

        shadowConfigWindow = dk.ui.TitledView('Config')
        origin = shadowConfigWindow.contentBounds().origin

        resWidth = 100
        typeWidth = 120
        itemHeight = 30

        resolutions = (0, 512)
        r = resolutions[-1]
        maxTexSize = dk.texture.maxTextureSize()
        while r < maxTexSize:
            r = r * 2
            resolutions = resolutions + (r,)

        # resolution radio-button rect
        resRect = dk.Rect(origin[0], origin[1], resWidth, len(resolutions) * itemHeight)

        configHeight = max(resRect.height, itemHeight*5)
        resRect.y = origin[1] + configHeight - resRect.height

        # shadow-type radio-button rect
        typeRect = dk.Rect(resRect.x + resRect.width, 0, typeWidth, itemHeight*2)
        typeRect.y = origin[1] + configHeight - typeRect.height

        # shadow-body-vector checkbox rect
        bvRect = dk.Rect(resRect.x + resRect.width, 0, typeWidth, itemHeight)
        bvRect.y = typeRect.y - bvRect.height

        # multi-sample checkbox rect
        msRect = dk.Rect(resRect.x + resRect.width, 0, typeWidth, itemHeight)
        msRect.y = bvRect.y - msRect.height

        # inspect-mode checkbox rect
        imRect = dk.Rect(resRect.x + resRect.width, 0, typeWidth, itemHeight)
        imRect.y = msRect.y - msRect.height

        frame = shadowConfigWindow.frameForContentFrame(dk.Rect(0, 0, resWidth+typeWidth, configHeight ))
        frame.origin = bounds.width - frame.width, bounds.height - frame.height
        shadowConfigWindow.frame = frame

        for btn, val in zip(
                dk.ui.radio.addItems(shadowConfigWindow, resolutions, resRect, columns=1),
                resolutions):
            btn.value = val
            btn.addTarget(self, self.onShadowResolutionChanged)
            btn.backgroundColor = dk.color.clear
            btn.setBlendState(dk.blendstate.defaultAlpha)

        for btn, val in zip(
                dk.ui.radio.addItems(shadowConfigWindow, ('Uniform', 'LiSPSM'), typeRect, columns=1),
                (SHADOW_MAP_UNIFORM, SHADOW_MAP_LISPSM)):
            btn.value = val
            if val == self.shadowMapInfo.type:
                btn.setSelected()
            btn.addTarget(self, self.onShadowTypeChanged)
            btn.backgroundColor = dk.color.clear
            btn.setBlendState(dk.blendstate.defaultAlpha)

        cb = dk.ui.Checkbox('B-Vector', value=True, frame=bvRect)
        cb.addTarget(self, self.onShadowBVectorValueChanged)
        cb.backgroundColor = dk.color.clear
        cb.setBlendState(dk.blendstate.defaultAlpha)
        shadowConfigWindow.addChild(cb)
        cb = dk.ui.Checkbox('Multi-Sample', value=False, frame=msRect)
        cb.addTarget(self, self.onShadowMultiSampleValueChanged)
        cb.backgroundColor = dk.color.clear
        cb.setBlendState(dk.blendstate.defaultAlpha)
        shadowConfigWindow.addChild(cb)
        cb = dk.ui.Checkbox('Inspect', value=False, frame=imRect)
        cb.addTarget(self, self.onShadowInspdeModeValueChanged)
        cb.backgroundColor = dk.color.clear
        cb.setBlendState(dk.blendstate.defaultAlpha)
        shadowConfigWindow.addChild(cb)
        shadowConfigWindow.backgroundColor = dk.Color(1.0, 1.0, 1.0, 0.9)
        shadowConfigWindow.setBlendState(dk.blendstate.defaultAlpha)
        self.addChild(shadowConfigWindow)

        # info labels
        self.infoLabel = dk.ui.Label()
        self.infoLabel.fontAttributes = dk.ui.font.attributes(18)
        self.infoLabel.align = dk.ui.label.ALIGN_LEFT
        self.infoLabel.backgroundColor = dk.Color(0.0, 0.0, 0.0, 0.3)
        self.infoLabel.textColor = dk.Color(1.0, 1.0, 1.0, 1.0)
        self.infoLabel.outlineColor = dk.Color(0.0, 0.0, 0.0, 1.0)
        self.infoLabel.setBlendState(dk.blendstate.defaultAlpha)
        self.infoLabel.enabled = False
        self.addChild(self.infoLabel)

        self.screen().postOperation(self.layout, ())

        # load model
        resourcePool = dk.ResourcePool()
        resourcePool.addSearchPath(dk.appInstance().resourceDir + '/desert')

        self.scene = dk.Scene()
        self.model = resourcePool.loadResource('desert.DKMODEL')
        self.scene.addObject(self.model)
        self.scene.ambientColor = dk.Color(0.1, 0.1, 0.1)
        self.scene.lights.append(dk.light.directional(dk.Vector3(0, -1, 0), dk.Color(1, 1, 1)))
        self.scene.updateLights()

        # calculate aabb
        def calcAABB(model, aabb):
            if isinstance(model, dk.Mesh):
                aabbMin, aabbMax = model.aabb()
                if aabbMin.x < aabb.min.x:  aabb.min.x = aabbMin.x
                if aabbMin.y < aabb.min.y:  aabb.min.y = aabbMin.y
                if aabbMin.z < aabb.min.z:  aabb.min.z = aabbMin.z
                if aabbMax.x > aabb.max.x:  aabb.max.x = aabbMax.x
                if aabbMax.y > aabb.max.y:  aabb.max.y = aabbMax.y
                if aabbMax.z > aabb.max.z:  aabb.max.z = aabbMax.z
            for c in model.children():
                calcAABB(c, aabb)

        aabb = dk.shadow.AABB(dk.Vector3(0, 0, 0), dk.Vector3(0, 0, 0))
        print('calculating scene AABB...')
        calcAABB(self.model, aabb)
        print('AABB: ', aabb)
        self.shadowMapInfo.aabb = aabb

        # generate box polygon mesh
        decl = dk.geometry.VertexBufferDecl()
        decl.add(dk.geometry.STREAM_ID.POSITION, 'position', dk.geometry.STREAM_TYPE.FLOAT3)
        data = array.array('f')
        data.extend(( 1.0,  1.0, -1.0))   # near right top
        data.extend(( 1.0, -1.0, -1.0))   # near right bottom
        data.extend((-1.0, -1.0, -1.0))   # near left bottom
        data.extend((-1.0,  1.0, -1.0))   # near left top
        data.extend(( 1.0,  1.0,  1.0))   # far right top
        data.extend(( 1.0, -1.0,  1.0))   # far right bottom
        data.extend((-1.0, -1.0,  1.0))   # far left bottom
        data.extend((-1.0,  1.0,  1.0))   # far left top

        faceIndices = (2,1,3,0,7,4,6,5,        # 2,1,3 / 3,1,0 / 3,0,7 / 7,0,4 / 7,4,6 / 6,4,5
                   5,4,
                   4,0,5,1,6,2,7,3)        # 4,0,5 / 5,0,1 / 5,1,6 / 6,1,2 / 6,2,7 / 7,2,3

        lineIndices = (0,1,1,2,2,3,3,0,0,4,4,5,5,1,3,7,7,6,6,2,6,5,7,4)

        vb = dk.geometry.createVertexBuffer(decl, data, 8)
        faceIb = dk.geometry.createIndexBuffer(faceIndices, dk.geometry.PRIMITIVE_TRIANGLE_STRIP)
        lineIb = dk.geometry.createIndexBuffer(lineIndices, dk.geometry.PRIMITIVE_LINES)

        inspectMaterial = resourcePool.loadResource('inspect.DKMATERIAL')
        inspectFaceMesh = dk.StaticMesh()
        # inspectFaceMesh.drawFace = dk.mesh.DRAW_FACE_CCW
        inspectFaceMesh.drawFace = dk.mesh.DRAW_FACE_BOTH
        inspectFaceMesh.setMaterial(inspectMaterial)
        inspectFaceMesh.addVertexBuffer(vb)
        inspectFaceMesh.setIndexBuffer(faceIb)

        inspectLineMesh = dk.StaticMesh()
        inspectLineMesh.drawFace = dk.mesh.DRAW_FACE_BOTH
        inspectLineMesh.setMaterial(inspectMaterial)
        inspectLineMesh.addVertexBuffer(vb)
        inspectLineMesh.setIndexBuffer(lineIb)

        # inspect mode items.
        self.inspectScene = dk.Scene()
        self.cameraFrustumFaceMesh = inspectFaceMesh.clone()
        self.shadowFrustumFaceMesh = inspectFaceMesh.clone()
        self.cameraFrustumLineMesh = inspectLineMesh.clone()
        self.shadowFrustumLineMesh = inspectLineMesh.clone()

        self.cameraFrustumFaceMesh.setMaterialProperty('color', floatings=dk.Color(1,0,0,0.1).tuple)
        self.shadowFrustumFaceMesh.setMaterialProperty('color', floatings=dk.Color(0,0,1,0.1).tuple)
        self.cameraFrustumLineMesh.setMaterialProperty('color', floatings=dk.Color(1,1,1).tuple)
        self.shadowFrustumLineMesh.setMaterialProperty('color', floatings=dk.Color(1,1,1).tuple)

        self.inspectScene.addObject(self.cameraFrustumFaceMesh)
        self.inspectScene.addObject(self.shadowFrustumFaceMesh)
        self.inspectScene.addObject(self.cameraFrustumLineMesh)
        self.inspectScene.addObject(self.shadowFrustumLineMesh)
Exemplo n.º 8
0
Arquivo: kine.py Projeto: DKGL/DKDemo
 def objColorCb(obj, *args):
     if obj.isKinematic():
         return None
     return dk.Color(1, 0.2, 0.2)
Exemplo n.º 9
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.backgroundColor = dk.Color(0.6, 0.6, 0.6)
     self.demoFrame = None
Exemplo n.º 10
0
Arquivo: ui.py Projeto: DKGL/DKDemo
    def onLoaded(self):
        super().onLoaded()
        self.backgroundColor = dk.Color(1.0, 1.0, 1.0)

        resourcePool = dk.ResourcePool()
        resourcePool.addSearchPath(dk.appInstance().resourceDir)
        image = resourcePool.loadResource('koo.jpg')

        subMenu1 = dk.ui.Menu()
        subMenu1.addItem('Menu Item 1')
        subMenu1.addItem('Menu Item 2')
        subMenu1.addItem('Menu Item 3')
        subMenu1.addSeparator()
        subMenu2 = dk.ui.Menu()
        subMenu2.addItem('Sub Menu Item 1')
        subMenu2.addItem('Sub Menu Item 2')
        subMenu1.addItem('Sub Menu').subMenu = subMenu2
        subMenu3 = dk.ui.Menu()
        subMenu3.addItem('Menu Item 4').checked = True
        subMenu3.addItem('Menu Item 5').checked = True
        subMenu3.addItem('Menu Item 6')
        subMenu4 = dk.ui.Menu()
        subMenu4.addItem('Menu Item 7')
        subMenu4.addItem('Menu Item 8')

        self.menuBar = dk.ui.Menu()
        self.menuBar.addItem('Menu 1').subMenu = subMenu1
        self.menuBar.addItem('Menu 2').subMenu = subMenu3
        self.menuBar.addSeparator()
        self.menuBar.addItem('Menu 3').subMenu = subMenu4
        self.addChild(self.menuBar)

        self.button1 = dk.ui.Button('Button')
        self.button2 = dk.ui.Button('Image Button', image=image)

        self.addChild(self.button1)
        self.addChild(self.button2)

        self.checkbox1 = dk.ui.Checkbox('Check Box 1 - region based',
                                        value=True)
        self.checkbox1.backgroundColor = dk.Color(0.8, 0.8, 0.8)
        self.checkbox1.interactOnlyInsideVisibleContentRect = False
        self.checkbox2 = dk.ui.Checkbox('Check Box 2 - content based',
                                        value=True)
        self.checkbox2.interactOnlyInsideVisibleContentRect = True
        self.addChild(self.checkbox1)
        self.addChild(self.checkbox2)

        self.radio1 = dk.ui.RadioButton('Radio Button 1',
                                        group=1,
                                        frame=dk.Rect(10, 10, 140, 48))
        self.radio2 = dk.ui.RadioButton('Radio Button 2',
                                        group=1,
                                        frame=dk.Rect(10, 10, 140, 48))
        self.radio3 = dk.ui.RadioButton('Radio Button 3',
                                        group=1,
                                        frame=dk.Rect(10, 10, 140, 48))
        self.radio1.setSelected()
        self.addChild(self.radio1)
        self.addChild(self.radio2)
        self.addChild(self.radio3)

        self.treeView = dk.ui.TreeView(frame=dk.Rect(20, 20, 200, 400))
        self.treeView.contentSize = dk.Size(300, 500)

        item1 = self.treeView.addItem('Tree Item 1')
        item11 = self.treeView.addItem('Sub Item 1', parent=item1)
        item12 = self.treeView.addItem('Sub Item 2', parent=item1)
        item11.state = item11.STATE_DISABLED
        self.treeView.addItem('Sub Sub Item 1', parent=item11)
        self.treeView.addItem('Sub Sub Item 2', parent=item12)
        item2 = self.treeView.addItem('Tree Item 2')
        item21 = self.treeView.addItem('Sub Item 1', parent=item2)

        for i in range(3, 10):
            item = self.treeView.addItem('Tree Item {}'.format(i))
            if i < 4:
                for j in range(1, 4):
                    item2 = self.treeView.addItem('Sub Item {}'.format(j),
                                                  parent=item)
                    for k in range(1, 3):
                        self.treeView.addItem('Sub Sub Item {}'.format(k),
                                              parent=item2)

        self.treeView.borderWidth = 1
        self.treeView.scrollBarBorderWidth = 1
        self.treeView.showVerticalScrollBar = True
        self.treeView.showHorizontalScrollBar = True
        self.treeView.showZoomButton = True
        self.treeView.leftScrollBar = True
        self.treeView.expandAllItems()
        self.addChild(self.treeView)

        self.textField = dk.ui.TextField('Text Input Field')
        self.addChild(self.textField)

        self.progressView = dk.ui.View()
        self.progressView.backgroundColor = dk.Color(0.9, 0.9, 0.9)
        self.addChild(self.progressView)

        progressAnim = 20.0
        prog1 = dk.ui.ProgressView(frame=dk.Rect(30, 10, 160, 20))
        prog1.progressAnimation = progressAnim
        prog1.setProgress(1)
        prog2 = dk.ui.ProgressView(frame=dk.Rect(10, 30, 20, 120))
        prog2.backgroundColor = dk.Color(0.4, 0.4, 0.4)
        prog2.progressColor = dk.Color(1.0, 1.0, 0.7)
        prog2.progressAnimation = progressAnim
        prog2.vertical = True
        prog2.setProgress(1)
        prog3 = dk.ui.ProgressView(circularType=True,
                                   frame=dk.Rect(70, 50, 100, 100))
        prog3.progressColor = dk.Color(0.7, 0.7, 1.0)
        prog3.progressAnimation = progressAnim
        prog3.borderWidth = 1
        prog3.innerCircleRadius = 40
        prog3.setProgress(1)
        prog4 = dk.ui.ProgressView(circularType=True,
                                   frame=dk.Rect(40, 40, 30, 30))
        prog4.progressColor = dk.Color(1.0, 0.7, 0.7)
        prog4.progressAnimation = progressAnim
        prog4.borderWidth = 1
        prog4.innerCircleRadius = 0
        prog4.setProgress(1)

        self.progressView.addChild(
            dk.ui.Label('Progress Views', frame=dk.Rect(2, 170, 196, 28)))
        self.progressView.addChild(prog1)
        self.progressView.addChild(prog2)
        self.progressView.addChild(prog3)
        self.progressView.addChild(prog4)

        class MyScrollView(dk.ui.ScrollView, dk.ui.TitledView):
            caption = 'Popup Window (Image scroll)'
            captionHeight = 24
            fontAttributes = dk.ui.font.attributes(14)
            pass

        imageView = dk.ui.ImageView(image=image, frame=dk.Rect(0, 0, 300, 300))
        imageView.enabled = False
        sv = MyScrollView(contentSize=dk.Size(imageView.frame.size),
                          frame=dk.Rect(500, 120, 250, 250))
        sv.addChild(imageView)
        sv.borderWidth = 1
        sv.showHorizontalScrollBar = True
        sv.showVerticalScrollBar = True
        sv.showZoomButton = True
        self.addChild(sv)
Exemplo n.º 11
0
 def chassisColor(co, *args):
     if co is self.carChassis:
         return dk.Color(1.0, 1.0, 1.0), dk.Color(0.0, 0.0, 0.0)
     if co.mass() > 0:
         return dk.Color(1.0, 0.0, 0.0), dk.Color(0.0, 0.0, 0.0)
     return args
Exemplo n.º 12
0
    def onLoaded(self):
        super().onLoaded()

        resourceDir = dk.appInstance().resourceDir
        self.resourcePool = dk.ResourcePool()

        # self.resourcePool.addSearchPath(resourceDir)
        self.resourcePool.addSearchPath(resourceDir)

        # create simulator scene
        self.scene = dk.DynamicsScene()
        self.scene.setAmbientColor(dk.color.white)
        self.scene.lights = [
            dk.light.directional(dk.Vector3(-1, -1, -1), dk.Color(1, 1, 1))
        ]
        self.scene.updateLights()
        # self.scene.setDrawMode(dk.scene.DRAW_MESHES, dk.scene.DRAW_COLLISION_SHAPES)
        self.scene.drawMode = dk.scene.DRAW_COLLISION_SHAPES
        self.scene.lights.append(dk.Light())

        # ground object
        groundShape = dk.BoxShape(500, 100, 500)
        self.groundObject = createRigidBody(0, groundShape,
                                            dk.Vector3(0, -100, 0),
                                            'groundObject')

        self.scene.addObject(self.groundObject)

        # vehicle chassis
        chassisShape = dk.CompoundShape()
        chassisShape.addChild(dk.BoxShape(1, 0.5, 2), dk.Vector3(0, 1, 0))
        chassisShape.addChild(dk.BoxShape(0.85, 0.3, 0.9),
                              dk.Vector3(0, 1.8, -0.3))

        self.carChassis = createRigidBody(800,
                                          chassisShape,
                                          name='car chassis')
        #self.carChassis.setWorldTransform(dk.NSTransform(dk.Quaternion(dk.Vector3(0,0,1), math.pi * 0.3), dk.Vector3(0,10,0)))

        self.scene.addObject(self.carChassis)

        self.resetScene()

        # vehicle object
        self.vehicle = dk.controller.Vehicle(self.carChassis)
        self.carChassis.keepActivating(True)

        # add vehicle into scene
        self.scene.addObject(self.vehicle)

        WHEEL_WIDTH = 0.4
        WHEEL_RADIUS = 0.5

        # wheel shape
        wheelShape = dk.CylinderShape(WHEEL_WIDTH, WHEEL_RADIUS, WHEEL_RADIUS,
                                      dk.collisionshape.UP_AXIS_LEFT)
        self.wheelShape = wheelShape
        self.wheelTrans = []

        CONNECTION_HEIGHT = 1.2
        CUBE_HALF_EXTENTS = 1.0

        suspensionResetLength = 0.6
        wheelDirectionCS0 = dk.Vector3(0, -1, 0)
        wheelAxleCS = dk.Vector3(-1, 0, 0)

        tunning = {
            'suspensionStiffness': 20.0,
            'suspensionDamping': 2.3,
            'suspensionCompression': 4.4,
            'frictionSlip': 1000.0,
            'rollInfluence': 0.1
        }

        xvalues = (CUBE_HALF_EXTENTS - (0.3 * WHEEL_WIDTH),
                   -CUBE_HALF_EXTENTS + (0.3 * WHEEL_WIDTH),
                   -CUBE_HALF_EXTENTS + (0.3 * WHEEL_WIDTH),
                   CUBE_HALF_EXTENTS - (0.3 * WHEEL_WIDTH))
        zvalues = (2 * CUBE_HALF_EXTENTS - WHEEL_RADIUS,
                   2 * CUBE_HALF_EXTENTS - WHEEL_RADIUS,
                   -2 * CUBE_HALF_EXTENTS + WHEEL_RADIUS,
                   -2 * CUBE_HALF_EXTENTS + WHEEL_RADIUS)

        for x, z in zip(xvalues, zvalues):
            connectionPointCS0 = dk.Vector3(x, CONNECTION_HEIGHT, z)
            print('connectionPointCS0: ', connectionPointCS0)
            self.vehicle.addWheel(connectionPointCS0, wheelDirectionCS0,
                                  wheelAxleCS, suspensionResetLength,
                                  WHEEL_RADIUS, **tunning)

        self.vehicle.resetSuspension()
        for wheel in self.vehicle.wheels:
            self.vehicle.updateWheelTransform(wheel)

        # shooting box!
        self.shootingShapes = (dk.BoxShape(0.5, 0.5,
                                           0.5), dk.BoxShape(1.0, 0.5, 1.0),
                               dk.BoxShape(2.0, 0.5, 1.0),
                               dk.CylinderShape(1.0, 1.0, 1.0),
                               dk.CylinderShape(1.5, 0.5,
                                                1.5), dk.ConeShape(1.0, 1.5),
                               dk.ConeShape(2.5, 1.5), dk.SphereShape(1.0))

        # ui buttons
        self.accelButton = SimpleButton('Acc', frame=dk.Rect(100, 100, 80, 80))
        self.accelButton.backgroundColor = dk.color.blue
        self.brakeButton = SimpleButton('Brake',
                                        frame=dk.Rect(10, 100, 80, 80))
        self.brakeButton.backgroundColor = dk.color.red
        self.cameraButton = dk.ui.Button(text='Reset Camera',
                                         frame=dk.Rect(10, 10, 80, 50))
        self.cameraButton.addTarget(self, self.resetCamera)

        self.addChild(self.accelButton)
        self.addChild(self.brakeButton)
        self.addChild(self.cameraButton)

        self.steerSlider = dk.ui.Slider(frame=dk.Rect(200, 100, 200, 50))
        self.steerSlider.thumbRadius = 20
        self.addChild(self.steerSlider)
        self.steerSlider.minimumValue = -0.3
        self.steerSlider.maximumValue = 0.3
        self.steerSlider.value = 0.0
        self.steerSlider.addTarget(self, self.onSteerChanged)

        # info labels
        self.infoLabel = dk.ui.Label()
        self.infoLabel.align = dk.ui.label.ALIGN_LEFT
        self.infoLabel.backgroundColor = dk.Color(0, 0, 0, 0.2)
        self.infoLabel.textColor = dk.Color(1, 1, 1, 1)
        self.infoLabel.outlineColor = dk.Color(0, 0, 0, 0.85)
        self.infoLabel.drawTextOutline = True
        self.addChild(self.infoLabel)

        # help labels
        self.helpText = dk.ui.Label(
            'Arrow Keys: move, left-click: move camera, right-click: shoot obstacle'
        )
        self.helpText.fontAttributes = dk.ui.font.attributes(14)
        self.helpText.backgroundColor = dk.color.clear
        self.helpText.outlineColor = dk.Color(0.2, 0.2, 0.2)
        self.helpText.textColor = dk.color.white
        self.helpText.setBlendState(dk.blendstate.defaultAlpha)
        self.addChild(self.helpText)

        self.captureKeyboard(0)
        #self.resetCamera()
        self.screen().postOperation(self.layout, ())
        self.autoShootingCount = 0
        self.autoShooting = self.screen().scheduleOperation(
            self.shootBoxFromAbove, (), interval=self.shootingInterval)