Code: Select all
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 "" "
Take the stick position from here.
The format of the return is a tuple (x, y).
Where each value determines the travel speed multiplier
object from -1 to 1 in inclusive, along the corresponding axis.
"" "
return tuple (
map (
lambda a: ((a * 2.) - 1.),
(self .__ xalign, self .__ yalign)
)
)
def get_circle (self, radius, color = u "fff"):
u "" "
Programmatically draws a circle.
: radius:
The radius of the circle.
: color:
Color tuple, or in hexadecimal.
"" "
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:
The object to move.
: time_speed:
Time in seconds, for full movement
from one end of the screen to the other.
"" "
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 "" "
From here take the current relative position on the screen.
Data type: The tuple of the form (xalign, yalign).
"" "
return (self .__ childXAlign, self .__ childYAlign)
def event (self, ev, x, y, st):
self .__ joystick.event (
ev,
(x is 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:
# Use either as a trance, or as a wrapper for an image.
show expression Text (u "Test 123", size = 150) at JoystickMove
$ ui.interact ()
return
https://pastebin.com/Nz6E5izG
video https://m.vk.com/video28531892_4562394 ... 3243_42433