GUI overhaul

This commit is contained in:
Ammar Askar 2012-04-17 01:09:43 +05:00
parent 1ecdf8e82f
commit a99b73c5b6
2 changed files with 164 additions and 187 deletions

142
GUI.py Normal file
View File

@ -0,0 +1,142 @@
import wx
import start
import threading
import time
#pydev error fix
wx=wx
class LoginFrame(wx.Frame):
def __init__(self, *args, **kwds):
self.loggedIn = False
# begin wxGlade: LoginFrame.__init__
kwds["style"] = wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.TAB_TRAVERSAL | wx.CLIP_CHILDREN
wx.Frame.__init__(self, *args, **kwds)
self.LoginStaticText = wx.StaticText(self, -1, "Login")
self.static_line_1 = wx.StaticLine(self, -1)
self.UsernameStaticText = wx.StaticText(self, -1, "Username")
self.UsernameEntry = wx.TextCtrl(self, -1, "", style=wx.TE_PROCESS_ENTER)
self.PasswordStaticText = wx.StaticText(self, -1, "Password")
self.PasswordEntry = wx.TextCtrl(self, -1, "", style=wx.TE_PASSWORD | wx.TE_PROCESS_ENTER)
self.LoginButton = wx.Button(self, -1, "&Login")
self.static_line_2 = wx.StaticLine(self, -1)
self.Status = wx.StaticText(self, -1, "pyCraft, python based minecraft client by Ammar Askar")
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_BUTTON, self.handleLogin, self.LoginButton)
self.UsernameEntry.Bind(wx.EVT_KEY_DOWN, self.onKeyDown)
self.PasswordEntry.Bind(wx.EVT_KEY_DOWN, self.onKeyDown)
# end wxGlade
def __set_properties(self):
# begin wxGlade: LoginFrame.__set_properties
self.SetTitle("pyCraft")
self.SetSize((500, 300))
self.SetBackgroundColour(wx.Colour(171, 171, 171))
self.SetFont(wx.Font(20, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
self.LoginStaticText.SetBackgroundColour(wx.Colour(171, 171, 171))
self.LoginStaticText.SetFont(wx.Font(65, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Minecraft"))
self.UsernameStaticText.SetBackgroundColour(wx.Colour(171, 171, 171))
self.UsernameStaticText.SetFont(wx.Font(14, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Lucida Sans"))
self.UsernameEntry.SetMinSize((150, 25))
self.UsernameEntry.SetFont(wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, ""))
self.PasswordStaticText.SetMinSize((87, 35))
self.PasswordStaticText.SetBackgroundColour(wx.Colour(171, 171, 171))
self.PasswordStaticText.SetFont(wx.Font(14, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Lucida Sans"))
self.PasswordEntry.SetMinSize((150, 25))
self.PasswordEntry.SetFont(wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, ""))
self.LoginButton.SetMinSize((80, 32))
self.LoginButton.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE))
self.LoginButton.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
self.Status.SetBackgroundColour(wx.Colour(171, 171, 171))
self.Status.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0))
# end wxGlade
def __do_layout(self):
# begin wxGlade: LoginFrame.__do_layout
sizer_5 = wx.BoxSizer(wx.VERTICAL)
sizer_8 = wx.BoxSizer(wx.HORIZONTAL)
sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
sizer_5.Add(self.LoginStaticText, 0, wx.LEFT | wx.TOP, 5)
sizer_5.Add((20, 19), 0, 0, 0)
sizer_5.Add((20, 48), 0, 0, 0)
sizer_5.Add(self.static_line_1, 0, wx.EXPAND, 0)
sizer_7.Add(self.UsernameStaticText, 0, wx.LEFT, 5)
sizer_7.Add(self.UsernameEntry, 0, wx.LEFT, 15)
sizer_5.Add(sizer_7, 1, wx.EXPAND, 0)
sizer_8.Add(self.PasswordStaticText, 0, wx.LEFT, 5)
sizer_8.Add(self.PasswordEntry, 0, wx.LEFT, 20)
sizer_8.Add(self.LoginButton, 0, wx.LEFT, 145)
sizer_5.Add(sizer_8, 1, wx.EXPAND, 0)
sizer_5.Add(self.static_line_2, 0, wx.EXPAND, 0)
sizer_5.Add(self.Status, 0, wx.LEFT, 5)
self.SetSizer(sizer_5)
self.Layout()
self.Centre()
# end wxGlade
def onKeyDown(self, event):
if(event.GetKeyCode() == 9):
if(event.GetEventObject() == self.UsernameEntry):
self.PasswordEntry.SetFocus()
if(event.GetEventObject() == self.PasswordEntry):
self.UsernameEntry.SetFocus()
else:
event.Skip()
def handlePostLogin(self, threadtokill):
threadtokill.Kill = True
self.Status.SetLabel("Logged in!") #For now just sets label to logged in, will call new frame in the future
pass
def handleEnter(self):
print "mebbe this works"
def handleLogin(self, event):
if(self.UsernameEntry.GetValue() == ""):
self.Status.SetForegroundColour(wx.RED)
self.Status.SetLabel("Enter a username")
return
if(self.PasswordEntry.GetValue() == ""):
self.Status.SetForegroundColour(wx.RED)
self.Status.SetLabel("Enter a password")
return
username = self.UsernameEntry.GetValue()
password = self.PasswordEntry.GetValue()
self.Status.SetForegroundColour(wx.BLUE)
self.Status.SetLabel("Connecting.")
RotationThread = ConnectingRotationThread(self)
RotationThread.start()
LoginThread = start.MinecraftLoginThread(self, RotationThread, username, password)
LoginThread.start()
class ConnectingRotationThread(threading.Thread):
def __init__(self, window):
threading.Thread.__init__(self)
self.window = window
self.Kill = False
def run(self):
i = 1
while True:
if(self.Kill == True):
break
if(i == 0):
self.window.Status.SetLabel("Connecting.")
if(i == 1):
self.window.Status.SetLabel("Connecting..")
if(i == 2):
self.window.Status.SetLabel("Connecting...")
if(i == 3):
self.window.Status.SetLabel("Connecting....")
if(i == 4):
self.window.Status.SetLabel("Connecting.....")
i = -1
i = i + 1
time.sleep(1)

209
start.py
View File

@ -1,156 +1,26 @@
import urllib
import urllib2
import getpass
import socket
import sys
import PacketListenerManager
import PacketSenderManager
import NetworkManager
import NoGUIstuff
import time
import threading
import thread
import wx
import wxPython
import GUI
sessionid = ""
username = ""
#Eclipse pyDev error fix
wx = wx
class Window(wx.Frame):
def __init__(self, parent, title):
self.username = ""
self.sessionID = ""
self.password = ""
self.LoggedIn = False
super(Window, self).__init__(parent, title=title,
size=(400, 100))
self.initialize()
self.Show()
def initialize(self):
self.sizer = wx.GridBagSizer()
self.label = wx.StaticText(self, -1, label=u'Username')
self.sizer.Add( self.label, (0,0),(1,1), wx.EXPAND )
self.entry = wx.TextCtrl(self,-1, size=(200,23))
self.sizer.Add(self.entry,(0,1),(1, 4),wx.EXPAND | wx.ALIGN_LEFT)
self.Bind(wx.EVT_TEXT_ENTER, self.onPressEnterOnFields, self.entry)
self.label2 = wx.StaticText(self, -1, label=u'Password')
self.sizer.Add( self.label2, (2,0),(1,2), wx.EXPAND)
self.entry2 = wx.TextCtrl(self, -1, size=(200,23), style = wx.TE_PASSWORD)
self.sizer.Add(self.entry2,(2,2),(2,4), wx.EXPAND | wx.ALIGN_LEFT)
self.Bind(wx.EVT_TEXT_ENTER, self.onPressEnterOnFields, self.entry2)
button = wx.Button(self,-1,label="Login")
self.sizer.Add(button, (5,0))
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, button)
self.status = wx.StaticText(self, -1,)
self.sizer.Add(self.status,(5,2),(5,15), wx.EXPAND)
self.sizer.AddGrowableCol(0)
self.SetSizerAndFit(self.sizer)
def InitializeServerBrowser(self):
self.sizer = wx.GridBagSizer()
#Top label
self.topLabel = wx.StaticText(self, -1, label=u'Logged in as ' + self.username + '! (Session id: ' + self.sessionID + ')')
self.sizer.Add(self.topLabel, (0,0),(1,1), wx.EXPAND)
#Top label
#Address label
self.addressLabel = wx.StaticText(self, -1, label=u'Server address')
self.sizer.Add(self.addressLabel, (2,0), (1,2), wx.EXPAND)
#Address lable
#Address entry box
self.AddressEntry = wx.TextCtrl(self, -1)
self.sizer.Add(self.AddressEntry,(2,2),(2,2), wx.EXPAND)
self.Bind(wx.EVT_TEXT_ENTER, self.onConnectClick, self.AddressEntry)
#Address entry box
self.connectbutton = wx.Button(self, -1, label="Connect")
self.sizer.Add(self.connectbutton,(4,0))
self.Bind(wx.EVT_BUTTON, self.onConnectClick, self.connectbutton)
self.sizer.AddGrowableCol(0)
self.SetSizerAndFit(self.sizer)
def onConnectClick(self, event):
StuffEnteredIntoBox = self.AddressEntry.GetValue()
if(StuffEnteredIntoBox == ""):
self.topLabel.SetLabel("Logged in as " + self.username + "! Enter a server address")
self.sizer.DeleteWindows()
self.sizer = wx.GridBagSizer()
self.connectStatus = wx.StaticText(self, -1, label=u'Connecting ...')
self.connectStatus.SetForegroundColour(wx.BLUE)
self.sizer.Add(self.connectStatus, (0,0), (0,0))
self.connectStatus.Center()
if ':' in StuffEnteredIntoBox:
StuffEnteredIntoBox = StuffEnteredIntoBox.split(":")
host = StuffEnteredIntoBox[0]
port = int(StuffEnteredIntoBox[1])
else:
host = StuffEnteredIntoBox
port = 25565
self.connection = NetworkManager.ServerConnection(self, self.username, self.password, self.sessionID, host, port)
self.connection.start()
def OnButtonClick(self, event):
if(self.entry.GetValue() == ""):
self.status.SetLabel("Enter a username sherlock")
return
if(self.entry2.GetValue() == ""):
self.status.SetLabel("Enter a password you derp")
return
password = self.entry2.GetValue()
username = self.entry.GetValue()
self.status.SetForegroundColour(wx.BLUE)
self.status.SetLabel("Logging in...")
thread = MinecraftLoginThread(self, username, password)
thread.start()
while(thread.is_alive() == True):
pass
self.sizer.DeleteWindows()
self.InitializeServerBrowser()
def onPressEnterOnFields(self, event):
if(self.entry.GetValue() == ""):
self.status.SetLabel("Enter a username sherlock")
return
if(self.entry2.GetValue() == ""):
self.status.SetLabel("Enter a password you derp")
return
password = self.entry2.GetValue()
username = self.entry.GetValue()
self.status.SetForegroundColour(wx.BLUE)
self.status.SetLabel("Logging in...")
thread = MinecraftLoginThread(self, username, password)
thread.start()
while(thread.is_alive() == True):
pass
self.sizer.DeleteWindows()
self.InitializeServerBrowser()
#pyDev error fix
wx=wx
class MinecraftLoginThread(threading.Thread):
def __init__(self, window, username, password):
def __init__(self, window, rotationthread, username, password):
threading.Thread.__init__(self)
self.window = window
self.username = username
self.password = password
self.window = window
self.rotationthread = rotationthread
def run(self):
url = 'https://login.minecraft.net'
@ -165,20 +35,23 @@ class MinecraftLoginThread(threading.Thread):
response = opener.open(req)
response = response.read()
except urllib2.URLError:
popup = wx.MessageBox('Connection to minecraft.net failed', 'Warning',
wx.OK | wx.ICON_ERROR)
popup.ShowModal()
self.rotationthread.Kill = True
self.window.Status.SetForegroundColour(wx.RED)
self.window.Status.SetLabel('Connection to minecraft.net failed')
return
if(response == "Bad login"):
self.window.status.SetForegroundColour(wx.RED)
self.window.status.SetLabel("Incorrect username or password!")
self.rotationthread.Kill = True
self.window.Status.SetForegroundColour(wx.RED)
self.window.Status.SetLabel('Incorrect username or password')
return
response = response.split(":")
self.window.username = response[2]
self.window.password = self.password
self.window.sessionID = response[3]
self.window.LoggedIn = True
username = response[2]
sessionID = response[3]
KeepConnectionAlive(self.username, self.password).start()
self.window.username = username
self.window.sessionID = sessionID
self.window.loggedIn = True
self.window.handlePostLogin(self.rotationthread)
class KeepConnectionAlive(threading.Thread):
@ -229,46 +102,8 @@ if __name__ == "__main__":
raw_input()
PacketSenderManager.send03(connection.grabSocket(), "/report Test report from pyCraft, hope this works :3")
else:
app = wx.App(0)
Window(None, title='pyCraft')
app = wx.PySimpleApp(0)
Login = GUI.LoginFrame(None, -1, "")
app.SetTopWindow(Login)
Login.Show()
app.MainLoop()
"""
url = 'https://login.minecraft.net'
header = {'Content-Type' : 'application/x-www-form-urlencoded'}
username = raw_input("Enter your username: ")
password = getpass.getpass("Enter your password: ")
data = {'user' : username,
'password' : password,
'version' : '13'}
data = urllib.urlencode(data)
req = urllib2.Request(url, data, header)
opener = urllib2.build_opener()
response = opener.open(req)
response = response.read()
response = response.split(":")
sessionid = response[3]
print "Your session id is: " + sessionid
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
host = raw_input("Please enter host: ");
port = raw_input("Please enter port (return for 25565): ");
if(port == ""):
port = 25565
else:
port = int(port)
mySocket.connect ( ( host, port ) )
PacketManager.sendString("\x02", username + host + ":" + str(port), mySocket)
response = PacketManager.readStringFromSocket(mySocket)
print "Server id is: " + response['string']
serverid = response['string']
url = "http://session.minecraft.net/game/joinserver.jsp?user=" + username + "&sessionId=" + sessionid + "&serverId=" + serverid
response = urllib2.urlopen(url).read()
print "Response: " + response
if(response != "OK"):
print "OH GOD RESPONSE IS NOT OK. QUITING NOW."
sys.exit()
PacketManager.sendLoginRequest(mySocket, username)
while True:
PacketManager.handleIncomingPacket(mySocket)
"""