demo 更新

This commit is contained in:
yhh
2020-08-21 19:21:40 +08:00
parent 6d4d787530
commit 646d1806ec
144 changed files with 51196 additions and 2531 deletions

25
demo/src/UI/PopManager.ts Normal file
View File

@@ -0,0 +1,25 @@
module manager {
export class AlterManager {
public static alter_tips(txt: string){
let item_tip = FUI.common.UI_com_tips.createInstance();
if (!item_tip)
return;
item_tip.m_content.text = txt;
item_tip.ensureBoundsCorrect();
item_tip.x = es.Core.Instance.stage.stageWidth / 2 - item_tip.width / 2;
item_tip.y = es.Core.Instance.stage.stageHeight / 2 - item_tip.height / 2;
LayerManager.getInstance().tipsLayer.addChild(item_tip.displayObject);
item_tip.alpha = 0;
let originY = item_tip.y;
egret.Tween.get(item_tip).to({alpha: 1, y: originY - 100}, 700, egret.Ease.cubicInOut).wait(1000)
.to({alpha: 0, y: originY - 200}, 700, egret.Ease.cubicInOut).call(()=>{
egret.Tween.removeTweens(item_tip.displayObject);
if (item_tip.displayObject && item_tip.displayObject.parent)
item_tip.displayObject.parent.removeChild(item_tip.displayObject);
item_tip.dispose();
item_tip = null;
});
}
}
}

View File

@@ -0,0 +1,27 @@
module loading {
export class LoadingControl {
private _eventManager: EventManager;
private _viewManager: ViewManager;
private static single: LoadingControl;
public static getInstance(){
if (!this.single) this.single = new LoadingControl();
return this.single;
}
constructor(){
this.addEvents();
}
private addEvents(){
this._eventManager = EventManager.getInstance();
this._viewManager = ViewManager.getInstance();
// 事件监听
this._eventManager.addListener(LoadingEvents.OPENVIEW, this.openView, this);
}
private openView(){
this._viewManager.openView(LoadingView);
}
}
}

View File

@@ -0,0 +1,5 @@
module loading {
export module LoadingEvents {
export const OPENVIEW = Symbol();
}
}

View File

@@ -0,0 +1,59 @@
module loading {
import commonBinder = FUI.common.commonBinder;
import ResourceInfo = RES.ResourceInfo;
export class LoadingView extends BaseView implements RES.PromiseTaskReporter {
private _ui: FUI.loading.UI_View_loading;
private _loadGroup = ["preload", "common"];
private _maxProgress = 0;
private _currentProgress = 0;
constructor() {
super("loading");
}
public init() {
this._ui = this._fuiView = FUI.loading.UI_View_loading.createInstance();
this.addChild(this._ui.displayObject);
super.init();
}
public async show() {
this._ui.m_pg_loading.value = 0;
for (let i = 0; i < this._loadGroup.length; i ++){
this._maxProgress += RES.getGroupByName(this._loadGroup[i]).length;
}
this._ui.m_pg_loading.max = this._maxProgress;
for (let i = 0; i < this._loadGroup.length; i ++){
await RES.loadGroup(this._loadGroup[i], 0, this);
}
}
public onProgress(current: number, total: number, resItem: ResourceInfo): void {
this._ui.m_pg_loading.tweenValue(this._currentProgress++, 1);
if (this._currentProgress == this._maxProgress) {
if (resItem.name == "common"){
fairygui.UIPackage.addPackage(resItem.name);
commonBinder.bindAll();
}
egret.setTimeout(() => {
this.openMainScene();
}, this, 1000)
}
}
public openMainScene() {
es.Core.scene = new samples.BasicScene();
EventManager.getInstance().dispatch(sc.ScEvents.OPENVIEW);
this.close();
}
public destroy() {
if (this._ui) {
this._ui.dispose();
this._ui = null;
}
super.destroy();
}
}
}

101
demo/src/UI/mvc/BaseView.ts Normal file
View File

