550 lines
13 KiB
Vue
Raw Normal View History

2019-03-15 10:08:39 +08:00
<template>
2021-04-04 15:42:16 +08:00
<div id="ui-prop">
2024-01-09 12:02:47 +08:00
<div
class="normal-data"
style="
display: flex;
flex-direction: row;
align-items: center;
min-height: 30px;
margin: 0;
"
>
<div
@mousedown="onPropNameMouseDown"
class="key"
@click="onClickFold"
:style="{ cursor: isArrayOrObject() ? 'pointer' : '' }"
2021-06-17 22:35:37 +08:00
>
2024-01-09 12:02:47 +08:00
<i
class="data-arrow"
v-if="arrow"
:class="fold ? 'el-icon-caret-right' : 'el-icon-caret-bottom'"
:style="{
visibility: isArrayOrObject() ? 'visible' : 'hidden',
'margin-left': indent * 10 + 'px',
}"
>
2021-06-17 21:37:09 +08:00
</i>
2021-11-06 20:43:45 +08:00
<div class="text" ref="propText">
2024-01-09 15:33:34 +08:00
<!-- 使用CCProp -->
<!-- <el-popover
2024-01-09 12:02:47 +08:00
placement="top"
trigger="hover"
:disabled="!isShowTooltip()"
>
<div>{{ name }}</div>
2024-01-09 12:02:47 +08:00
<span>{{ name }}</span>
2024-01-09 15:33:34 +08:00
</el-popover> -->
</div>
2021-04-04 15:42:16 +08:00
</div>
2021-06-15 22:19:47 +08:00
<div class="value">
2024-01-09 15:33:34 +08:00
<div v-if="value.isInvalid()" class="invalid">
2021-11-07 00:09:02 +08:00
{{ value.data }}
2021-11-06 21:42:37 +08:00
</div>
2024-01-09 15:33:34 +08:00
<CCInput
v-if="value.isString()"
2024-01-09 12:02:47 +08:00
v-model="value.data"
:disabled="value.readonly"
@change="onChangeValue"
>
2024-01-09 15:33:34 +08:00
</CCInput>
<CCInput
v-if="value.isText()"
2024-01-09 12:02:47 +08:00
type="textarea"
:autosize="{ minRows: 3, maxRows: 5 }"
placeholder="请输入内容"
:disabled="value.readonly"
@change="onChangeValue"
v-model="value.data"
>
2024-01-09 15:33:34 +08:00
</CCInput>
<CCInputNumber
v-if="value.isNumber()"
2024-01-09 12:02:47 +08:00
style="width: 100%; text-align: left"
v-model="value.data"
:step="step"
:disabled="value.readonly"
@change="onChangeValue"
controls-position="right"
2024-01-09 15:33:34 +08:00
></CCInputNumber>
2021-06-15 22:19:47 +08:00
2024-01-09 15:33:34 +08:00
<div v-if="value.isVec2() || value.isVec3()" class="vec">
2024-01-09 12:02:47 +08:00
<ui-prop
v-for="(vec, index) in value.data"
:key="index"
:arrow="false"
:value="vec.value"
:name="vec.name"
>
2021-06-15 22:19:47 +08:00
</ui-prop>
</div>
2024-01-09 15:33:34 +08:00
<CCSelect
2024-01-09 12:02:47 +08:00
v-model="value.data"
:disabled="value.readonly"
2024-01-09 15:33:34 +08:00
:data="getEnumValues(value)"
v-if="value.isEnum()"
2024-01-09 12:02:47 +08:00
style="width: 100%"
@change="onChangeValue"
>
2024-01-09 15:33:34 +08:00
</CCSelect>
<CCCheckBox
2024-01-09 12:02:47 +08:00
v-model="value.data"
2024-01-09 15:33:34 +08:00
v-if="value.isBool()"
2024-01-09 12:02:47 +08:00
:disabled="value.readonly"
@change="onChangeValue"
>
2024-01-09 15:33:34 +08:00
</CCCheckBox>
<div class="color" v-if="value.isColor()">
<CCColor
2024-01-09 12:02:47 +08:00
style="position: absolute"
:disabled="value.readonly"
v-model="value.data"
@change="onChangeValue"
>
2024-01-09 15:33:34 +08:00
</CCColor>
2024-01-09 12:02:47 +08:00
<div class="hex" :style="{ color: colorReverse(value.data) }">
{{ value.data }}
</div>
2021-04-06 18:41:54 +08:00
</div>
2024-01-09 15:33:34 +08:00
<div v-if="value.isImage()" class="image-property">
<!-- TODO: 适配 -->
<div v-if="value.isImage() || true" placement="top" trigger="hover">
2021-06-15 22:19:47 +08:00
<div
2024-01-09 12:02:47 +08:00
style="
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
"
>
<img
:src="value.data"
alt="图片"
style="max-width: 100px; max-height: 100px; object-fit: contain"
/>
2021-06-15 22:19:47 +08:00
</div>
2024-01-09 12:02:47 +08:00
<img :src="value.data" style="height: 36px" alt="图片" />
2024-01-09 15:33:34 +08:00
</div>
2024-01-09 12:02:47 +08:00
<div style="flex: 1; display: flex; flex-direction: row-reverse">
2024-01-09 15:33:34 +08:00
<CCButton @click="onShowValueInConsole">log</CCButton>
2021-06-15 20:51:19 +08:00
</div>
2021-06-15 22:19:47 +08:00
</div>
2024-01-09 15:33:34 +08:00
<div v-if="value.isEngine()" class="engine">
2021-06-19 19:51:05 +08:00
<div class="head">
<i class="icon" :class="getEngineTypeIcon()"></i>
2024-01-09 15:33:34 +08:00
<div class="type">{{ value["engineType"] }}</div>
2021-06-19 19:51:05 +08:00
</div>
2024-01-09 15:33:34 +08:00
<div class="name">{{ value["engineName"] }}</div>
<CCButton
2024-01-09 12:02:47 +08:00
@click="onPlaceInTree"
type="primary"
icon="el-icon-place"
2024-01-09 15:33:34 +08:00
></CCButton>
2021-06-19 19:51:05 +08:00
</div>
2024-01-09 15:33:34 +08:00
<div v-if="value.isObject() && fold" class="objectDesc">
2021-11-06 20:19:21 +08:00
{{ value.data }}
</div>
2024-01-09 15:33:34 +08:00
<div v-if="value.isArray()" class="array">
Array({{ value.data.length }})
</div>
2021-11-06 21:14:38 +08:00
<div class="slot" v-if="false">
2021-06-15 22:19:47 +08:00
<slot></slot>
2021-06-14 19:41:58 +08:00
</div>
2021-04-06 18:41:54 +08:00
</div>
2021-06-15 22:19:47 +08:00
</div>
2021-06-17 22:35:37 +08:00
<div v-if="isArrayOrObject()">
2024-01-09 12:02:47 +08:00
<div
v-show="!fold && subData"
style="display: flex; flex-direction: column"
>
<ui-prop
v-for="(arr, index) in subData"
:key="index"
:indent="indent + 1"
:value="arr.value"
2024-01-09 15:33:34 +08:00
:name="getName(value.isArray(), arr)"
2021-06-17 22:35:37 +08:00
>
2021-06-15 22:19:47 +08:00
</ui-prop>
2019-03-15 10:08:39 +08:00
</div>
</div>
</div>
</template>
2021-04-02 22:34:09 +08:00
<script lang="ts">
2024-01-09 12:02:47 +08:00
import {
defineComponent,
ref,
toRaw,
watch,
onUnmounted,
onMounted,
PropType,
} from "vue";
import { DataType, EngineData, EnumData, Info, Property } from "../data";
import { connectBackground } from "../connectBackground";
import { Msg } from "../../../core/types";
import Bus, { BusMsg } from "../bus";
2024-01-09 15:33:34 +08:00
import { Option } from "@xuyanfeng/cc-ui/types/cc-select/const";
import ccui from "@xuyanfeng/cc-ui";
const { CCInput, CCButton, CCInputNumber, CCSelect, CCCheckBox, CCColor } =
ccui.components;
2024-01-09 12:02:47 +08:00
export default defineComponent({
2021-11-13 12:02:29 +08:00
name: "UiProp",
2024-01-09 15:33:34 +08:00
components: {
CCInput,
CCButton,
CCInputNumber,
CCSelect,
CCCheckBox,
CCColor,
},
2024-01-09 12:02:47 +08:00
props: {
2024-01-09 15:33:34 +08:00
name: { type: String, default: "" },
indent: { type: Number, default: 0 },
arrow: { type: Boolean, default: true },
step: { type: Number, default: 1 },
2024-01-09 12:02:47 +08:00
value: {
type: Object as PropType<Info | EngineData | EnumData>,
default: () => {},
},
},
2024-01-09 15:33:34 +08:00
setup(props, { emit }) {
const { value, step } = props;
const fold = ref(true);
2024-01-09 12:02:47 +08:00
let clientX: number = 0;
onMounted(() => {
watchValue();
});
function watchValue() {
2024-01-09 15:33:34 +08:00
fold.value = true;
if (value.isArray()) {
subData.value = value.data;
2024-01-09 12:02:47 +08:00
} else {
2024-01-09 15:33:34 +08:00
subData.value = null;
2021-06-19 19:51:05 +08:00
}
2021-04-06 18:41:54 +08:00
}
2024-01-09 12:02:47 +08:00
watch(props.value, () => {
watchValue();
});
const subData = ref<Property[]>([]);
2024-01-09 15:33:34 +08:00
const propText = ref<HTMLDivElement>();
function _onMouseMove(event: MouseEvent) {
let x = event.clientX;
let calcStep = step || 0;
if (x > clientX) {
calcStep = Math.abs(calcStep);
} else {
calcStep = -Math.abs(calcStep);
}
emit("movestep", calcStep);
clientX = x;
}
2024-01-09 12:02:47 +08:00
2024-01-09 15:33:34 +08:00
function _onMouseUp(event: MouseEvent) {
document.removeEventListener("mousemove", _onMouseMove);
document.removeEventListener("mouseup", _onMouseUp);
document.removeEventListener("onselectstart", _onSelect);
}
function _onSelect() {
return false;
}
2024-01-09 12:02:47 +08:00
return {
fold,
2024-01-09 15:33:34 +08:00
propText,
2024-01-09 12:02:47 +08:00
subData,
2024-01-09 15:33:34 +08:00
getEnumValues(data: any): Option[] {
const value: EnumData = data;
const ret: Option[] = [];
value.values.map((item) => {
ret.push({
label: item.name,
value: item.value,
});
});
return ret;
2024-01-09 12:02:47 +08:00
},
isArrayOrObject() {
2024-01-09 15:33:34 +08:00
return value && (value.isArray() || value.isObject());
2024-01-09 12:02:47 +08:00
},
isImageValid() {
2024-01-09 15:33:34 +08:00
return !!value.data;
2024-01-09 12:02:47 +08:00
},
onPlaceInTree() {
2024-01-09 15:33:34 +08:00
Bus.emit(BusMsg.ShowPlace, value);
2024-01-09 12:02:47 +08:00
},
isShowTooltip() {
2024-01-09 15:33:34 +08:00
const el: HTMLDivElement = propText.value as HTMLDivElement;
2024-01-09 12:02:47 +08:00
if (el) {
if (el.scrollWidth > el.offsetWidth) {
// 出现了省略号
return true;
}
}
return false;
},
getEngineTypeIcon() {
2024-01-09 15:33:34 +08:00
switch ((value as EngineData).engineType) {
2024-01-09 12:02:47 +08:00
case "cc_Sprite": {
return "el-icon-picture-outline";
}
case "cc_Label": {
return "el-icon-third-text";
}
case "cc_Node": {
return "el-icon-third-node";
}
}
return "el-icon-third-unknow";
},
getName(isArray: boolean, arr: Property) {
const type = arr.value.type;
if (isArray) {
return `[${arr.name}]`;
} else {
return arr.name;
}
},
onClickFold() {
2024-01-09 15:33:34 +08:00
if (value.isObject() && fold && !subData) {
2024-01-09 12:02:47 +08:00
// 请求object的item数据
2024-01-09 15:33:34 +08:00
Bus.emit(BusMsg.RequestObjectData, value, (info: Property[]) => {
fold.value = false;
subData.value = info;
2024-01-09 12:02:47 +08:00
});
} else {
2024-01-09 15:33:34 +08:00
fold.value = !fold.value;
2024-01-09 12:02:47 +08:00
}
},
onShowValueInConsole() {
2024-01-09 15:33:34 +08:00
if (Array.isArray(value.path)) {
let uuid = value.path[0];
let key = value.path[1]; // todo 暂时只支持一级key
2024-01-09 12:02:47 +08:00
if (uuid && key) {
chrome.devtools.inspectedWindow.eval(
`window.CCInspector.logValue('${uuid}','${key}')`
);
}
}
},
onChangeValue() {
2024-01-09 15:33:34 +08:00
if (!value.readonly) {
connectBackground.postMessageToBackground(Msg.SetProperty, value);
2024-01-09 12:02:47 +08:00
}
},
onPropNameMouseDown(event: MouseEvent) {
2024-01-09 15:33:34 +08:00
document.addEventListener("mousemove", _onMouseMove);
document.addEventListener("mouseup", _onMouseUp);
document.addEventListener("onselectstart", _onSelect);
2024-01-09 12:02:47 +08:00
},
colorReverse(OldColorValue: string) {
OldColorValue = "0x" + OldColorValue.replace(/#/g, "");
var str = "000000" + (0xffffff - parseInt(OldColorValue)).toString(16);
return "#" + str.substring(str.length - 6, str.length);
},
2024-01-09 15:33:34 +08:00
_onMouseMove,
_onMouseUp,
_onSelect,
2024-01-09 12:02:47 +08:00
};
},
});
2019-03-15 10:08:39 +08:00
</script>
2021-04-04 15:42:16 +08:00
<style scoped lang="less">
#ui-prop {
min-height: 30px;
2021-06-17 22:35:37 +08:00
margin: 1px 0;
2021-04-04 15:42:16 +08:00
display: flex;
2021-06-15 22:19:47 +08:00
flex-direction: column;
justify-content: center;
2021-04-04 15:42:16 +08:00
2021-06-15 22:19:47 +08:00
.normal-data {
margin: 0;
min-height: 30px;
overflow: hidden;
width: 100%;
2021-04-04 15:42:16 +08:00
display: flex;
flex-direction: row;
align-items: center;
2021-06-15 22:19:47 +08:00
.key {
flex: 1;
float: left;
text-align: left;
2021-04-04 15:42:16 +08:00
display: flex;
flex-direction: row;
2021-04-04 19:04:55 +08:00
align-items: center;
2021-06-15 22:19:47 +08:00
min-width: 90px;
2021-04-04 15:42:16 +08:00
2021-06-15 22:19:47 +08:00
.data-arrow {
width: 20px;
height: 16px;
font-size: 16px;
cursor: pointer;
2021-04-04 15:42:16 +08:00
}
2021-06-15 22:19:47 +08:00
.text {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
2021-06-15 22:19:47 +08:00
user-select: none;
font-size: 12px;
margin: 3px;
2021-11-06 20:19:21 +08:00
span {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
2021-04-04 15:42:16 +08:00
}
}
2021-06-15 22:19:47 +08:00
.value {
flex: 3;
text-align: left;
height: 100%;
2021-04-06 18:41:54 +08:00
overflow: hidden;
2021-06-15 22:19:47 +08:00
min-width: 400px;
2021-04-06 18:41:54 +08:00
2021-06-15 22:19:47 +08:00
.color {
position: relative;
height: 30px;
.hex {
line-height: 30px;
position: relative;
text-align: center;
user-select: none;
pointer-events: none;
}
2021-04-06 18:41:54 +08:00
}
2021-11-06 21:42:37 +08:00
.invalid {
color: grey;
}
2021-11-06 21:14:38 +08:00
.objectDesc {
2021-11-06 20:19:21 +08:00
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
user-select: none;
}
2021-11-06 21:14:38 +08:00
.array {
display: flex;
flex-direction: column;
color: red;
}
2021-06-19 19:51:05 +08:00
.engine {
display: flex;
flex-direction: row;
2024-01-09 12:02:47 +08:00
border: solid #409eff 1px;
2021-06-19 19:51:05 +08:00
border-radius: 5px;
align-items: center;
align-content: center;
.head {
background-color: cornflowerblue;
height: 28px;
align-items: center;
align-content: center;
display: flex;
flex-direction: row;
.icon {
font-size: 20px;
width: 20px;
margin-left: 5px;
}
.type {
display: flex;
align-content: center;
align-items: center;
margin: 0 5px;
}
}
.name {
flex: 1;
height: 28px;
padding-left: 5px;
background-color: gold;
display: flex;
align-items: center;
align-content: center;
}
}
2021-06-15 22:19:47 +08:00
.vec {
width: 100%;
display: flex;
flex-direction: row;
align-items: center;
#ui-prop {
2021-06-17 21:37:09 +08:00
margin: 0 10px;
flex: 1;
2021-06-15 22:19:47 +08:00
.normal-data {
.value {
2021-11-13 11:51:48 +08:00
min-width: 50px;
2021-06-15 22:19:47 +08:00
}
.key {
2021-06-17 21:37:09 +08:00
min-width: unset;
display: block;
margin-right: 5px;
flex: unset;
2021-06-15 22:19:47 +08:00
}
}
}
2021-06-14 19:41:58 +08:00
2021-06-17 21:37:09 +08:00
#ui-prop:first-child {
margin-left: 0;
2021-06-15 22:19:47 +08:00
}
2021-06-17 21:37:09 +08:00
#ui-prop:last-child {
margin-right: 0;
2021-06-15 22:19:47 +08:00
}
2021-06-17 21:37:09 +08:00
}
.array-object {
flex: 1;
max-width: 100%;
overflow: hidden;
display: flex;
flex-direction: row;
align-items: center;
2021-06-15 22:19:47 +08:00
2021-06-17 21:37:09 +08:00
.text {
flex: 1;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
2021-06-15 22:19:47 +08:00
}
}
2021-06-17 21:37:09 +08:00
.image-property {
display: flex;
flex-direction: row;
align-content: center;
align-items: center;
height: 36px;
}
.slot {
display: flex;
width: 100%;
}
2021-04-04 15:42:16 +08:00
}
}
2021-04-01 17:47:56 +08:00
}
2019-03-15 10:08:39 +08:00
</style>