Skip to content

Commit 6d3910e

Browse files
committed
finish CustomFormMenu
1 parent 6fc0f4b commit 6d3910e

10 files changed

Lines changed: 526 additions & 4 deletions

src/main/java/me/hsgamer/bettergui/betterforms/impl/custom/CustomFormMenu.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
Copyright 2024 Huynh Tien
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
116
package me.hsgamer.bettergui.betterforms.impl.custom;
217

318
import me.hsgamer.bettergui.betterforms.api.builder.ComponentProviderBuilder;
@@ -12,7 +27,14 @@ public class CustomFormMenu extends FormMenu<CustomForm, CustomFormResponse, Cus
1227
private static final ComponentProviderBuilder<CustomForm, CustomFormResponse, CustomForm.Builder> builder = new ComponentProviderBuilder<>();
1328

1429
static {
15-
30+
builder.register(SubmitComponentProvider::new, "submit", "button");
31+
builder.register(IconComponentProvider::new, "icon", "image");
32+
builder.register(LabelComponentProvider::new, "label", "text", "content");
33+
builder.register(DropdownComponentProvider::new, "dropdown", "select");
34+
builder.register(InputComponentProvider::new, "input");
35+
builder.register(SubmitComponentProvider::new, "slider");
36+
builder.register(StepSliderComponentProvider::new, "step-slider", "step");
37+
builder.register(SubmitComponentProvider::new, "toggle", "switch");
1638
}
1739

1840
public CustomFormMenu(FormSender sender, Config config) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2024 Huynh Tien
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package me.hsgamer.bettergui.betterforms.impl.custom;
17+
18+
import me.hsgamer.bettergui.betterforms.api.builder.ComponentProviderBuilder;
19+
import me.hsgamer.bettergui.util.StringReplacerApplier;
20+
import me.hsgamer.hscore.common.CollectionUtils;
21+
import me.hsgamer.hscore.common.MapUtils;
22+
import me.hsgamer.hscore.common.Validate;
23+
import org.geysermc.cumulus.form.CustomForm;
24+
import org.geysermc.cumulus.response.CustomFormResponse;
25+
26+
import java.util.*;
27+
import java.util.concurrent.ConcurrentHashMap;
28+
import java.util.stream.Collectors;
29+
30+
import static java.util.Optional.ofNullable;
31+
32+
public class DropdownComponentProvider extends ValueComponentProvider {
33+
private final Map<UUID, List<String>> optionsMap = new ConcurrentHashMap<>();
34+
private final String text;
35+
private final List<String> options;
36+
private final String defaultOption;
37+
38+
public DropdownComponentProvider(ComponentProviderBuilder.Input input) {
39+
super(input);
40+
this.text = ofNullable(MapUtils.getIfFound(input.options, "text"))
41+
.map(Object::toString)
42+
.orElse("");
43+
this.options = ofNullable(MapUtils.getIfFound(input.options, "option", "options"))
44+
.map(CollectionUtils::createStringListFromObject)
45+
.orElse(Collections.emptyList());
46+
this.defaultOption = ofNullable(MapUtils.getIfFound(input.options, "default-option", "default"))
47+
.map(Objects::toString)
48+
.orElse("0");
49+
}
50+
51+
@Override
52+
protected void apply(UUID uuid, CustomForm.Builder builder) {
53+
String replacedText = StringReplacerApplier.replace(this.text, uuid, this);
54+
List<String> replacedOptions = this.options.stream()
55+
.map(s -> StringReplacerApplier.replace(s, uuid, this))
56+
.collect(Collectors.toList());
57+
String replacedDefaultOption = StringReplacerApplier.replace(this.defaultOption, uuid, this);
58+
int defaultOption = Validate.getNumber(replacedDefaultOption).map(Number::intValue).orElse(0);
59+
60+
builder.dropdown(replacedText, replacedOptions, defaultOption);
61+
this.optionsMap.put(uuid, replacedOptions);
62+
}
63+
64+
@Override
65+
protected String getValue(UUID uuid, CustomFormResponse response) {
66+
return this.optionsMap.get(uuid).get(response.asDropdown());
67+
}
68+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2024 Huynh Tien
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package me.hsgamer.bettergui.betterforms.impl.custom;
17+
18+
import me.hsgamer.bettergui.betterforms.api.builder.ComponentProviderBuilder;
19+
import me.hsgamer.bettergui.betterforms.api.component.Component;
20+
import me.hsgamer.bettergui.betterforms.common.BaseComponentProvider;
21+
import me.hsgamer.bettergui.betterforms.util.ComponentUtil;
22+
import org.geysermc.cumulus.form.CustomForm;
23+
import org.geysermc.cumulus.response.CustomFormResponse;
24+
import org.geysermc.cumulus.util.FormImage;
25+
26+
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.UUID;
29+
import java.util.function.Function;
30+
31+
public class IconComponentProvider extends BaseComponentProvider<CustomForm, CustomFormResponse, CustomForm.Builder> {
32+
private final Function<UUID, FormImage> imageFunction;
33+
34+
public IconComponentProvider(ComponentProviderBuilder.Input input) {
35+
super(input);
36+
37+
imageFunction = ComponentUtil.createImageFunction(input.options, this);
38+
}
39+
40+
@Override
41+
protected List<Component<CustomForm, CustomFormResponse, CustomForm.Builder>> provideChecked(UUID uuid, int index) {
42+
return Collections.singletonList(new Component<CustomForm, CustomFormResponse, CustomForm.Builder>() {
43+
@Override
44+
public void apply(CustomForm.Builder builder) {
45+
FormImage image = imageFunction.apply(uuid);
46+
if (image != null) {
47+
builder.icon(image);
48+
}
49+
}
50+
51+
@Override
52+
public void handle(CustomForm form, CustomFormResponse response) {
53+
// EMPTY
54+
}
55+
});
56+
}
57+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2024 Huynh Tien
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package me.hsgamer.bettergui.betterforms.impl.custom;
17+
18+
import me.hsgamer.bettergui.betterforms.api.builder.ComponentProviderBuilder;
19+
import me.hsgamer.bettergui.util.StringReplacerApplier;
20+
import me.hsgamer.hscore.common.MapUtils;
21+
import org.geysermc.cumulus.form.CustomForm;
22+
import org.geysermc.cumulus.response.CustomFormResponse;
23+
24+
import java.util.Optional;
25+
import java.util.UUID;
26+
27+
public class InputComponentProvider extends ValueComponentProvider {
28+
private final String text;
29+
private final String placeholder;
30+
private final String defaultText;
31+
32+
public InputComponentProvider(ComponentProviderBuilder.Input input) {
33+
super(input);
34+
35+
this.text = Optional.ofNullable(MapUtils.getIfFound(input.options, "text"))
36+
.map(Object::toString)
37+
.orElse("");
38+
this.placeholder = Optional.ofNullable(MapUtils.getIfFound(input.options, "placeholder"))
39+
.map(Object::toString)
40+
.orElse("");
41+
this.defaultText = Optional.ofNullable(MapUtils.getIfFound(input.options, "default", "default-text"))
42+
.map(Object::toString)
43+
.orElse("");
44+
}
45+
46+
@Override
47+
protected void apply(UUID uuid, CustomForm.Builder builder) {
48+
String replacedText = StringReplacerApplier.replace(text, uuid, this);
49+
String replacedPlaceholder = StringReplacerApplier.replace(placeholder, uuid, this);
50+
String replacedDefaultText = StringReplacerApplier.replace(defaultText, uuid, this);
51+
builder.input(replacedText, replacedPlaceholder, replacedDefaultText);
52+
}
53+
54+
@Override
55+
protected String getValue(UUID uuid, CustomFormResponse response) {
56+
return response.asInput();
57+
}
58+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2024 Huynh Tien
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package me.hsgamer.bettergui.betterforms.impl.custom;
17+
18+
import me.hsgamer.bettergui.betterforms.api.builder.ComponentProviderBuilder;
19+
import me.hsgamer.bettergui.betterforms.api.component.Component;
20+
import me.hsgamer.bettergui.betterforms.common.BaseComponentProvider;
21+
import me.hsgamer.bettergui.util.StringReplacerApplier;
22+
import me.hsgamer.hscore.common.MapUtils;
23+
import org.geysermc.cumulus.form.CustomForm;
24+
import org.geysermc.cumulus.response.CustomFormResponse;
25+
26+
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.Optional;
29+
import java.util.UUID;
30+
31+
public class LabelComponentProvider extends BaseComponentProvider<CustomForm, CustomFormResponse, CustomForm.Builder> {
32+
private final String value;
33+
34+
public LabelComponentProvider(ComponentProviderBuilder.Input input) {
35+
super(input);
36+
37+
value = Optional.ofNullable(MapUtils.getIfFound(input.options, "value", "text", "content", "label"))
38+
.map(Object::toString)
39+
.orElse("");
40+
}
41+
42+
@Override
43+
protected List<Component<CustomForm, CustomFormResponse, CustomForm.Builder>> provideChecked(UUID uuid, int index) {
44+
return Collections.singletonList(new Component<CustomForm, CustomFormResponse, CustomForm.Builder>() {
45+
@Override
46+
public void apply(CustomForm.Builder builder) {
47+
builder.label(StringReplacerApplier.replace(value, uuid, LabelComponentProvider.this));
48+
}
49+
50+
@Override
51+
public void handle(CustomForm form, CustomFormResponse response) {
52+
// EMPTY
53+
}
54+
});
55+
}
56+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2024 Huynh Tien
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package me.hsgamer.bettergui.betterforms.impl.custom;
17+
18+
import me.hsgamer.bettergui.betterforms.api.builder.ComponentProviderBuilder;
19+
import me.hsgamer.bettergui.util.StringReplacerApplier;
20+
import me.hsgamer.hscore.common.MapUtils;
21+
import me.hsgamer.hscore.common.Validate;
22+
import org.geysermc.cumulus.form.CustomForm;
23+
import org.geysermc.cumulus.response.CustomFormResponse;
24+
25+
import java.util.Optional;
26+
import java.util.UUID;
27+
28+
public class SliderComponentProvider extends ValueComponentProvider {
29+
private final String text;
30+
private final String min;
31+
private final String max;
32+
private final String step;
33+
private final String defaultValue;
34+
35+
public SliderComponentProvider(ComponentProviderBuilder.Input input) {
36+
super(input);
37+
38+
this.text = Optional.ofNullable(MapUtils.getIfFound(input.options, "text"))
39+
.map(Object::toString)
40+
.orElse("");
41+
this.min = Optional.ofNullable(MapUtils.getIfFound(input.options, "min"))
42+
.map(Object::toString)
43+
.orElse("0");
44+
this.max = Optional.ofNullable(MapUtils.getIfFound(input.options, "max"))
45+
.map(Object::toString)
46+
.orElse("100");
47+
this.step = Optional.ofNullable(MapUtils.getIfFound(input.options, "step"))
48+
.map(Object::toString)
49+
.orElse("1");
50+
this.defaultValue = Optional.ofNullable(MapUtils.getIfFound(input.options, "default"))
51+
.map(Object::toString)
52+
.orElse("0");
53+
}
54+
55+
@Override
56+
protected void apply(UUID uuid, CustomForm.Builder builder) {
57+
String replacedText = StringReplacerApplier.replace(text, uuid, this);
58+
String replacedMin = StringReplacerApplier.replace(min, uuid, this);
59+
String replacedMax = StringReplacerApplier.replace(max, uuid, this);
60+
String replacedStep = StringReplacerApplier.replace(step, uuid, this);
61+
String replacedDefaultValue = StringReplacerApplier.replace(defaultValue, uuid, this);
62+
63+
float min = Validate.getNumber(replacedMin).map(Number::floatValue).orElse(0F);
64+
float max = Validate.getNumber(replacedMax).map(Number::floatValue).orElse(100F);
65+
float step = Validate.getNumber(replacedStep).map(Number::floatValue).orElse(1F);
66+
float defaultValue = Validate.getNumber(replacedDefaultValue).map(Number::floatValue).orElse(0F);
67+
68+
max = Math.max(min, max);
69+
defaultValue = Math.min(max, Math.max(min, defaultValue));
70+
71+
builder.slider(replacedText, min, max, step, defaultValue);
72+
}
73+
74+
@Override
75+
protected String getValue(UUID uuid, CustomFormResponse response) {
76+
return Float.toString(response.asSlider());
77+
}
78+
}

0 commit comments

Comments
 (0)