@@ -0,0 +1,101 @@
class BaseView extends egret.DisplayObjectContainer {
private _name: string;
public get name(){
return this._name;
}
public set name(value: string){
this._name = value;
}
public viewLayer: viewLayer = viewLayer.pop;
/** 是否可以被销毁 */
protected _destoryChildren: boolean;
protected _isDispose: boolean;
/** fui界面 */
protected _fuiView: fairygui.GComponent;
/** 是否已经显示 */
protected _isShow: boolean;
/** 视图数据 */
protected _showData: any;
constructor(name?: string){
super();
this._isDispose = false;
if (name){
this.name = name;
}
}
/**
* 当界面被初始化时调用
*/
public init(){
this._destoryChildren = true;
}
/**
* 当界面大小被改变时
*/
public resize(){
if (this._fuiView && this._isShow){
this._fuiView.width = LayerManager.getInstance().stage.stageWidth;
this._fuiView.height = LayerManager.getInstance().stage.stageHeight;
}
}
/**
*
* @param showData
*/
public baseShow(showData?: any){
this._isShow = true;
this._showData = showData;
egret.callLater(this.resize, this);
}
/**
* 打开界面时调用
*/
public show() {
}
/**
* 当界面关闭时调用
*/
public close() {
this._isShow = false;
ViewManager.getInstance().closeView(this);
}
/**
*
* @param showData
*/
public baseRefresh(showData?: any){
this._showData = showData;
}
/**
* 当界面刷新时调用
*/
public refresh(showData?: any){
this._showData = showData;
}
/**
* 当界面销毁时调用
*/
public destroy(){
this._isDispose = true;
if (this.parent)
this.parent.removeChild(this);
if (this._destoryChildren){
this._fuiView = null;
while (this.numChildren > 0){
this.removeChildAt(0);
}
this._destoryChildren = false;
}
}
}

View File

@@ -0,0 +1,104 @@
class EventManager {
private static _single: EventManager;
private _dict: any;
private _eventList: EventVo[];
public static getInstance() {
if (!this._single) this._single = new EventManager();
return this._single;
}
constructor() {
this._dict = {};
this._eventList = [];
}
/**
* 添加消息监听
* @param type
* @param listener
* @param thisObj
*/
public addListener(type: string | symbol, listener: Function, thisObj: any): void {
var arr: any[] = this._dict[type];
if (arr == null) {
arr = [];
this._dict[type] = arr;
}
var i: number, len: number = arr.length;
for (i = 0; i < len; i++) {
if (arr[i][0] == listener && arr[i][1] == thisObj) return;
}
arr.push([listener, thisObj]);
}
/**
* 移除监听
* @param type
* @param listener
* @param thisObj
*/
public removeListener(type: string | symbol, listener: Function, thisObj: any): void {
var arr: any[] = this._dict[type];
if (arr == null) return;
var i: number, len: number = Array.length;
for (i = 0; i < len; i++) {
if (arr[i][0] == listener && arr[i][1] == thisObj) {
arr.splice(i, 1);
i--;
break;
}
}
if (arr.length == 0) {
delete this._dict[type];
this._dict[type] = null;
}
}
/**
* 事件派发
* @param type
* @param param
*/
public dispatch(type: string | symbol, ...param: any[]): void {
if (this._dict[type] == null) return;
var vo: EventVo = new EventVo();
vo.type = type;
vo.param = param;
this.dealMsg(vo);
}
/**
* 事件处理
* @param vo
*/
private dealMsg(vo: EventVo): void {
var listeners: Array<any> = this._dict[vo.type];
if (listeners && listeners.length > 0) {
var i: number = 0, len: number = listeners.length, listener: Array<any> = null;
while (i < len) {
listener = listeners[i];
listener[0].apply(listener[1], vo.param);
if (listeners.length != len) {
len = listeners.length;
i--;
}
i++;
}
}
vo.dispose();
}
}
class EventVo {
public type: string | symbol;
public param: any[];
public dispose() {
this.type = null;
this.param = null;
}
}

View File

