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

259 lines
8.6 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-25 10:24:46 +08:00
class TkCocosDialog(Frame):
def __init__(self, parent):
Frame.__init__(self,parent)
2013-12-24 09:42:37 +08:00
self.projectName = ""
self.packageName = ""
self.language = ""
2013-12-25 10:24:46 +08:00
self.parent = parent
self.columnconfigure(3, weight=1)
self.rowconfigure(5, weight=1)
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
# project name frame
2013-12-25 10:24:46 +08:00
self.labName = Label(self, text="projectName:")
2013-12-24 15:07:51 +08:00
self.strName = StringVar()
2013-12-24 09:42:37 +08:00
self.strName.set("MyGame")
2013-12-25 10:24:46 +08:00
self.editName = Entry(self, textvariable=self.strName)
self.labName.grid(sticky=W, pady=4, padx=5)
self.editName.grid(row=0, column=1, columnspan=3,padx=5, pady=2,sticky=E+W)
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
# package name frame
2013-12-25 10:24:46 +08:00
self.labPackage = Label(self, text="packageName:")
2013-12-24 09:42:37 +08:00
self.strPackage=StringVar()
self.strPackage.set("com.MyCompany.AwesomeGame")
2013-12-25 10:24:46 +08:00
self.editPackage = Entry(self, textvariable=self.strPackage)
self.labPackage.grid(row=1, column=0,sticky=W, padx=5)
self.editPackage.grid(row=1, column=1, columnspan=3,padx=5, pady=2,sticky=E+W)
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
# project path frame
2013-12-25 10:24:46 +08:00
self.labPath = Label(self, text="projectPath:")
self.editPath = Entry(self)
self.btnPath = Button(self, text="...", width = 6, command = self.pathCallback)
self.labPath.grid(row=2, column=0,sticky=W, pady=4, padx=5)
self.editPath.grid(row=2, column=1, columnspan=3,padx=5, pady=2, sticky=E+W)
self.btnPath.grid(row=2, column=4,)
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
# language frame
2013-12-25 10:24:46 +08:00
self.labLanguage = Label(self, text="language:")
2013-12-24 09:42:37 +08:00
self.var=IntVar()
self.var.set(1)
2013-12-25 10:24:46 +08:00
self.checkcpp = Radiobutton(self, text="cpp", variable=self.var, value=1)
self.checklua = Radiobutton(self, text="lua", variable=self.var, value=2)
self.checkjs = Radiobutton(self, text="javascript", variable=self.var, value=3)
self.labLanguage.grid(row=3, column=0,sticky=W, padx=5)
self.checkcpp.grid(row=3, column=1,sticky=N+W)
self.checklua.grid(row=3, column=2,padx=5,sticky=N+W)
self.checkjs.grid(row=3, column=3,padx=5,sticky=N+W)
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
# show progress
2013-12-25 10:24:46 +08:00
self.progress = Scale(self, state= DISABLED, from_=0, to=100, orient=HORIZONTAL)
2013-12-24 09:42:37 +08:00
self.progress.set(0)
2013-12-25 10:24:46 +08:00
self.progress.grid(row=4, column=0, columnspan=4,padx=5, pady=2,sticky=E+W+S+N)
2013-12-24 15:07:51 +08:00
# msg text frame
2013-12-25 10:24:46 +08:00
self.text=Text(self,background = '#d9efff')
2013-12-24 15:07:51 +08:00
self.text.bind("<KeyPress>", lambda e : "break")
2013-12-25 10:24:46 +08:00
self.text.grid(row=5, column=0, columnspan=4, rowspan=1, padx=5, sticky=E+W+S+N)
2013-12-24 15:07:51 +08:00
# new project button
2013-12-25 10:24:46 +08:00
self.btnCreate = Button(self, text="create", command = self.createBtnCallback)
self.btnCreate.grid(row=7, column=3, columnspan=1, rowspan=1,pady=2,ipadx=15,ipady =10, sticky=W)
2013-12-24 09:42:37 +08:00
2013-12-24 15:07:51 +08:00
#center window on desktop
2013-12-25 10:24:46 +08:00
curWidth = 500
curHeight = 450
scnWidth = self.parent.winfo_screenwidth()
scnHeight = self.parent.winfo_screenheight()
2013-12-24 15:07:51 +08:00
tmpcnf = '%dx%d+%d+%d'%(curWidth, curHeight, int((scnWidth-curWidth)/2), int((scnHeight-curHeight)/2))
2013-12-25 10:24:46 +08:00
self.parent.geometry(tmpcnf)
self.parent.title("CocosCreateProject")
2013-12-24 15:07:51 +08:00
#fix size
2013-12-25 10:24:46 +08:00
#self.parent.maxsize(curWidth, curHeight)
#self.parent.minsize(curWidth, curHeight)
2013-12-24 15:07:51 +08:00
#redirect out to text
2013-12-25 10:24:46 +08:00
self.pack(fill=BOTH, expand=1)
2013-12-24 15:07:51 +08:00
sys.stdout = StdoutRedirector(self.text)
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():
2013-12-25 10:24:46 +08:00
self.parent.after(100, self.process_queue)
2013-12-24 09:42:37 +08:00
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
2013-12-25 10:24:46 +08:00
self.parent.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()
2013-12-25 10:24:46 +08:00
self.parent.after(100, self.process_queue)
2013-12-24 09:42:37 +08:00
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)
2013-12-25 10:24:46 +08:00
def createTkCocosDialog():
old_stdout = sys.stdout
root = Tk()
app = TkCocosDialog(root)
root.mainloop()
sys.stdout = old_stdout
2013-12-24 09:42:37 +08:00
if __name__ =='__main__':
2013-12-25 10:24:46 +08:00
createTkCocosDialog()