138 lines
3.9 KiB
Plaintext
138 lines
3.9 KiB
Plaintext
|
|
/*
|
||
|
|
文件名:NumberInputBox.nut
|
||
|
|
路径:User/UI/Widget/NumberInputBox.nut
|
||
|
|
创建日期:2025-02-07 20:31
|
||
|
|
文件用途:数字输入框
|
||
|
|
*/
|
||
|
|
class Yosin_NumberInputBox extends Yosin_CommonUi {
|
||
|
|
//框背景
|
||
|
|
BoxBackground = null;
|
||
|
|
//文本
|
||
|
|
TextObject = null;
|
||
|
|
Text = "0";
|
||
|
|
//输入光标
|
||
|
|
InputCursor = null;
|
||
|
|
//输入位置
|
||
|
|
InputPos = 0;
|
||
|
|
//是否获取焦点
|
||
|
|
IsFocus = false;
|
||
|
|
|
||
|
|
|
||
|
|
constructor(w, h) {
|
||
|
|
base.constructor(0, 0, w, h);
|
||
|
|
|
||
|
|
//输入框
|
||
|
|
BoxBackground = Yosin_NineBoxStretch(w, h, "sprite/interface/newstyle/windows/chatting/chatting_ver4.img", 265);
|
||
|
|
BoxBackground.SetPosition(0, 0);
|
||
|
|
Addchild(BoxBackground);
|
||
|
|
|
||
|
|
//文本
|
||
|
|
TextObject = Yosin_Mininumber(null);
|
||
|
|
TextObject.SetPosition(6, 5);
|
||
|
|
Addchild(TextObject);
|
||
|
|
|
||
|
|
|
||
|
|
//输入光标
|
||
|
|
InputCursor = CreateCursor();
|
||
|
|
InputCursor.SetUpdateFunc(function(Object, Dt) {
|
||
|
|
//处于焦点中执行
|
||
|
|
if (IsFocus) {
|
||
|
|
//光标闪烁逻辑
|
||
|
|
{
|
||
|
|
if (!(Object.Var.rawin("TimeFlag"))) {
|
||
|
|
Object.Var.TimeFlag <- 0;
|
||
|
|
Object.Var.VisibleFlag <- false;
|
||
|
|
}
|
||
|
|
Object.Var.TimeFlag += Dt;
|
||
|
|
if (Object.Var.TimeFlag >= 500) {
|
||
|
|
Object.Var.TimeFlag <- 0;
|
||
|
|
Object.SetVisible(Object.Var.VisibleFlag);
|
||
|
|
Object.Var.VisibleFlag <- !Object.Var.VisibleFlag;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//同步光标位置逻辑
|
||
|
|
{
|
||
|
|
Object.SetPosition(TextObject.X + TextObject.DrawTextWidth, 4);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}.bindenv(this));
|
||
|
|
Addchild(InputCursor);
|
||
|
|
|
||
|
|
_Imm_Input_Func_.rawset(C_Object, Imm_Input.bindenv(this));
|
||
|
|
}
|
||
|
|
|
||
|
|
//判断是否中文字符
|
||
|
|
function IsChineseChar(code) {
|
||
|
|
return (code & 0x80) != 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
//接收文本数据
|
||
|
|
function Imm_Input(str) {
|
||
|
|
if (!this) return -1;
|
||
|
|
if (!IsFocus) return;
|
||
|
|
//退格键
|
||
|
|
if (str == "\b") {
|
||
|
|
if (this.Text.len() > 0) {
|
||
|
|
this.Text = Sq_RemoveStringLast(this.Text);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//只记录数字
|
||
|
|
else if (regexp("[0-9]").match(str)) {
|
||
|
|
if (this.Text.tointeger()< 9999999999999) {
|
||
|
|
if (this.Text == "0") this.Text = str;
|
||
|
|
else this.Text += str;
|
||
|
|
} else this.Text = "9999999999999";
|
||
|
|
}
|
||
|
|
|
||
|
|
//同步数字数据
|
||
|
|
TextObject.SetNumber(this.Text);
|
||
|
|
//每次设置文本时确保光标显示
|
||
|
|
InputCursor.SetVisible(true);
|
||
|
|
InputCursor.Var.TimeFlag <- 0;
|
||
|
|
InputCursor.Var.VisibleFlag <- false;
|
||
|
|
}
|
||
|
|
|
||
|
|
function CreateCursor() {
|
||
|
|
local Canvas = CL_CanvasObject();
|
||
|
|
// 重设大小并清空
|
||
|
|
Canvas.ResizeAndClear(1, 15);
|
||
|
|
// 开始绘制
|
||
|
|
Canvas.BeginDraw();
|
||
|
|
|
||
|
|
Canvas.SetFillBrush(sq_RGBA(255, 255, 255, 250));
|
||
|
|
Canvas.SetStrokeBrush(sq_RGBA(255, 255, 255, 250));
|
||
|
|
Canvas.DrawLine(1, 1, 1, 10);
|
||
|
|
// 结束绘制
|
||
|
|
Canvas.EndDraw();
|
||
|
|
Canvas.SetVisible(false);
|
||
|
|
return Canvas;
|
||
|
|
}
|
||
|
|
|
||
|
|
//鼠标左键单击回调
|
||
|
|
function OnMouseLbClick(MousePos_X, MousePos_Y, WindowInteractiveFlag) {
|
||
|
|
local Pos = GetWorldPosition();
|
||
|
|
if (Math.IsIntersectRect(MousePos_X, MousePos_Y, 1, 1, Pos.x, Pos.y, Width, Height)) {
|
||
|
|
SetFocus(true);
|
||
|
|
} else {
|
||
|
|
SetFocus(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//设置焦点模式
|
||
|
|
function SetFocus(Flag) {
|
||
|
|
IsFocus = Flag;
|
||
|
|
InputCursor.SetVisible(Flag);
|
||
|
|
Sq_SetImmEnabled(Flag);
|
||
|
|
}
|
||
|
|
|
||
|
|
//设置数字
|
||
|
|
function SetNumber(Number) {
|
||
|
|
if (Number< 0) return;
|
||
|
|
this.Text = Number.tostring();
|
||
|
|
TextObject.SetNumber(this.Text);
|
||
|
|
}
|
||
|
|
//获取数字
|
||
|
|
function GetNumber() {
|
||
|
|
return this.Text.tointeger();
|
||
|
|
}
|
||
|
|
}
|