@@ -0,0 +1,389 @@
declare interface String {
/**
* 去除字符串头部空格或指定字符
* @param c 指定字符(字符串)
*/
trimStart(c);
/**
* 去除字符串尾部空格或指定字符
* @param c 指定字符(字符串)
*/
trimEnd(c);
}
String.prototype.trimStart = function (c) {
function trimStart(str, c) {
let regExp;
if (!c) {
regExp = new RegExp(/^\s*/);
} else {
regExp = new RegExp("^" + c);
}
return str.replace(regExp, "");
}
return trimStart(this, c);
};
String.prototype.trimEnd = function (c) {
function trimEnd(str, c) {
let regExp;
if (!c) {
regExp = new RegExp(/\s*$/);
} else {
regExp = new RegExp(c + "$");
}
return str.replace(regExp, "");
}
return trimEnd(this, c);
};
declare interface Array<T> {
/**
* 获取满足表达式的数组元素索引
* @param predicate 表达式
*/
findIndex(predicate: Function): number;
/**
* 是否存在满足表达式的数组元素
* @param predicate 表达式
*/
any(predicate: Function): boolean;
/**
* 获取满足表达式的第一个或默认数组元素
* @param predicate 表达式
*/
firstOrDefault(predicate: Function): T;
/**
* 获取满足表达式的第一个数组元素
* @param predicate 表达式
*/
find(predicate: Function): T;
/**
* 筛选满足表达式的数组元素
* @param predicate 表达式
*/
where(predicate: Function): Array<T>;
/**
* 获取满足表达式的数组元素的计数
* @param predicate 表达式
*/
count(predicate: Function): number;
/**
* 获取满足表达式的数组元素的数组
* @param predicate 表达式
*/
findAll(predicate: Function): Array<T>;
/**
* 移除满足表达式的数组元素
* @param predicate 表达式
*/
removeAll(predicate: Function): void;
/**
* 移除数组元素
* @param element 数组元素
*/
remove(element): boolean;
/**
* 移除特定索引数组元素
* @param index 索引
*/
removeAt(index): void;
/**
* 移除范围数组元素
* @param index 开始索引
* @param count 删除的个数
*/
removeRange(index, count): void;
/**
* 获取通过选择器转换的数组
* @param selector 选择器
*/
select(selector: Function): Array<T>;
/**
* 排序(升序)
* @param keySelector key选择器
* @param comparer 比较器
*/
orderBy(keySelector: Function, comparer: Function): Array<T>;
/**
* 排序(降序)
* @param keySelector key选择器
* @param comparer 比较器
*/
orderByDescending(keySelector: Function, comparer: Function): Array<T>;
/**
* 分组
* @param keySelector key选择器
*/
groupBy(keySelector: Function): Array<T>;
/**
* 求和
* @param selector 选择器
*/
sum(selector);
}
Array.prototype.findIndex = function (predicate) {
function findIndex(array, predicate) {
for (let i = 0, len = array.length; i < len; i++) {
if (predicate.call(arguments[2], array[i], i, array)) {
return i;
}
}
return -1;
}
return findIndex(this, predicate);
};
Array.prototype.any = function (predicate) {
function any(array, predicate) {
return array.findIndex(predicate) > -1;
}
return any(this, predicate);
};
Array.prototype.firstOrDefault = function (predicate) {
function firstOrDefault(array, predicate) {
let index = array.findIndex(predicate);
return index == -1 ? null : array[index];
}
return firstOrDefault(this, predicate);
};
Array.prototype.find = function (predicate) {
function find(array, predicate) {
return array.firstOrDefault(predicate);
}
return find(this, predicate);
};
Array.prototype.where = function (predicate) {
function where(array, predicate) {
if (typeof array.reduce === "function") {
return array.reduce(function (ret, element, index) {
if (predicate.call(arguments[2], element, index, array)) {
ret.push(element);
}
return ret;
}, []);
} else {
let ret = [];
for (let i = 0, len = array.length; i < len; i++) {
let element = array[i];
if (predicate.call(arguments[2], element, i, array)) {
ret.push(element);
}
}
return ret;
}
}
return where(this, predicate);
};
Array.prototype.count = function (predicate) {
function count(array, predicate) {
return array.where(predicate).length;
}
return count(this, predicate);
};
Array.prototype.findAll = function (predicate) {
function findAll(array, predicate) {
return array.where(predicate);
}
return findAll(this, predicate);
};
Array.prototype.removeAll = function (predicate) {
function removeAll(array, predicate) {
let index;
do {
index = array.findIndex(predicate);
if (index >= 0) {
array.splice(index, 1);
}
} while (index >= 0);
}
removeAll(this, predicate);
};
Array.prototype.remove = function (element) {
function remove(array, element) {
let index = array.findIndex(function (x) {
return x === element;
});
if (index >= 0) {
array.splice(index, 1);
return true;
} else {
return false;
}
}
return remove(this, element);
};
Array.prototype.removeAt = function (index) {
function removeAt(array, index) {
array.splice(index, 1);
}
return removeAt(this, index);
};
Array.prototype.removeRange = function (index, count) {
function removeRange(array, index, count) {
array.splice(index, count);
}
return removeRange(this, index, count);
};
Array.prototype.select = function (selector) {
function select(array, selector) {
if (typeof array.reduce === "function") {
return array.reduce(function (ret, element, index) {
ret.push(selector.call(arguments[2], element, index, array));
return ret;
}, []);
} else {
let ret = [];
for (let i = 0, len = array.length; i < len; i++) {
ret.push(selector.call(arguments[2], array[i], i, array));
}
return ret;
}
}
return select(this, selector);
};
Array.prototype.orderBy = function (keySelector, comparer) {
function orderBy(array, keySelector, comparer) {
array.sort(function (x, y) {
let v1 = keySelector(x);
let v2 = keySelector(y);
if (comparer) {
return comparer(v1, v2);
} else {
return v1 > v2 ? 1 : -1;
}
});
return array;
}
return orderBy(this, keySelector, comparer);
};
Array.prototype.orderByDescending = function (keySelector, comparer) {
function orderByDescending(array, keySelector, comparer) {
array.sort(function (x, y) {
let v1 = keySelector(x);
let v2 = keySelector(y);
if (comparer) {
return -comparer(v1, v2);
} else {
return v1 < v2 ? 1 : -1;
}
});
return array;
}
return orderByDescending(this, keySelector, comparer);
};
Array.prototype.groupBy = function (keySelector) {
function groupBy(array, keySelector) {
if (typeof array.reduce === "function") {
let keys = [];
return array.reduce(function (groups, element, index) {
let key = JSON.stringify(
keySelector.call(arguments[1], element, index, array)
);
let index2 = keys.findIndex(function (x) {
return x === key;
});
if (index2 < 0) {
index2 = keys.push(key) - 1;
}
if (!groups[index2]) {
groups[index2] = [];
}
groups[index2].push(element);
return groups;
}, []);
} else {
let groups = [];
let keys = [];
for (let i = 0, len = array.length; i < len; i++) {
let key = JSON.stringify(
keySelector.call(arguments[1], array[i], i, array)
);
let index = keys.findIndex(function (x) {
return x === key;
});
if (index < 0) {
index = keys.push(key) - 1;
}
if (!groups[index]) {
groups[index] = [];
}
groups[index].push(array[i]);
}
return groups;
}
}
return groupBy(this, keySelector);
};
Array.prototype.sum = function (selector) {
function sum(array, selector) {
let ret;
for (let i = 0, len = array.length; i < len; i++) {
if (i == 0) {
if (selector) {
ret = selector.call(arguments[2], array[i], i, array);
} else {
ret = array[i];
}
} else {
if (selector) {
ret += selector.call(arguments[2], array[i], i, array);
} else {
ret += array[i];
}
}
}
return ret;
}
return sum(this, selector);
};

View File

@@ -0,0 +1,25 @@
class FguiUtils {
/** 包的命名空间 */
public static packageNamespace: any;
/** 加载fgui资源 */
public static load(name: string): Promise<any> {
return new Promise((resolve, reject) => {
let existPkg = fairygui.UIPackage.getByName(name);
if (existPkg) {
resolve();
}
RES.loadGroup(name, 0).then(()=>{
fairygui.UIPackage.addPackage(name);
if (this.packageNamespace[name][name + "Binder"])
this.packageNamespace[name][name + "Binder"].bindAll();
resolve();
}).catch(err => {
console.error("loadfgui error:" + err);
reject();
});
});
}
}

View File

@@ -0,0 +1,46 @@
/**
* 视图类型
*/
enum viewLayer {
pop,
}
/**
* 视图管理器
*/
class LayerManager {
private static _single: LayerManager;
public static getInstance(): LayerManager{
if (!this._single) this._single = new LayerManager();
return this._single;
}
private _stage: egret.Stage;
/** 弹窗层 */
public popLayer: egret.DisplayObjectContainer;
public tipsLayer: egret.DisplayObjectContainer;
/** 获取舞台 */
public get stage(){
return this._stage;
}
public init(stage: egret.Stage){
this._stage = stage;
this.popLayer = new egret.DisplayObjectContainer();
this.tipsLayer = new egret.DisplayObjectContainer();
this._stage.addChild(this.popLayer);
this._stage.addChild(this.tipsLayer);
}
/**
* 添加到layer上
* @param view
*/
public addLayerToView(view: BaseView){
let layerName = viewLayer[view.viewLayer] + "Layer";
this[layerName].addChild(view);
}
}

View File

