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