axmol/tools/project-creator/module/ui.py

267 lines
9.2 KiB
Python
Raw Normal View History

2013-12-24 09:42:37 +08:00
#!/usr/bin/python
#coding=utf-8
# ui.py
# Create cross-platform cocos2d-x project
# Copyright (c) 2012 cocos2d-x.org
# Author: chuanwei
import platform
import os, os.path
import shutil
import threading
import Queue
import time
from core import CocosProject
2013-12-24 15:07:51 +08:00
#import head files by python version.
2013-12-24 09:42:37 +08:00
if int(platform.python_version().split('.')[0])>=3:
from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
else:
from Tkinter import *
from tkFileDialog import *
from tkMessageBox import *
class ThreadedTask(threading.Thread):
2013-12-24 15:07:51 +08:00
"""Create cocos project thread.
"""
2013-12-24 09:42:37 +08:00
def __init__(self, queue, projectName, packageName, language, projectPath):
threading.Thread.__init__(self)
self.queue = queue
self.projectName = projectName
self.packageName = packageName
self.language = language
self.projectPath = projectPath
def run(self):
2013-12-24 15:07:51 +08:00
"""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.
2013-12-24 09:42:37 +08:00
if os.path.exists(os.path.join(self.projectPath, self.projectName)):
2013-12-24 15:07:51 +08:00
print ("###begin remove... " + self.projectName)
2013-12-24 09:42:37 +08:00
shutil.rmtree(os.path.join(self.projectPath, self.projectName))
2013-12-24 15:07:51 +08:00
print ("###remove finish: " + self.projectName)
putMsg = "begin@%d@%d@%s" %(0, 100, "begin create")
2013-12-24 09:42:37 +08:00
self.queue.put(putMsg)
2013-12-24 15:07:51 +08:00
2013-12-24 09:42:37 +08:00
project = CocosProject()
2013-12-24 15:07:51 +08:00
breturn = project.createPlatformProjects(
2013-12-24 09:42:37 +08:00
self.projectName,
self.packageName,
self.language,
self.projectPath,
self.newProjectCallBack
)
if breturn:
2013-12-24 15:07:51 +08:00
putMsg = "end@%d@%d@%s" %(100, 100, "create successful")
2013-12-24 09:42:37 +08:00
else:
2013-12-24 15:07:51 +08:00
putMsg = "end@%d@%d@%s" %(100, 100, "create failure")
2013-12-24 09:42:37 +08:00
self.queue.put(putMsg)
def newProjectCallBack(self, step, totalStep, showMsg):
2013-12-24 15:07:51 +08:00
"""Creating cocos project callback.
"""
putMsg = "doing@%d@%d@%s" %(step, totalStep, showMsg)
2013-12-24 09:42:37 +08:00
self.queue.put(putMsg)
2013-12-24 15:07:51 +08:00
class StdoutRedirector(object):
"""Redirect output.
"""
def __init__(self, text_area):
self.text_area = text_area
def write(self, str):
self.text_area.insert(END, str)
self.text_area.see(END)
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
class TkCocosDialog():
def __init__(self):
2013-12-24 09:42:37 +08:00
self.projectName = ""
self.packageName = ""
self.language = ""
2013-12-24 15:07:51 +08:00
self.old_stdout = sys.stdout
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
print("create_project.py -n MyGame -k com.MyCompany.AwesomeGame -l javascript -p c:/mycompany")
# top window
2013-12-24 09:42:37 +08:00
self.top = Tk()
self.top.title("CocosCreateProject")
2013-12-24 15:07:51 +08:00
# project name frame
2013-12-24 09:42:37 +08:00
self.frameName = Frame(self.top)
2013-12-24 15:07:51 +08:00
self.labName = Label(self.frameName, text="projectName:", width=15)
self.strName = StringVar()
2013-12-24 09:42:37 +08:00
self.strName.set("MyGame")
2013-12-24 15:07:51 +08:00
self.editName = Entry(self.frameName, textvariable=self.strName, width=40 )
self.labName.pack(side = LEFT, fill = BOTH)
2013-12-24 09:42:37 +08:00
self.editName.pack(side = LEFT)
2013-12-24 15:07:51 +08:00
self.frameName.pack(padx=0, pady=10, anchor="nw")
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
# package name frame
2013-12-24 09:42:37 +08:00
self.framePackage = Frame(self.top)
2013-12-24 15:07:51 +08:00
self.labPackage = Label(self.framePackage, text="packageName:", width=15)
2013-12-24 09:42:37 +08:00
self.strPackage=StringVar()
self.strPackage.set("com.MyCompany.AwesomeGame")
2013-12-24 15:07:51 +08:00
self.editPackage = Entry(self.framePackage, textvariable=self.strPackage, width=40 )
2013-12-24 09:42:37 +08:00
self.labPackage.pack(side = LEFT)
self.editPackage.pack(side = LEFT)
self.framePackage.pack(padx=0, anchor="nw")
2013-12-24 15:07:51 +08:00
# project path frame
2013-12-24 09:42:37 +08:00
self.framePath = Frame(self.top)
2013-12-24 15:07:51 +08:00
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)
2013-12-24 09:42:37 +08:00
self.labPath.pack(side = LEFT)
self.editPath.pack(side = LEFT)
self.btnPath.pack(side = RIGHT)
2013-12-24 15:07:51 +08:00
self.framePath.pack(padx=0, pady=10, anchor="nw")
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
# language frame
2013-12-24 09:42:37 +08:00
self.frameLanguage = Frame(self.top)
2013-12-24 15:07:51 +08:00
self.labLanguage = Label(self.frameLanguage, text="language:", width=15)
2013-12-24 09:42:37 +08:00
self.var=IntVar()
self.var.set(1)
2013-12-24 15:07:51 +08:00
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)
2013-12-24 09:42:37 +08:00
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")
2013-12-24 15:07:51 +08:00
# show progress
self.progress = Scale(self.top, state= DISABLED, length=400, from_=0, to=100, orient=HORIZONTAL)
2013-12-24 09:42:37 +08:00
self.progress.set(0)
2013-12-24 15:07:51 +08:00
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
2013-12-24 09:42:37 +08:00
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)
2013-12-24 15:07:51 +08:00
#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)
2013-12-24 09:42:37 +08:00
self.top.mainloop()
2013-12-24 15:07:51 +08:00
sys.stdout = self.old_stdout
2013-12-24 09:42:37 +08:00
def process_queue(self):
2013-12-24 15:07:51 +08:00
"""
"""
#message is empty
2013-12-24 09:42:37 +08:00
if self.queue.empty():
self.top.after(100, self.process_queue)
return
2013-12-24 15:07:51 +08:00
#parse message
2013-12-24 09:42:37 +08:00
msg = self.queue.get(0)
msglist = msg.split("@")
if len(msglist) < 4:
return
2013-12-24 15:07:51 +08:00
#begin
2013-12-24 09:42:37 +08:00
if msglist[0] == "begin":
2013-12-24 15:07:51 +08:00
self.progress['state'] = NORMAL
#doing
2013-12-24 09:42:37 +08:00
elif msglist[0] == "doing":
2013-12-24 15:07:51 +08:00
pass
2013-12-24 09:42:37 +08:00
self.progress.set(int(int(msglist[1])*100/int(msglist[2])))
2013-12-24 15:07:51 +08:00
#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)
2013-12-24 09:42:37 +08:00
def createBtnCallback(self):
2013-12-24 15:07:51 +08:00
"""Create button event.
"""
#Check project name
2013-12-24 09:42:37 +08:00
projectName = self.editName.get()
if projectName == "":
2013-12-24 15:07:51 +08:00
showwarning("warning", "projectName is empty")
2013-12-24 09:42:37 +08:00
return
2013-12-24 15:07:51 +08:00
#Check the package name is effective
2013-12-24 09:42:37 +08:00
packageName = self.editPackage.get()
2013-12-24 15:07:51 +08:00
packageList = packageName.split(".")
if len(packageList) < 2:
showwarning("warning", "packageName format error!")
2013-12-24 09:42:37 +08:00
return
2013-12-24 15:07:51 +08:00
for index in range(len(packageList)):
if (packageList[index] == "") or (packageList[index][0].isdigit()):
showwarning("warning", "packageName format error!")
return
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
# get select language type
2013-12-24 09:42:37 +08:00
language = "cpp"
if self.var.get() == 1:
language = "cpp"
elif self.var.get() == 2:
language = "lua"
elif self.var.get() == 3:
language = "javascript"
projectPath = self.editPath.get()
if projectPath == "":
2013-12-24 15:07:51 +08:00
showwarning("warning", "projectPath is empty")
2013-12-24 09:42:37 +08:00
return
2013-12-24 15:07:51 +08:00
# if project has already exist,....
2013-12-24 09:42:37 +08:00
if os.path.exists(os.path.join(projectPath, projectName)):
if not askyesno("warning", "%s had exist,do you want to recreate!" %projectName ):
return
2013-12-24 15:07:51 +08:00
#create a new thread to deal with create new project.
2013-12-24 09:42:37 +08:00
self.btnCreate['state'] = DISABLED
self.queue = Queue.Queue()
ThreadedTask(self.queue, projectName, packageName, language, projectPath).start()
self.top.after(100, self.process_queue)
def pathCallback(self):
2013-12-24 15:07:51 +08:00
"""Paht button event.
"""
2013-12-24 09:42:37 +08:00
filepath = askdirectory()
if filepath:
self.editPath.delete(0, END)
self.editPath.insert(0, filepath)
if __name__ =='__main__':
2013-12-24 15:07:51 +08:00
TkCocosDialog()