66
77width , height = 1024 , 768
88
9- def lineWrap (txt , w ):
10- processed = []
11- temp = ""
12- for i in range (txt .__len__ ()):
13- if i != 0 and i % w == 0 :
14- processed .append (framework .raster .render (temp , True , (0 , 0 , 0 )))
15- temp = ""
16- temp += txt [i ]
17- processed .append (framework .raster .render (temp , True , (0 , 0 , 0 )))
18- return processed
19-
209class Pic (object ):
2110 def __init__ (self , fileName ):
2211 img = pygame .image .load (fileName )
@@ -38,8 +27,7 @@ def __init__(self):
3827 self .screen = pygame .display .set_mode ((width , height ))
3928 pygame .display .set_caption ("Windows 95 Simulated" )
4029 self .clock = pygame .time .Clock ()
41- self .font32 = pygame .font .Font ("res/segoeui.ttf" , 32 )
42- self .raster = pygame .font .Font ("res/vga936.fon" , 32 )
30+ self .raster = pygame .font .Font ("res/pkmndp.ttf" , 19 )
4331 self .speed = 5
4432 self .mousePos = (0 , 0 )
4533 self .apps = []
@@ -63,11 +51,10 @@ def addDialog(self, dialog):
6351 dialog .dialogID = len (self .dialogs )
6452 dialog .closeBtn = Secret ((dialog .x + 357 , dialog .y + 6 , 17 , 16 ), dialog .dialogID )
6553 self .dialogs .append (dialog )
66- # TODO: Add KEYUP/KEYDOWN support
6754 def keyUp (self , key ):
68- return
55+ self . apps [ self . appID ]. keyUp ( key )
6956 def keyDown (self , key ):
70- return
57+ self . apps [ self . appID ]. keyDown ( key )
7158 def mouseDown (self , pos , button ):
7259 self .apps [self .appID ].mouseDown (pos , button )
7360 try :
@@ -86,7 +73,8 @@ def __init__(self, picName):
8673 self .appID = 0
8774 self .btnList = []
8875 self .tooltipList = []
89- self .txtFieldList = []
76+ self .txtField = TxtField (0 , 0 , 0 , 0 )
77+ self .txtFieldEnabled = False
9078 self .secretList = []
9179 def draw (self , screen ):
9280 if framework .appID != self .appID :
@@ -96,11 +84,20 @@ def draw(self, screen):
9684 button .draw (screen )
9785 for tooltip in self .tooltipList :
9886 tooltip .draw (screen )
87+ if self .txtFieldEnabled :
88+ self .txtField .content = self .txtField .wrap (self .txtField .txtBuffer )
89+ self .txtField .content = self .txtField .content [- self .txtField .h :]
90+ self .txtField .draw (screen , self .txtField .content )
9991 def addButton (self , b ):
10092 self .btnList .append (b )
10193 def addTooltip (self , txt , font , x , y , c , rect ):
102- t = Txt (txt , font , x , y , c , rect )
103- self .txtList .append (t )
94+ tt = Tooltip (txt , font , x , y , c , rect )
95+ self .txtList .append (tt )
96+ def enableTxtField (self , x , y , w , h , placeholder = "/# " ):
97+ self .txtFieldEnabled = True
98+ self .txtField .x , self .txtField .y = x , y
99+ self .txtField .w , self .txtField .h = w , h
100+ self .txtField .placeholder = placeholder
104101 def mouseDown (self , pos , button ):
105102 for btn in self .btnList :
106103 btn .mouseDown (pos , button )
@@ -113,14 +110,15 @@ def mouseMotion(self, pos):
113110 framework .mousePos = pos
114111 for btn in self .btnList :
115112 btn .mouseMove (pos )
116- def keyUp (self , button ):
117- framework .keyUp (button )
118- def keyDown (self , button ):
119- framework .keyDown (button )
113+ def keyUp (self , key ):
114+ if self .txtFieldEnabled :
115+ self .txtField .keyUp (key )
116+ def keyDown (self , key ):
117+ if self .txtFieldEnabled :
118+ self .txtField .keyDown (key )
120119
121120class Button :
122- def __init__ (self , name , picFile , x , y , appID , ** txt ):
123- self .name = name
121+ def __init__ (self , picFile , x , y , appID , ** txt ):
124122 self .img = pygame .image .load (picFile ).convert ()
125123 self .w , self .h = self .img .get_width () // 3 , self .img .get_height ()
126124 self .x , self .y = x , y
@@ -142,16 +140,8 @@ def mouseUp(self, pos, button):
142140 self .status = 0
143141 if not self .rect .collidepoint (pos ):
144142 return
145- if self .name == 'U' :
146- framework .apps [self .appID ].pic .draw (framework .screen , framework .speed )
147- if self .name == 'D' :
148- framework .apps [self .appID ].pic .draw (framework .screen , framework .speed )
149- if self .name == 'L' :
150- framework .apps [self .appID ].pic .draw (framework .screen , framework .speed )
151- if self .name == 'R' :
152- framework .apps [self .appID ].pic .draw (framework .screen , framework .speed )
143+ framework .apps [self .appID ].pic .draw (framework .screen , framework .speed )
153144 framework .appID = self .appID
154-
155145 def mouseMove (self , pos ):
156146 if self .rect .collidepoint (pos ):
157147 self .status = 1
@@ -169,14 +159,72 @@ def draw(self, screen):
169159 if self .rect .collidepoint (framework .mousePos ):
170160 screen .blit (self .img , (self .x , self .y ))
171161
162+ class TxtField :
163+ def __init__ (self , x , y , w , h , placeholder = "" ):
164+ self .x , self .y = x , y
165+ self .w , self .h = w , h
166+ self .placeholder = placeholder
167+ self .txtBuffer = []
168+ self .content = []
169+ self .caps = { '`' : '~' , '1' : '!' , '2' : '@' , '3' : '#' , '4' : '$' , '5' : '%' , '6' : '^' , '7' : '&' , '8' : '*' , '9' : '(' , '0' : ')' , '-' : '_' , '=' : '+' , '[' : '{' , ']' : '}' , '\\ ' : '|' , ';' : ':' , '\' ' : '"' , ',' : '<' , '.' : '>' , '/' : '?' , 'a' : 'A' , 'b' : 'B' , 'c' : 'C' , 'd' : 'D' , 'e' : 'E' , 'f' : 'F' , 'g' : 'G' , 'h' : 'H' , 'i' : 'I' , 'j' : 'J' , 'k' : 'K' , 'l' : 'L' , 'm' : 'M' , 'n' : 'N' , 'o' : 'O' , 'p' : 'P' , 'q' : 'Q' , 'r' : 'R' , 's' : 'S' , 't' : 'T' , 'u' : 'U' , 'v' : 'V' , 'w' : 'W' , 'x' : 'X' , 'y' : 'Y' , 'z' : 'Z' }
170+ self .shift , self .capsLock = False , False
171+ self .raster = pygame .font .Font ("res/pkmndp.ttf" , 28 )
172+ def wrap (self , txtBuffer ):
173+ lines = []
174+ temp = self .placeholder
175+ for i in range (len (txtBuffer )):
176+ if txtBuffer [i ] == '\n ' :
177+ lines .append (temp )
178+ temp = self .placeholder
179+ elif (i != 0 and i % self .w == 0 ):
180+ lines .append (temp )
181+ temp = ""
182+ else :
183+ temp += txtBuffer [i ]
184+ lines .append (temp )
185+ return lines
186+ def draw (self , screen , lines , c = (255 , 255 , 255 ), y = 0 ):
187+ for line in lines :
188+ img = self .raster .render (line , True , c )
189+ screen .blit (img , (0 , y ))
190+ y += 30
191+ def keyUp (self , key ):
192+ if key == pygame .K_LSHIFT or key == pygame .K_RSHIFT :
193+ self .shift = False
194+ elif key == pygame .K_CAPSLOCK :
195+ self .capsLock = 1 - self .capsLock
196+ def keyDown (self , key ):
197+ if key == pygame .K_BACKSPACE :
198+ try :
199+ self .txtBuffer .pop ()
200+ except :
201+ pass
202+ elif key == pygame .K_RETURN :
203+ self .txtBuffer .append ('\n ' )
204+ elif key == pygame .K_TAB :
205+ for i in range (4 ):
206+ self .txtBuffer .append (' ' )
207+ elif key == pygame .K_LSHIFT or key == pygame .K_RSHIFT :
208+ self .shift = True
209+ elif key == pygame .K_CAPSLOCK :
210+ self .capsLock = 1 - self .capsLock
211+ else :
212+ if 32 <= key <= 126 :
213+ # ↓ Also magic! ↓
214+ if (key == 39 or 44 <= key <= 57 or key == 59 or key == 61 or key == 96 or 91 <= key <= 93 ) and self .shift :
215+ self .txtBuffer .append (self .caps [chr (key )])
216+ elif 97 <= event .key <= 122 and (self .shift or self .capsLock ):
217+ self .txtBuffer .append (self .caps [chr (key )])
218+ else :
219+ self .txtBuffer .append (chr (key ))
220+
172221class Secret :
173222 def __init__ (self , rect , dialogID ):
174223 self .rect = pygame .Rect (rect )
175224 self .dialogID = dialogID
176225 def mouseDown (self , pos , button ):
177226 if self .rect .collidepoint (pos ):
178227 framework .dialogs .pop ()
179- print (len (framework .dialogs ) - 1 )
180228
181229class DlgStatus (Enum ):
182230 INFO = 0
@@ -189,11 +237,21 @@ def __init__(self, title, content, status = DlgStatus.INFO):
189237 self .icon = pygame .transform .scale (pygame .image .load ("res/dialog/" + str (status ) + ".bmp" ), (32 , 32 ))
190238 self .icon .set_colorkey ((255 , 0 , 255 ))
191239 self .title = framework .raster .render (title , True , (255 , 255 , 255 ))
192- self .content = lineWrap (content , 35 )
240+ self .content = self . wrap (content , 35 )
193241 self .dialogID = 0
194242 self .w , self .h = self .img .get_size ()
195243 self .x , self .y = 322 , 284
196244 self .closeBtn = None
245+ def wrap (self , txt , w ):
246+ processed = []
247+ temp = ""
248+ for i in range (txt .__len__ ()):
249+ if i != 0 and i % w == 0 :
250+ processed .append (framework .raster .render (temp , True , (0 , 0 , 0 )))
251+ temp = ""
252+ temp += txt [i ]
253+ processed .append (framework .raster .render (temp , True , (0 , 0 , 0 )))
254+ return processed
197255 def draw (self , screen ):
198256 screen .blit (self .img , (self .x , self .y ))
199257 screen .blit (self .title , (self .x + 10 , self .y + 6 ))
@@ -210,8 +268,9 @@ def mouseDown(self, pos, button):
210268framework .addApp (bg )
211269framework .addApp (term )
212270framework .addDialog (Dialog ("Hey there!" , "Welcome to our OS emulator!" ))
213- bg .addButton (Button ('U' , "res/button/txt_btn.bmp" , width // 2 - 35 , 20 , term .appID , font = framework .raster , content = "TERMINAL" ))
214- term .addButton (Button ('D' , "res/button/txt_btn.bmp" , width // 2 - 35 , 20 , bg .appID , font = framework .raster , content = "CLOSE" ))
271+ bg .addButton (Button ("res/button/txt_btn.bmp" , width // 2 - 35 , 20 , term .appID , font = framework .raster , content = "TERMINAL" ))
272+ term .addButton (Button ("res/button/txt_btn.bmp" , width // 2 - 35 , 20 , bg .appID , font = framework .raster , content = "CLOSE" ))
273+ term .enableTxtField (0 , 0 , 80 , 20 )
215274
216275while True :
217276 for event in pygame .event .get ():
@@ -220,10 +279,8 @@ def mouseDown(self, pos, button):
220279 sys .exit ()
221280 if event .type == pygame .KEYDOWN :
222281 framework .keyDown (event .key )
223- print (event .key )
224282 elif event .type == pygame .KEYUP :
225283 framework .keyUp (event .key )
226- print (event .key )
227284 if event .type == pygame .MOUSEBUTTONDOWN :
228285 framework .mouseDown (event .pos , event .button )
229286 elif event .type == pygame .MOUSEBUTTONUP :
0 commit comments