update
This commit is contained in:
@@ -4,11 +4,10 @@ import { DialogBubble } from "./DialogBubble";
|
||||
import { Dialog } from "./DemoData";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
const BUTTOM_Y = -346.354;
|
||||
const BUTTOM_Y = -955.571;
|
||||
|
||||
@ccclass("ChatContentsLayout")
|
||||
export class ChatContentsLayout extends Component {
|
||||
@property(DemoManager)
|
||||
manager: DemoManager = null;
|
||||
|
||||
@property(DialogBubble)
|
||||
@@ -21,6 +20,11 @@ export class ChatContentsLayout extends Component {
|
||||
protected start(): void {
|
||||
this.lBubble.node.active = false;
|
||||
this.rBubble.node.active = false;
|
||||
}
|
||||
|
||||
protected onEnable(): void {
|
||||
if (!this.manager) this.manager = DemoManager.getInstance();
|
||||
|
||||
this.manager.layoutout = this;
|
||||
}
|
||||
|
||||
@@ -43,11 +47,13 @@ export class ChatContentsLayout extends Component {
|
||||
|
||||
newBubbleNode.setParent(this.node);
|
||||
newBubble.node.active = true;
|
||||
|
||||
let offset = newBubble.updateBubbleContent(dialog.content);
|
||||
|
||||
let pos = newBubble.node.position;
|
||||
newBubble.node.position = new Vec3(pos.x, initPosY, pos.z);
|
||||
|
||||
let offset = newBubble.updateBubbleContent(dialog.content);
|
||||
initPosY += offset + 40;
|
||||
initPosY += offset + 20;
|
||||
this.bubbles.push(newBubble);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import { _decorator, Component, EditBox, Label, Node } from "cc";
|
||||
import { l10n } from "db://localization-editor/l10n";
|
||||
import { DemoManager } from "./DemoManager";
|
||||
const { ccclass, property } = _decorator;
|
||||
import { _decorator, Component } from "cc";
|
||||
|
||||
@ccclass("ChatGPTService")
|
||||
export class ChatGPTService extends Component {
|
||||
@property(DemoManager)
|
||||
manager: DemoManager = null;
|
||||
export class ChatGPTService {
|
||||
private static _instance: ChatGPTService;
|
||||
|
||||
@property(EditBox)
|
||||
editBox: EditBox = null;
|
||||
public static getInstance() {
|
||||
if (!this._instance) {
|
||||
this._instance = new ChatGPTService();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
private apiurl: string = "https://cloud.fastgpt.cn/api/v1/chat/completions";
|
||||
private apikey: string =
|
||||
@@ -36,39 +35,6 @@ export class ChatGPTService extends Component {
|
||||
xhr.send(JSON.stringify(data));
|
||||
});
|
||||
}
|
||||
|
||||
protected async start(): Promise<void> {
|
||||
// console.log(l10n.t("hello"));
|
||||
// l10n.changeLanguage("zh-Hans-CN");
|
||||
// console.log(l10n.t("hello"));
|
||||
// l10n.changeLanguage("en");
|
||||
// console.log(l10n.t("hello"));
|
||||
}
|
||||
|
||||
public async OnClickSend() {
|
||||
const str = this.editBox.string;
|
||||
if (!str || str == "") return;
|
||||
console.log("post:" + str);
|
||||
|
||||
if (this.manager) {
|
||||
this.manager.updateDialog(true, str);
|
||||
}
|
||||
|
||||
let ret = await this.Post({
|
||||
model: "llama3.1:8b",
|
||||
messages: [{ role: "user", content: str }],
|
||||
temperature: 0.8,
|
||||
});
|
||||
|
||||
const rep = ret.choices[0].message.content;
|
||||
if (rep == null) {
|
||||
console.warn("rep null");
|
||||
}
|
||||
if (this.manager) {
|
||||
this.manager.updateDialog(false, rep);
|
||||
}
|
||||
console.log("ret:" + rep);
|
||||
}
|
||||
}
|
||||
|
||||
export type GPTResquest = {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { _decorator, Component, EditBox, Node } from "cc";
|
||||
import { DemoManager } from "./DemoManager";
|
||||
import { ChatGPTService } from "./ChatGPTService";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("ChatPanel")
|
||||
export class ChatPanel extends Component {
|
||||
manager: DemoManager = null;
|
||||
|
||||
@property(EditBox)
|
||||
editBox: EditBox = null;
|
||||
|
||||
protected onEnable(): void {
|
||||
if (!this.manager) this.manager = DemoManager.getInstance();
|
||||
}
|
||||
|
||||
public async OnClickSend() {
|
||||
const str = this.editBox.string;
|
||||
if (!str || str == "") return;
|
||||
console.log("post:" + str);
|
||||
|
||||
this.editBox.string = "";
|
||||
DemoManager.getInstance().updateDialog(true, str);
|
||||
|
||||
let ret = await ChatGPTService.getInstance().Post({
|
||||
model: "llama3.1:8b",
|
||||
messages: [{ role: "user", content: str }],
|
||||
temperature: 0.8,
|
||||
});
|
||||
|
||||
const rep = ret.choices[0].message.content;
|
||||
if (rep == null) {
|
||||
console.warn("rep null");
|
||||
}
|
||||
if (this.manager) {
|
||||
this.manager.updateDialog(false, rep);
|
||||
}
|
||||
console.log("ret:" + rep);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "21603c89-8340-4ef7-90c2-abc81f7d915c",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,16 +1,32 @@
|
||||
import { _decorator, Component, Node } from "cc";
|
||||
import { DemoData, Dialog } from "./DemoData";
|
||||
import { find } from "cc";
|
||||
import { ChatContentsLayout } from "./ChatContentsLayout";
|
||||
const { ccclass, property } = _decorator;
|
||||
import { DemoData } from "./DemoData";
|
||||
|
||||
export class DemoManager {
|
||||
private static _instance: DemoManager;
|
||||
public static getInstance() {
|
||||
if (!this._instance) {
|
||||
this._instance = new DemoManager();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
private constructor() {
|
||||
this.demoData = new DemoData();
|
||||
}
|
||||
|
||||
@ccclass("DemoManager")
|
||||
export class DemoManager extends Component {
|
||||
demoData: DemoData;
|
||||
|
||||
public layoutout: ChatContentsLayout;
|
||||
|
||||
start() {
|
||||
this.demoData = new DemoData();
|
||||
EnterChat(id: number) {
|
||||
find("Canvas/ChatPanel").active = true;
|
||||
find("Canvas/GirlDetailPanel").active = false;
|
||||
}
|
||||
|
||||
EnterDetail(id: number) {
|
||||
find("Canvas/GirlListPanel").active = false;
|
||||
find("Canvas/GirlDetailPanel").active = true;
|
||||
}
|
||||
|
||||
public updateDialog(isPlayer: boolean, str: string) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { _decorator, Component, Label, Node, Size, UITransform } from "cc";
|
||||
import Tools from "./tools";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("DialogBubble")
|
||||
@@ -11,13 +12,46 @@ export class DialogBubble extends Component {
|
||||
contentT: UITransform = null;
|
||||
|
||||
updateBubbleContent(str: string) {
|
||||
const lines = str.split("\n");
|
||||
let len = 0;
|
||||
let index = 0;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const element = lines[i];
|
||||
if (element.length > len) {
|
||||
len = element.length;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
let finalLen = 0;
|
||||
// for (let i = 0; i < lines[index].length; i++) {
|
||||
// const element = lines[index][i];
|
||||
// if ("\u4e00" <= element[i] && element[i] <= "\u9fff") {
|
||||
// finalLen += 35;
|
||||
// } else {
|
||||
// finalLen += 18;
|
||||
// }
|
||||
// }
|
||||
|
||||
if (Tools.IsChinese(lines[index])) {
|
||||
finalLen = 35 * lines[index].length;
|
||||
} else {
|
||||
finalLen = 21 * lines[index].length;
|
||||
}
|
||||
|
||||
this.content.string = str;
|
||||
this.contentT.setContentSize(
|
||||
new Size(Math.min(finalLen, 950), this.contentT.contentSize.y)
|
||||
);
|
||||
this.content.updateRenderData();
|
||||
|
||||
this.bg.setContentSize(
|
||||
new Size(this.contentT.contentSize.x + 5, this.contentT.contentSize.y + 5)
|
||||
new Size(
|
||||
this.contentT.contentSize.x + 30,
|
||||
this.contentT.contentSize.y + 20
|
||||
)
|
||||
);
|
||||
|
||||
return this.bg.contentSize.y / 2;
|
||||
return this.bg.contentSize.y;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { _decorator, Component, Label, Node, Sprite } from "cc";
|
||||
import { DemoManager } from "./DemoManager";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("GirlDetailPanel")
|
||||
export class GirlDetailPanel extends Component {
|
||||
@property(Sprite)
|
||||
avatar: Sprite;
|
||||
@property(Label)
|
||||
girlName: Label;
|
||||
@property(Label)
|
||||
desc: Label;
|
||||
|
||||
@property(Node)
|
||||
chatBtn: Node;
|
||||
id = 0;
|
||||
start() {
|
||||
this.id = 1;
|
||||
}
|
||||
OnClickChatBtn() {
|
||||
DemoManager.getInstance().EnterChat(this.id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ded86cbd-223c-467b-873f-3a8ac30222a9",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "35555c27-2be1-4310-9685-8944c2895a50",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import {
|
||||
_decorator,
|
||||
Component,
|
||||
find,
|
||||
Label,
|
||||
Node,
|
||||
NodeEventType,
|
||||
Sprite,
|
||||
} from "cc";
|
||||
import { DemoManager } from "../DemoManager";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("GirlListItem")
|
||||
export class GirlListItem extends Component {
|
||||
@property(Sprite)
|
||||
avatar: Sprite;
|
||||
@property(Label)
|
||||
girlName: Label;
|
||||
@property(Label)
|
||||
desc: Label;
|
||||
@property(Label)
|
||||
price: Label;
|
||||
@property(Node)
|
||||
chatBtn: Node;
|
||||
|
||||
id: number = -1;
|
||||
start() {
|
||||
// this.avatar = this.node
|
||||
// .getChildByName("avatar/Avatar")
|
||||
// .getComponent(Sprite);
|
||||
// this.girlName = this.node.getChildByName("Desc/Name").getComponent(Label);
|
||||
// this.desc = this.node.getChildByName("Desc/desc").getComponent(Label);
|
||||
// this.price = this.node.getChildByName("purchase/price").getComponent(Label);
|
||||
// this.chatBtn = this.node.getChildByName("purchase/Button");
|
||||
|
||||
this.id = 1;
|
||||
}
|
||||
|
||||
refreshData() {}
|
||||
|
||||
onClickDetail() {
|
||||
DemoManager.getInstance().EnterDetail(this.id);
|
||||
}
|
||||
|
||||
update(deltaTime: number) {}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "72a618a4-f0ec-4a35-a867-e0583cd4b931",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { _decorator, Component, Node } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('GirlListPanel')
|
||||
export class GirlListPanel extends Component {
|
||||
start() {
|
||||
|
||||
}
|
||||
|
||||
update(deltaTime: number) {
|
||||
|
||||
}
|
||||
}
|
||||
␍
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "879c46b0-1dbd-4038-a620-b162fd51034a",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
export default class Tools {
|
||||
private static chineseReg: RegExp;
|
||||
public static IsChinese(s: string): boolean {
|
||||
if (!this.chineseReg) {
|
||||
this.chineseReg = new RegExp("^[\u4E00-\u9FFFF]+$");
|
||||
}
|
||||
if (!this.chineseReg.test(s)) {
|
||||
return false;
|
||||
} else return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "1f5a82c7-aaf4-49e9-a874-58d6cda51644",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user