@@ -0,0 +1,125 @@
/** 窗口控制器 */
class ViewManager {
private static _single: ViewManager;
/** 已打开的列表 存储视图实例 */
private _openDic: BaseView[] = [];
/** 跳过关闭的列表 用于主视图 存储视图类型 */
private _skipCloseDic = [];
/** 层级管理器 */
private _layerManager: LayerManager;
public static getInstance(): ViewManager{
if (!this._single) this._single = new ViewManager();
return this._single;
}
constructor(){
this._layerManager = LayerManager.getInstance();
}
/**
* 打开界面
* @param viewCls 视图类型
* @param viewData 视图数据
*/
public openView(viewCls, viewData?: any): Promise<any>{
return new Promise((resolve, reject)=>{
let newView: BaseView = this.getView<BaseView>(viewCls);
if (!newView) newView = new viewCls();
if (this.isOpenView(viewCls)){
newView.refresh(viewData);
return;
}
this._openDic.push(newView);
FguiUtils.load(newView.name).then(()=>{
newView.init();
newView.baseShow(viewData);
newView.show();
this._layerManager.addLayerToView(newView);
resolve();
}).catch(()=>{
this._openDic.remove(newView);
newView.destroy();
newView = null;
reject();
});
});
}
/**
* 根据视图关闭
* @param view 视图实例
*/
public closeView(view: BaseView){
let wantToCloseView = this._openDic.firstOrDefault(a => {
return a == view;
})
if (wantToCloseView) {
this._openDic.remove(wantToCloseView);
wantToCloseView.destroy();
wantToCloseView = null;
}
}
/**
* 关闭所有视图
*/
public closeAll(){
for (let z = 0; z < this._openDic.length; z++) {
let element = this._openDic[z];
let canClose = true;
for (let i = 0; i < this._skipCloseDic.length; i ++){
if (element == this.getView(this._skipCloseDic[i])){
canClose = false;
}
}
if (canClose) {
this.closeView(element);
z--;
}
}
}
/**
* 根据视图类型关闭窗口
* @param viewCls 视图类型
*/
public closeViewByCls(viewCls){
let wantToCloseView = this._openDic.firstOrDefault(a => {
return a instanceof viewCls;
})
if (wantToCloseView) {
this._openDic.remove(wantToCloseView);
wantToCloseView.destroy();
wantToCloseView = null;
}
}
/**
* 根据视图类型获得窗口
* 如果窗口未打开则返回null
* @param viewCls 视图类型
*/
public getView<T>(viewCls: new () => T): T{
let result: any = this._openDic.firstOrDefault(a => {
return a instanceof viewCls;
});
return result as T;
}
/**
* 根据视图类型获得窗口是否打开
* @param viewCls
*/
public isOpenView(viewCls: any): boolean {
return this._openDic.findIndex(a => {
return a instanceof viewCls;
}) != -1;
}
}

View File

@@ -0,0 +1,27 @@
module sc {
export class ScControl {
private _eventManager: EventManager;
private _viewManager: ViewManager;
private static single: ScControl;
public static getInstance(){
if (!this.single) this.single = new ScControl();
return this.single;
}
constructor(){
this.addEvents();
}
private addEvents(){
this._eventManager = EventManager.getInstance();
this._viewManager = ViewManager.getInstance();
// 事件监听
this._eventManager.addListener(ScEvents.OPENVIEW, this.openView, this);
}
private openView(){
this._viewManager.openView(ScView);
}
}
}

View File

@@ -0,0 +1,5 @@
module sc {
export module ScEvents {
export const OPENVIEW = Symbol();
}
}

55
demo/src/UI/sc/ScView.ts Normal file
View File

@@ -0,0 +1,55 @@
module sc {
export class ScView extends BaseView {
private _ui: FUI.sc.UI_View_sc;
private _sceneList: SceneData[] = [
new SceneData("空白场景", samples.BasicScene),
new SceneData("Tiled Tiles", samples.AnimatedTilesScene),
];
constructor() {
super("sc");
}
public init() {
this._ui = this._fuiView = FUI.sc.UI_View_sc.createInstance();
this.addChild(this._ui.displayObject);
super.init();
}
public show() {
this._ui.m_list_sc.callbackThisObj = this;
this._ui.m_list_sc.itemRenderer = this.scItemRender;
this._ui.m_list_sc.numItems = this._sceneList.length;
}
public scItemRender(index: number, item: FUI.sc.UI_btn_sc){
let sceneData = this._sceneList[index];
item.m_name.text = sceneData.name;
item.data = sceneData.type;
item.addClickListener(this.scItemOnClick, this);
}
private scItemOnClick(evt: egret.Event){
let data = evt.currentTarget.data;
es.Core.scene = new data();
}
public destroy() {
if (this._ui) {
this._ui.dispose();
this._ui = null;
}
super.destroy();
}
}
export class SceneData {
public name: string;
public type: any;
constructor(name: string, type: any){
this.name = name;
this.type = type;
}
}
}