Exemple #1
0
def randomRot(allowCameraMoving=False, printInfo=True):
    global mappedTo
    maxspeed = gf.get_speed()
    dim = gf.get_dimen()

    # Create two dimensional list of angular velocities (one for every pair of axes)
    velocities = [0]
    for i in range(1, dim + 1):
        velocities = velocities + [[0] * i]

    if allowCameraMoving:
        # Create list of camera distances (in different levels of persp. proj.) and list of their current speeds
        camPosL = [0] * (dim + 1)
        for i in range(3, dim + 1):
            camPosL[i] = gf.get_camposl(i)
        camPosLAcc = [0] * (dim + 1)

    lastTime = gf.time()
    minDelay = 1000 / gf.get_maxfps()
    while gf.sleep(
            minDelay
    ):  # Repeat till user abort; repaint and wait at least 1ms before every course
        t = gf.time()  # Time measuring to deal with different sleeping time
        if printInfo:
            gf.clear()
            # echo prints every line below the previous one, clearing is necessary
            gf.echo("--- Random rotation ---")

        # Rotate figure and write velocities to console
        for i in range(1, dim + 1):
            for j in range(1, i):
                velocities[i][j] = velocities[i][j] + (random.random() -
                                                       0.5) * maxspeed / 20
                if velocities[i][j] > maxspeed / 2:
                    velocities[i][j] = maxspeed / 2
                if velocities[i][j] < -maxspeed / 2:
                    velocities[i][j] = -maxspeed / 2
                if printInfo:
                    gf.echo("  {}{}: {:6.2f} deg/sec".format(
                        j, i, -velocities[i][j]))
                gf.rotate(i, j, velocities[i][j] * (t - lastTime) / 1000)

        if allowCameraMoving:
            # Change camera distances and write new values to console
            for i in range(3, dim + 1):
                camPosLAcc[i] = camPosLAcc[i] + (random.random() - 0.5) / 64
                if camPosLAcc[i] < 0:
                    if camPosLAcc[i] < -0.01:
                        camPosLAcc[i] = -0.01
                    camPosL[i] = camPosL[i] + (camPosL[i] -
                                               0.2) * camPosLAcc[i]
                if camPosLAcc[i] > 0:
                    if camPosLAcc[i] > 0.01:
                        camPosLAcc[i] = 0.01
                    camPosL[i] = camPosL[i] + (10 - camPosL[i]) * camPosLAcc[i]
                if printInfo:
                    gf.echo("  camposl{}={:6.2f}".format(i, camPosL[i]))
                gf.set_camposl(i, camPosL[i])

        lastTime = t
        if printInfo:
            if auto:
                gf.echo("Automatic rotation enabled")
            else:
                gf.echo("Automatic rotation disabled")
        if mappedTo:
            if printInfo:
                gf.clearAfterCmd(
                    "Press " + mappedTo +
                    " to toggle camera moving or any other key to stop moving"
                )  # Remove only this line when user types command
            if allowCameraMoving:
                gf.map(mappedTo, "randomRot.randomRot(False)"
                       )  # Tab always rotates without camera moving (default)
            else:
                gf.map(
                    mappedTo, "randomRot.randomRot(gf.time()<" +
                    str(gf.time() + 100) + ")"
                )  # Pressing tab in 100ms will rotate with camera moving
                # If tab was used to abort rotation, the same press will resume it
        else:
            if printInfo:
                gf.clearAfterCmd("Press any key to stop moving")
def randomRot(allowCameraMoving=False, printInfo=True):
    global mappedTo
    maxspeed = gf.get_speed()
    dim = gf.get_dimen()

    # Create two dimensional list of angular velocities (one for every pair of axes)
    velocities = [0]
    for i in range(1, dim + 1):
        velocities = velocities + [[0] * i]

    if allowCameraMoving:
        # Create list of camera distances (in different levels of persp. proj.) and list of their current speeds
        camPosL = [0] * (dim + 1)
        for i in range(3, dim + 1):
            camPosL[i] = gf.get_camposl(i)
        camPosLAcc = [0] * (dim + 1)

    lastTime = gf.time()
    minDelay = 1000 / gf.get_maxfps()
    while gf.sleep(minDelay):  # Repeat till user abort; repaint and wait at least 1ms before every course
        t = gf.time()  # Time measuring to deal with different sleeping time
        if printInfo:
            gf.clear()
            # echo prints every line below the previous one, clearing is necessary
            gf.echo("--- Random rotation ---")

            # Rotate figure and write velocities to console
        for i in range(1, dim + 1):
            for j in range(1, i):
                velocities[i][j] = velocities[i][j] + (random.random() - 0.5) * maxspeed / 20
                if velocities[i][j] > maxspeed / 2:
                    velocities[i][j] = maxspeed / 2
                if velocities[i][j] < -maxspeed / 2:
                    velocities[i][j] = -maxspeed / 2
                if printInfo:
                    gf.echo("  {}{}: {:6.2f} deg/sec".format(j, i, -velocities[i][j]))
                gf.rotate(i, j, velocities[i][j] * (t - lastTime) / 1000)

        if allowCameraMoving:
            # Change camera distances and write new values to console
            for i in range(3, dim + 1):
                camPosLAcc[i] = camPosLAcc[i] + (random.random() - 0.5) / 64
                if camPosLAcc[i] < 0:
                    if camPosLAcc[i] < -0.01:
                        camPosLAcc[i] = -0.01
                    camPosL[i] = camPosL[i] + (camPosL[i] - 0.2) * camPosLAcc[i]
                if camPosLAcc[i] > 0:
                    if camPosLAcc[i] > 0.01:
                        camPosLAcc[i] = 0.01
                    camPosL[i] = camPosL[i] + (10 - camPosL[i]) * camPosLAcc[i]
                if printInfo:
                    gf.echo("  camposl{}={:6.2f}".format(i, camPosL[i]))
                gf.set_camposl(i, camPosL[i])

        lastTime = t
        if printInfo:
            if auto:
                gf.echo("Automatic rotation enabled")
            else:
                gf.echo("Automatic rotation disabled")
        if mappedTo:
            if printInfo:
                gf.clearAfterCmd(
                    "Press " + mappedTo + " to toggle camera moving or any other key to stop moving"
                )  # Remove only this line when user types command
            if allowCameraMoving:
                gf.map(mappedTo, "randomRot.randomRot(False)")  # Tab always rotates without camera moving (default)
            else:
                gf.map(
                    mappedTo, "randomRot.randomRot(gf.time()<" + str(gf.time() + 100) + ")"
                )  # Pressing tab in 100ms will rotate with camera moving
                # If tab was used to abort rotation, the same press will resume it
        else:
            if printInfo:
                gf.clearAfterCmd("Press any key to stop moving")
def idle():
    global axes
    global buttons
    global lock
    global idleTime
    if not axes:
        if not os.access(device, os.R_OK):
            off()
            raise IOError("Device " + device + " cannot be opened")
        axes = [0] * 6

        def handler():
            global axes
            global device
            global buttons
            global lock
            eventFormat = "llHHi"
            eventSize = struct.calcsize(eventFormat)
            openTime = time.time()
            skipping = True
            try:
                dev = open(device, "rb")
                event = dev.read(eventSize)
            except IOError as ex:
                with lock:
                    axes = None
                    raise ex
            while event:
                if skipping:  # events that happened before opening the device
                    if time.time() - openTime > 0.1:
                        skipping = False
                    event = dev.read(eventSize)
                    continue
                (tv_sec, tv_usec, type, code,
                 value) = struct.unpack(eventFormat, event)
                if type == 2:
                    with lock:
                        axes[code] += value
                elif type == 1 and value == 1:
                    buttons.append(code)
                try:
                    event = dev.read(eventSize)
                except IOError as ex:
                    with lock:
                        axes = None
                        raise ex

        t = threading.Thread(target=handler, args=())
        t.daemon = True
        t.start()

    currTime = gf.time()
    if currTime - idleTime > 100:
        with lock:
            axes = [0] * 6
        del buttons[:]
    else:
        dim = gf.get_dimen()
        with lock:
            if not axes:
                return
            (right, near, down, tiltNear, tiltLeft, rotRight) = axes
            axes = [0] * 6
        if dim >= 2:
            if tiltLeft:  # ifs needed not to force repainting
                gf.rotate(1, 2, sensitivity * tiltLeft)
        if dim >= 3:
            if tiltNear:
                gf.rotate(2, 3, sensitivity * tiltNear)
            if rotRight:
                gf.rotate(1, 3, sensitivity * rotRight)
        if dim >= 4:
            if right:
                gf.rotate(4, 1, sensitivity * right)
            if near:
                gf.rotate(4, 3, sensitivity * near)
            if down:
                gf.rotate(2, 4, sensitivity * down)

        while len(buttons):
            gf.clear()
            if buttons.pop() == 256:
                if leftBtnFunc:
                    leftBtnFunc()
            else:
                if rightBtnFunc:
                    rightBtnFunc()
    idleTime = currTime
def idle():
	global axes
	global buttons
	global lock
	global idleTime
	if not axes:
		if not os.access(device, os.R_OK):
			off()
			raise IOError("Device " + device + " cannot be opened")
		axes=[0]*6
		def handler():
			global axes
			global device
			global buttons
			global lock
			eventFormat="llHHi"
			eventSize=struct.calcsize(eventFormat)
			openTime=time.time()
			skipping=True
			try:
				dev=open(device, "rb")
				event=dev.read(eventSize)
			except IOError as ex:
				with lock:
					axes=None
					raise ex
			while event:
				if skipping: # events that happened before opening the device
					if time.time()-openTime > 0.1:
						skipping=False
					event=dev.read(eventSize)
					continue
				(tv_sec, tv_usec, type, code, value)=struct.unpack(eventFormat, event)
				if type==2:
					with lock:
						axes[code]+=value
				elif type==1 and value==1:
					buttons.append(code)
				try:
					event=dev.read(eventSize)
				except IOError as ex:
					with lock:
						axes=None
						raise ex
		t=threading.Thread(target=handler, args=())
		t.daemon=True
		t.start()

	currTime=gf.time()
	if currTime-idleTime>100:
		with lock:
			axes=[0]*6
		del buttons[:]
	else:
		dim=gf.get_dimen()
		with lock:
			if not axes:
				return;
			(right, near, down, tiltNear, tiltLeft, rotRight)=axes
			axes=[0]*6
		if dim>=2:
			if tiltLeft: # ifs needed not to force repainting
				gf.rotate(1, 2, sensitivity*tiltLeft)
		if dim>=3:
			if tiltNear:
				gf.rotate(2, 3, sensitivity*tiltNear)
			if rotRight:
				gf.rotate(1, 3, sensitivity*rotRight)
		if dim>=4:
			if right:
				gf.rotate(4, 1, sensitivity*right)
			if near:
				gf.rotate(4, 3, sensitivity*near)
			if down:
				gf.rotate(2, 4, sensitivity*down)

		while len(buttons):
			gf.clear()
			if buttons.pop()==256:
				if leftBtnFunc:
					leftBtnFunc()
			else:
				if rightBtnFunc:
					rightBtnFunc()
	idleTime=currTime