skelform-pygame 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of skelform-pygame might be problematic. Click here for more details.
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
import skelform_python
|
|
3
|
+
import math
|
|
4
|
+
import copy
|
|
5
|
+
import zipfile
|
|
6
|
+
import json
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def load_skelform(path):
|
|
10
|
+
with zipfile.ZipFile(path, "r") as zip_file:
|
|
11
|
+
skelform_root = json.load(zip_file.open("armature.json"))
|
|
12
|
+
texture_img = pygame.image.load(zip_file.open("textures.png"))
|
|
13
|
+
|
|
14
|
+
return (skelform_root, texture_img)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class AnimOptions:
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
# Offset armature's position by this much.
|
|
21
|
+
pos_offset: pygame.Vector2 = pygame.Vector2(0, 0),
|
|
22
|
+
# Scale armature by a factor of this.
|
|
23
|
+
scale_factor=0.25,
|
|
24
|
+
# Should the armature immediately be rendered?
|
|
25
|
+
# Set to False if you would like to process the armature some more before rendering.
|
|
26
|
+
render=True,
|
|
27
|
+
):
|
|
28
|
+
self.pos_offset = pos_offset
|
|
29
|
+
self.scale_factor = scale_factor
|
|
30
|
+
self.render = render
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# Animate a SkelForm armature.
|
|
34
|
+
def animate(
|
|
35
|
+
screen,
|
|
36
|
+
armature,
|
|
37
|
+
texture_img,
|
|
38
|
+
anim_idx,
|
|
39
|
+
frame=-1,
|
|
40
|
+
elapsed_time=-1,
|
|
41
|
+
anim_options=AnimOptions(),
|
|
42
|
+
):
|
|
43
|
+
if elapsed_time != -1:
|
|
44
|
+
frame = get_frame_by_time(armature, anim_idx, elapsed_time, False)
|
|
45
|
+
|
|
46
|
+
props = skelform_python.animate(armature, anim_idx, frame)
|
|
47
|
+
|
|
48
|
+
ao = anim_options
|
|
49
|
+
|
|
50
|
+
for prop in props:
|
|
51
|
+
if prop["tex_idx"] == -1:
|
|
52
|
+
continue
|
|
53
|
+
|
|
54
|
+
tex = armature["textures"][prop["tex_idx"]]
|
|
55
|
+
tex_surf = clip(
|
|
56
|
+
texture_img,
|
|
57
|
+
tex["offset"]["x"],
|
|
58
|
+
tex["offset"]["y"],
|
|
59
|
+
tex["size"]["x"],
|
|
60
|
+
tex["size"]["y"],
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
scale_x = ao.scale_factor * prop["scale"]["x"]
|
|
64
|
+
scale_y = ao.scale_factor * prop["scale"]["y"]
|
|
65
|
+
tex_surf = pygame.transform.scale_by(
|
|
66
|
+
tex_surf,
|
|
67
|
+
(scale_x, scale_y),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# pygame treats positive y as down
|
|
71
|
+
prop["pos"]["y"] = -prop["pos"]["y"]
|
|
72
|
+
|
|
73
|
+
# adjust positions for scale factor
|
|
74
|
+
# actual scale is already accounted for in core logic
|
|
75
|
+
prop["pos"]["x"] *= ao.scale_factor
|
|
76
|
+
prop["pos"]["y"] *= ao.scale_factor
|
|
77
|
+
|
|
78
|
+
# push textures back left and up so that it's centered
|
|
79
|
+
prop["pos"]["x"] -= tex_surf.get_size()[0] / 2
|
|
80
|
+
prop["pos"]["y"] -= tex_surf.get_size()[1] / 2
|
|
81
|
+
|
|
82
|
+
deg = prop["rot"] * 180 / 3.14
|
|
83
|
+
(tex_surf, rect) = rot_center(tex_surf, tex_surf.get_rect(), deg)
|
|
84
|
+
|
|
85
|
+
if not ao.render:
|
|
86
|
+
continue
|
|
87
|
+
|
|
88
|
+
screen.blit(
|
|
89
|
+
tex_surf,
|
|
90
|
+
rect.move(
|
|
91
|
+
prop["pos"]["x"] + ao.pos_offset.x,
|
|
92
|
+
prop["pos"]["y"] + ao.pos_offset.y,
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
return props
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def get_frame_by_time(armature, anim_idx, elapsed, reverse):
|
|
100
|
+
return skelform_python.get_frame_by_time(armature, anim_idx, elapsed, reverse)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
# https://stackoverflow.com/a/71370036
|
|
104
|
+
def clip(surface, x, y, x_size, y_size): # Get a part of the image
|
|
105
|
+
handle_surface = surface.copy() # Sprite that will get process later
|
|
106
|
+
clipRect = pygame.Rect(x, y, x_size, y_size) # Part of the image
|
|
107
|
+
handle_surface.set_clip(clipRect) # Clip or you can call cropped
|
|
108
|
+
image = surface.subsurface(handle_surface.get_clip()) # Get subsurface
|
|
109
|
+
return image.copy() # Return
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
# https://www.pygame.org/wiki/RotateCenter
|
|
113
|
+
def rot_center(image, rect, angle):
|
|
114
|
+
rot_image = pygame.transform.rotate(image, angle)
|
|
115
|
+
rot_rect = rot_image.get_rect(center=rect.center)
|
|
116
|
+
return rot_image, rot_rect
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: skelform_pygame
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: SkelForm runtime for Pygame. Uses the generic runtime
|
|
5
|
+
Author-email: Retropaint <darkglasses1122@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Requires-Python: >=3.9
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# SkelForm Pygame
|
|
13
|
+
|
|
14
|
+
SkelForm runtime for Pygame. Uses the [generic runtime](https://pypi.org/project/skelform-python/)
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
skelform_pygame/__init__.py,sha256=g89dou77P_GzkwEAmJmmV-dRlI5BqUWCTKdWMEMBpq4,3393
|
|
2
|
+
skelform_pygame-0.1.0.dist-info/METADATA,sha256=4xCg2o2KVIZMSSsGLedEtEjeNh2nwMH_O2sSJ4IQENc,476
|
|
3
|
+
skelform_pygame-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
4
|
+
skelform_pygame-0.1.0.dist-info/RECORD,,
|