From 1ce75bb444a90e5f4ca473a0d66fbeda78407024 Mon Sep 17 00:00:00 2001 From: chuanweizhang Date: Tue, 24 Dec 2013 15:07:51 +0800 Subject: [PATCH] modify ui --- tools/project-creator/create_project.py | 10 +- tools/project-creator/module/core.py | 7 +- tools/project-creator/module/ui.py | 172 +++++++++++++++++------- 3 files changed, 132 insertions(+), 57 deletions(-) diff --git a/tools/project-creator/create_project.py b/tools/project-creator/create_project.py index 48eb9d0ef0..685fbc3f03 100755 --- a/tools/project-creator/create_project.py +++ b/tools/project-creator/create_project.py @@ -6,13 +6,19 @@ # Author: chuanwei import sys -from module.ui import TkProjectDialog +from module.ui import TkCocosDialog from module.core import CocosProject # ------------ 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: - TkProjectDialog() + TkCocosDialog() else: project = CocosProject() name, package, language, path = project.checkParams() diff --git a/tools/project-creator/module/core.py b/tools/project-creator/module/core.py index 80c01d6c57..fea4c38832 100644 --- a/tools/project-creator/module/core.py +++ b/tools/project-creator/module/core.py @@ -166,8 +166,8 @@ class CocosProject: shutil.rmtree(self.context["dst_project_path"]) return False - print ("\n") - print ("New project has been created in this path: " + self.context["dst_project_path"].replace("\\", "/")) + print ("###New project has been created in this path: ") + print (self.context["dst_project_path"].replace("\\", "/")) print ("Have Fun!") return True @@ -190,8 +190,7 @@ class CocosProject: os.makedirs(dstPath) # begin copy - print("\n") - print("begin copy engine...") + print("###begin copy engine...") # list engine root dir. dirlist = os.listdir(self.cocos_root) for line in dirlist: diff --git a/tools/project-creator/module/ui.py b/tools/project-creator/module/ui.py index 8ce767a1df..40e1cefac6 100644 --- a/tools/project-creator/module/ui.py +++ b/tools/project-creator/module/ui.py @@ -15,6 +15,7 @@ import time from core import CocosProject +#import head files by python version. if int(platform.python_version().split('.')[0])>=3: from tkinter import * from tkinter.filedialog import * @@ -25,8 +26,9 @@ else: from tkMessageBox import * - class ThreadedTask(threading.Thread): + """Create cocos project thread. + """ def __init__(self, queue, projectName, packageName, language, projectPath): threading.Thread.__init__(self) self.queue = queue @@ -36,13 +38,23 @@ class ThreadedTask(threading.Thread): self.projectPath = projectPath 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)): + print ("###begin remove... " + 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) + project = CocosProject() - breturn=project.createPlatformProjects( + breturn = project.createPlatformProjects( self.projectName, self.packageName, self.language, @@ -50,118 +62,175 @@ class ThreadedTask(threading.Thread): self.newProjectCallBack ) if breturn: - putMsg = "end@%d@%d@%s" %(100,100,"create successful") + putMsg = "end@%d@%d@%s" %(100, 100, "create successful") else: - putMsg = "end@%d@%d@%s" %(100,100,"create failure") + putMsg = "end@%d@%d@%s" %(100, 100, "create failure") self.queue.put(putMsg) 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) -class TkProjectDialog(): - def __init__(self): +class StdoutRedirector(object): + """Redirect output. + """ + def __init__(self, text_area): + self.text_area = text_area - #reload(sys) - #sys.setdefaultencoding('utf-8') + def write(self, str): + self.text_area.insert(END, str) + self.text_area.see(END) + +class TkCocosDialog(): + def __init__(self): self.projectName = "" self.packageName = "" 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() - 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") + # project name frame self.frameName = Frame(self.top) - self.labName = Label(self.frameName,text="project_name:",width=15) - self.strName=StringVar() + self.labName = Label(self.frameName, text="projectName:", width=15) + self.strName = StringVar() self.strName.set("MyGame") - self.editName = Entry(self.frameName,textvariable=self.strName,width=40 ) - self.labName.pack(side = LEFT,fill = BOTH) + self.editName = Entry(self.frameName, textvariable=self.strName, width=40 ) + self.labName.pack(side = LEFT, fill = BOTH) 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.labPackage = Label(self.framePackage,text="package_name:",width=15) + self.labPackage = Label(self.framePackage, text="packageName:", width=15) self.strPackage=StringVar() 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.editPackage.pack(side = LEFT) self.framePackage.pack(padx=0, anchor="nw") + # project path frame self.framePath = Frame(self.top) - self.labPath = Label(self.framePath,text="project_path:",width=15) - self.editPath = Entry(self.framePath,width=40) - self.btnPath = Button(self.framePath,text="...",width=10,command = self.pathCallback) + self.labPath = Label(self.framePath, text="projectPath:", width=15) + self.editPath = Entry(self.framePath, width=40) + self.btnPath = Button(self.framePath, text="...", width=10, command = self.pathCallback) self.labPath.pack(side = LEFT) self.editPath.pack(side = LEFT) 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.labLanguage = Label(self.frameLanguage,text="language:",width=15) + self.labLanguage = Label(self.frameLanguage, text="language:", width=15) self.var=IntVar() self.var.set(1) - 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.checkjs = Radiobutton(self.frameLanguage,text="javascript", variable=self.var, value=3, width=15) + 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.checkjs = Radiobutton(self.frameLanguage, text="javascript", variable=self.var, value=3, width=15) self.labLanguage.pack(side = LEFT) self.checkcpp.pack(side = LEFT) self.checklua.pack(side = LEFT) self.checkjs.pack(side = LEFT) 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.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("", 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.btnCreate = Button(self.frameOperate, text="create", width=15, height =5, command = self.createBtnCallback) self.btnCreate.pack(side = LEFT) 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() + sys.stdout = self.old_stdout def process_queue(self): - + """ + """ + #message is empty if self.queue.empty(): self.top.after(100, self.process_queue) return + + #parse message msg = self.queue.get(0) msglist = msg.split("@") if len(msglist) < 4: return + #begin if msglist[0] == "begin": - pass + self.progress['state'] = NORMAL + #doing elif msglist[0] == "doing": - self.progress.set(0) - elif msglist[0] == "end": - showwarning("create", msglist[3]) - self.btnCreate['state'] = NORMAL + pass 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): - + """Create button event. + """ + #Check project name projectName = self.editName.get() if projectName == "": - showwarning("warning", "project_name is empty") + showwarning("warning", "projectName is empty") return + #Check the package name is effective packageName = self.editPackage.get() - if packageName == "": - showwarning("warning", "package_name is empty") + packageList = packageName.split(".") + if len(packageList) < 2: + showwarning("warning", "packageName format error!") 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" if self.var.get() == 1: language = "cpp" @@ -172,26 +241,27 @@ class TkProjectDialog(): projectPath = self.editPath.get() if projectPath == "": - showwarning("warning", "project_path is empty") + showwarning("warning", "projectPath is empty") return + # if project has already exist,.... if os.path.exists(os.path.join(projectPath, projectName)): if not askyesno("warning", "%s had exist,do you want to recreate!" %projectName ): return - print("state start") + #create a new thread to deal with create new project. self.btnCreate['state'] = DISABLED self.queue = Queue.Queue() - print("state end") ThreadedTask(self.queue, projectName, packageName, language, projectPath).start() self.top.after(100, self.process_queue) def pathCallback(self): + """Paht button event. + """ filepath = askdirectory() if filepath: self.editPath.delete(0, END) self.editPath.insert(0, filepath) - #entry.insert(0,filepath) if __name__ =='__main__': - TkProjectDialog() \ No newline at end of file + TkCocosDialog() \ No newline at end of file