Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.56 KB | None | 0 0
  1. from OpenGL.GL import *
  2. from OpenGL.GLU import *
  3. from OpenGL.GLUT import *
  4. import PyCEGUI
  5. import PyCEGUIOpenGLRenderer
  6. from twisted.internet import reactor,protocol
  7. from twisted.protocols.basic import Int32StringReceiver, StatefulStringProtocol
  8. import threading
  9. import sys, os
  10. from PyCEGUIOpenGLRenderer import OpenGLRenderer as Renderer
  11. path="datafiles\\"
  12. PATH_RESOURCES = './'
  13.  
  14. KEYMAP_ASCII = {8 : PyCEGUI.Key.Scan.Backspace,
  15.                 13 : PyCEGUI.Key.Scan.Return,
  16.                 27 : PyCEGUI.Key.Scan.Escape,
  17.                 127 : PyCEGUI.Key.Scan.Delete}
  18.  
  19. KEYMAP_GLUT = {GLUT_KEY_LEFT : PyCEGUI.Key.Scan.ArrowLeft,
  20.                GLUT_KEY_RIGHT : PyCEGUI.Key.Scan.ArrowRight,
  21.                GLUT_KEY_HOME : PyCEGUI.Key.Scan.Home,
  22.                GLUT_KEY_END : PyCEGUI.Key.Scan.End}
  23.  
  24. prot = None
  25. loginWindow=None
  26.  
  27. def loginClicked(user, password):
  28.     global prot
  29.     prot.sendString('Login:')
  30.     prot.sendString(str(user))
  31.     prot.sendString(str(password))
  32.  
  33. def gotProtocol(proto):
  34.     global prot
  35.     prot = proto
  36.  
  37. class BaseApp(object):
  38.     def __init__(self):
  39.         super(BaseApp, self).__init__()
  40.         self.lastFrameTime=0
  41.         self.updateFPS = 0
  42.         return
  43.        
  44.     def initializeOpenGL(self):
  45.         glutInit()
  46.         glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA)
  47.         glutInitWindowSize(1280, 1024)
  48.         glutInitWindowPosition(-1,-1)
  49.         glutCreateWindow("")
  50.         glutSetCursor(GLUT_CURSOR_NONE)
  51.         return
  52.  
  53.     def initializeHandlers(self):
  54.         glutDisplayFunc(self.displayFunc)
  55.         glutReshapeFunc(self.reshapeFunc)
  56.         glutMouseFunc(self.mouseFunc)
  57.         glutMotionFunc(self.mouseMotionFunc)
  58.         glutPassiveMotionFunc(self.mouseMotionFunc)
  59.         glutKeyboardFunc(self.handlerKeyboard)
  60.         glutSpecialFunc(self.handlerKeyboardSpecial)
  61.  
  62.     def initializePyCEGUI(self):
  63.         Renderer.bootstrapSystem()
  64.         self.sys=PyCEGUI.System.getSingleton()
  65.         parser = PyCEGUI.System.getSingleton().getXMLParser()
  66.         if parser.isPropertyPresent("SchemaDefaultResourceGroup"):
  67.             parser.setProperty("SchemaDefaultResourceGroup", "schemas")    
  68.  
  69.     def initializeGUI(self):
  70.         PyCEGUI.SchemeManager.getSingleton().create(path+"VanillaSkin.scheme")
  71.         PyCEGUI.System.getSingleton().setDefaultFont("AnkeCalligraph")
  72.         PyCEGUI.System.getSingleton().setDefaultMouseCursor("Vanilla-Images", "MouseArrow")
  73.         layout=PyCEGUI.WindowManager.getSingleton().loadWindowLayout("datafiles\\login.layout")
  74.         interface=PyCEGUI.WindowManager.getSingleton().loadWindowLayout("datafiles\\interface.layout")
  75.         PyCEGUI.System.getSingleton().setGUISheet(layout)
  76.         self.loginWindow=PyCEGUI.WindowManager.getSingleton().getWindow("Root/Login")
  77.         self.submit=PyCEGUI.WindowManager.getSingleton().getWindow("Root/Login/Submit")
  78.         self.username=PyCEGUI.WindowManager.getSingleton().getWindow("Root/Login/Username")
  79.         self.password=PyCEGUI.WindowManager.getSingleton().getWindow("Root/Login/Password")
  80.         self.submit.subscribeEvent(PyCEGUI.PushButton.EventClicked, self, 'login')
  81.         self.noUsername=PyCEGUI.WindowManager.getSingleton().getWindow("Root/Login/NoUsername")
  82.         self.noUsernameOk=PyCEGUI.WindowManager.getSingleton().getWindow("Root/Login/NoUsername/Ok")
  83.         self.noUsernameOk.subscribeEvent(PyCEGUI.PushButton.EventClicked, self, 'noUsernameOkClicked')
  84.         self.noPassword=PyCEGUI.WindowManager.getSingleton().getWindow("Root/Login/NoPassword")
  85.         self.noPasswordOk=PyCEGUI.WindowManager.getSingleton().getWindow("Root/Login/NoPassword/Ok")
  86.         self.noPasswordOk.subscribeEvent(PyCEGUI.PushButton.EventClicked, self, 'noPasswordOkClicked')
  87.        
  88.     def Initialize(self):
  89.         self.initializeOpenGL()
  90.         self.initializeHandlers()
  91.         self.initializePyCEGUI()
  92.         self.initializeGUI()
  93.  
  94.     def displayFunc(self):
  95.         now = glutGet(GLUT_ELAPSED_TIME)
  96.         elapsed = (now - self.lastFrameTime) / 1000.0
  97.         self.lastFrameTime = now
  98.         self.updateFPS = self.updateFPS - elapsed
  99.         self.sys.injectTimePulse(elapsed)
  100.  
  101.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  102.         self.sys.renderGUI()
  103.         glutPostRedisplay()
  104.         glutSwapBuffers()
  105.  
  106.     def reshapeFunc(self, width, height):
  107.         glViewport(0, 0, width, height)
  108.         self.sys.notifyDisplaySizeChanged(PyCEGUI.Size(width, height))
  109.         return
  110.  
  111.     def mouseFunc(self, button, state, x, y):
  112.         if button == GLUT_LEFT_BUTTON:
  113.             if state == GLUT_UP:
  114.                 PyCEGUI.System.getSingleton().injectMouseButtonUp(PyCEGUI.LeftButton)
  115.             else:
  116.                 PyCEGUI.System.getSingleton().injectMouseButtonDown(PyCEGUI.LeftButton)
  117.  
  118.         elif button == GLUT_RIGHT_BUTTON:
  119.             if state == GLUT_UP:
  120.                 PyCEGUI.System.getSingleton().injectMouseButtonUp(PyCEGUI.RightButton)
  121.             else:
  122.                 PyCEGUI.System.getSingleton().injectMouseButtonDown(PyCEGUI.RightButton)
  123.  
  124.     def mouseMotionFunc(self, x, y):
  125.         PyCEGUI.System.getSingleton().injectMousePosition(x, y)
  126.  
  127.     def handlerAssistKeyboard(self, key):
  128.         try:
  129.             self.sys.injectKeyDown(KEYMAP_ASCII[key])
  130.         except KeyError:
  131.             return False
  132.         return True
  133.  
  134.     # Handler: Keyboard
  135.     def handlerKeyboard(self, key, x, y):
  136.         k = ord(key)
  137.         if not self.handlerAssistKeyboard(k):
  138.             self.sys.injectChar(ord(key))
  139.         return
  140.  
  141.     # Handler: Keyboard special
  142.     def handlerKeyboardSpecial(self, key, x, y):
  143.         try:
  144.             self.sys.injectKeyDown(KEYMAP_GLUT[key])
  145.         except KeyError:
  146.             # Ignore it.
  147.             pass
  148.         return
  149.        
  150.     def run(self):
  151.         self.lastFrameTime = glutGet(GLUT_ELAPSED_TIME)
  152.         glutMainLoop()
  153.  
  154.     def login(self, args):
  155.         user=self.username.getText()
  156.         if user=="":
  157.             self.noUsername.setProperty("Visible", "True")
  158.         else:
  159.             password=self.password.getText()
  160.             if password=="":
  161.                 self.noPassword.setProperty("Visible", "True")
  162.             else:
  163.                 reactor.callFromThread(loginClicked, user, password)
  164.  
  165.     def noUsernameOkClicked(self,args):
  166.         self.noUsername.setProperty("Visible", "False")
  167.        
  168.     def noPasswordOkClicked(self,args):
  169.         self.noPassword.setProperty("Visible", "False")
  170.        
  171. def inv():
  172.     print "inv"
  173.    
  174. def loggedin():
  175.     print "logged in"
  176.     #hide loginWindow here
  177.    
  178.  
  179. class EchoClient(StatefulStringProtocol,Int32StringReceiver):
  180.     def connectionMade(self):
  181.         pass
  182.  
  183.     def proto_init(self, data):
  184.         if data=="inv":
  185.             reactor.callInThread(inv)
  186.            
  187.         if data=="logged in":
  188.             reactor.callInThread(loggedin)
  189.  
  190.     def connectionLost(self, reason):
  191.         print "connection lost"
  192.  
  193. class EchoFactory(protocol.ClientFactory):
  194.     protocol = EchoClient
  195.  
  196.     def clientConnectionFailed(self, connector, reason):
  197.         print "Connection failed - goodbye!"
  198.         reactor.stop()
  199.    
  200.     def clientConnectionLost(self, connector, reason):
  201.         print "Connection lost - goodbye!"
  202.         reactor.stop()
  203.  
  204. class twistedThread(threading.Thread):
  205.     def run(self):
  206.         creator = protocol.ClientCreator(reactor, EchoClient)
  207.         d = creator.connectTCP("localhost", 5000)
  208.         d.addCallback(gotProtocol)
  209.         reactor.run(False)
  210.        
  211. twistedThread().start()
  212. app=BaseApp()
  213. app.Initialize()
  214. app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement