modify ui

This commit is contained in:
chuanweizhang 2013-12-24 15:07:51 +08:00
parent 2308e94c2f
commit 1ce75bb444
3 changed files with 132 additions and 57 deletions

View File

@ -6,13 +6,19 @@
# Author: chuanwei # Author: chuanwei
import sys import sys
from module.ui import TkProjectDialog from module.ui import TkCocosDialog
from module.core import CocosProject from module.core import CocosProject
# ------------ main -------------- # ------------ main --------------
if __name__ == '__main__': if __name__ == '__main__':
"""
There have double ways to create cocos project.
1.UI
2.console
#create_project.py -n MyGame -k com.MyCompany.AwesomeGame -l javascript -p c:/mycompany
"""
if len(sys.argv)==1: if len(sys.argv)==1:
TkProjectDialog() TkCocosDialog()
else: else:
project = CocosProject() project = CocosProject()
name, package, language, path = project.checkParams() name, package, language, path = project.checkParams()

View File

@ -166,8 +166,8 @@ class CocosProject:
shutil.rmtree(self.context["dst_project_path"]) shutil.rmtree(self.context["dst_project_path"])
return False return False
print ("\n") print ("###New project has been created in this path: ")
print ("New project has been created in this path: " + self.context["dst_project_path"].replace("\\", "/")) print (self.context["dst_project_path"].replace("\\", "/"))
print ("Have Fun!") print ("Have Fun!")
return True return True
@ -190,8 +190,7 @@ class CocosProject:
os.makedirs(dstPath) os.makedirs(dstPath)
# begin copy # begin copy
print("\n") print("###begin copy engine...")
print("begin copy engine...")
# list engine root dir. # list engine root dir.
dirlist = os.listdir(self.cocos_root) dirlist = os.listdir(self.cocos_root)
for line in dirlist: for line in dirlist:

View File

