rp_11001/assets/Game/scripts/game/Rules.ts
2025-12-03 16:20:46 +08:00

94 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { _decorator, Component, EventMouse, JsonAsset, Label, Layout, math, Node, ScrollView, UITransform } from 'cc';
import { getAllRichTexts, hideToBottom, isBrahmic, updateLang, wrapTextBySpace } from './Tools';
import { getCsymbol, getLanguage } from 'db://assets/Loading/scripts/comm';
const { ccclass, property } = _decorator;
@ccclass('Rules')
export class Rules extends Component {
@property(JsonAsset)
langJson: JsonAsset | null = null;
dict = null;
scrollView: ScrollView = null
scrollSpeed = 1;
protected onLoad(): void {
this.dict = this.langJson.json[getLanguage()] ?? this.langJson.json["en"];
this.scrollView = this.node.getChildByName("ScrollView").getComponent(ScrollView)
this.scrollView.node.on('scrolling', this.onScrolling, this);
this.node.on(Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
}
protected onDisable(): void {
this.scrollView.node.on('scrolling', this.onScrolling, this);
this.node.off(Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
}
start() {
this.updateLang();
let lyt = this.scrollView.content.getComponent(Layout)
lyt.updateLayout();
lyt.enabled = false;
this.refreshVisibleZone(this.scrollView.getScrollOffset())
}
updateLang() {
let langText = updateLang(this.node, this.dict, getLanguage())
//有一条规则内容要获取货币符号显示
const content = this.node.getChildByName("ScrollView").getComponent(ScrollView).content
const Label_1_3 = content.getChildByName("Label_1_3").getComponent(Label)
let str = Label_1_3.string.replace("{currencySymbol}", getCsymbol())
Label_1_3.string = str
}
clickCloseRules() {
const destroyFunc = () => { this.node.destroy() }
hideToBottom(this.node, destroyFunc)
}
onScrolling(scrollView: ScrollView) {
this.refreshVisibleZone(scrollView.getScrollOffset())
}
// 只显示上下相邻的区域远离view 窗口的content内容都隐藏掉
refreshVisibleZone(offset: math.Vec2) {
let height = this.scrollView.node.getChildByName("view").getComponent(UITransform).height
this.scrollView.content.children.forEach(child => {
let pos = child.position
if (Math.abs(-offset.y - pos.y) < (height + 300)) {
child.active = true
} else {
child.active = false
}
})
}
onMouseWheel(event: EventMouse) {
// console.log("鼠标滚动")
// deltaY > 0向下滚deltaY < 0向上滚
const delta = event.getScrollY() * this.scrollSpeed;
// 获取当前滚动位置
const offset = this.scrollView.getScrollOffset();
let vec2 = offset.add2f(0, -delta)
this.refreshVisibleZone(vec2)
// 更新纵向滚动
this.scrollView.scrollToOffset(vec2, 0.1, true);
}
}