client.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from direct.showbase.ShowBase import ShowBase
  2. from direct.distributed.ClientRepository import ClientRepository
  3. from panda3d.core import URLSpec, ConfigVariableInt, ConfigVariableString
  4. from message import Message
  5. from ClientRepository import GameClientRepository
  6. from direct.gui.DirectGui import DirectButton, DirectEntry, DirectFrame
  7. from direct.gui import DirectGuiGlobals as DGG
  8. # initialize the engine
  9. base = ShowBase()
  10. client = GameClientRepository()
  11. # Setup the GUI
  12. # this frame will contain all our GUI elements
  13. frameMain = DirectFrame(frameColor = (0, 0, 0, 1))
  14. def clearText():
  15. ''' Write an empty string in the textbox '''
  16. txt_msg.enterText('')
  17. def setDefaultText():
  18. ''' Write the default message in the textbox '''
  19. txt_msg.enterText('Your Message')
  20. def send(args=None):
  21. ''' Send the text written in the message textbox to the clients '''
  22. client.sendMessage(txt_msg.get())
  23. txt_msg.enterText('')
  24. # the Textbox where we write our Messages, which we
  25. # want to send over to the other users
  26. txt_msg = DirectEntry(
  27. initialText = 'Your Message',
  28. cursorKeys = True,
  29. pos = (-0.9,0,-0.9),
  30. scale = 0.1,
  31. width = 14,
  32. focusInCommand = clearText,
  33. focusOutCommand = setDefaultText,
  34. command = send,
  35. parent = frameMain)
  36. # a button to initiate the sending of the message
  37. btn_send = DirectButton(
  38. text = 'Send',
  39. pos = (0.75,0,-0.9),
  40. scale = 0.15,
  41. command = send,
  42. parent = frameMain)
  43. # This will show all sent messages
  44. txt_messages = DirectEntry(
  45. cursorKeys = False,
  46. pos = (-0.8,0,0.5),
  47. scale = 0.1,
  48. width = 14,
  49. numLines = 10,
  50. state = DGG.DISABLED,
  51. parent = frameMain)
  52. # Function which will write the given text in the
  53. # textbox, where we store all send and recieved messages.
  54. # This Function will only write the last 10 messages and
  55. # cut of the erlier messages
  56. def setText(messageText):
  57. # get all messages from the textbox
  58. parts = txt_messages.get().split('\n')
  59. if len(parts) >= 10:
  60. cutParts = ''
  61. # as the textbox can only hold 10 lines cut out the first entry
  62. for i in range(1,len(parts)):
  63. cutParts += parts[i] + '\n'
  64. txt_messages.enterText(cutParts + messageText)
  65. else:
  66. txt_messages.enterText(txt_messages.get() + '\n' + messageText)
  67. # create a DirectObject instance, which will then catch the events and
  68. # handle them with the given functions
  69. base.accept('setText', setText)
  70. base.accept('escape', exit)
  71. # start the application
  72. base.run()