e2D 1.4.11__py3-none-any.whl → 1.4.12__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.
e2D/envs.py CHANGED
@@ -1,15 +1,13 @@
1
1
  from __future__ import annotations
2
+ from typing import Literal
2
3
 
3
4
  from .utils import *
4
5
  import pygame as pg
5
- from pygame.event import Event
6
- from time import time as __tm__
7
- import math as _mt
8
6
 
9
7
  """ CODE EXAMPLE FOR RootEnv
10
8
  from e2D.envs import *
11
9
 
12
- class Env:
10
+ class Env(DefEnv):
13
11
  def __init__(self) -> None:
14
12
  pass
15
13
 
@@ -25,190 +23,141 @@ while not rootEnv.quit:
25
23
  """
26
24
 
27
25
  pg.init()
28
- pg.font.init()
29
- FONT_ARIAL_16 = pg.font.SysFont("Arial", 16)
30
- FONT_ARIAL_32 = pg.font.SysFont("Arial", 32)
31
- FONT_ARIAL_64 = pg.font.SysFont("Arial", 64)
32
- new_font = lambda size, name="Arial", bold=False, italic=False: pg.font.SysFont(name, size, bold, italic)
33
-
34
- TEXT_FIXED_SIDES_TOP_LEFT = 0
35
- TEXT_FIXED_SIDES_TOP_MIDDLE = 1
36
- TEXT_FIXED_SIDES_TOP_RIGHT = 2
37
- TEXT_FIXED_SIDES_MIDDLE_LEFT = 3
38
- TEXT_FIXED_SIDES_MIDDLE_MIDDLE = 4
39
- TEXT_FIXED_SIDES_MIDDLE_RIGHT = 5
40
- TEXT_FIXED_SIDES_BOTTOM_LEFT = 6
41
- TEXT_FIXED_SIDES_BOTTOM_MIDDLE = 7
42
- TEXT_FIXED_SIDES_BOTTOM_RIGHT = 8
43
-
44
- FIXED_SIDES_MULTIPLIER = [V2(x,y) for y in [0, .5, 1] for x in [0, .5, 1]]
45
-
46
- INPUT_CELL_ASCII_TYPE = 0
47
- INPUT_CELL_ALPHANUM_TYPE = 1
48
- INPUT_CELL_ALPHA_TYPE = 2
49
- INPUT_CELL_NUM_TYPE = 3
50
-
51
- class Utils:
52
- def __init__(self) -> None:
53
- self.rootEnv : RootEnv = None # type: ignore
54
- self.surface : pg.Surface
55
- def draw(self) -> None: pass
56
- def update(self) -> None: pass
57
26
 
58
- class InputCell(Utils):
59
- def __init__(self,
60
- initial_value:str,
61
- position:V2|Vector2D,
62
- size:V2|Vector2D,
63
- prefix:str|None=None,
64
- text_color:tuple[int,int,int]|list[int]=(255,255,255),
65
- bg_color:None|tuple[int,int,int]|list[int]=None,
66
- border_color:None|tuple[int,int,int]|list[int]=(255,255,255),
67
- border_width:float=0,
68
- border_radius:int|list[int]|tuple[int,int,int,int]=-1,
69
- margin:V2|Vector2D=V2z,
70
- fixed_sides:int=TEXT_FIXED_SIDES_MIDDLE_MIDDLE,
71
- font:pg.font.Font=FONT_ARIAL_32,
72
- personalized_surface:pg.Surface|None=None,
73
- on_enter_pressed=None,
74
- check_when_adding=None) -> None:
75
- super().__init__()
76
-
77
- self.on_enter_pressed = on_enter_pressed
78
- self.check_when_adding = check_when_adding
79
- self.prefix = prefix if prefix != None else ""
80
-
81
- self.text_color = text_color
82
- self.font = font
83
- self.surface = personalized_surface
84
- self.bg_color = bg_color
85
- # size = V2(*self.text_box.get_size()) + self.margin * 2
86
- self.size = size
87
- self.position = (position - size * FIXED_SIDES_MULTIPLIER[fixed_sides] + margin)
88
- self.bg_rect = [0, 0] + size()
89
- self.margin_rect = (margin * -1)() + size()
90
- self.border_color = border_color
91
- self.border_radius = [border_radius]*4 if not any(isinstance(border_radius, cls) for cls in {tuple, list}) else border_radius
92
- self.border_width = border_width
93
-
94
- self.value = initial_value
95
- self.update_text()
96
-
97
- self.text_surface = pg.Surface(self.size(), pg.SRCALPHA, 32).convert_alpha()
98
-
99
- def draw(self) -> None:
100
- self.text_surface.fill((0,0,0,0))
101
- if self.bg_color != None:
102
- pg.draw.rect(self.text_surface, self.bg_color, self.bg_rect, 0, -1, *self.border_radius)
103
-
104
- self.text_surface.blit(self.text_box, self.text_position())
105
-
106
- if self.rootEnv.selected_util != self:
107
- if self.border_width:
108
- pg.draw.rect(self.text_surface, self.border_color, self.margin_rect, self.border_width, -1, *self.border_radius)
109
- else:
110
- pg.draw.rect(self.text_surface, [127 + 127 * _mt.sin(self.rootEnv.get_time_from_start() * 10)]*3, self.margin_rect, self.border_width if self.border_width else 10, -1, *self.border_radius)
27
+ __LITERAL_PIVOT_POSITIONS__ = Literal["top_left", "top_center", "top_right", "center_left", "center_center", "center_right", "bottom_left", "bottom_center", "bottom_right"]
28
+ __PIVOT_POSITIONS_MULTIPLIER__ = dict(zip(("top_left", "top_center", "top_right", "center_left", "center_center", "center_right", "bottom_left", "bottom_center", "bottom_right"), (Vector2D(x,y) for y in [0, .5, 1] for x in [0, .5, 1])))
111
29
 
112
- self.surface.blit(self.text_surface, self.position())
113
-
114
- def update(self) -> None:
115
- if self.rootEnv.mouse.just_pressed[0]:
116
- if self.position.x < self.rootEnv.mouse.position.x < self.position.x + self.size.x and\
117
- self.position.y < self.rootEnv.mouse.position.y < self.position.y + self.size.y:
118
- self.rootEnv.selected_util = self if self.rootEnv.selected_util != self else None
119
- self.update_text()
120
- if self.rootEnv.selected_util == self:
121
- for event in self.rootEnv.events:
122
- if event.type == pg.TEXTINPUT:
123
- self.value += event.text if self.check_when_adding == None else self.check_when_adding(event.text)
124
- elif event.type == pg.KEYDOWN and event.key == pg.K_BACKSPACE:
125
- self.value = self.value[:-1]
126
- if self.rootEnv.keyboard.get_key(pg.K_DELETE):
127
- self.value = self.value[:-1]
128
- if self.rootEnv.keyboard.get_key(pg.K_RETURN, KEY_MODE_JUST_PRESSED):
129
- if callable(self.on_enter_pressed): self.on_enter_pressed(self.value)
130
- if self.rootEnv.keyboard.get_key(pg.K_ESCAPE, KEY_MODE_JUST_PRESSED):
131
- self.rootEnv.selected_util = self if self.rootEnv.selected_util != self else None
132
- self.update_text()
133
-
134
- def update_text(self) -> None:
135
- self.text_box = self.font.render(self.prefix + self.value, True, self.text_color)
136
- if self.rootEnv != None and self.rootEnv.selected_util == self:
137
- # self.text_position = self.position + self.size * V2(.85, .5) - V2(*self.text_box.get_size()) * V2(1, .5) - self.position
138
- self.text_position = self.position + self.size * .5 - V2(*self.text_box.get_size()) * V2(.5, .5) - self.position
139
- else:
140
- self.text_position = self.position + self.size * .5 - V2(*self.text_box.get_size()) * V2(.5, .5) - self.position
30
+ class DefEnv:
31
+ def __init__(self) -> None: ...
32
+
33
+ def draw(self) -> None: ...
34
+
35
+ def update(self) -> None: ...
141
36
 
142
37
  class RootEnv:
143
- def __init__(self, screen_size:V2|Vector2D=V2(1920, 1080), vsync:bool=True, target_fps:int=60, show_fps=True, quit_on_key_pressed:None|int=pg.K_x, window_flag:int=0, clear_screen_each_frame:bool=True) -> None:
38
+ def __init__(self,
39
+ screen_size : Vector2D = Vector2D(1920, 1080),
40
+ target_fps : int = 60,
41
+ show_fps = True,
42
+ quit_on_key_pressed : None|int = pg.K_x,
43
+ vsync : bool = True,
44
+ window_flag : int = 0,
45
+ clear_screen_each_frame : bool = True) -> None:
144
46
  self.quit = False
145
- self.screen_size :V2|Vector2D= screen_size
146
- self.screen = pg.display.set_mode(self.screen_size(), vsync=vsync, flags=window_flag)
47
+ self.__screen_size__ :Vector2D= screen_size
48
+
49
+ self.__vsync__ = vsync
50
+ self.__flags__ = window_flag
51
+ self.screen = pg.display.set_mode(self.__screen_size__(), vsync=self.__vsync__, flags=self.__flags__)
52
+
53
+ self.clock = pg.time.Clock()
54
+ self.keyboard = Keyboard()
55
+ self.mouse = Mouse(self)
56
+
147
57
  self.target_fps = target_fps
148
58
  self.current_fps = self.target_fps if self.target_fps != 0 else 1
149
- self.delta = 1 / self.current_fps
150
59
  self.current_frame = 0
151
60
  self.show_fps = show_fps
152
- self.clock = pg.time.Clock()
153
- self.keyboard = Keyboard(self)
154
- self.mouse = Mouse(self)
155
- self.events :list[Event]= []
61
+ self.events :list[pg.event.Event]= []
156
62
  self.background_color = rgb(0,0,0)
157
- self._quit_on_key_pressed = quit_on_key_pressed
158
63
  self.clear_screen_each_frame = clear_screen_each_frame
159
- self.starting_time :float= __tm__()
160
- self.utils :list[Utils]= []
161
- self.selected_util :Utils|None = None
64
+ self.utils :dict[int|str, Util]= {}
65
+ self.selected_util :Util|None = None
66
+ self.__quit_on_key_pressed__ = quit_on_key_pressed
67
+
68
+
69
+ @property
70
+ def screen_size(self) -> Vector2D:
71
+ return self.__screen_size__
72
+
73
+ @screen_size.setter
74
+ def screen_size(self, new_size:Vector2D) -> None:
75
+ self.screen = pg.display.set_mode(self.__screen_size__(), vsync=self.__vsync__, flags=self.__flags__)
76
+
77
+ @property
78
+ def delta(self) -> int:
79
+ return self.clock.get_time()
80
+
81
+ def get_teoric_max_fps(self) -> float:
82
+ rawdelta = self.clock.get_rawtime()
83
+ return (1000 / rawdelta) if rawdelta != 0 else 1
84
+
85
+ def update_screen_mode(self, vsync:None|bool=None, flags=None) -> None:
86
+ pass
87
+
88
+ def sleep(self, seconds:int|float, precise_delay=False) -> None:
89
+ if precise_delay:
90
+ pg.time.delay(seconds * 1000)
91
+ else:
92
+ pg.time.wait(seconds * 1000)
162
93
 
163
- def add_utils(self, *utils:Utils) -> None:
94
+ def add_utils(self, *utils:Util) -> None:
164
95
  for util in utils:
165
96
  if util.surface == None: util.surface = self.screen
166
- util.rootEnv = self
167
- self.utils.extend(utils)
97
+ util.rootEnv = self
98
+ self.utils[util.id] = util
168
99
 
169
- def remove_utils(self, *utils:list[Utils]) -> None:
170
- for u in utils: self.utils.remove(u)
100
+ def remove_utils(self, *utils:int|str|Util) -> None:
101
+ for uid in utils:
102
+ if uid in self.utils:
103
+ del self.utils[uid]
104
+ elif isinstance(uid, Util):
105
+ del self.utils[uid.id]
106
+ else:
107
+ raise Exception(f"Unknown util type: {uid}")
171
108
 
172
- def get_time_from_start(self) -> float:
173
- return __tm__() - self.starting_time
109
+ @property
110
+ def runtime_seconds(self) -> float:
111
+ return pg.time.get_ticks()
174
112
 
175
- def init(self, sub_env) -> None:
113
+ def init(self, sub_env:DefEnv) -> None:
176
114
  self.env = sub_env
177
115
 
178
116
  def clear(self) -> None:
179
117
  self.screen.fill(self.background_color)
180
118
 
181
- def clear_rect(self, position:V2|Vector2D, size:V2|Vector2D) -> None:
119
+ def clear_rect(self, position:Vector2D, size:Vector2D) -> None:
182
120
  self.screen.fill(self.background_color, position() + size())
183
121
 
184
- def print(self, text:str, position:V2|Vector2D, color:tuple[float,float,float]=(255,255,255), fixed_sides=TEXT_FIXED_SIDES_TOP_LEFT, font:pg.font.Font=FONT_ARIAL_32, bg_color:None|tuple[int,int,int]|list[int]=None, border_color:None|tuple[int,int,int]|list[int]=(255,255,255), border_width:float=0, border_radius:int|list[int]|tuple[int,int,int,int]=-1, margin:V2|Vector2D=V2z, personalized_surface:pg.Surface|None=None) -> None:
122
+ def print(self,
123
+ text : str,
124
+ position : Vector2D,
125
+ color : tuple[float,float,float] = (255,255,255),
126
+ pivot_position : __LITERAL_PIVOT_POSITIONS__ = "top_left",
127
+ font : pg.font.Font = FONT_ARIAL_32,
128
+ bg_color : None|tuple[int,int,int]|list[int] = None,
129
+ border_color : None|tuple[int,int,int]|list[int] = (255,255,255),
130
+ border_width : float = 0,
131
+ border_radius : int|list[int]|tuple[int,int,int,int] = -1,
132
+ margin : Vector2D = Vector2D.zero(),
133
+ personalized_surface : pg.Surface|None = None
134
+ ) -> None:
185
135
  text_box = font.render(text, True, color)
186
- size = V2(*text_box.get_size()) + margin * 2
187
- position = position - size * FIXED_SIDES_MULTIPLIER[fixed_sides] + margin
136
+ size = Vector2D(*text_box.get_size()) + margin * 2
137
+ pivotted_position = position - size * __PIVOT_POSITIONS_MULTIPLIER__[pivot_position] + margin
188
138
  if not any(isinstance(border_radius, cls) for cls in {tuple, list}): border_radius = [border_radius]*4
189
139
  surface = (self.screen if personalized_surface == None else personalized_surface)
190
140
  if bg_color != None:
191
- pg.draw.rect(surface, bg_color, (position - margin)() + size(), 0, -1, *border_radius)
141
+ pg.draw.rect(surface, bg_color, (pivotted_position - margin)() + size(), 0, -1, *border_radius)
192
142
  if border_width:
193
- pg.draw.rect(surface, border_color, (position - margin)() + size(), border_width, -1, *border_radius)
194
- surface.blit(text_box, position())
143
+ pg.draw.rect(surface, border_color, (pivotted_position - margin)() + size(), border_width, -1, *border_radius)
144
+ surface.blit(text_box, pivotted_position())
195
145
 
196
146
  def __draw__(self) -> None:
197
147
  self.clock.tick(self.target_fps)
198
148
  self.current_fps = self.clock.get_fps()
199
- self.delta = 1 / (self.current_fps if self.current_fps != 0 else 1)
200
149
  if self.clear_screen_each_frame: self.clear()
201
150
  if self.show_fps: self.print(str(round(self.current_fps,2)), self.screen_size * .01, bg_color=(0,0,0))
202
151
 
203
152
  self.env.draw()
204
- for util in self.utils: util.draw()
153
+ for util in self.utils.values(): util.draw()
205
154
  pg.display.update()
206
155
 
207
156
  def __update__(self) -> None:
208
157
  self.mouse.update()
209
158
  self.keyboard.update()
210
159
  self.env.update()
211
- for util in self.utils: util.update()
160
+ for util in self.utils.values(): util.update()
212
161
 
213
162
  def frame(self) -> None:
214
163
  self.events = pg.event.get()
@@ -217,5 +166,5 @@ class RootEnv:
217
166
  self.__draw__()
218
167
 
219
168
  for event in self.events:
220
- if event.type == pg.QUIT or ((event.type == pg.KEYDOWN and event.key == self._quit_on_key_pressed and self.selected_util == None) if self._quit_on_key_pressed != None else False):
169
+ if event.type == pg.QUIT or ((event.type == pg.KEYDOWN and event.key == self.__quit_on_key_pressed__ and self.selected_util == None) if self.__quit_on_key_pressed__ != None else False):
221
170
  self.quit = True