Commit 9194fbdb authored by 范雪寒's avatar 范雪寒

feat:

parent 396d96dd
...@@ -5,6 +5,7 @@ import { initResMgr_NGT_02 } from "../manager/res_mgr"; ...@@ -5,6 +5,7 @@ import { initResMgr_NGT_02 } from "../manager/res_mgr";
import { initSndMgr_NGT_02 } from "../manager/snd_mgr"; import { initSndMgr_NGT_02 } from "../manager/snd_mgr";
import { initUtils_NGT_02 } from "./utils"; import { initUtils_NGT_02 } from "./utils";
import { initEnumMgr_NGT_02 } from "../game/speaker"; import { initEnumMgr_NGT_02 } from "../game/speaker";
// 全局环境预声明 // 全局环境预声明
export function initGlobal_NGT_02() { export function initGlobal_NGT_02() {
console.log('window.g init'); console.log('window.g init');
......
import { initGlobal_NGT_02 } from "./_preDefine";
/** /**
* 适配 * 适配
*/ */
cc.Class({ cc.Class({
extends: cc.Component, extends: cc.Component,
properties: { properties: {
UIMax: { UIMax: {
default: [], default: [],
type: cc.Node, type: cc.Node,
displayName: "等比放大" displayName: "等比放大",
}, },
topUI: { topUI: {
default: [], default: [],
type: cc.Widget, type: cc.Widget,
}, },
bgScaleMax: { bgScaleMax: {
default: [], default: [],
type: cc.Node, type: cc.Node,
displayName: "背景适配" displayName: "背景适配",
}, },
nodeUIOffset: { nodeUIOffset: {
default: [], default: [],
type: cc.Node, type: cc.Node,
displayName: "节点偏移" displayName: "节点偏移",
}, },
canvasView: { canvasView: {
default: null, default: null,
type: cc.Canvas, type: cc.Canvas,
displayName: "根节点" displayName: "根节点",
}, },
isCanvas: { isCanvas: {
default: false, default: false,
} },
}, },
onLoad: function () { //开启适配 onLoad: function () {
this.widgetList = []; initGlobal_NGT_02();
this.scheduleOnce(function () { //开启适配
if (!this.isCanvas) { this.widgetList = [];
this.init();
} else { this.scheduleOnce(function () {
var canvaSize = this.findCanvas(); if (!this.isCanvas) {
var w = (canvaSize.width) / 720; this.init();
var h = (canvaSize.height) / 1280; } else {
this.MaxSize = w / h > 1 ? w / h : h / w; var canvaSize = this.findCanvas();
var w = canvaSize.width / 720;
this.setUIMax(this.MaxSize); var h = canvaSize.height / 1280;
this.settopUI(this.MaxSize); this.MaxSize = w / h > 1 ? w / h : h / w;
this.setBgScale();
this.setUiPositon(); this.setUIMax(this.MaxSize);
var scene = cc.director.getScene(); this.settopUI(this.MaxSize);
this.setBgScale();
var list = scene.getComponentsInChildren(cc.Widget) this.setUiPositon();
for (var i in list) { var scene = cc.director.getScene();
list[i].updateAlignment();
} var list = scene.getComponentsInChildren(cc.Widget);
} for (var i in list) {
}, 0); list[i].updateAlignment();
}
g.event_mgr.reg("adjustUI", () => { }
this.setBgScale(); }, 0);
this.setUiPositon();
}); g.event_mgr.reg("adjustUI", () => {
this.MaxSize = 1; this.setBgScale();
}, this.setUiPositon();
init: function () { });
var canvaSize = this.findCanvas(); this.MaxSize = 1;
cc.director._scene.width = canvaSize.width; },
cc.director._scene.height = canvaSize.height; init: function () {
var canvaSize = this.findCanvas();
var diff = canvaSize.width / canvaSize.height; cc.director._scene.width = canvaSize.width;
var bili = 750 / 1334; cc.director._scene.height = canvaSize.height;
if (diff > bili) {
this.canvasView.fitWidth = false; var diff = canvaSize.width / canvaSize.height;
this.canvasView.fitHeight = true; var bili = 750 / 1334;
} else if (diff < bili) { if (diff > bili) {
this.canvasView.fitWidth = true; this.canvasView.fitWidth = false;
this.canvasView.fitHeight = false; this.canvasView.fitHeight = true;
} else { } else if (diff < bili) {
this.canvasView.fitWidth = true; this.canvasView.fitWidth = true;
this.canvasView.fitHeight = true; this.canvasView.fitHeight = false;
} } else {
var w = (canvaSize.width) / 750; this.canvasView.fitWidth = true;
var h = (canvaSize.height) / 1334; this.canvasView.fitHeight = true;
this.MaxSize = w / h > 1 ? w / h : h / w; }
var w = canvaSize.width / 750;
g.data_mgr.MaxSize = this.MaxSize; var h = canvaSize.height / 1334;
this.setUIMax(this.MaxSize); this.MaxSize = w / h > 1 ? w / h : h / w;
this.settopUI(this.MaxSize);
this.setBgScale(); g.data_mgr.MaxSize = this.MaxSize;
this.setUiPositon(); this.setUIMax(this.MaxSize);
var scene = cc.director.getScene(); this.settopUI(this.MaxSize);
var list = scene.getComponentsInChildren(cc.Widget) this.setBgScale();
for (var i in list) { this.setUiPositon();
list[i].updateAlignment(); var scene = cc.director.getScene();
} var list = scene.getComponentsInChildren(cc.Widget);
}, for (var i in list) {
list[i].updateAlignment();
onDestroy: function () { }
if (g.event_pump) { },
g.event_pump.unReg("adjustUI");
} onDestroy: function () {
}, if (g.event_pump) {
settopUI: function (s) { g.event_pump.unReg("adjustUI");
if (!g.data_mgr.phoneInfo) { }
return; },
} settopUI: function (s) {
var top = g.data_mgr.phoneInfo; if (!g.data_mgr.phoneInfo) {
top = top * s; return;
for (var i in this.topUI) { }
if (this.topUI[i].perTop == undefined) { var top = g.data_mgr.phoneInfo;
this.topUI[i].perTop = this.topUI[i].top; top = top * s;
for (var i in this.topUI) {
} else { if (this.topUI[i].perTop == undefined) {
this.topUI[i].top = this.topUI[i].perTop; this.topUI[i].perTop = this.topUI[i].top;
} } else {
this.topUI[i].top += top; this.topUI[i].top = this.topUI[i].perTop;
console.log(this.topUI[i].top); }
} this.topUI[i].top += top;
}, console.log(this.topUI[i].top);
}
//背景适配 },
setBgScale: function () {
for (var i in this.bgScaleMax) { //背景适配
// 1. 先找到 SHOW_ALL 模式适配之后,本节点的实际宽高以及初始缩放值 setBgScale: function () {
let scaleForShowAll = Math.min( for (var i in this.bgScaleMax) {
cc.view.getCanvasSize().width / this.bgScaleMax[i].width, // 1. 先找到 SHOW_ALL 模式适配之后,本节点的实际宽高以及初始缩放值
cc.view.getCanvasSize().height / this.bgScaleMax[i].height let scaleForShowAll = Math.min(
); cc.view.getCanvasSize().width / this.bgScaleMax[i].width,
let realWidth = this.bgScaleMax[i].width * scaleForShowAll; cc.view.getCanvasSize().height / this.bgScaleMax[i].height
let realHeight = this.bgScaleMax[i].height * scaleForShowAll; );
let realWidth = this.bgScaleMax[i].width * scaleForShowAll;
// 2. 基于第一步的数据,再做缩放适配 let realHeight = this.bgScaleMax[i].height * scaleForShowAll;
this.bgScaleMax[i].scale = Math.max(
cc.view.getCanvasSize().width / realWidth, // 2. 基于第一步的数据,再做缩放适配
cc.view.getCanvasSize().height / realHeight this.bgScaleMax[i].scale = Math.max(
); cc.view.getCanvasSize().width / realWidth,
} cc.view.getCanvasSize().height / realHeight
}, );
}
//适配节点位置 },
setUiPositon: function () {
for (var i in this.nodeUIOffset) { //适配节点位置
// 1. 先找到 SHOW_ALL 模式适配之后,本节点的实际宽高以及初始缩放值 setUiPositon: function () {
let srcScaleForShowAll = Math.min( for (var i in this.nodeUIOffset) {
cc.view.getCanvasSize().width / 1280, // 1. 先找到 SHOW_ALL 模式适配之后,本节点的实际宽高以及初始缩放值
cc.view.getCanvasSize().height / 720 let srcScaleForShowAll = Math.min(
); cc.view.getCanvasSize().width / 1280,
let realWidth = 1280 * srcScaleForShowAll; cc.view.getCanvasSize().height / 720
let realHeight = 720 * srcScaleForShowAll; );
let realWidth = 1280 * srcScaleForShowAll;
// 2. 基于第一步的数据,再做节点宽高重置 let realHeight = 720 * srcScaleForShowAll;
this.nodeUIOffset[i].width = 1280 * (cc.view.getCanvasSize().width / realWidth);
this.nodeUIOffset[i].height = 720 * (cc.view.getCanvasSize().height / realHeight); // 2. 基于第一步的数据,再做节点宽高重置
} this.nodeUIOffset[i].width =
}, 1280 * (cc.view.getCanvasSize().width / realWidth);
this.nodeUIOffset[i].height =
setUIMax: function (size) { 720 * (cc.view.getCanvasSize().height / realHeight);
for (var i in this.UIMax) { }
if (this.UIMax[i].perScale == undefined) { },
this.UIMax[i].perScale = this.UIMax[i].scaleX;
setUIMax: function (size) {
} else { for (var i in this.UIMax) {
this.UIMax[i].scaleX = this.UIMax[i].perScale; if (this.UIMax[i].perScale == undefined) {
this.UIMax[i].scaleY = this.UIMax[i].perScale; this.UIMax[i].perScale = this.UIMax[i].scaleX;
} } else {
this.UIMax[i].scaleX *= size; this.UIMax[i].scaleX = this.UIMax[i].perScale;
this.UIMax[i].scaleY *= size; this.UIMax[i].scaleY = this.UIMax[i].perScale;
} }
}, this.UIMax[i].scaleX *= size;
findCanvas: function () { this.UIMax[i].scaleY *= size;
if (cc.sys.isNative) { }
return { },
width: cc.view.getFrameSize().width, findCanvas: function () {
height: cc.view.getFrameSize().height if (cc.sys.isNative) {
}; return {
} else { width: cc.view.getFrameSize().width,
return { height: cc.view.getFrameSize().height,
width: cc.game.canvas.clientWidth, };
height: cc.game.canvas.clientHeight } else {
}; return {
} width: cc.game.canvas.clientWidth,
} height: cc.game.canvas.clientHeight,
};
}
},
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment