88#make variation of window that shows text only.
99#add blinking cursor animation for text editing
1010
11- buttonsTexture = convertSurfToTex (loadImageAsSurf (paths .buttons ))
11+ buttonsSurf = loadImageAsSurf (paths .buttons )
12+ buttonsTexture = convertSurfToTex (buttonsSurf )
13+ sdl2 .SDL_FreeSurface (buttonsSurf )
1214
1315class inputField : #contains the header for field section and the attributes
1416 #type 0 = int, type 1 = directory, type 2 = bool, type 3 = string, type 4 = file path
@@ -18,6 +20,8 @@ def __init__(self, text, defaultInput, type):
1820 self .realInput = defaultInput
1921 self .defaultInput = self .encode (defaultInput )
2022 self .userInput = ''
23+
24+ self .textTex = createEmptyTexture ()
2125
2226 self .active = False #if active then render highlighted
2327
@@ -44,6 +48,8 @@ def createText(self, color, typeInput): #generates text texture and also checks
4448 typeInput = self .defaultInput
4549 elif typeInput == 1 :
4650 typeInput = self .userInput
51+
52+ sdl2 .SDL_DestroyTexture (self .textTex )
4753 if len (str (typeInput )) == 0 : #No text at all, so don't generate text texture because it will create a null pointer.
4854 self .textTex = sdl2 .SDL_CreateTexture (const .renderer .sdlrenderer ,
4955 sdl2 .SDL_PIXELFORMAT_RGBA8888 ,
@@ -52,16 +58,16 @@ def createText(self, color, typeInput): #generates text texture and also checks
5258 12 )
5359 self .textSrcRect = rect (0 , 0 , 10 , 12 )
5460 self .textDestRect = rect (self .textXPos , self .textYPos , 10 , 12 )
55- dontConvertToTex = True
5661 else : #generate text because it WON'T result in a null pointer
57- self . textTex = generateText (typeInput , color , 'normal' )
58- if self . textTex .contents .w <= 46 : #text width is less than 46 means it fits into the box so it's not cropped
59- self .textSrcRect = rect (0 , 0 , self . textTex .contents .w , 12 )
60- self .textDestRect = rect (self .textXPos , self .textYPos , self . textTex .contents .w , 12 )
62+ textSurf = generateText (typeInput , color , 'normal' )
63+ if textSurf .contents .w <= 46 : #text width is less than 46 means it fits into the box so it's not cropped
64+ self .textSrcRect = rect (0 , 0 , textSurf .contents .w , 12 )
65+ self .textDestRect = rect (self .textXPos , self .textYPos , textSurf .contents .w , 12 )
6166 else : #text is too long to fit in box so it's cropped
62- self .textSrcRect = rect (self . textTex . contents .w - 46 , 0 , self . textTex .contents .w , 12 )
67+ self .textSrcRect = rect (textSurf . contents .w - 46 , 0 , textSurf .contents .w , 12 )
6368 self .textDestRect = rect (self .textXPos - 2 , self .textYPos , 46 , 12 )
64- self .textTex = convertSurfToTex (self .textTex )
69+ self .textTex = convertSurfToTex (textSurf )
70+ sdl2 .SDL_FreeSurface (textSurf )
6571
6672 def disable (self ):
6773 self .disabled = True
@@ -112,6 +118,21 @@ def destroy(self): #delete surface
112118 const .surfaceFunctions .remove (self )
113119 mouse .resetSurfaces ()
114120
121+ sdl2 .SDL_DestroyTexture (self .windowTex )
122+
123+
124+ sdl2 .SDL_DestroyTexture (self .inputFieldNormalTex )
125+ sdl2 .SDL_DestroyTexture (self .inputFieldHighlightedTex )
126+
127+ sdl2 .SDL_DestroyTexture (self .inputFieldButtonsTex )
128+ sdl2 .SDL_DestroyTexture (self .inputFieldTextTex )
129+
130+ for tex in self .buttons : #tex is a tuple, [1] is texture index
131+ sdl2 .SDL_DestroyTexture (tex [1 ])
132+
133+ for entry in self .inputFields :
134+ for tex in entry .fieldAreaTextTextures :
135+ sdl2 .SDL_DestroyTexture (tex )
115136
116137 # for inputField in self.inputFields:
117138 # for field in inputField.inputFields:
@@ -133,31 +154,36 @@ def calculateWindowSize(self):
133154
134155 textColor = sdl2 .SDL_Color (207 , 219 , 199 )
135156
136- self .headerTextTex = generateText (self .headerText , textColor , 'bold' )
137- headerWidth = self .headerTextTex .contents .w * 2 #sprite is 2 times big.
138- headerHeight = self .headerTextTex .contents .h * 2
157+ self .headerTextSurf = generateText (self .headerText , textColor , 'bold' )
158+ headerWidth = self .headerTextSurf .contents .w * 2 #sprite is 2 times big.
159+ headerHeight = self .headerTextSurf .contents .h * 2
160+
139161 windowHeaderLength = headerWidth
140- self .headerTextTex = convertSurfToTex (self .headerTextTex )
162+ self .headerTextTex = convertSurfToTex (self .headerTextSurf )
163+ sdl2 .SDL_FreeSurface (self .headerTextSurf )
141164
142165
143166 self .height = 29 #header height
144167 #self.height accumulates height through this loop:
145168 for entry in self .inputFields :
146- entryTextTex = generateText (entry .headerText , textColor , 'bold' )
147- if entryTextTex .contents .w > inputStarterHeaderLength :
148- inputStarterHeaderLength = entryTextTex .contents .w
169+ entryTextSurf = generateText (entry .headerText , textColor , 'bold' )
170+ if entryTextSurf .contents .w > inputStarterHeaderLength :
171+ inputStarterHeaderLength = entryTextSurf .contents .w
149172 self .height += 18 #input field header height
150- entryTextTex = convertSurfToTex (entryTextTex )
173+ entryTextTex = convertSurfToTex (entryTextSurf )
174+ sdl2 .SDL_FreeSurface (entryTextSurf )
175+
151176 self .fieldHeaderTextTextures .append (entryTextTex )
152-
153177 for attribute in entry .inputFields :
154- attributeTextTex = generateText (attribute .text , textColor , 'bold' )
155- if attributeTextTex .contents .w + 67 > inputFieldTextLength :
156- inputFieldTextLength = attributeTextTex .contents .w + 67
178+ attributeTextSurf = generateText (attribute .text , textColor , 'bold' )
179+ if attributeTextSurf .contents .w + 67 > inputFieldTextLength :
180+ inputFieldTextLength = attributeTextSurf .contents .w + 67
157181 self .height += 17 #input field height
158182
159- attributeTextTex = convertSurfToTex (attributeTextTex )
183+ attributeTextTex = convertSurfToTex (attributeTextSurf )
160184 entry .fieldAreaTextTextures .append (attributeTextTex )
185+ sdl2 .SDL_FreeSurface (attributeTextSurf )
186+
161187 self .height += 17 #height until next header.
162188 self .height += 30 #height to botom.
163189
@@ -236,6 +262,9 @@ def calculateWindowSize(self):
236262 textYPos += 16
237263 fieldAreaIndex = 0
238264 fieldHeaderIndex += 1
265+
266+ for tex in self .fieldHeaderTextTextures :
267+ sdl2 .SDL_DestroyTexture (tex )
239268
240269 #BUTTONS:
241270
@@ -321,13 +350,15 @@ def calculateWindowSize(self):
321350
322351 def drawButton (self , text ):
323352 #texture is twice as big for both normalTex and pressedTex
324- textTex = generateText (text , sdl2 .SDL_Color (207 , 219 , 199 ), 'bold' )
325- width = textTex .contents .w + 16
353+ width = ctypes .c_int ()
354+ sdl2 .sdlttf .TTF_SizeText (const .fontKeroM , bytes (text , 'utf-8' ), width , None )
355+ width = width .value + 16
326356 tex = sdl2 .SDL_CreateTexture (const .renderer .sdlrenderer ,
327357 sdl2 .SDL_PIXELFORMAT_RGB888 ,
328358 sdl2 .SDL_TEXTUREACCESS_TARGET ,
329359 width * 2 ,
330360 16 )
361+
331362 def drawButtonTex (xPos , bgColor , shineColor , shadeColor , textColor ): #*bgColor
332363 sdl2 .SDL_SetRenderDrawColor (const .renderer .sdlrenderer , * bgColor , 255 )
333364 sdl2 .SDL_RenderFillRect (const .renderer .sdlrenderer , rect (xPos , 0 , width , 16 ))
@@ -343,26 +374,18 @@ def drawButtonTex(xPos, bgColor, shineColor, shadeColor, textColor): #*bgColor
343374 sdl2 .SDL_SetRenderDrawColor (const .renderer .sdlrenderer , 51 , 71 , 66 , 255 )
344375 sdl2 .SDL_RenderDrawRect (const .renderer .sdlrenderer , rect (xPos , 0 , width , 16 ))
345376
346- textTex = generateText (text , textColor , 'bold' )
347- sdl2 .SDL_RenderCopy (const .renderer .sdlrenderer , convertSurfToTex (textTex ), None , rect (xPos + 8 , 1 , textTex .contents .w , textTex .contents .h ))
377+ textSurf = generateText (text , textColor , 'bold' )
378+ textTex = convertSurfToTex (textSurf )
379+ sdl2 .SDL_RenderCopy (const .renderer .sdlrenderer , textTex , None , rect (xPos + 8 , 1 , textSurf .contents .w , textSurf .contents .h ))
380+ sdl2 .SDL_FreeSurface (textSurf )
381+ sdl2 .SDL_DestroyTexture (textTex )
348382
349383 sdl2 .SDL_SetRenderTarget (const .renderer .sdlrenderer , tex )
350384 drawButtonTex (0 , (105 , 130 , 109 ), (148 , 176 , 131 ), (72 , 98 , 99 ), sdl2 .SDL_Color (207 , 219 , 199 ))
351385 drawButtonTex (width , (77 , 104 , 82 ), (68 , 85 , 101 ), (105 , 130 , 109 ), sdl2 .SDL_Color (51 , 71 , 66 ))
352386
353387 normalRect = rect (0 , 0 , width , 16 )
354388 pressedRect = rect (width , 0 , width , 16 )
355- # if type == 0:
356- # col0 = (105, 130, 109)
357- # col1 = (148, 176, 131)
358- # col2 = (72, 98, 99)
359- # col3 = sdl2.SDL_Color(207, 219, 199) #text
360- # elif type == 1:
361- # col0 = (77, 104, 82)
362- # col1 = (68, 85, 101)
363- # col2 = (105, 130, 109)
364- # col3 = sdl2.SDL_Color(51, 71, 66) #text
365-
366389 return tex , normalRect , pressedRect
367390
368391 def createInputFieldTextures (self ):
@@ -398,17 +421,9 @@ def drawInputField(tex, bgCol, shadeCol, shineCol, outlineCol):
398421 52 ,
399422 16
400423 )
401- self .inputFieldDisabledTex = sdl2 .SDL_CreateTexture (
402- const .renderer .sdlrenderer ,
403- sdl2 .SDL_PIXELFORMAT_RGB888 ,
404- sdl2 .SDL_TEXTUREACCESS_TARGET ,
405- 52 ,
406- 16
407- )
408424
409425 drawInputField (self .inputFieldNormalTex , (77 , 104 , 82 ), (68 , 85 , 101 ), (105 , 130 , 109 ), (51 , 71 , 66 ))
410426 drawInputField (self .inputFieldHighlightedTex , (105 , 130 , 109 ), (72 , 98 , 99 ), (148 , 176 , 131 ), (51 , 71 , 66 ))
411- drawInputField (self .inputFieldDisabledTex , (67 , 81 , 74 ), (67 , 81 , 74 ), (83 , 101 , 93 ), (50 , 61 , 61 ))
412427
413428 def moveDestRect (self ):
414429 """ Moves the rectangle while dragging based on mouse position. """
0 commit comments