batframework 1.0.10__py3-none-any.whl → 2.0.0a1__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.
Files changed (81) hide show
  1. batFramework/__init__.py +83 -52
  2. batFramework/action.py +280 -252
  3. batFramework/actionContainer.py +105 -38
  4. batFramework/animatedSprite.py +81 -117
  5. batFramework/animation.py +91 -0
  6. batFramework/audioManager.py +156 -85
  7. batFramework/baseScene.py +249 -0
  8. batFramework/camera.py +245 -123
  9. batFramework/constants.py +57 -75
  10. batFramework/cutscene.py +239 -119
  11. batFramework/cutsceneManager.py +34 -0
  12. batFramework/drawable.py +107 -0
  13. batFramework/dynamicEntity.py +30 -23
  14. batFramework/easingController.py +58 -0
  15. batFramework/entity.py +130 -123
  16. batFramework/enums.py +171 -0
  17. batFramework/fontManager.py +65 -0
  18. batFramework/gui/__init__.py +28 -14
  19. batFramework/gui/animatedLabel.py +90 -0
  20. batFramework/gui/button.py +18 -84
  21. batFramework/gui/clickableWidget.py +244 -0
  22. batFramework/gui/collapseContainer.py +98 -0
  23. batFramework/gui/constraints/__init__.py +1 -0
  24. batFramework/gui/constraints/constraints.py +1066 -0
  25. batFramework/gui/container.py +220 -49
  26. batFramework/gui/debugger.py +140 -47
  27. batFramework/gui/draggableWidget.py +63 -0
  28. batFramework/gui/image.py +61 -23
  29. batFramework/gui/indicator.py +116 -40
  30. batFramework/gui/interactiveWidget.py +243 -22
  31. batFramework/gui/label.py +147 -110
  32. batFramework/gui/layout.py +442 -81
  33. batFramework/gui/meter.py +155 -0
  34. batFramework/gui/radioButton.py +43 -0
  35. batFramework/gui/root.py +228 -60
  36. batFramework/gui/scrollingContainer.py +282 -0
  37. batFramework/gui/selector.py +232 -0
  38. batFramework/gui/shape.py +286 -86
  39. batFramework/gui/slider.py +353 -0
  40. batFramework/gui/style.py +10 -0
  41. batFramework/gui/styleManager.py +49 -0
  42. batFramework/gui/syncedVar.py +43 -0
  43. batFramework/gui/textInput.py +331 -0
  44. batFramework/gui/textWidget.py +308 -0
  45. batFramework/gui/toggle.py +140 -62
  46. batFramework/gui/tooltip.py +35 -0
  47. batFramework/gui/widget.py +546 -307
  48. batFramework/manager.py +131 -50
  49. batFramework/particle.py +118 -0
  50. batFramework/propertyEaser.py +79 -0
  51. batFramework/renderGroup.py +34 -0
  52. batFramework/resourceManager.py +130 -0
  53. batFramework/scene.py +31 -226
  54. batFramework/sceneLayer.py +134 -0
  55. batFramework/sceneManager.py +200 -165
  56. batFramework/scrollingSprite.py +115 -0
  57. batFramework/sprite.py +46 -0
  58. batFramework/stateMachine.py +49 -51
  59. batFramework/templates/__init__.py +2 -0
  60. batFramework/templates/character.py +15 -0
  61. batFramework/templates/controller.py +158 -0
  62. batFramework/templates/stateMachine.py +39 -0
  63. batFramework/tileset.py +46 -0
  64. batFramework/timeManager.py +213 -0
  65. batFramework/transition.py +162 -157
  66. batFramework/triggerZone.py +22 -22
  67. batFramework/utils.py +306 -184
  68. {batframework-1.0.10.dist-info → batframework-2.0.0a1.dist-info}/LICENSE +20 -20
  69. {batframework-1.0.10.dist-info → batframework-2.0.0a1.dist-info}/METADATA +2 -2
  70. batframework-2.0.0a1.dist-info/RECORD +72 -0
  71. batFramework/cutsceneBlocks.py +0 -176
  72. batFramework/debugger.py +0 -48
  73. batFramework/easing.py +0 -71
  74. batFramework/gui/constraints.py +0 -204
  75. batFramework/gui/frame.py +0 -19
  76. batFramework/particles.py +0 -77
  77. batFramework/time.py +0 -75
  78. batFramework/transitionManager.py +0 -0
  79. batframework-1.0.10.dist-info/RECORD +0 -43
  80. {batframework-1.0.10.dist-info → batframework-2.0.0a1.dist-info}/WHEEL +0 -0
  81. {batframework-1.0.10.dist-info → batframework-2.0.0a1.dist-info}/top_level.txt +0 -0
@@ -1,38 +1,105 @@
1
- import batFramework as bf
2
-
3
-
4
- class ActionContainer:
5
- def __init__(self, *actions: list[bf.Action]) -> None:
6
- self._actions: dict[str, bf.Action] = {}
7
- if actions:
8
- self.add_action(*actions)
9
-
10
- def clear(self):
11
- self._actions = {}
12
-
13
- def add_action(self, *actions: bf.Action):
14
- for action in actions:
15
- self._actions[action.get_name()] = action
16
-
17
- def get(self,name:str)->bf.Action:
18
- return self._actions.get(name)
19
-
20
- def has_action(self, name:str):
21
- return name in self._actions
22
-
23
- def is_active(self, *names:str)->bool:
24
- return all(self._actions.get(name).is_active() if name in self._actions else False for name in names)
25
-
26
- def process_event(self, event):
27
- for action in self._actions.values():
28
- a = action.process_event(event)
29
- if a and action._unique:
30
- break
31
-
32
- def reset(self):
33
- for action in self._actions.values():
34
- action.reset()
35
-
36
- def hard_reset(self):
37
- for action in self._actions.values():
38
- action.hard_reset()
1
+ import batFramework as bf
2
+ import pygame
3
+
4
+
5
+ class ActionContainer:
6
+ def __init__(self, *actions: list[bf.Action]) -> None:
7
+ self._actions: dict[str, bf.Action] = {}
8
+ if actions:
9
+ self.add_actions(*actions)
10
+
11
+ def __iter__(self):
12
+ return iter(self._actions.values())
13
+
14
+ def __getitem__(self, key):
15
+ return self._actions[key]
16
+
17
+ def __setitem__(self, key, value):
18
+ self._actions[key] = value
19
+
20
+ def __delitem__(self, key):
21
+ del self._actions[key]
22
+
23
+ def __contains__(self, key):
24
+ return key in self._actions
25
+
26
+ def __repr__(self):
27
+ return repr(self._actions)
28
+
29
+
30
+
31
+ def clear(self):
32
+ self._actions = {}
33
+
34
+ def add_actions(self, *actions: bf.Action):
35
+ for action in actions:
36
+ self._actions[action.name] = action
37
+
38
+ def get(self, name: str) -> bf.Action:
39
+ return self._actions.get(name)
40
+
41
+ def has_action(self, name: str):
42
+ return name in self._actions
43
+
44
+ def get_all(self) -> list[bf.Action]:
45
+ return self._actions
46
+
47
+ def is_active(self, *names: str) -> bool:
48
+ return all(
49
+ self._actions.get(name).active if name in self._actions else False
50
+ for name in names
51
+ )
52
+
53
+ def is_any_active(self,*names:str) -> bool:
54
+ return any(
55
+ self._actions.get(name).active if name in self._actions else False
56
+ for name in names
57
+ )
58
+
59
+ def process_event(self, event):
60
+ if event.consumed:
61
+ return
62
+ for action in self._actions.values():
63
+ action.process_event(event)
64
+ if event.consumed == True:
65
+ break
66
+
67
+ def reset(self):
68
+ for action in self._actions.values():
69
+ action.reset()
70
+
71
+ def hard_reset(self):
72
+ for action in self._actions.values():
73
+ action.hard_reset()
74
+
75
+
76
+ class DirectionalKeyControls(ActionContainer):
77
+ def __init__(self):
78
+ super().__init__(
79
+ bf.Action("up").add_key_control(pygame.K_UP).set_holding(),
80
+ bf.Action("down").add_key_control(pygame.K_DOWN).set_holding(),
81
+ bf.Action("left").add_key_control(pygame.K_LEFT).set_holding(),
82
+ bf.Action("right").add_key_control(pygame.K_RIGHT).set_holding(),
83
+ )
84
+
85
+
86
+ class WASDControls(ActionContainer):
87
+ def __init__(self):
88
+ super().__init__(
89
+ bf.Action("up").add_key_control(pygame.K_w).set_holding(),
90
+ bf.Action("down").add_key_control(pygame.K_s).set_holding(),
91
+ bf.Action("left").add_key_control(pygame.K_a).set_holding(),
92
+ bf.Action("right").add_key_control(pygame.K_d).set_holding(),
93
+ )
94
+
95
+
96
+ class HybridControls(ActionContainer):
97
+ def __init__(self):
98
+ super().__init__(
99
+ bf.Action("up").add_key_control(pygame.K_UP, pygame.K_w).set_holding(),
100
+ bf.Action("down").add_key_control(pygame.K_DOWN, pygame.K_s).set_holding(),
101
+ bf.Action("left").add_key_control(pygame.K_LEFT, pygame.K_a).set_holding(),
102
+ bf.Action("right")
103
+ .add_key_control(pygame.K_RIGHT, pygame.K_r)
104
+ .set_holding(),
105
+ )
@@ -1,117 +1,81 @@
1
- import batFramework as bf
2
- import pygame
3
-
4
-
5
-
6
- def search_index(target, lst):
7
- cumulative_sum = 0
8
- for index, value in enumerate(lst):
9
- cumulative_sum += value
10
- if cumulative_sum >= target:
11
- return index
12
- return -1
13
-
14
-
15
- class AnimState:
16
- def __init__(self, file, width, height, frame_length_list:list|int) -> None:
17
- self.frames: list[pygame.Surface] = bf.utils.img_slice(file, width, height)
18
- self.frames_flipX: list[pygame.Surface] = bf.utils.img_slice(
19
- file, width, height, True
20
- )
21
-
22
- self.frame_length_list = []
23
- self.ffl_length = 0
24
- self.set_frame_length_list(frame_length_list)
25
-
26
- def get_frame_index(self, counter:float|int):
27
- return search_index(int(counter % self.ffl_length), self.frame_length_list)
28
-
29
- def get_frame(self, counter, flip):
30
- i = self.get_frame_index(counter)
31
- return self.frames_flipX[i] if flip else self.frames[i]
32
-
33
- def set_frame_length_list(self,frame_length_list:list[int]|int):
34
- if isinstance(frame_length_list,int):
35
- frame_length_list = [frame_length_list] * len(self.frames)
36
- if len(frame_length_list) != len(self.frames) :
37
- raise ValueError("frame_length_list should have values for all frames")
38
- self.frame_length_list = frame_length_list
39
- self.ffl_length = sum(self.frame_length_list)
40
-
41
-
42
- class AnimatedSprite(bf.DynamicEntity):
43
- def __init__(self, size=None) -> None:
44
- super().__init__(size, no_surface=True)
45
- self.float_counter = 0
46
- self.animStates: dict[str, AnimState] = {}
47
- self.current_animState :str = ""
48
- self.flipX = False
49
- self._locked = False
50
-
51
- def set_counter(self,value:float):
52
- self.float_counter = value
53
-
54
-
55
- def lock_animState(self):
56
- self._locked = True
57
-
58
- def unlock_animState(self):
59
- self._locked = False
60
-
61
- def set_flipX(self, value):
62
- self.flipX = value
63
-
64
- def remove_animState(self, name:str):
65
- if not name in self.animStates :return
66
- self.animStates.pop(name)
67
- if self.current_animState == name : self.current_animState = list(self.animStates.keys())[0] if self.animStates else ""
68
-
69
- def add_animState(
70
- self, name: str, file: str, size: tuple[int, int], frame_length_list: list[int]
71
- ):
72
- if name in self.animStates:
73
- return
74
- self.animStates[name] = AnimState(file, *size, frame_length_list)
75
- if len(self.animStates) == 1 : self.set_animState(name)
76
-
77
- def set_animState(self, state:str, reset_counter=True, lock=False):
78
- if state not in self.animStates or self._locked:
79
- return False
80
- self.current_animState = state
81
- self.rect = (
82
- self.animStates[self.current_animState]
83
- .frames[0]
84
- .get_frect(center=self.rect.center)
85
- )
86
- if reset_counter or self.float_counter > sum(
87
- self.get_state().frame_length_list
88
- ):
89
- self.float_counter = 0
90
- if lock:
91
- self.lock_animState()
92
- return True
93
-
94
- def get_state(self):
95
- return self.animStates.get(self.current_animState,None)
96
-
97
- def get_frame_index(self):
98
- return self.animStates[self.current_animState].get_frame_index(
99
- self.float_counter
100
- )
101
-
102
- def update(self, dt: float):
103
- if not self.animStates : return
104
- self.float_counter += 60 * dt
105
- if self.float_counter > self.get_state().ffl_length:
106
- self.float_counter = 0
107
- self.do_update(dt)
108
-
109
- def draw(self, camera: bf.Camera) -> bool:
110
- if not self.visible or not camera.intersects(self.rect) or not self.animStates:
111
- return False
112
- # pygame.draw.rect(camera.surface,"purple",camera.transpose(self.rect).move(2,2))
113
- camera.surface.blit(
114
- self.get_state().get_frame(self.float_counter, self.flipX),
115
- camera.transpose(self.rect),
116
- )
117
- return True
1
+ import batFramework as bf
2
+ import pygame
3
+ from typing import List, Dict, Tuple, Union, Optional, Self, Callable, Any
4
+ from .animation import Animation
5
+
6
+ class AnimatedSprite(bf.Drawable):
7
+ def __init__(self,*args,**kwargs) -> None:
8
+ super().__init__((0,0),*args,**kwargs)
9
+ self.animations : dict[str,Animation] = {}
10
+ self.counter = 0 # int counter
11
+ self._fcounter = 0.0 # counter
12
+ self.current_animation : str = None
13
+ self.end_callback : Callable[[],Any] = None
14
+ self._flipX : bool = False
15
+ self.animation_loop : int = -1
16
+ self.queued_animation : str = None
17
+
18
+ @property
19
+ def flipX(self)->bool:
20
+ return self._flipX
21
+
22
+ @flipX.setter
23
+ def flipX(self,value:bool):
24
+ self._flipX = value
25
+
26
+ def set_animation_end_callback(self,callback : Callable[[],Any]):
27
+ self.end_callback = callback
28
+
29
+ def add_animation(self,animation:Animation)->Self:
30
+ self.animations[animation.name] = animation
31
+ if self.rect.size == (0,0):
32
+ self.rect.size = animation.frames[0].get_size()
33
+ self.surface = animation.frames[0].copy()
34
+ return self
35
+
36
+ def set_animation(self,name:str,reset_counter:bool=True,loop:int=-1,queued_animation:str=None):
37
+ """
38
+ Sets the current animation,
39
+ if animation with given name hasn't been added, nothing happens
40
+ """
41
+ if name not in self.animations :
42
+ return
43
+ self.current_animation = name
44
+ if reset_counter:
45
+ self._fcounter = 0
46
+ self.counter = 0
47
+ self.animation_loop = loop
48
+ if loop != -1:
49
+ self.queued_animation = queued_animation
50
+ else:
51
+ self.queued_animation = None
52
+
53
+ def get_current_frame(self)->int|None:
54
+ if not self.current_animation:
55
+ return None
56
+ return self.animations[self.current_animation].counter_to_frame(self._fcounter)
57
+
58
+ def update(self, dt):
59
+ super().update(dt)
60
+ if not self.current_animation:
61
+ return
62
+ self._fcounter += dt * 60
63
+ self.counter = int(self._fcounter)
64
+ # self.counter = self.get_current_frame()
65
+ # print(f"{self.current_animation}:{self.counter}/{self.animations[self.current_animation].duration_list_length}")
66
+ if self.counter >= self.animations[self.current_animation].duration_list_length:
67
+ #one animation cycle ended
68
+ if self.animation_loop > 0:
69
+ self.animation_loop -= 1
70
+ elif self.queued_animation is not None:
71
+ # print("set to queued :",self.queued_animation)
72
+ self.set_animation(self.queued_animation,True)
73
+
74
+ if self.end_callback:
75
+ self.end_callback()
76
+
77
+ def draw(self, camera):
78
+ # print(self.current_animation, f"{self.counter}/{self.animations[self.current_animation].duration_list_length}")
79
+ self.surface = self.animations[self.current_animation].get_frame(self.counter,self.flipX)#,(0,0)
80
+ super().draw(camera)
81
+
@@ -0,0 +1,91 @@
1
+ import pygame
2
+ import batFramework as bf
3
+ from typing import List, Dict, Tuple, Union, Optional, Self, Iterable
4
+
5
+
6
+ def search_index(target: int, lst: List[int]) -> int:
7
+ cumulative_sum = 0
8
+ for index, value in enumerate(lst):
9
+ cumulative_sum += value
10
+ if cumulative_sum >= target:
11
+ return index
12
+ return -1
13
+
14
+
15
+ class Animation:
16
+ def __init__(
17
+ self,
18
+ name: str
19
+ ) -> None:
20
+ """
21
+ Class to hold 2D animation data.
22
+ All frames are expected to have the same size.
23
+ This class does not do anything on its own, but can be used to easily manage
24
+ multiple animations using a simple counter.
25
+ The duration list provides a entry point for tweaking the timings,
26
+ so image data can be saved (no need for contiguous duplicate frames)
27
+ """
28
+ self.name = name
29
+ self.frames: list[pygame.Surface] = []
30
+ self.frames_flipX : list[pygame.Surface] = []
31
+ self.duration_list = []
32
+ self.duration_list_length = 0
33
+ self.numFrames : int = 0
34
+
35
+ def from_surface(self,surface:pygame.Surface,frame_size : Tuple[int,int])->Self:
36
+ """
37
+ Loads frames from a spritesheet containing all animation frames aligned horizontally, left to right
38
+ Frames are cut and stored in 2 versions, original and flipped on the horizontal axis.
39
+ Flipping sprites being pretty common, this serves as a builtin cache.
40
+ """
41
+ self.frames : List[pygame.Surface] = list(
42
+ bf.utils.split_surface(surface, frame_size).values()
43
+ )
44
+ self.frames_flipX : List[pygame.Surface] = list(
45
+ bf.utils.split_surface(
46
+ surface, frame_size,
47
+ func=lambda s : pygame.transform.flip(s,True,False)
48
+ ).values()
49
+ )
50
+ self.duration_list_length = len(self.frames)
51
+ self.numFrames = self.duration_list_length
52
+ if not self.duration_list:
53
+ self.duration_list = [1]*self.duration_list_length
54
+ return self
55
+
56
+ def from_path(
57
+ self,
58
+ path: str,
59
+ frame_size: Tuple[int, int],
60
+ convert_alpha: bool = True
61
+ ) -> Self:
62
+ """
63
+ Loads frames from a spritesheet at the given path.
64
+ Uses ResourceManager to load the image.
65
+ """
66
+ surface = bf.ResourceManager().get_image(path, convert_alpha)
67
+ return self.from_surface(surface, frame_size)
68
+
69
+
70
+ def __repr__(self):
71
+ return f"Animation({self.name})"
72
+
73
+ def counter_to_frame(self, counter: Union[float, int]) -> int:
74
+ if not self.frames :
75
+ raise ValueError("Animation has no frames")
76
+ return search_index(
77
+ int(counter % self.duration_list_length), self.duration_list
78
+ )
79
+
80
+ def get_frame(self, counter: Union[float, int], flip: bool) -> pygame.Surface:
81
+ i = self.counter_to_frame(counter)
82
+ return self.frames_flipX[i] if flip else self.frames[i]
83
+
84
+ def set_duration_list(self, duration_list: Union[List[int], int]) -> Self:
85
+ if not isinstance(duration_list, Iterable):
86
+ duration_list = [duration_list] * len(self.frames)
87
+ if len(duration_list) != self.numFrames:
88
+ raise ValueError("duration_list should have values for all frames")
89
+ self.duration_list = duration_list
90
+ self.duration_list_length = sum(self.duration_list)
91
+ return self