-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathanimation-maker.py
More file actions
3136 lines (2819 loc) · 185 KB
/
animation-maker.py
File metadata and controls
3136 lines (2819 loc) · 185 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import csv
import os
import random
import re
import sys
import time
from math import atan2, degrees, radians
from os import sep
from os.path import join, split, normpath, abspath
from pathlib import Path
import pygame
from PIL import Image, ImageFilter, ImageEnhance, ImageOps
from pygame.transform import smoothscale, rotate, flip as pyflip
from engine.data.datalocalisation import Localisation
from engine.data.datasound import SoundData
from engine.game.game import Game
from engine.uibattle.uibattle import UIScroll
from engine.uimenu.uimenu import MenuCursor, NameList, MenuButton, TextPopup, InputUI, InputBox, ListBox
from engine.utils.data_loading import csv_read, load_image, load_images, load_base_button, recursive_image_load, \
filename_convert_readable as fcv
from engine.utils.rotation import rotation_xy
from engine.utils.sprite_altering import sprite_rotate, apply_sprite_effect, apply_sprite_colour
main_dir = split(abspath(__file__))[0]
main_data_dir = join(main_dir, "data")
current_dir = join(main_dir, "animation-maker") # animation maker folder
current_data_dir = join(main_dir, "animation-maker", "data") # animation maker folder
sys.path.insert(1, current_dir)
from script import listpopup, pool, showroom # keep here as it need to get sys path insert
setup_list = listpopup.setup_list
list_scroll = listpopup.list_scroll
popup_list_open = listpopup.popup_list_open
read_anim_data = pool.read_anim_data
anim_to_pool = pool.anim_to_pool
anim_save_pool = pool.anim_save_pool
anim_del_pool = pool.anim_del_pool
screen_size = (1300, 900)
screen_scale = (1, 1)
inf = float("inf")
pygame.init()
pen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Animation Maker") # set the self name on program border/tab
pygame.mouse.set_visible(False) # set mouse as invisible, use cursor object instead
data_dir = join(main_dir, "data")
animation_dir = join(data_dir, "animation")
language = "en"
default_sprite_size = (500, 500)
ui = pygame.sprite.LayeredUpdates()
fake_group = pygame.sprite.LayeredUpdates() # just fake group to add for container and not get auto update
Game.main_dir = main_dir
Game.data_dir = data_dir
Game.screen_size = screen_size
Game.screen_scale = screen_scale
Game.language = language
Game.ui_font = csv_read(data_dir, "ui_font.csv", ("ui",), header_key=True)
Game.font_dir = join(data_dir, "font")
Game.ui_updater = ui
localisation = Localisation()
Game.localisation = localisation
for item in Game.ui_font: # add ttf file extension for font data reading.
Game.ui_font[item] = join(Game.font_dir, Game.ui_font[item]["Font"] + ".ttf")
MenuCursor.containers = ui
cursor_images = load_images(data_dir, subfolder=("ui", "cursor_menu")) # no need to scale cursor
cursor = MenuCursor(cursor_images)
Game.cursor = cursor
max_person = 4
max_frame = 26
p_list = tuple(["p" + str(p) for p in range(1, max_person + 1)])
part_column_header = ["head", "neck", "body", "r_arm_up", "r_arm_low", "r_hand", "l_arm_up",
"l_arm_low", "l_hand", "r_leg_up", "r_leg_low", "r_foot", "l_leg_up", "l_leg_low", "l_foot",
"main_weapon", "sub_weapon", "special_1", "special_2", "special_3", "special_4", "special_5",
"special_6", "special_7", "special_8", "special_9", "special_10"]
anim_column_header = ["Name"]
for p in range(1, max_person + 1):
p_name = "p" + str(p) + "_"
anim_column_header += [p_name + item for item in part_column_header]
anim_column_header += ["effect_1", "effect_2", "effect_3", "effect_4", "effect_5", "effect_6", "effect_7",
"effect_8",
"frame_property", "animation_property", "sound_effect"] # For csv saving and accessing
frame_property_list = ["hold", "stoppable", "play_time_mod_", "effect_blur_", "effect_fade_",
"effect_contrast_", "effect_brightness_", "effect_grey",
"effect_colour_"] # starting property list
anim_property_list = ["interuptrevert", "norestart"] + frame_property_list
"""Property explanation:
hold: Frame or entire animation can be played until release from holding input
play_time_mod_: Value of play time modification, higher mean longer play time
effect_blur_: Put blur effect on entire frame based on the input value
effect_contrast_: Put colour contrast effect on entire frame based on the input value
effect_brightness_: Change brightness of entire frame based on the input value
effect_fade_: Put fade effect on entire frame based on the input value
effect_grey: Put greyscale effect on entire frame
effect_colour_: Colourise entire frame based on the input value
"""
def reload_animation(animation, char, specific_frame=None):
"""Reload animation frames"""
frames = [this_image for this_image in char.animation_list if
this_image is not None]
for frame_index in range(max_frame):
if (not specific_frame and activate_list[frame_index]) or (specific_frame and frame_index == specific_frame):
frames[frame_index] = apply_sprite_effect(frames[frame_index],
frame_property_select[frame_index] + anim_property_select)
filmstrip_list[frame_index].add_strip(frames[frame_index])
animation.reload(frames)
for helper in helper_list:
helper.stat1 = char.part_name_list[current_frame]
helper.stat2 = char.animation_part_list[current_frame]
if char.part_selected: # not empty
for part in char.part_selected:
part = tuple(char.mask_part_list.keys())[part]
helper.select_part((0, 0), True, False, specific_part=part)
else:
helper.select_part(None, shift_press, False)
helper.blit_part()
def property_to_pool_data(which):
if which == "anim":
for frame in model.frame_list:
frame["animation_property"] = select_list
if anim_prop_list_box.rect.collidepoint(mouse_pos):
for frame in range(len(current_pool[animation_race][animation_name])):
current_pool[animation_race][animation_name][frame]["animation_property"] = select_list.copy()
elif which == "frame":
model.frame_list[current_frame]["frame_property"] = select_list.copy()
current_pool[animation_race][animation_name][current_frame]["frame_property"] = select_list.copy()
def change_animation_race(new_race):
global animation_race
animation_race = new_race
for p in range(1, max_person + 1):
model.sprite_mode |= {p: "Normal"}
sprite_mode_selector.change_name("Normal")
change_animation(tuple(current_pool[animation_race].keys())[0])
def change_animation_chapter(new_chapter, change_race=True):
global animation_chapter, animation_pool_data, part_name_header, body_sprite_pool, effect_sprite_pool
animation_pool_data, part_name_header = read_anim_data(join(animation_dir, str(animation_chapter)),
anim_column_header)
body_sprite_pool = {}
for char in char_list:
if char != "":
race_file_name = fcv(char, revert=True)
try:
[split(
os.sep.join(normpath(x).split(os.sep)[normpath(x).split(os.sep).index("sprite"):]))
for
x in Path(join(animation_dir, "sprite", "character", race_file_name)).iterdir() if
x.is_dir()] # check if char folder exist
body_sprite_pool[char] = {}
part_folder = Path(join(animation_dir, "sprite", "character", race_file_name))
sub1_directories = [split(
os.sep.join(
normpath(x).split(os.sep)[normpath(x).split(os.sep).index(race_file_name):]))
for x in part_folder.iterdir() if x.is_dir()]
for folder1 in sub1_directories:
body_sprite_pool[char][folder1[-1]] = {}
sub_part_folder = Path(
join(animation_dir, "sprite", "character", race_file_name, folder1[-1],
str(animation_chapter)))
recursive_image_load(body_sprite_pool[char][folder1[-1]], screen_scale, sub_part_folder)
except FileNotFoundError as b:
print("file not found", b)
effect_sprite_pool = {}
part_folder = Path(join(animation_dir, "sprite", "effect"))
sub1_directories = [split(
sep.join(normpath(x).split(sep)[normpath(x).split(sep).index("animation"):])) for x
in part_folder.iterdir() if x.is_dir()]
for folder_base in sub1_directories:
effect_sprite_pool[fcv(folder_base[-1])] = {}
part_folder2 = Path(join(animation_dir, "sprite", "effect", folder_base[-1]))
subdirectories = [split(
sep.join(normpath(x).split(sep)[normpath(x).split(sep).index("animation"):])) for x
in part_folder2.iterdir() if x.is_dir()]
for folder in subdirectories: # chapter
folder_data_name = fcv(folder[-1])
if int(folder_data_name) >= new_chapter:
images = load_images(part_folder2, subfolder=(folder[-1],), key_file_name_readable=True)
true_name_list = []
for key, value in images.items():
if key.split("_")[-1].isdigit():
true_name = " ".join([string for string in key.split("_")[:-1]]) + "#"
else:
true_name = key
if true_name not in true_name_list:
true_name_list.append(true_name)
for true_name in set(true_name_list): # create effect animation list
final_name = true_name
if "#" in true_name: # has animation to play
final_name = true_name[:-1]
sprite_animation_list = [value for key, value in images.items() if final_name ==
" ".join([string for string in key.split("_")[:-1]])]
else: # single frame animation
sprite_animation_list = [value for key, value in images.items() if
final_name == key]
effect_sprite_pool[fcv(folder_base[-1])][final_name] = sprite_animation_list
animation_chapter = new_chapter
if change_race:
change_animation_race("Miquella")
def change_animation(new_name):
global animation_name, current_frame, current_anim_row, current_frame_row, anim_property_select, frame_property_select
anim_prop_list_box.namelist = anim_property_list + ["Custom"] # reset property list
anim_property_select = []
frame_prop_list_box.namelist = [frame_property_list + ["Custom"] for _ in range(max_frame)]
frame_property_select = [[] for _ in range(max_frame)]
current_anim_row = 0
current_frame_row = 0
model.read_animation(new_name)
if not activate_list[current_frame]:
current_frame = 0
setup_list(NameList, current_frame_row, frame_prop_list_box.namelist[current_frame], frame_prop_namegroup,
frame_prop_list_box, ui, screen_scale, layer=9,
old_list=frame_property_select[current_frame]) # change frame property list
anim.show_frame = current_frame
animation_name = new_name
animation_selector.change_name(new_name)
reload_animation(anim, model)
anim_prop_list_box.scroll.change_image(new_row=0, row_size=len(anim_prop_list_box.namelist))
frame_prop_list_box.scroll.change_image(new_row=0, row_size=len(frame_prop_list_box.namelist[current_frame]))
model.clear_history()
if "sound_effect" in model.frame_list[current_frame] and model.frame_list[current_frame]["sound_effect"]:
sound_selector.change_name(str(model.frame_list[current_frame]["sound_effect"][0]))
sound_distance_selector.change_name(str(model.frame_list[current_frame]["sound_effect"][1]))
else:
sound_selector.change_name("None")
sound_distance_selector.change_name("")
def change_frame_process():
global current_frame_row
anim.show_frame = current_frame
model.edit_part(mouse_pos, "change")
if model.frame_list[current_frame]["sound_effect"]:
sound_selector.change_name(str(model.frame_list[current_frame]["sound_effect"][0]))
sound_distance_selector.change_name(str(model.frame_list[current_frame]["sound_effect"][1]))
else:
sound_selector.change_name("None")
sound_distance_selector.change_name("")
current_frame_row = 0
setup_list(NameList, current_frame_row, frame_prop_list_box.namelist[current_frame], frame_prop_namegroup,
frame_prop_list_box, ui, screen_scale, layer=9,
old_list=frame_property_select[current_frame]) # change frame property list
def recal_camera_pos(model):
global showroom_base_point, showroom
showroom_base_point = ((default_sprite_size[0] * model.size / 2) + (showroom_camera_pos[0] * model.size),
(default_sprite_size[1] * model.size * 0.8) + (showroom_camera_pos[1] * model.size))
showroom.showroom_base_point = ((showroom_size[0] / 2) + showroom_camera_pos[0],
(showroom_size[1] * 0.8) + showroom_camera_pos[1])
animation_race = "Miquella"
animation_chapter = 1
char_list = []
for x in Path(join(animation_dir, "sprite", "character")).iterdir(): # grab char with sprite
if normpath(x).split(os.sep)[-1] != "weapon": # exclude weapon as char
char_list.append(fcv(normpath(x).split(os.sep)[-1]))
chapter_list = []
for x in Path(join(animation_dir)).iterdir(): # grab folder with number chapter
if normpath(x).split(os.sep)[-1].isdigit(): # exclude weapon as char
chapter_list.append(int(normpath(x).split(os.sep)[-1]))
animation_pool_data = {}
part_name_header = {}
body_sprite_pool = {}
effect_sprite_pool = {}
change_animation_chapter(animation_chapter, change_race=False)
try:
animation_name = tuple(animation_pool_data[animation_race].keys())[0]
except:
animation_name = None
with open(join(data_dir, "character", "character.csv"),
encoding="utf-8", mode="r") as edit_file:
rd = tuple(csv.reader(edit_file, quoting=csv.QUOTE_ALL))
for row_index, row in enumerate(rd[1:]):
if row[0] not in char_list:
char_list.append(row[0])
default_mode = {}
with open(join(data_dir, "animation", "template.csv"),
encoding="utf-8", mode="r") as edit_file:
rd = tuple(csv.reader(edit_file, quoting=csv.QUOTE_ALL))
for index, stuff in enumerate(rd[0]):
if "special" in stuff: # remove number after special
rd[0][index] = "_".join(rd[0][index].split("_")[:-1])
default_mode["Normal"] = {stuff: "Normal" for
index, stuff in enumerate(rd[0]) if stuff[0] == "p"}
character_mode_list = {}
for char in char_list:
character_mode_list[char] = {}
if os.path.exists(
os.path.join(data_dir, "character", "character", char + ".csv")):
with open(os.path.join(data_dir, "character", "character", char + ".csv"),
encoding="utf-8", mode="r") as edit_file2:
rd2 = tuple(csv.reader(edit_file2, quoting=csv.QUOTE_ALL))
header2 = rd2[0]
for row_index2, row2 in enumerate(rd2[1:]):
character_mode_list[char][row2[0]] = {header2[index + 1]: stuff for
index, stuff in enumerate(row2[1:])}
else: # no specific mode list, has only normal mode
character_mode_list[char]["Normal"] = default_mode["Normal"]
sound_effect_pool = SoundData().sound_effect_pool
class Filmstrip(pygame.sprite.Sprite):
"""animation sprite filmstrip"""
base_image = None
def __init__(self, pos):
self._layer = 5
pygame.sprite.Sprite.__init__(self, self.containers)
self.pos = pos
self.image = self.base_image.copy() # original no sprite
self.base_image2 = self.base_image.copy() # after add sprite but before activate or deactivate
self.base_image3 = self.base_image2.copy() # before adding selected corner
self.rect = self.image.get_rect(topleft=self.pos)
self.image_scale = (self.image.get_width() / 100, self.image.get_height() / 120)
self.blit_image = None
self.strip_rect = None
self.activate = False
def update(self, *args):
self.image = self.base_image3.copy()
def selected(self, select=False):
self.image = self.base_image3.copy()
select_colour = (200, 100, 100)
if self.activate:
select_colour = (150, 200, 100)
if select:
pygame.draw.rect(self.image, select_colour, (0, 0, self.image.get_width(), self.image.get_height()),
int(self.image.get_width() / 5))
def add_strip(self, image=None, change=True):
if change:
self.image = self.base_image.copy()
if image is not None:
self.blit_image = smoothscale(image.copy(), (
int(100 * self.image_scale[0]), int(100 * self.image_scale[1])))
self.strip_rect = self.blit_image.get_rect(
center=(self.image.get_width() / 2, self.image.get_height() / 2))
self.image.blit(self.blit_image, self.strip_rect)
self.base_image2 = self.image.copy()
else:
self.image = self.base_image2.copy()
self.base_image3 = self.base_image2.copy()
if not self.activate: # draw black corner and replace film dot
pygame.draw.rect(self.base_image3, (0, 0, 0), (0, 0, self.image.get_width(), self.image.get_height()),
int(self.image.get_width() / 5))
class Button(pygame.sprite.Sprite):
"""Normal button"""
def __init__(self, text, image, pos, description=None, font_size=16):
self._layer = 5
pygame.sprite.Sprite.__init__(self, self.containers)
self.font = pygame.font.Font(Game.ui_font["text_paragraph"], int(font_size * screen_scale[1]))
self.image = image.copy()
self.base_image = self.image.copy()
self.description = description
self.text = text
self.pos = pos
text_surface = self.font.render(str(text), True, (0, 0, 0))
text_rect = text_surface.get_rect(center=(int(self.image.get_width() / 2), int(self.image.get_height() / 2)))
self.image.blit(text_surface, text_rect)
self.rect = self.image.get_rect(center=self.pos)
def change_text(self, text):
if text != self.text:
self.image = self.base_image.copy()
self.text = text
text_surface = self.font.render(self.text.capitalize(), True, (0, 0, 0))
text_rect = text_surface.get_rect(
center=(int(self.image.get_width() / 2), int(self.image.get_height() / 2)))
self.image.blit(text_surface, text_rect)
self.rect = self.image.get_rect(center=self.pos)
def update(self, *args):
if "ON" in help_button.text: # enable help description
if self.rect.collidepoint(mouse_pos) and self.description is not None and not mouse_left_up:
text_popup.popup(cursor.rect, self.description)
ui.add(text_popup)
class SwitchButton(Button):
"""Button that switch text/option"""
def __init__(self, text_list, image, pos, description=None, font_size=16):
self.current_option = 0
self.text_list = text_list
self.text = self.text_list[self.current_option]
Button.__init__(self, self.text, image, pos, description, font_size)
self.change_text(self.text)
def change_option(self, option):
if self.current_option != option:
self.current_option = option
self.image = self.base_image.copy()
self.text = self.text_list[self.current_option]
self.change_text(self.text)
def change_text(self, text):
text_surface = self.font.render(str(text), True, (0, 0, 0))
text_rect = text_surface.get_rect(center=(int(self.image.get_width() / 2), int(self.image.get_height() / 2)))
self.image.blit(text_surface, text_rect)
class BodyHelper(pygame.sprite.Sprite):
def __init__(self, size, pos, ui_type, part_images):
self._layer = 6
pygame.sprite.Sprite.__init__(self, self.containers)
self.font_size = int(9 * screen_scale[1])
self.font = pygame.font.Font(Game.ui_font["text_paragraph"], self.font_size)
self.size = size
self.image = pygame.Surface(self.size, pygame.SRCALPHA)
self.image.fill((255, 255, 200))
pygame.draw.rect(self.image, (100, 150, 150), (0, 0, self.image.get_width(), self.image.get_height()), 3)
self.base_image = self.image.copy() # for original before add part and click
self.rect = self.image.get_rect(center=pos)
self.ui_type = ui_type
self.part_images_original = [image.copy() for image in part_images]
if "effect" not in self.ui_type:
self.box_font = pygame.font.Font(Game.ui_font["text_paragraph"], int(22 * screen_scale[1]))
empty_box = self.part_images_original[-1]
self.part_images_original = self.part_images_original[:-1]
for box_part in ("W1", "W2"):
text_surface = self.box_font.render(box_part, True, (0, 0, 0))
text_rect = text_surface.get_rect(center=(empty_box.get_width() / 2, empty_box.get_height() / 2))
new_box = empty_box.copy()
new_box.blit(text_surface, text_rect)
self.part_images_original.append(new_box)
else:
self.box_font = pygame.font.Font(Game.ui_font["text_paragraph"], int(18 * screen_scale[1]))
empty_box = self.part_images_original[0]
self.part_images_original = self.part_images_original[:-1]
for box_part in ("S1", "S2", "S3", "S4", "S5", "E1", "E2", "E3", "E4",
"S6", "S7", "S8", "S9", "S10", "E5", "E6", "E7", "E8"):
text_surface = self.box_font.render(box_part, True, (0, 0, 0))
text_rect = text_surface.get_rect(center=(empty_box.get_width() / 2, empty_box.get_height() / 2))
new_box = empty_box.copy()
new_box.blit(text_surface, text_rect)
self.part_images_original.append(new_box)
self.part_images = [image.copy() for image in self.part_images_original]
self.selected_part_images = [apply_sprite_colour(image, (34, 177, 76), white_colour=False) for image in
self.part_images_original]
self.part_selected = []
self.stat1 = {}
self.stat2 = {}
self.change_p_type(self.ui_type)
for key, item in self.part_pos.items():
self.part_pos[key] = (item[0] * screen_scale[0], item[1] * screen_scale[1])
self.blit_part()
def change_p_type(self, new_type, player_change=False):
"""For helper that can change person"""
self.ui_type = new_type
if "effect" not in self.ui_type:
self.rect_part_list = {self.ui_type + "_head": None, self.ui_type + "_neck": None,
self.ui_type + "_body": None, self.ui_type + "_r_arm_up": None,
self.ui_type + "_r_arm_low": None, self.ui_type + "_r_hand": None,
self.ui_type + "_l_arm_up": None, self.ui_type + "_l_arm_low": None,
self.ui_type + "_l_hand": None,
self.ui_type + "_r_leg_up": None, self.ui_type + "_r_leg_low": None,
self.ui_type + "_r_foot": None,
self.ui_type + "_l_leg_up": None, self.ui_type + "_l_leg_low": None,
self.ui_type + "_l_foot": None, self.ui_type + "_main_weapon": None,
self.ui_type + "_sub_weapon": None}
self.part_pos = {self.ui_type + "_head": (self.image.get_width() / 2, 70),
self.ui_type + "_neck": (self.image.get_width() / 2, 100),
self.ui_type + "_body": (self.image.get_width() / 2, 140),
self.ui_type + "_r_arm_up": (self.image.get_width() / 2 -
(self.image.get_width() / 20), 105),
self.ui_type + "_r_arm_low": (self.image.get_width() / 2 -
(self.image.get_width() / 20), 135),
self.ui_type + "_r_hand": (self.image.get_width() / 2 -
(self.image.get_width() / 20), 165),
self.ui_type + "_l_arm_up": (self.image.get_width() / 2 +
(self.image.get_width() / 20), 105),
self.ui_type + "_l_arm_low": (self.image.get_width() / 2 +
(self.image.get_width() / 20), 135),
self.ui_type + "_l_hand": (self.image.get_width() / 2 +
(self.image.get_width() / 20), 165),
self.ui_type + "_r_leg_up": (self.image.get_width() / 2 -
(self.image.get_width() / 40), 195),
self.ui_type + "_r_leg_low": (self.image.get_width() / 2 -
(self.image.get_width() / 40), 226),
self.ui_type + "_r_foot": (self.image.get_width() / 2 -
(self.image.get_width() / 40), 256),
self.ui_type + "_l_leg_up": (self.image.get_width() / 2 +
(self.image.get_width() / 40), 195),
self.ui_type + "_l_leg_low": (self.image.get_width() / 2 +
(self.image.get_width() / 40), 226),
self.ui_type + "_l_foot": (self.image.get_width() / 2 +
(self.image.get_width() / 40), 256),
self.ui_type + "_main_weapon": (self.image.get_width() / 2 -
(self.image.get_width() / 20), 25),
self.ui_type + "_sub_weapon": (self.image.get_width() / 2 +
(self.image.get_width() / 20), 25)}
else:
p_type = self.ui_type[:2]
self.rect_part_list = {p_type + "_special_1": None, p_type + "_special_2": None,
p_type + "_special_3": None,
p_type + "_special_4": None, p_type + "_special_5": None,
"effect_1": None, "effect_2": None, "effect_3": None, "effect_4": None,
p_type + "_special_6": None, p_type + "_special_7": None,
p_type + "_special_8": None,
p_type + "_special_9": None, p_type + "_special_10": None,
"effect_5": None, "effect_6": None, "effect_7": None, "effect_8": None}
self.part_pos = {p_type + "_special_1": (20, 15), p_type + "_special_2": (20, 45),
p_type + "_special_3": (20, 75),
p_type + "_special_4": (20, 105), p_type + "_special_5": (20, 135),
"effect_1": (20, 165), "effect_2": (20, 195), "effect_3": (20, 225), "effect_4": (20, 255),
p_type + "_special_6": (300, 15), p_type + "_special_7": (300, 45),
p_type + "_special_8": (300, 75),
p_type + "_special_9": (300, 105), p_type + "_special_10": (300, 135),
"effect_5": (300, 165), "effect_6": (300, 195), "effect_7": (300, 225),
"effect_8": (300, 255)}
if player_change:
self.select_part(None, False, False) # reset first
for part in model.part_selected: # blit selected part that is in helper
if tuple(model.mask_part_list.keys())[part] in self.rect_part_list:
self.select_part(mouse_pos, True, False, tuple(model.mask_part_list.keys())[part])
self.blit_part()
def blit_part(self):
self.image = self.base_image.copy()
for index, image in enumerate(self.part_images):
this_key = tuple(self.part_pos.keys())[index]
pos = self.part_pos[this_key]
new_image = image
if this_key in self.part_selected: # highlight selected part
new_image = self.selected_part_images[index]
rect = new_image.get_rect(center=pos)
self.image.blit(new_image, rect)
self.rect_part_list[this_key] = rect
self.add_stat()
def select_part(self, check_mouse_pos, shift_press, ctrl_press, specific_part=None):
return_selected_part = None
if specific_part is not None:
if not specific_part:
self.part_selected = []
elif specific_part in list(self.part_pos.keys()):
if shift_press:
if specific_part not in self.part_selected:
self.part_selected.append(specific_part)
elif ctrl_press:
if specific_part in self.part_selected:
self.part_selected.remove(specific_part)
else:
self.part_selected = [specific_part]
else: # click on helper ui
click_any = False
if check_mouse_pos is not None:
for index, rect in enumerate(self.rect_part_list):
this_rect = self.rect_part_list[rect]
if this_rect is not None and this_rect.collidepoint(check_mouse_pos):
click_any = True
return_selected_part = tuple(self.part_pos.keys())[index]
if shift_press:
if tuple(self.part_pos.keys())[index] not in self.part_selected:
self.part_selected.append(tuple(self.part_pos.keys())[index])
elif ctrl_press:
if tuple(self.part_pos.keys())[index] in self.part_selected:
self.part_selected.remove(tuple(self.part_pos.keys())[index])
else:
self.part_selected = [tuple(self.part_pos.keys())[index]]
break
if check_mouse_pos is None or (
not click_any and (not shift_press and not ctrl_press)): # click at empty space
self.part_selected = []
return return_selected_part
def add_stat(self):
for index, part in enumerate(self.rect_part_list):
if self.stat2 is not None and part in self.stat2 and self.stat1[part] is not None and self.stat2[
part] is not None:
stat = self.stat1[part] + self.stat2[part]
if len(stat) > 2:
stat.pop(2)
stat.pop(2)
stat[1] = str(stat[1])
if len(stat) > 3:
try:
stat[2] = str([[int(stat[2][0]), int(stat[2][1])]])
except TypeError:
stat[2] = str([0, 0])
for index2, change in enumerate(["F", "FH", "FV", "FHV"]):
if stat[4] == index2:
stat[4] = change
stat[3] = str(round(stat[3], 1))
stat[5] = "L" + str(int(stat[5]))
stat1 = stat[0:2] # first line with part char, name
stat1 = str(stat1).replace("'", "")
stat2 = stat[2:] # second line with stat
stat2 = str(stat2).replace("'", "")
text_colour = (0, 0, 0)
if part in self.part_selected: # green text for selected part
text_colour = (20, 90, 20)
text_surface1 = self.font.render(stat1, True, text_colour)
text_surface2 = self.font.render(stat2, True, text_colour)
shift_x = 50 * screen_scale[0]
if any(ext in part for ext in ("effect", "special")):
shift_x = 30 * screen_scale[0]
text_rect1 = text_surface1.get_rect(
midleft=(self.part_pos[part][0] + shift_x, self.part_pos[part][1] - 10))
text_rect2 = text_surface2.get_rect(
midleft=(self.part_pos[part][0] + shift_x, self.part_pos[part][1] - 10 + self.font_size + 2))
elif "body" in part:
head_name = part[0:2] + "_head"
text_rect1 = text_surface1.get_rect(
midleft=(self.part_pos[head_name][0] + shift_x, self.part_pos[head_name][1] - 5))
text_rect2 = text_surface2.get_rect(
midleft=(
self.part_pos[head_name][0] + shift_x,
self.part_pos[head_name][1] - 5 + self.font_size + 2))
elif "neck" in part:
head_name = part[0:2] + "_head"
text_rect1 = text_surface1.get_rect(
midleft=(self.part_pos[head_name][0] + shift_x, self.part_pos[head_name][1] - 25))
text_rect2 = text_surface2.get_rect(
midleft=(
self.part_pos[head_name][0] + shift_x,
self.part_pos[head_name][1] - 25 + self.font_size + 2))
elif "head" in part:
text_rect1 = text_surface1.get_rect(
midright=(self.part_pos[part][0] - shift_x, self.part_pos[part][1] - 10))
text_rect2 = text_surface2.get_rect(
midright=(self.part_pos[part][0] - shift_x, self.part_pos[part][1] - 10 + self.font_size + 2))
else:
shift_x = 14 * screen_scale[0]
if "weapon" in part:
shift_x = 26 * screen_scale[0]
if self.part_pos[part][0] > self.image.get_width() / 2:
text_rect1 = text_surface1.get_rect(
midleft=(self.part_pos[part][0] + shift_x, self.part_pos[part][1] - 15))
text_rect2 = text_surface2.get_rect(
midleft=(
self.part_pos[part][0] + shift_x, self.part_pos[part][1] - 15 + self.font_size + 2))
else:
text_rect1 = text_surface1.get_rect(
midright=(self.part_pos[part][0] - shift_x, self.part_pos[part][1] - 15))
text_rect2 = text_surface2.get_rect(
midright=(
self.part_pos[part][0] - shift_x, self.part_pos[part][1] - 15 + self.font_size + 2))
self.image.blit(text_surface1, text_rect1)
self.image.blit(text_surface2, text_rect2)
# else:
# text_surface = self.font.render("None", 1, (0, 0, 0))
# text_rect = text_surface.get_rect(midleft=self.part_pos[part])
# self.image.blit(text_surface, text_rect)
class NameBox(pygame.sprite.Sprite):
def __init__(self, size, pos, description=None):
self._layer = 6
pygame.sprite.Sprite.__init__(self, self.containers)
self.font_size = int(22 * screen_scale[1])
self.font = pygame.font.Font(Game.ui_font["text_paragraph"], int(self.font_size * screen_scale[1]))
self.description = description
self.size = size
self.image = pygame.Surface(self.size)
self.image.fill((182, 233, 242))
pygame.draw.rect(self.image, (100, 200, 0), (0, 0, self.image.get_width(), self.image.get_height()), 2)
self.base_image = self.image.copy()
self.pos = pos
self.rect = self.image.get_rect(midtop=self.pos)
self.text = None
def change_name(self, text):
if text != self.text:
self.image = self.base_image.copy()
self.text = text
text_surface = self.font.render(self.text, True, (0, 0, 0))
text_rect = text_surface.get_rect(
center=(int(self.image.get_width() / 2), int(self.image.get_height() / 2)))
self.image.blit(text_surface, text_rect)
def update(self, *args):
if "ON" in help_button.text: # enable help description
if self.rect.collidepoint(mouse_pos) and self.description is not None and not mouse_left_up:
text_popup.popup(cursor.rect, self.description)
ui.add(text_popup)
class ColourWheel(pygame.sprite.Sprite):
def __init__(self, image, pos):
self._layer = 30
pygame.sprite.Sprite.__init__(self)
self.image = image
self.pos = pos
self.rect = self.image.get_rect(center=self.pos)
def get_colour(self):
pos = mouse_pos[0] - self.rect.topleft[0], mouse_pos[1] - self.rect.topleft[1]
colour = self.image.get_at(pos) # get colour at pos
return colour
class Model:
def __init__(self):
self.animation_list = {} # dict of animation frame image surface
self.animation_part_list = {} # dict of part image surface
self.bodypart_list = [] # list of part stat
self.part_name_list = [] # list of part name
self.frame_list = [{}] * max_frame
self.current_history = -1 # start with -1 as the first history will be added later to 0
self.animation_history = []
self.body_part_history = []
self.part_name_history = []
self.mask_part_list = {}
self.all_part_list = {}
for p in range(1, max_person + 1):
self.mask_part_list = self.mask_part_list | {"p" + str(p) + "_head": None,
"p" + str(p) + "_neck": None,
"p" + str(p) + "_body": None,
"p" + str(p) + "_r_arm_up": None,
"p" + str(p) + "_r_arm_low": None,
"p" + str(p) + "_r_hand": None,
"p" + str(p) + "_l_arm_up": None,
"p" + str(p) + "_l_arm_low": None,
"p" + str(p) + "_l_hand": None,
"p" + str(p) + "_r_leg_up": None,
"p" + str(p) + "_r_leg_low": None,
"p" + str(p) + "_r_foot": None,
"p" + str(p) + "_l_leg_up": None,
"p" + str(p) + "_l_leg_low": None,
"p" + str(p) + "_l_foot": None,
"p" + str(p) + "_main_weapon": None,
"p" + str(p) + "_sub_weapon": None,
"p" + str(p) + "_special_1": None,
"p" + str(p) + "_special_2": None,
"p" + str(p) + "_special_3": None,
"p" + str(p) + "_special_4": None,
"p" + str(p) + "_special_5": None,
"p" + str(p) + "_special_6": None,
"p" + str(p) + "_special_7": None,
"p" + str(p) + "_special_8": None,
"p" + str(p) + "_special_9": None,
"p" + str(p) + "_special_10": None}
self.all_part_list = self.all_part_list | {"p" + str(p) + "_head": None,
"p" + str(p) + "_neck": None,
"p" + str(p) + "_body": None, "p" + str(p) + "_r_arm_up": None,
"p" + str(p) + "_r_arm_low": None,
"p" + str(p) + "_r_hand": None,
"p" + str(p) + "_l_arm_up": None,
"p" + str(p) + "_l_arm_low": None,
"p" + str(p) + "_l_hand": None,
"p" + str(p) + "_r_leg_up": None,
"p" + str(p) + "_r_leg_low": None,
"p" + str(p) + "_r_foot": None,
"p" + str(p) + "_l_leg_up": None,
"p" + str(p) + "_l_leg_low": None,
"p" + str(p) + "_l_foot": None,
"p" + str(p) + "_main_weapon": None,
"p" + str(p) + "_sub_weapon": None,
"p" + str(p) + "_special_1": None,
"p" + str(p) + "_special_2": None,
"p" + str(p) + "_special_3": None,
"p" + str(p) + "_special_4": None,
"p" + str(p) + "_special_5": None,
"p" + str(p) + "_special_6": None,
"p" + str(p) + "_special_7": None,
"p" + str(p) + "_special_8": None,
"p" + str(p) + "_special_9": None,
"p" + str(p) + "_special_10": None}
self.mask_part_list = self.mask_part_list | {"effect_1": None, "effect_2": None, "effect_3": None,
"effect_4": None,
"effect_5": None, "effect_6": None, "effect_7": None,
"effect_8": None,
}
self.all_part_list = self.all_part_list | {"effect_1": None, "effect_2": None, "effect_3": None,
"effect_4": None,
"effect_5": None, "effect_6": None, "effect_7": None,
"effect_8": None}
self.part_selected = []
self.sprite_mode = {}
for p in range(1, max_person + 1):
self.sprite_mode |= {p: "Normal"}
self.size = 1 # size scale of sprite
try:
self.read_animation(tuple((animation_pool_data[animation_race].keys()))[0])
except IndexError: # empty animation file
self.read_animation(None)
def make_layer_list(self, sprite_part):
pose_layer_list = {k: v[5] for k, v in sprite_part.items() if v is not None and v != []}
pose_layer_list = dict(sorted(pose_layer_list.items(), key=lambda item: item[1], reverse=True))
return pose_layer_list
def read_animation(self, name, old=False):
global activate_list, showroom_base_point
# sprite animation generation from data
self.animation_part_list = [{key: None for key in self.mask_part_list}] * max_frame
self.animation_list = [self.create_animation_film(None, current_frame, empty=True)] * max_frame
self.bodypart_list = [{key: value for key, value in self.all_part_list.items()}] * max_frame
self.part_name_list = [{key: None for key in self.mask_part_list}] * max_frame
for key, value in self.mask_part_list.items(): # reset rect list
self.mask_part_list[key] = None
if name is not None:
frame_list = current_pool[animation_race][name].copy()
if old:
frame_list = self.frame_list
while len(frame_list) < max_frame: # add empty item
frame_list.append({})
recal_camera_pos(self)
size_button.change_text("Zoom: " + str(self.size))
for index, pose in enumerate(frame_list):
sprite_part = {key: None for key in self.mask_part_list}
link_list = {key: None for key in self.mask_part_list}
bodypart_list = {key: value for key, value in self.all_part_list.items()}
for part in pose:
if pose[part] and "property" not in part and "sound_effect" not in part:
link_list[part] = [pose[part][2], pose[part][3]]
bodypart_list[part] = [pose[part][0], pose[part][1]]
elif "property" in part and pose[part] != [""]:
if "animation" in part:
for stuff in pose[part]:
if stuff not in anim_prop_list_box.namelist:
anim_prop_list_box.namelist.insert(-1, stuff)
if stuff not in anim_property_select:
anim_property_select.append(stuff)
elif "frame" in part and pose[part] != 0:
for stuff in pose[part]:
if stuff not in frame_prop_list_box.namelist[index]:
frame_prop_list_box.namelist[index].insert(-1, stuff)
if stuff not in frame_property_select[index]:
frame_property_select[index].append(stuff)
self.bodypart_list[index] = bodypart_list
self.generate_body(self.bodypart_list[index])
part_name = {key: None for key in self.mask_part_list}
for part in part_name_header:
if part in pose and pose[part]:
sprite_part[part] = [self.sprite_image[part], "center", link_list[part], pose[part][4],
pose[part][5], pose[part][6], pose[part][7], pose[part][8], pose[part][9]]
part_name[part] = [pose[part][0], pose[part][1]]
pose_layer_list = self.make_layer_list(sprite_part)
self.animation_part_list[index] = sprite_part
self.part_name_list[index] = part_name
image = self.create_animation_film(pose_layer_list, index)
self.animation_list[index] = image
self.frame_list = frame_list
activate_list = [False] * max_frame
for strip_index, strip in enumerate(filmstrips):
strip.activate = False
for stuff in self.animation_part_list[strip_index].values():
if stuff:
strip.activate = True
activate_list[strip_index] = True
break
# recreate property list
anim_prop_list_box.namelist = [item for item in anim_prop_list_box.namelist if item in anim_property_select] + \
[item for item in anim_prop_list_box.namelist if item not in anim_property_select]
for frame in range(len(frame_prop_list_box.namelist)):
frame_prop_list_box.namelist[frame] = [item for item in frame_prop_list_box.namelist[frame] if
item in frame_property_select[frame]] + \
[item for item in frame_prop_list_box.namelist[frame] if
item not in frame_property_select[frame]]
setup_list(NameList, current_anim_row, anim_prop_list_box.namelist, anim_prop_namegroup,
anim_prop_list_box, ui, screen_scale, layer=9, old_list=anim_property_select)
setup_list(NameList, current_frame_row, frame_prop_list_box.namelist[current_frame], frame_prop_namegroup,
frame_prop_list_box, ui, screen_scale, layer=9, old_list=frame_property_select[current_frame])
def create_animation_film(self, pose_layer_list, frame, empty=False):
image = pygame.Surface((default_sprite_size[0], default_sprite_size[1]),
pygame.SRCALPHA) # default size will scale down later
save_mask = False
if frame == current_frame:
save_mask = True
if not empty:
for index, layer in enumerate(pose_layer_list):
part = self.animation_part_list[frame][layer]
if part is not None and part[0] is not None:
image = self.part_to_sprite(image, part[0], layer, part[2], part[3], part[4], part[6], part[7],
save_mask=save_mask)
return image
def generate_body(self, bodypart_list):
self.sprite_image = {key: None for key in self.mask_part_list}
for stuff in bodypart_list: # create stat and sprite image
if bodypart_list[stuff] and bodypart_list[stuff][1]:
if "effect_" in stuff:
self.sprite_image[stuff] = effect_sprite_pool[bodypart_list[stuff][0]][
bodypart_list[stuff][1]][0].copy()
else:
new_part_name = stuff
mode_part_check = stuff
p = stuff[1]
if any(ext in stuff for ext in p_list):
part_name = stuff[3:] # remove p*number*_ to get part name
new_part_name = part_name
if "special" in stuff:
part_name = "special"
new_part_name = part_name
mode_part_check = "_".join(
mode_part_check.split("_")[0:-1]) # remove _number at the end of special
elif "weapon" in stuff:
part_name = "weapon"
new_part_name = part_name
if "r_" in part_name[0:2] or "l_" in part_name[0:2]:
new_part_name = part_name[2:] # remove part side
try:
self.sprite_image[stuff] = body_sprite_pool[bodypart_list[stuff][0]][new_part_name][
character_mode_list[animation_race][self.sprite_mode[int(p)]][mode_part_check]][
bodypart_list[stuff][1]].copy()
except KeyError: # try normal mode if part not exist for specified mode
self.sprite_image[stuff] = body_sprite_pool[bodypart_list[stuff][0]][new_part_name][
character_mode_list[animation_race]["Normal"][mode_part_check]][
bodypart_list[stuff][1]].copy()
def click_part(self, mouse_pos, shift_press, ctrl_press, part=None):
if part is None:
click_part = False
if not shift_press and not ctrl_press:
self.part_selected = []
else:
click_part = True
for index, rect in enumerate(self.mask_part_list):
this_rect = self.mask_part_list[rect]
if this_rect is not None and this_rect[0].collidepoint(mouse_pos) and this_rect[1].get_at(
mouse_pos - this_rect[0].topleft) == 1:
click_part = True
if shift_press: # add
if index not in self.part_selected:
self.part_selected.append(index)
elif ctrl_press: # remove
if index in self.part_selected:
self.part_selected.remove(index)
else: # select
self.part_selected = [index]
break
if not click_part:
self.part_selected = []
else:
if shift_press:
self.part_selected.append(tuple(self.mask_part_list.keys()).index(part))
self.part_selected = list(set(self.part_selected))
elif ctrl_press:
if tuple(self.mask_part_list.keys()).index(part) in self.part_selected:
self.part_selected.remove(list(self.mask_part_list.keys()).index(part))
else:
self.part_selected = [tuple(self.mask_part_list.keys()).index(part)]
def edit_part(self, edit_mouse_pos, edit_type, specific_frame=None, check_delay=True):