DangerOnTheRanger

Untitled

Oct 14th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.56 KB | None | 0 0
  1. import direct.directbase.DirectStart
  2. from direct.showbase.DirectObject       import DirectObject      
  3. from direct.gui.OnscreenText            import OnscreenText
  4. from direct.gui.DirectGui               import *
  5. from pandac.PandaModules                import *
  6.  
  7.  
  8. ### client logic
  9. # connect to patcher to see if update is available
  10. # connect to login server
  11. # send login credentials
  12.  
  13.  
  14.  
  15.  
  16. class GameClient(DirectObject):
  17.     def __init__(self):
  18.         base.setBackgroundColor( 0, 0, 0 )
  19.         # sets the background color to black because the
  20.         # default grey color bugs me for some reason
  21.    
  22.         self.loginScreen()
  23.         # draws the login screen
  24.        
  25.         self.usernameBox['focus'] = 1
  26.         # sets the cursor to the username field by default
  27.        
  28.         self.accept('tab', self.cycleLoginBox)
  29.         # enables the user to cycle through the text fields with the tab key
  30.         # this is a standard feature on most login forms
  31.        
  32.         self.accept('enter', self.attemptLogin)          
  33.         # submits the login form, or you can just click the Login button
  34.  
  35. ##### EDITED #####
  36.     def hide(self):
  37.  
  38.     self.textObject.hide()
  39.     self.loginButton.hide()
  40.     self.statusText.hide()
  41.     self.passwordBox.hide()
  42.     self.usernameBox.hide()
  43.        
  44.     def loginScreen(self):
  45.         # creates a basic login screen that asks for a username/password
  46.        
  47.         boxloc = Vec3(0.0, 0.0, 0.0)
  48.         # all items in the login form will have a position relative to this
  49.         # this makes it easier to shift the entire form around once we have
  50.         # some graphics to display with it without having to change the
  51.         # positioning of every form element
  52.        
  53.         # p is the position of the form element relative to the boxloc
  54.         # coordinates set above it is changed for every form element
  55.         p = boxloc + Vec3(-0.5, 0, 0.0)                                  
  56.         self.textObject = OnscreenText(text = "Username:", pos = p, scale = 0.07,fg=(1, 1, 1, 1),align=TextNode.ALeft)
  57.         # "Username: " text that appears beside the username box
  58.        
  59.         p = boxloc + Vec3(-0.1, 0.0, 0.0)
  60.         self.usernameBox = DirectEntry(text = "" , pos = p, scale=.05, initialText="", numLines = 1)
  61.         # Username textbox where you type in your username
  62.        
  63.         p = boxloc + Vec3(-0.5, -0.1, 0.0)        
  64.         self.textObject = OnscreenText(text = "Password:", pos = p, scale = 0.07,fg=(1, 1, 1, 1),align=TextNode.ALeft)
  65.         # "Password: " text that appears beside the password box
  66.        
  67.         p = boxloc + Vec3(-0.1, 0, -0.1)
  68.         self.passwordBox = DirectEntry(text = "" , pos = p, scale=.05, initialText="", numLines = 1, obscured = 1)
  69.         # Password textbox where you type in your password
  70.         # Note - obscured = 1 denotes that all text entered will be replaced
  71.         # with a * like a standard password box
  72.        
  73.         p = boxloc + Vec3(0, 0, -0.2)
  74.         self.loginButton = DirectButton(text = ("Login", "Login", "Login", "Login"), pos = p, scale = 0.075, command=self.attemptLogin)
  75.         # The 'Login' button that will trigger the attemptLogin function
  76.         # when clicked
  77.        
  78.         p = boxloc + Vec3(-0.5, -0.4, 0)
  79.         self.statusText = OnscreenText(text = "", pos = p, scale = 0.05, fg = (1, 0, 0, 1), align=TextNode.ALeft)
  80.         # A simple text object that you can display an error/status messages
  81.         # to the user
  82.    
  83.     def updateStatus(self, statustext):
  84.         self.statusText.setText(statustext)
  85.         # all this does is change the status text.
  86.    
  87.     def attemptLogin(self):
  88.         # checks to make sure the user inputed a username and password:
  89.         #       if they didn't it will spit out an error message
  90.         #       if they did, it will try to connect to the login server
  91.         #               (under construction)
  92.        
  93.         if(self.usernameBox.get() == ""):
  94.             if(self.passwordBox.get() == ""):
  95.                 self.updateStatus("ERROR: You must enter a username and password before logging in.")
  96.             else:
  97.                 self.updateStatus("ERROR: You must specify a username")
  98.             self.passwordBox['focus'] = 0
  99.             self.usernameBox['focus'] = 1
  100.                
  101.         elif(self.passwordBox.get() == ""):
  102.             self.updateStatus("ERROR: You must enter a password")
  103.             self.usernameBox['focus'] = 0
  104.             self.passwordBox['focus'] = 1
  105.            
  106.         else:
  107.             self.updateStatus("Attempting to login...")
  108.             print "Attempting to connect to Server with credentials: (" + self.usernameBox.get() + ", " + self.passwordBox.get() + ")"
  109.             #
  110.             # this is where the networking code will get put in
  111.            
  112. ##### EDITED #####
  113.         self.hide()
  114.            
  115.     def cycleLoginBox(self):
  116.         # function is triggered by the tab key so you can cycle between
  117.         # the two input fields like on most login screens
  118.        
  119.         # IMPORTANT: When you change the focus to one of the text boxes,
  120.         # you have to unset the focus on the other textbox.  If you do not
  121.         # do this Panda seems to get confused.
  122.         if(self.passwordBox['focus'] == 1):
  123.             self.passwordBox['focus'] = 0
  124.             self.usernameBox['focus'] = 1
  125.         elif(self.usernameBox['focus'] == 1):
  126.             self.usernameBox['focus'] = 0
  127.             self.passwordBox['focus'] = 1
  128.    
  129.        
  130.  
  131. Client = GameClient()
  132. run()
  133.  
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment