Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.57 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. import Queue
  12. path="datafiles\\"
  13. PATH_RESOURCES = './'
  14. queue=Queue.Queue(-1)
  15.  
  16. KEYMAP_ASCII = {8 : PyCEGUI.Key.Scan.Backspace,
  17.                 13 : PyCEGUI.Key.Scan.Return,
  18.                 27 : PyCEGUI.Key.Scan.Escape,
  19.                 127 : PyCEGUI.Key.Scan.Delete}
  20.  
  21. KEYMAP_GLUT = {GLUT_KEY_LEFT : PyCEGUI.Key.Scan.ArrowLeft,
  22.                GLUT_KEY_RIGHT : PyCEGUI.Key.Scan.ArrowRight,
  23.                GLUT_KEY_HOME : PyCEGUI.Key.Scan.Home,
  24.                GLUT_KEY_END : PyCEGUI.Key.Scan.End}
  25.  
  26. prot = None
  27.  
  28. def loginClicked(user, password):
  29.     global prot
  30.     prot.sendString('Login:')
  31.     prot.sendString(str(user))
  32.     prot.sendString(str(password))
  33.  
  34. def gotProtocol(proto):
  35.     global prot
  36.     prot = proto
  37.  
  38. class BaseApp(object):
  39.     def __init__(self):
  40.         super(BaseApp, self).__init__()
  41.         self.lastFrameTime=0
  42.         self.updateFPS = 0
  43.         return
  44.        
  45.     def initializeOpenGL(self):
  46.         glutInit()
  47.         glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA)
  48.         glutInitWindowSize(1280, 1024)
  49.         glutInitWindowPosition(-1,-1)
  50.         glutCreateWindow("")
  51.         glutSetCursor(GLUT_CURSOR_NONE)
  52.         return
  53.  
  54.     def initializeHandlers(self):
  55.         glutDisplayFunc(self.displayFunc)
  56.         glutReshapeFunc(self.reshapeFunc)
  57.         glutMouseFunc(self.mouseFunc)
  58.         glutMotionFunc(self.mouseMotionFunc)
  59.         glutPassiveMotionFunc(self.mouseMotionFunc)
  60.         glutKeyboardFunc(self.handlerKeyboard)
  61.         glutSpecialFunc(self.handlerKeyboardSpecial)
  62.  
  63.     def initializePyCEGUI(self):
  64.         Renderer.bootstrapSystem()
  65.         self.sys=PyCEGUI.System.getSingleton()
  66.         parser = PyCEGUI.System.getSingleton().getXMLParser()
  67.         if parser.isPropertyPresent("SchemaDefaultResourceGroup"):
  68.             parser.setProperty("SchemaDefaultResourceGroup", "schemas")    
  69.  
  70.     def initializeGUI(self):
  71.         PyCEGUI.SchemeManager.getSingleton().create(path+"VanillaSkin.scheme")
  72.         PyCEGUI.System.getSingleton().setDefaultFont("AnkeCalligraph")
  73.         PyCEGUI.System.getSingleton().setDefaultMouseCursor("Vanilla-Images", "MouseArrow")
  74.         layout=PyCEGUI.WindowManager.getSingleton().loadWindowLayout("datafiles\\login.layout")
  75.         PyCEGUI.System.getSingleton().setGUISheet(layout)
  76.         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.     def handlerKeyboard(self, key, x, y):
  135.         k = ord(key)
  136.         if not self.handlerAssistKeyboard(k):
  137.             self.sys.injectChar(ord(key))
  138.         return
  139.  
  140.     def handlerKeyboardSpecial(self, key, x, y):
  141.         try:
  142.             self.sys.injectKeyDown(KEYMAP_GLUT[key])
  143.         except KeyError:
  144.             pass
  145.         return
  146.        
  147.     def run(self):
  148.         global queue
  149.         self.lastFrameTime = glutGet(GLUT_ELAPSED_TIME)
  150.         while queue.empty==False:
  151.             queue.get()
  152.             callable()
  153.         glutMainLoop()
  154.  
  155.     def login(self, args):
  156.         user=self.username.getText()
  157.         if user=="":
  158.             self.noUsername.setProperty("Visible", "True")
  159.         else:
  160.             password=self.password.getText()
  161.             if password=="":
  162.                 self.noPassword.setProperty("Visible", "True")
  163.             else:
  164.                 reactor.callFromThread(loginClicked, user, password)
  165.  
  166.     def noUsernameOkClicked(self,args):
  167.         self.noUsername.setProperty("Visible", "False")
  168.        
  169.     def noPasswordOkClicked(self,args):
  170.         self.noPassword.setProperty("Visible", "False")
  171.        
  172. def inv():
  173.     print "inv"
  174.    
  175. def loggedin():
  176.     print "logged in"
  177.     global queue
  178.     queue.put(lambda:loginWindow.setProperty("Visible", "False"))
  179.    
  180. class EchoClient(StatefulStringProtocol,Int32StringReceiver):
  181.     def connectionMade(self):
  182.         pass
  183.  
  184.     def proto_init(self, data):
  185.         if data=="inv":
  186.             reactor.callInThread(inv)
  187.            
  188.         if data=="logged in":
  189.             reactor.callInThread(loggedin)
  190.  
  191.     def connectionLost(self, reason):
  192.         print "connection lost"
  193.  
  194. class EchoFactory(protocol.ClientFactory):
  195.     protocol = EchoClient
  196.  
  197.     def clientConnectionFailed(self, connector, reason):
  198.         print "Connection failed - goodbye!"
  199.         reactor.stop()
  200.    
  201.     def clientConnectionLost(self, connector, reason):
  202.         print "Connection lost - goodbye!"
  203.         reactor.stop()
  204.  
  205. class twistedThread(threading.Thread):
  206.     def run(self):
  207.         creator = protocol.ClientCreator(reactor, EchoClient)
  208.         d = creator.connectTCP("localhost", 5000)
  209.         d.addCallback(gotProtocol)
  210.         reactor.run(False)
  211.        
  212. twistedThread().start()
  213. while True:
  214.     app=BaseApp()
  215.     app.Initialize()
  216.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement