Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- init python:
- import pygame_sdl2 as pygame
- class __VirtualJoystick(renpy.Displayable):
- __author__ = u"Vladya"
- def __init__(self):
- super(self.__class__, self).__init__()
- self.__stick_is_pressed = False
- self.__size = None
- self.__xalign = self.__yalign = .5
- @property
- def stick(self):
- u"""
- Забирать положение стика отсюда.
- Формат возрата - кортеж (x, y).
- Где каждое значение определяет множитель скорости передвижения
- объекта от -1, до 1 ввключительно, по соответствующей оси.
- """
- return tuple(
- map(
- lambda a: ((a * 2.) - 1.),
- (self.__xalign, self.__yalign)
- )
- )
- def get_circle(self, radius, color=u"fff"):
- u"""
- Программно отрисовывает круг.
- :radius:
- Радиус круга.
- :color:
- Цвет кортежом, или в 16ричном виде.
- """
- radius = int(radius)
- diameter = radius * 2
- canvas = renpy.Render(diameter, diameter).canvas()
- canvas.circle(color=color, pos=(radius, radius), radius=radius)
- return (canvas.surf, diameter)
- def event(self, ev, x, y, st):
- if not self.__size:
- return
- w, h, x, y = map(float, (self.__size + (x, y)))
- if ev.type == pygame.MOUSEBUTTONDOWN:
- self.__stick_is_pressed = bool(
- ((.0 <= x <= w) and (.0 <= y <= h))
- )
- elif ev.type == pygame.MOUSEBUTTONUP:
- self.__stick_is_pressed = False
- if self.__stick_is_pressed:
- self.__xalign, self.__yalign = map(
- lambda a: max(min(a, 1.), .0),
- ((x / w), (y / h))
- )
- else:
- self.__xalign = self.__yalign = .5
- def render(self, width, height, st, at):
- circle_radius = min((width * .1), (height * .1))
- big_circle, bigD = self.get_circle(circle_radius)
- smallMultipler = (.8 if self.__stick_is_pressed else 1.)
- small_circle, smallD = self.get_circle(
- (circle_radius * .5 * smallMultipler),
- u"000"
- )
- baseRenderObject = renpy.Render(bigD, bigD)
- baseRenderObject.blit(big_circle, (0, 0))
- smallXPos, smallYPos = map(
- lambda align: int(((bigD * align) - (smallD * align))),
- (self.__xalign, self.__yalign)
- )
- baseRenderObject.blit(small_circle, (smallXPos, smallYPos))
- self.__size = (bigD, bigD)
- return baseRenderObject
- class JoystickMove(renpy.Displayable):
- __author__ = u"Vladya"
- joystickAlign = (.05, .95)
- def __init__(self, child, time_speed=3.):
- u"""
- :child:
- Объект, для перемещения.
- :time_speed:
- Время в секундах, на полное перемещение
- от одного конца экрана, до другого.
- """
- super(self.__class__, self).__init__()
- self.__child = child
- self.__joystick = __VirtualJoystick()
- self.__time_speed = float(time_speed)
- self.__jxPos = self.__jyPos = 0
- self.__last_st = .0
- self.__childXAlign = self.__childYAlign = .5
- @property
- def align(self):
- u"""
- Отсюда забирать текущее относительное положение на экране.
- Тип данных: Кортеж вида (xalign, yalign).
- """
- return (self.__childXAlign, self.__childYAlign)
- def event(self, ev, x, y, st):
- self.__joystick.event(
- ev,
- (x - self.__jxPos),
- (y - self.__jyPos),
- st
- )
- def render(self, width, height, st, at):
- stickXSpeed, stickYSpeed = self.__joystick.stick
- increment = (st - self.__last_st) / self.__time_speed
- xIncrement = stickXSpeed * increment
- yIncrement = stickYSpeed * increment
- newX, newY = (
- (self.__childXAlign + xIncrement),
- (self.__childYAlign + yIncrement)
- )
- self.__childXAlign, self.__childYAlign = map(
- lambda a: max(min(a, 1.), .0),
- (newX, newY)
- )
- child = self.__child.render(width, height, st, at)
- childXSize, childYSize = child.get_size()
- childXPos = int(
- (
- (
- (width * self.__childXAlign)
- ) - (
- (childXSize * self.__childXAlign)
- )
- )
- )
- childYPos = int(
- (
- (
- (height * self.__childYAlign)
- ) - (
- (childYSize * self.__childYAlign)
- )
- )
- )
- baseRenderObject = renpy.Render(*map(int, (width, height)))
- joystick = self.__joystick.render(width, height, st, at)
- jw, jh = joystick.get_size()
- jxAl, jyAl = self.joystickAlign
- self.__jxPos = int(((width * jxAl) - (jw * jxAl)))
- self.__jyPos = int(((height * jyAl) - (jh * jyAl)))
- baseRenderObject.blit(child, (childXPos, childYPos))
- baseRenderObject.blit(joystick, (self.__jxPos, self.__jyPos))
- self.__last_st = st
- renpy.redraw(self, .0)
- return baseRenderObject
- label start:
- # Юзать либо как трансу, либо как обёртку для изображения.
- show expression Text(u"Test 123", size=150) at JoystickMove
- $ ui.interact()
- return
Add Comment
Please, Sign In to add comment