@ -15,6 +15,7 @@ import time
from core import CocosProject from core import CocosProject
#import head files by python version.
if int(platform.python_version().split('.')[0])>=3: if int(platform.python_version().split('.')[0])>=3:
from tkinter import * from tkinter import *
from tkinter.filedialog import * from tkinter.filedialog import *
@ -25,8 +26,9 @@ else:
from tkMessageBox import * from tkMessageBox import *
class ThreadedTask(threading.Thread): class ThreadedTask(threading.Thread):
"""Create cocos project thread.
"""
def __init__(self, queue, projectName, packageName, language, projectPath): def __init__(self, queue, projectName, packageName, language, projectPath):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.queue = queue self.queue = queue
@ -36,13 +38,23 @@ class ThreadedTask(threading.Thread):
self.projectPath = projectPath self.projectPath = projectPath
def run(self): def run(self):
"""Create cocos project.
custom message rules to notify ui
As follow:
begin@%d@%d@%s --- create before
doing@%d@%d@%s --- creating
end@%d@%d@%s --- create after
"""
#delete exist project.
if os.path.exists(os.path.join(self.projectPath, self.projectName)): if os.path.exists(os.path.join(self.projectPath, self.projectName)):
print ("###begin remove... " + self.projectName)
shutil.rmtree(os.path.join(self.projectPath, self.projectName)) shutil.rmtree(os.path.join(self.projectPath, self.projectName))
putMsg = "begin@%d@%d@%s" %(0,100,"begin create") print ("###remove finish: " + self.projectName)
putMsg = "begin@%d@%d@%s" %(0, 100, "begin create")
self.queue.put(putMsg) self.queue.put(putMsg)
project = CocosProject() project = CocosProject()
breturn=project.createPlatformProjects( breturn = project.createPlatformProjects(
self.projectName, self.projectName,
self.packageName, self.packageName,
self.language, self.language,
@ -50,118 +62,175 @@ class ThreadedTask(threading.Thread):
self.newProjectCallBack self.newProjectCallBack
) )
if breturn: if breturn:
putMsg = "end@%d@%d@%s" %(100,100,"create successful") putMsg = "end@%d@%d@%s" %(100, 100, "create successful")
else: else:
putMsg = "end@%d@%d@%s" %(100,100,"create failure") putMsg = "end@%d@%d@%s" %(100, 100, "create failure")
self.queue.put(putMsg) self.queue.put(putMsg)
def newProjectCallBack(self, step, totalStep, showMsg): def newProjectCallBack(self, step, totalStep, showMsg):
putMsg = "doing@%d@%d@%s" %(step,totalStep,showMsg) """Creating cocos project callback.
"""
putMsg = "doing@%d@%d@%s" %(step, totalStep, showMsg)
self.queue.put(putMsg) self.queue.put(putMsg)
class TkProjectDialog(): class StdoutRedirector(object):
def __init__(self): """Redirect output.
"""
def __init__(self, text_area):
self.text_area = text_area
#reload(sys) def write(self, str):
#sys.setdefaultencoding('utf-8') self.text_area.insert(END, str)
self.text_area.see(END)
class TkCocosDialog():
def __init__(self):
self.projectName = "" self.projectName = ""
self.packageName = "" self.packageName = ""
self.language = "" self.language = ""
self.old_stdout = sys.stdout
print("create_project.py -n MyGame -k com.MyCompany.AwesomeGame -l javascript -p c:/mycompany")
# top window
self.top = Tk() self.top = Tk()
top_width = 500
top_height = 250
screen_width = self.top.winfo_screenwidth()
screen_height = self.top.winfo_screenheight()
#center window on desktop
#self.top.geometry('%sx%s+%s+%s' % (top_width,top_height, int((screen_width - top_width)/2), int((screen_height - top_height)/2)) )
#self.top.maxsize(top_width,top_height)
#self.top.minsize(top_width,top_height)
self.top.title("CocosCreateProject") self.top.title("CocosCreateProject")
# project name frame
self.frameName = Frame(self.top) self.frameName = Frame(self.top)
self.labName = Label(self.frameName,text="project_name:",width=15) self.labName = Label(self.frameName, text="projectName:", width=15)
self.strName=StringVar() self.strName = StringVar()
self.strName.set("MyGame") self.strName.set("MyGame")
self.editName = Entry(self.frameName,textvariable=self.strName,width=40 ) self.editName = Entry(self.frameName, textvariable=self.strName, width=40 )
self.labName.pack(side = LEFT,fill = BOTH) self.labName.pack(side = LEFT, fill = BOTH)
self.editName.pack(side = LEFT) self.editName.pack(side = LEFT)
self.frameName.pack(padx=0,pady=10,anchor="nw") self.frameName.pack(padx=0, pady=10, anchor="nw")
# package name frame
self.framePackage = Frame(self.top) self.framePackage = Frame(self.top)
self.labPackage = Label(self.framePackage,text="package_name:",width=15) self.labPackage = Label(self.framePackage, text="packageName:", width=15)
self.strPackage=StringVar() self.strPackage=StringVar()
self.strPackage.set("com.MyCompany.AwesomeGame") self.strPackage.set("com.MyCompany.AwesomeGame")
self.editPackage = Entry(self.framePackage,textvariable=self.strPackage,width=40 ) self.editPackage = Entry(self.framePackage, textvariable=self.strPackage, width=40 )
self.labPackage.pack(side = LEFT) self.labPackage.pack(side = LEFT)
self.editPackage.pack(side = LEFT) self.editPackage.pack(side = LEFT)
self.framePackage.pack(padx=0, anchor="nw") self.framePackage.pack(padx=0, anchor="nw")
# project path frame
self.framePath = Frame(self.top) self.framePath = Frame(self.top)
self.labPath = Label(self.framePath,text="project_path:",width=15) self.labPath = Label(self.framePath, text="projectPath:", width=15)
self.editPath = Entry(self.framePath,width=40) self.editPath = Entry(self.framePath, width=40)
self.btnPath = Button(self.framePath,text="...",width=10,command = self.pathCallback) self.btnPath = Button(self.framePath, text="...", width=10, command = self.pathCallback)
self.labPath.pack(side = LEFT) self.labPath.pack(side = LEFT)
self.editPath.pack(side = LEFT) self.editPath.pack(side = LEFT)
self.btnPath.pack(side = RIGHT) self.btnPath.pack(side = RIGHT)
self.framePath.pack(padx=0,pady=10,anchor="nw") self.framePath.pack(padx=0, pady=10, anchor="nw")
# language frame
self.frameLanguage = Frame(self.top) self.frameLanguage = Frame(self.top)
self.labLanguage = Label(self.frameLanguage,text="language:",width=15) self.labLanguage = Label(self.frameLanguage, text="language:", width=15)
self.var=IntVar() self.var=IntVar()
self.var.set(1) self.var.set(1)
self.checkcpp = Radiobutton(self.frameLanguage,text="cpp", variable=self.var, value=1, width=8) self.checkcpp = Radiobutton(self.frameLanguage, text="cpp", variable=self.var, value=1, width=8)
self.checklua = Radiobutton(self.frameLanguage,text="lua", variable=self.var, value=2, width=8) self.checklua = Radiobutton(self.frameLanguage, text="lua", variable=self.var, value=2, width=8)
self.checkjs = Radiobutton(self.frameLanguage,text="javascript", variable=self.var, value=3, width=15) self.checkjs = Radiobutton(self.frameLanguage, text="javascript", variable=self.var, value=3, width=15)
self.labLanguage.pack(side = LEFT) self.labLanguage.pack(side = LEFT)
self.checkcpp.pack(side = LEFT) self.checkcpp.pack(side = LEFT)
self.checklua.pack(side = LEFT) self.checklua.pack(side = LEFT)
self.checkjs.pack(side = LEFT) self.checkjs.pack(side = LEFT)
self.frameLanguage.pack(padx=0,anchor="nw") self.frameLanguage.pack(padx=0,anchor="nw")
self.progress = Scale(self.top,state= DISABLED,length=400,from_=0,to=100,orient=HORIZONTAL) # show progress
self.progress = Scale(self.top, state= DISABLED, length=400, from_=0, to=100, orient=HORIZONTAL)
self.progress.set(0) self.progress.set(0)
self.progress.pack(padx=10,pady=0,anchor="nw") self.progress.pack(padx=10, pady=0, anchor="nw")
# msg text frame
self.frameText = Frame(self.top)
self.text=Text(self.frameText, height=10, width=50)
self.text.bind("<KeyPress>", lambda e : "break")
self.scroll=Scrollbar(self.frameText, command=self.text.yview)
self.text.configure(yscrollcommand=self.scroll.set)
self.text.pack(side=LEFT, anchor="nw")
self.scroll.pack(side=RIGHT, fill=Y)
self.frameText.pack(padx=10, pady=5, anchor="nw")
# new project button
self.frameOperate = Frame(self.top) self.frameOperate = Frame(self.top)
self.btnCreate = Button(self.frameOperate, text="create", width=15, height =5, command = self.createBtnCallback) self.btnCreate = Button(self.frameOperate, text="create", width=15, height =5, command = self.createBtnCallback)
self.btnCreate.pack(side = LEFT) self.btnCreate.pack(side = LEFT)
self.frameOperate.pack(pady=20) self.frameOperate.pack(pady=20)
#center window on desktop
self.top.update()
curWidth = self.top.winfo_reqwidth()
curHeight = self.top.winfo_height()
scnWidth = self.top.winfo_screenwidth()
scnHeight = self.top.winfo_screenheight()
tmpcnf = '%dx%d+%d+%d'%(curWidth, curHeight, int((scnWidth-curWidth)/2), int((scnHeight-curHeight)/2))
self.top.geometry(tmpcnf)
#fix size
self.top.maxsize(curWidth, curHeight)
self.top.minsize(curWidth, curHeight)
#redirect out to text
sys.stdout = StdoutRedirector(self.text)
self.top.mainloop() self.top.mainloop()
sys.stdout = self.old_stdout
def process_queue(self): def process_queue(self):
"""
"""
#message is empty
if self.queue.empty(): if self.queue.empty():
self.top.after(100, self.process_queue) self.top.after(100, self.process_queue)
return return
#parse message
msg = self.queue.get(0) msg = self.queue.get(0)
msglist = msg.split("@") msglist = msg.split("@")
if len(msglist) < 4: if len(msglist) < 4:
return return
#begin
if msglist[0] == "begin": if msglist[0] == "begin":
pass self.progress['state'] = NORMAL
#doing
elif msglist[0] == "doing": elif msglist[0] == "doing":
self.progress.set(0) pass
elif msglist[0] == "end":
showwarning("create", msglist[3])
self.btnCreate['state'] = NORMAL
self.progress.set(int(int(msglist[1])*100/int(msglist[2]))) self.progress.set(int(int(msglist[1])*100/int(msglist[2])))
#end
if msglist[0] == "end":
showwarning("create", msglist[3])
self.progress.set(0)
self.text.insert(END,"=================END==============\n")
self.progress['state'] = DISABLED
self.btnCreate['state'] = NORMAL
return
self.top.after(100, self.process_queue)
def createBtnCallback(self): def createBtnCallback(self):
"""Create button event.
"""
#Check project name
projectName = self.editName.get() projectName = self.editName.get()
if projectName == "": if projectName == "":
showwarning("warning", "project_name is empty") showwarning("warning", "projectName is empty")
return return
#Check the package name is effective
packageName = self.editPackage.get() packageName = self.editPackage.get()
if packageName == "": packageList = packageName.split(".")
showwarning("warning", "package_name is empty") if len(packageList) < 2:
showwarning("warning", "packageName format error!")
return return
for index in range(len(packageList)):
if (packageList[index] == "") or (packageList[index][0].isdigit()):
showwarning("warning", "packageName format error!")
return
# get select language type
language = "cpp" language = "cpp"
if self.var.get() == 1: if self.var.get() == 1:
language = "cpp" language = "cpp"
@ -172,26 +241,27 @@ class TkProjectDialog():
projectPath = self.editPath.get() projectPath = self.editPath.get()
if projectPath == "": if projectPath == "":
showwarning("warning", "project_path is empty") showwarning("warning", "projectPath is empty")
return return
# if project has already exist,....
if os.path.exists(os.path.join(projectPath, projectName)): if os.path.exists(os.path.join(projectPath, projectName)):
if not askyesno("warning", "%s had exist,do you want to recreate!" %projectName ): if not askyesno("warning", "%s had exist,do you want to recreate!" %projectName ):
return return
print("state start") #create a new thread to deal with create new project.
self.btnCreate['state'] = DISABLED self.btnCreate['state'] = DISABLED
self.queue = Queue.Queue() self.queue = Queue.Queue()
print("state end")
ThreadedTask(self.queue, projectName, packageName, language, projectPath).start() ThreadedTask(self.queue, projectName, packageName, language, projectPath).start()
self.top.after(100, self.process_queue) self.top.after(100, self.process_queue)
def pathCallback(self): def pathCallback(self):
"""Paht button event.
"""
filepath = askdirectory() filepath = askdirectory()
if filepath: if filepath:
self.editPath.delete(0, END) self.editPath.delete(0, END)
self.editPath.insert(0, filepath) self.editPath.insert(0, filepath)
#entry.insert(0,filepath)
if __name__ =='__main__': if __name__ =='__main__':
TkProjectDialog() TkCocosDialog()