Skip to content

Commit 5468522

Browse files
committed
feat: add support for Header, Label, and Divider elements in GuiAPI.
add SimpleForm.addHeader add SimpleForm.addLabel add SimpleForm.addDivider add CustomForm.addHeader add CustomForm.addDivider update form docs
1 parent 6c615cc commit 5468522

File tree

4 files changed

+203
-40
lines changed

4 files changed

+203
-40
lines changed

docs/apis/GuiAPI/FormBuilder.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@ For each button on the form, set the corresponding icon as follows:
5757
2. If you use a URL path, you can put the full URL here, like `https://www.baidu.com/img/flexible/logo/pc/result.png`
5858
3. If you don't need to display an image for this button, just don't pass in this parameter.
5959

60+
#### Add a Header to the Form
61+
62+
`fm.addHeader(text)`
63+
64+
- Parameters:
65+
- text : `String`
66+
Header.
67+
- Return value: The processed form object (for other operations in the chain).
68+
- Return value type: `SimpleForm`
69+
70+
#### Add a Line of Text to the Form
71+
72+
`fm.addLabel(text)`
73+
74+
- Parameters:
75+
- text : `String`
76+
Line of text.
77+
- Return value: The processed form object (for other operations in the chain).
78+
- Return value type: `SimpleForm`
79+
80+
#### Add a Divider to the Form
81+
82+
`fm.addDivider()`
83+
84+
- Return value: The processed form object (for other operations in the chain).
85+
- Return value type: `SimpleForm`
86+
6087
### Send Form
6188

6289
Finally, when everything is in place, you can send the configured form object to the player and listen for player
@@ -118,6 +145,16 @@ For a specific form object `fm`, the following functions are available:
118145
- Return value: The processed form object (for other operations in the chain).
119146
- Return value type: `CustomForm`
120147

148+
#### Add a Header to the Form
149+
150+
`fm.addHeader(text)`
151+
152+
- Parameters:
153+
- text : `String`
154+
Header.
155+
- Return value: The processed form object (for other operations in the chain).
156+
- Return value type: `CustomForm`
157+
121158
#### Add a Line of Text to the Form
122159

123160
`fm.addLabel(text)`
@@ -128,6 +165,13 @@ For a specific form object `fm`, the following functions are available:
128165
- Return value: The processed form object (for other operations in the chain).
129166
- Return value type: `CustomForm`
130167

168+
#### Add a Divider to the Form
169+
170+
`fm.addDivider()`
171+
172+
- Return value: The processed form object (for other operations in the chain).
173+
- Return value type: `CustomForm`
174+
131175
#### Add a Row of Input Boxes to the Form
132176

133177
`fm.addInput(title[,placeholder,default])`

docs/apis/GuiAPI/FormBuilder.zh.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,35 @@
5757
2. 如果使用URL路径,那么在这里放入完整的URL即可,形如 `https://www.baidu.com/img/flexible/logo/pc/result.png`
5858
3. 如果这个按钮你不需要显示图片,那不传入此参数即可
5959

60+
61+
#### 向表单内增加标头
62+
63+
`fm.addHeader(text)`
64+
65+
- 参数:
66+
- text : `String`
67+
标头内容
68+
- 返回值:处理完毕的表单对象(便于连锁进行其他操作)
69+
- 返回值类型:`SimpleForm`
70+
71+
#### 向表单内增加一行文本
72+
73+
`fm.addLabel(text)`
74+
75+
- 参数:
76+
- text : `String`
77+
一行文本
78+
- 返回值:处理完毕的表单对象(便于连锁进行其他操作)
79+
- 返回值类型:`SimpleForm`
80+
81+
#### 向表单内增加分隔线
82+
83+
`fm.addDivider()`
84+
85+
- 返回值:处理完毕的表单对象(便于连锁进行其他操作)
86+
- 返回值类型:`SimpleForm`
87+
88+
6089
### 发送表单
6190

6291
最后,在一切就绪之后,你可以将配置好的表单对象发送给玩家,并监听玩家的互动消息
@@ -116,6 +145,16 @@ reason可能会是`null`.
116145
- 返回值:处理完毕的表单对象(便于连锁进行其他操作)
117146
- 返回值类型:`CustomForm`
118147

148+
#### 向表单内增加标头
149+
150+
`fm.addHeader(text)`
151+
152+
- 参数:
153+
- text : `String`
154+
标头内容
155+
- 返回值:处理完毕的表单对象(便于连锁进行其他操作)
156+
- 返回值类型:`CustomForm`
157+
119158
#### 向表单内增加一行文本
120159

121160
`fm.addLabel(text)`
@@ -126,6 +165,13 @@ reason可能会是`null`.
126165
- 返回值:处理完毕的表单对象(便于连锁进行其他操作)
127166
- 返回值类型:`CustomForm`
128167

168+
#### 向表单内增加分隔线
169+
170+
`fm.addDivider()`
171+
172+
- 返回值:处理完毕的表单对象(便于连锁进行其他操作)
173+
- 返回值类型:`CustomForm`
174+
129175
#### 向表单内增加一行输入框
130176

131177
`fm.addInput(title[,placeholder,default])`

src/legacy/api/GuiAPI.cpp

Lines changed: 104 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ ClassDefine<SimpleFormClass> SimpleFormClassBuilder = defineClass<SimpleFormClas
1919
.instanceFunction("setTitle", &SimpleFormClass::setTitle)
2020
.instanceFunction("setContent", &SimpleFormClass::setContent)
2121
.instanceFunction("addButton", &SimpleFormClass::addButton)
22+
.instanceFunction("addHeader", &SimpleFormClass::addHeader)
23+
.instanceFunction("addLabel", &SimpleFormClass::addLabel)
24+
.instanceFunction("addDivider", &SimpleFormClass::addDivider)
2225
.build();
2326

2427
ClassDefine<CustomFormClass> CustomFormClassBuilder =
2528
defineClass<CustomFormClass>("LLSE_CustomForm")
2629
.constructor(nullptr)
2730
.instanceFunction("setTitle", &CustomFormClass::setTitle)
31+
.instanceFunction("addHeader", &CustomFormClass::addHeader)
2832
.instanceFunction("addLabel", &CustomFormClass::addLabel)
33+
.instanceFunction("addDivider", &CustomFormClass::addDivider)
2934
.instanceFunction("addInput", &CustomFormClass::addInput)
3035
.instanceFunction("addSwitch", &CustomFormClass::addSwitch)
3136
.instanceFunction("addDropdown", &CustomFormClass::addDropdown)
@@ -49,25 +54,29 @@ ll::form::SimpleForm* SimpleFormClass::extract(Local<Value> v) {
4954
else return nullptr;
5055
}
5156

52-
void SimpleFormClass::sendForm(ll::form::SimpleForm* form, Player* player, script::Local<Function>& callback) {
57+
void SimpleFormClass::sendForm(
58+
ll::form::SimpleForm* form,
59+
Player* player,
60+
script::Local<Function>& callback,
61+
bool update
62+
) {
5363
script::Global<Function> callbackFunc{callback};
54-
form->sendTo(
55-
*player,
56-
[engine{EngineScope::currentEngine()},
57-
callback{std::move(callbackFunc)}](Player& pl, int chosen, FormCancelReason reason) {
58-
if ((ll::getGamingStatus() != ll::GamingStatus::Running)) return;
59-
if (!EngineManager::isValid(engine)) return;
60-
if (callback.isEmpty()) return;
61-
62-
EngineScope scope(engine);
63-
try {
64-
auto reasonValue = reason.has_value() ? Number::newNumber((uchar)reason.value()) : Local<Value>();
65-
if (chosen < 0) callback.get().call({}, PlayerClass::newPlayer(&pl), reasonValue);
66-
else callback.get().call({}, PlayerClass::newPlayer(&pl), Number::newNumber(chosen), reasonValue);
67-
}
68-
CATCH_IN_CALLBACK("sendForm")
64+
auto cb = [engine{EngineScope::currentEngine()},
65+
callback{std::move(callbackFunc)}](Player& pl, int chosen, FormCancelReason reason) {
66+
if ((ll::getGamingStatus() != ll::GamingStatus::Running)) return;
67+
if (!EngineManager::isValid(engine)) return;
68+
if (callback.isEmpty()) return;
69+
70+
EngineScope scope(engine);
71+
try {
72+
auto reasonValue = reason.has_value() ? Number::newNumber((uchar)reason.value()) : Local<Value>();
73+
if (chosen < 0) callback.get().call({}, PlayerClass::newPlayer(&pl), reasonValue);
74+
else callback.get().call({}, PlayerClass::newPlayer(&pl), Number::newNumber(chosen), reasonValue);
6975
}
70-
);
76+
CATCH_IN_CALLBACK("sendForm")
77+
};
78+
if (update) form->sendUpdate(*player, std::move(cb));
79+
else form->sendTo(*player, std::move(cb));
7180
}
7281

7382
// 成员函数
@@ -107,6 +116,38 @@ Local<Value> SimpleFormClass::addButton(const Arguments& args) {
107116
CATCH("Fail in addButton!")
108117
}
109118

119+
Local<Value> SimpleFormClass::addHeader(const Arguments& args) {
120+
CHECK_ARGS_COUNT(args, 1);
121+
CHECK_ARG_TYPE(args[0], ValueKind::kString);
122+
123+
try {
124+
form.appendLabel(args[0].asString().toString());
125+
return this->getScriptObject();
126+
}
127+
CATCH("Fail in addHeader!")
128+
}
129+
130+
Local<Value> SimpleFormClass::addLabel(const Arguments& args) {
131+
CHECK_ARGS_COUNT(args, 1);
132+
CHECK_ARG_TYPE(args[0], ValueKind::kString);
133+
134+
try {
135+
form.appendLabel(args[0].asString().toString());
136+
return this->getScriptObject();
137+
}
138+
CATCH("Fail in addLabel!")
139+
}
140+
141+
Local<Value> SimpleFormClass::addDivider(const Arguments& args) {
142+
CHECK_ARGS_COUNT(args, 0);
143+
144+
try {
145+
form.appendDivider();
146+
return this->getScriptObject();
147+
}
148+
CATCH("Fail in addDivider!")
149+
}
150+
110151
//////////////////// Custom Form ////////////////////
111152

112153
CustomFormClass::CustomFormClass() : ScriptClass(ScriptClass::ConstructFromCpp<CustomFormClass>{}), form("") {}
@@ -124,29 +165,33 @@ lse::form::CustomFormWrapper* CustomFormClass::extract(Local<Value> v) {
124165
}
125166

126167
// 成员函数
127-
void CustomFormClass::sendForm(lse::form::CustomFormWrapper* form, Player* player, script::Local<Function>& callback) {
168+
void CustomFormClass::sendForm(
169+
lse::form::CustomFormWrapper* form,
170+
Player* player,
171+
script::Local<Function>& callback,
172+
bool update
173+
) {
128174
script::Global<Function> callbackFunc{callback};
129-
form->sendTo(
130-
*player,
131-
[engine{EngineScope::currentEngine()},
132-
callback{std::move(callbackFunc)
133-
}](Player& player, lse::form::CustomFormResult const& data, FormCancelReason reason) {
134-
if (ll::getGamingStatus() != ll::GamingStatus::Running) return;
135-
if (!EngineManager::isValid(engine)) return;
136-
if (callback.isEmpty()) return;
137-
138-
EngineScope scope(engine);
139-
Local<Value> result;
140-
if (data) {
141-
result = JsonToValue(*data);
142-
}
143-
auto reasonVal = reason.has_value() ? Number::newNumber((uchar)reason.value()) : Local<Value>();
144-
try {
145-
callback.get().call({}, PlayerClass::newPlayer(&player), result, reasonVal);
146-
}
147-
CATCH_IN_CALLBACK("sendForm")
175+
auto cb = [engine{EngineScope::currentEngine()},
176+
callback{std::move(callbackFunc)
177+
}](Player& player, lse::form::CustomFormResult const& data, FormCancelReason reason) {
178+
if (ll::getGamingStatus() != ll::GamingStatus::Running) return;
179+
if (!EngineManager::isValid(engine)) return;
180+
if (callback.isEmpty()) return;
181+
182+
EngineScope scope(engine);
183+
Local<Value> result;
184+
if (data) {
185+
result = JsonToValue(*data);
186+
}
187+
auto reasonVal = reason.has_value() ? Number::newNumber((uchar)reason.value()) : Local<Value>();
188+
try {
189+
callback.get().call({}, PlayerClass::newPlayer(&player), result, reasonVal);
148190
}
149-
);
191+
CATCH_IN_CALLBACK("sendForm")
192+
};
193+
if (update) form->sendUpdate(*player, std::move(cb));
194+
else form->sendTo(*player, std::move(cb));
150195
}
151196

152197
Local<Value> CustomFormClass::setTitle(const Arguments& args) {
@@ -160,6 +205,17 @@ Local<Value> CustomFormClass::setTitle(const Arguments& args) {
160205
CATCH("Fail in setTitle!")
161206
}
162207

208+
Local<Value> CustomFormClass::addHeader(const Arguments& args) {
209+
CHECK_ARGS_COUNT(args, 1)
210+
CHECK_ARG_TYPE(args[0], ValueKind::kString)
211+
212+
try {
213+
form.appendLabel(args[0].asString().toString());
214+
return this->getScriptObject();
215+
}
216+
CATCH("Fail in addHeader!")
217+
}
218+
163219
Local<Value> CustomFormClass::addLabel(const Arguments& args) {
164220
CHECK_ARGS_COUNT(args, 1)
165221
CHECK_ARG_TYPE(args[0], ValueKind::kString)
@@ -171,6 +227,16 @@ Local<Value> CustomFormClass::addLabel(const Arguments& args) {
171227
CATCH("Fail in addLabel!")
172228
}
173229

230+
Local<Value> CustomFormClass::addDivider(const Arguments& args) {
231+
CHECK_ARGS_COUNT(args, 0)
232+
233+
try {
234+
form.appendDivider();
235+
return this->getScriptObject();
236+
}
237+
CATCH("Fail in addDivider!")
238+
}
239+
174240
Local<Value> CustomFormClass::addInput(const Arguments& args) {
175241
CHECK_ARGS_COUNT(args, 1)
176242
CHECK_ARG_TYPE(args[0], ValueKind::kString)

src/legacy/api/GuiAPI.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ class SimpleFormClass : public ScriptClass {
1616

1717
static Local<Object> newForm();
1818
static ll::form::SimpleForm* extract(Local<Value> v);
19-
static void sendForm(ll::form::SimpleForm* form, Player* player, script::Local<Function>& callback);
19+
static void
20+
sendForm(ll::form::SimpleForm* form, Player* player, script::Local<Function>& callback, bool update = false);
2021

2122
Local<Value> setTitle(const Arguments& args);
2223
Local<Value> setContent(const Arguments& args);
2324
Local<Value> addButton(const Arguments& args);
25+
Local<Value> addHeader(const Arguments& args);
26+
Local<Value> addLabel(const Arguments& args);
27+
Local<Value> addDivider(const Arguments& args);
2428
};
2529
extern ClassDefine<SimpleFormClass> SimpleFormClassBuilder;
2630

@@ -35,10 +39,13 @@ class CustomFormClass : public ScriptClass {
3539

3640
static Local<Object> newForm();
3741
static lse::form::CustomFormWrapper* extract(Local<Value> v);
38-
static void sendForm(lse::form::CustomFormWrapper* form, Player* player, script::Local<Function>& callback);
42+
static void
43+
sendForm(lse::form::CustomFormWrapper* form, Player* player, script::Local<Function>& callback, bool update = true);
3944

4045
Local<Value> setTitle(const Arguments& args);
46+
Local<Value> addHeader(const Arguments& args);
4147
Local<Value> addLabel(const Arguments& args);
48+
Local<Value> addDivider(const Arguments& args);
4249
Local<Value> addInput(const Arguments& args);
4350
Local<Value> addSwitch(const Arguments& args);
4451
Local<Value> addDropdown(const Arguments& args);

0 commit comments

Comments
 (0)