10
10
You can also use :py:data:`casioplot_settings` to change some behavior.
11
11
"""
12
12
13
- from os import path
14
13
from typing import Literal
15
14
from PIL import Image , ImageTk
16
15
import tkinter as tk
17
- from configs import get_config
16
+ from configs import _get_config
17
+ from characters import _get_char
18
18
19
19
# color type
20
20
COLOR = tuple [int , int , int ]
21
21
# some frequently used colors
22
- _WHITE : COLOR = (255 , 255 , 255 ) # RGBA white
22
+ _WHITE : COLOR = (255 , 255 , 255 ) # RGB white
23
23
_BLACK : COLOR = (0 , 0 , 0 ) # RGBA black
24
24
# a list of all settings that if change should trigger the _redraw_screen function
25
25
redraw_settings : tuple = ('width' , 'height' , 'left_margin' , 'right_margin' , 'top_margin' , 'bottom_margin' )
@@ -71,8 +71,8 @@ def _screen_dimensions(self) -> tuple[int, int]:
71
71
72
72
73
73
def config_to (self , config : str = "default" ) -> None :
74
- global _screen
75
- for setting , value in get_config (config ).items ():
74
+ global _screen , _window
75
+ for setting , value in _get_config (config ).items ():
76
76
setattr (self , setting , value )
77
77
78
78
# a configuration may have a background image
@@ -102,6 +102,7 @@ def set(self, **settings) -> None:
102
102
should_redraw_screen = True
103
103
104
104
elif setting == "show_screen" :
105
+ global _window
105
106
if value is True :
106
107
_window .deiconify ()
107
108
else :
@@ -127,7 +128,7 @@ def _redraw_screen() -> None:
127
128
Only called when casioplot_settings.set() is called,
128
129
used to redraw _image with custom margins, width and height.
129
130
"""
130
- global _screen
131
+ global _screen , _window
131
132
screen_width , screen_height = casioplot_settings ._screen_dimensions ()
132
133
133
134
# Create a new white image
@@ -167,6 +168,7 @@ def show_screen() -> None:
167
168
The image is saved with the filename found in `casioplot_settings.get('filename')`
168
169
"""
169
170
if casioplot_settings .get ("show_screen" ) is True :
171
+ global _photo_image , _screen_display , _window
170
172
# show the screen
171
173
_photo_image = ImageTk .PhotoImage (_screen )
172
174
_screen_display ["image" ] = _photo_image
@@ -206,61 +208,45 @@ def set_pixel(x: int, y: int, color: COLOR = _BLACK) -> None:
206
208
:param y: y coordinate (from the top)
207
209
:param color: The pixel color. A tuple that contain 3 integers from 0 to 255.
208
210
"""
211
+ global _screen
209
212
if _coordenates_in_bounds (x , y ):
210
213
_screen .putpixel (_canvas_to_screen (x , y ), color )
211
214
212
215
213
- def _get_filename (character , size : Literal ["small" , "medium" , "large" ] = "medium" ):
214
- """Get the file where a character is saved and return the ``space`` file if the character doesn't exist.
215
-
216
- :param character: The character to find
217
- :param size: The size of the character
218
- :return: The character filename. A string: "{``./chars`` folder absolute path}/{character}_{size}.png"
219
- """
220
- special_chars = {" " : "space" }
221
- filename = special_chars .get (character , character ) + ".txt"
222
- file_path = path .join (path .abspath (path .dirname (__file__ )), "chars" , size , filename )
223
-
224
- if not path .isfile (file_path ):
225
- print (f'WARNING: No character "{ character } " found for size "{ size } ".' )
226
- file_path = path .join (
227
- path .abspath (path .dirname (__file__ )), "chars" , size , "space.txt"
228
- )
229
- return file_path
230
-
231
-
232
216
def draw_string (
233
217
x : int ,
234
218
y : int ,
235
219
text : str ,
236
220
color : COLOR = _BLACK ,
237
- size : Literal ["small" , "medium" , "large" ] = "medium" ,
221
+ size : Literal ["small" , "medium" , "large" ] = "medium"
238
222
) -> None :
239
223
"""Draw a string on the virtual screen with the given RGB color and size.
240
224
241
225
:param x: x coordinate (from the left)
242
226
:param y: y coordinate (from the top)
243
- :param text: text that will be shown
244
- :param color: The text color. A tuple that contain 3 integers from 0 to 255.
227
+ :param text: text that will be drawn
228
+ :param color: The color of the text . A tuple that contain 3 integers from 0 to 255.
245
229
:param size: Size of the text. String from the following values: "small", "medium" or "large".
246
230
:raise ValueError: Raise a ValueError if the size isn't correct.
247
231
"""
248
- sizes = {"small" : (10 , 10 ), "medium" : (13 , 17 ), "large" : (18 , 23 )}
249
-
250
- if size not in sizes .keys ():
251
- raise ValueError (
252
- f'Unknown size "{ size } ". Size must be one of the following: "small", "medium" or "large"'
253
- )
254
-
255
- for character in text :
256
- filename = _get_filename (character , size )
257
- n = 0
258
- with open (filename , "r" ) as c :
259
- count = 0
260
- for line in c :
261
- for j , k in enumerate (line ):
262
- if k in ["$" ]:
263
- set_pixel (x + j , y + count , color )
264
- n = j
265
- count += 1
266
- x += n
232
+ def draw_char (x : int , y : int , char_map : tuple , color : COLOR ) -> None :
233
+ """Draws a single character
234
+
235
+ :param x: x coordinate (from the left)
236
+ :param y: y coordinate (from the top)
237
+ :param char_mpa: a tuple of strings, where every string represents a row
238
+ of the character that is going to be drawn
239
+ :param color: The color of the character
240
+ """
241
+ for y2 , row in enumerate (char_map ):
242
+ for x2 , pixel in enumerate (row ):
243
+ if pixel == 'X' :
244
+ set_pixel (x + x2 , y + y2 , color )
245
+
246
+ for char in text :
247
+ if not _coordenates_in_bounds (x , y ):
248
+ return
249
+
250
+ char_map = _get_char (char , size )
251
+ draw_char (x , y , char_map , color )
252
+ x += len (char_map [0 ])
0 commit comments