Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.67 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.         # process the queue right before we get to render the GUI
  102.         global queue
  103.         while not queue.empty:
  104.             callable = queue.get()
  105.             callable()
  106.             queue.task_done()
  107.  
  108.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  109.         self.sys.renderGUI()
  110.         glutPostRedisplay()
  111.         glutSwapBuffers()
  112.  
  113.     def reshapeFunc(self, width, height):
  114.         glViewport(0, 0, width, height)
  115.         self.sys.notifyDisplaySizeChanged(PyCEGUI.Size(width, height))
  116.         return
  117.  
  118.     def mouseFunc(self, button, state, x, y):
  119.         if button == GLUT_LEFT_BUTTON:
  120.             if state == GLUT_UP:
  121.                 PyCEGUI.System.getSingleton().injectMouseButtonUp(PyCEGUI.LeftButton)
  122.             else:
  123.                 PyCEGUI.System.getSingleton().injectMouseButtonDown(PyCEGUI.LeftButton)
  124.  
  125.         elif button == GLUT_RIGHT_BUTTON:
  126.             if state == GLUT_UP:
  127.                 PyCEGUI.System.getSingleton().injectMouseButtonUp(PyCEGUI.RightButton)
  128.             else:
  129.                 PyCEGUI.System.getSingleton().injectMouseButtonDown(PyCEGUI.RightButton)
  130.  
  131.     def mouseMotionFunc(self, x, y):
  132.         PyCEGUI.System.getSingleton().injectMousePosition(x, y)
  133.  
  134.     def handlerAssistKeyboard(self, key):
  135.         try:
  136.             self.sys.injectKeyDown(KEYMAP_ASCII[key])
  137.         except KeyError:
  138.             return False
  139.         return True
  140.  
  141.     def handlerKeyboard(self, key, x, y):
  142.         k = ord(key)
  143.         if not self.handlerAssistKeyboard(k):
  144.             self.sys.injectChar(ord(key))
  145.         return
  146.  
  147.     def handlerKeyboardSpecial(self, key, x, y):
  148.         try:
  149.             self.sys.injectKeyDown(KEYMAP_GLUT[key])
  150.         except KeyError:
  151.             pass
  152.         return
  153.        
  154.     def run(self):
  155.         self.lastFrameTime = glutGet(GLUT_ELAPSED_TIME)
  156.         glutMainLoop()
  157.  
  158.     def login(self, args):
  159.         user=self.username.getText()
  160.         if user=="":
  161.             self.noUsername.setProperty("Visible", "True")
  162.         else:
  163.             password=self.password.getText()
  164.             if password=="":
  165.                 self.noPassword.setProperty("Visible", "True")
  166.             else:
  167.                 reactor.callFromThread(loginClicked, user, password)
  168.  
  169.     def noUsernameOkClicked(self,args):
  170.         self.noUsername.setProperty("Visible", "False")
  171.        
  172.     def noPasswordOkClicked(self,args):
  173.         self.noPassword.setProperty("Visible", "False")
  174.        
  175. def inv():
  176.     print "inv"
  177.    
  178. def loggedin():
  179.     print "logged in"
  180.     global queue
  181.     queue.put(lambda:loginWindow.setProperty("Visible", "False"))
  182.    
  183. class EchoClient(StatefulStringProtocol,Int32StringReceiver):
  184.     def connectionMade(self):
  185.         pass
  186.  
  187.     def proto_init(self, data):
  188.         if data=="inv":
  189.             reactor.callInThread(inv)
  190.            
  191.         if data=="logged in":
  192.             reactor.callInThread(loggedin)
  193.  
  194.     def connectionLost(self, reason):
  195.         print "connection lost"
  196.  
  197. class EchoFactory(protocol.ClientFactory):
  198.     protocol = EchoClient
  199.  
  200.     def clientConnectionFailed(self, connector, reason):
  201.         print "Connection failed - goodbye!"
  202.         reactor.stop()
  203.    
  204.     def clientConnectionLost(self, connector, reason):
  205.         print "Connection lost - goodbye!"
  206.         reactor.stop()
  207.  
  208. class twistedThread(threading.Thread):
  209.     def run(self):
  210.         creator = protocol.ClientCreator(reactor, EchoClient)
  211.         d = creator.connectTCP("localhost", 5000)
  212.         d.addCallback(gotProtocol)
  213.         reactor.run(False)
  214.        
  215. twistedThread().start()
  216. while True:
  217.     app=BaseApp()
  218.     app.Initialize()
  219.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement