ConfigBuilderImpl.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package me.shedaniel.clothconfig2.impl;
  2. import com.google.common.collect.Lists;
  3. import com.google.common.collect.Maps;
  4. import me.shedaniel.clothconfig2.api.ConfigBuilder;
  5. import me.shedaniel.clothconfig2.api.ConfigCategory;
  6. import me.shedaniel.clothconfig2.api.Expandable;
  7. import me.shedaniel.clothconfig2.api.TabbedConfigScreen;
  8. import me.shedaniel.clothconfig2.gui.AbstractConfigScreen;
  9. import me.shedaniel.clothconfig2.gui.ClothConfigScreen;
  10. import me.shedaniel.clothconfig2.gui.GlobalizedClothConfigScreen;
  11. import net.fabricmc.api.EnvType;
  12. import net.fabricmc.api.Environment;
  13. import net.minecraft.client.gui.DrawableHelper;
  14. import net.minecraft.client.gui.screen.Screen;
  15. import net.minecraft.text.Text;
  16. import net.minecraft.text.TranslatableText;
  17. import net.minecraft.util.Identifier;
  18. import org.jetbrains.annotations.ApiStatus;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Objects;
  22. import java.util.function.Consumer;
  23. @Environment(EnvType.CLIENT)
  24. @ApiStatus.Internal
  25. public class ConfigBuilderImpl implements ConfigBuilder {
  26. private Runnable savingRunnable;
  27. private Screen parent;
  28. private Text title = new TranslatableText("text.cloth-config.config");
  29. private boolean globalized = false;
  30. private boolean globalizedExpanded = true;
  31. private boolean editable = true;
  32. private boolean tabsSmoothScroll = true;
  33. private boolean listSmoothScroll = true;
  34. private boolean doesConfirmSave = true;
  35. private boolean transparentBackground = false;
  36. private Identifier defaultBackground = DrawableHelper.BACKGROUND_TEXTURE;
  37. private Consumer<Screen> afterInitConsumer = screen -> {};
  38. private final Map<Text, Identifier> categoryBackground = Maps.newHashMap();
  39. private final Map<Text, List<Object>> dataMap = Maps.newLinkedHashMap();
  40. private final Map<Text, ConfigCategory> categoryMap = Maps.newHashMap();
  41. private Text fallbackCategory = null;
  42. private boolean alwaysShowTabs = false;
  43. @ApiStatus.Internal
  44. public ConfigBuilderImpl() {
  45. }
  46. @Override
  47. public void setGlobalized(boolean globalized) {
  48. this.globalized = globalized;
  49. }
  50. @Override
  51. public void setGlobalizedExpanded(boolean globalizedExpanded) {
  52. this.globalizedExpanded = globalizedExpanded;
  53. }
  54. @Override
  55. public boolean isAlwaysShowTabs() {
  56. return alwaysShowTabs;
  57. }
  58. @Override
  59. public ConfigBuilder setAlwaysShowTabs(boolean alwaysShowTabs) {
  60. this.alwaysShowTabs = alwaysShowTabs;
  61. return this;
  62. }
  63. @Override
  64. public ConfigBuilder setTransparentBackground(boolean transparentBackground) {
  65. this.transparentBackground = transparentBackground;
  66. return this;
  67. }
  68. @Override
  69. public ConfigBuilder setAfterInitConsumer(Consumer<Screen> afterInitConsumer) {
  70. this.afterInitConsumer = afterInitConsumer;
  71. return this;
  72. }
  73. @Override
  74. public ConfigBuilder setFallbackCategory(ConfigCategory fallbackCategory) {
  75. this.fallbackCategory = Objects.requireNonNull(fallbackCategory).getCategoryKey();
  76. return this;
  77. }
  78. @Override
  79. public Screen getParentScreen() {
  80. return parent;
  81. }
  82. @Override
  83. public ConfigBuilder setParentScreen(Screen parent) {
  84. this.parent = parent;
  85. return this;
  86. }
  87. @Override
  88. public Text getTitle() {
  89. return title;
  90. }
  91. @Override
  92. public ConfigBuilder setTitle(Text title) {
  93. this.title = title;
  94. return this;
  95. }
  96. @Override
  97. public boolean isEditable() {
  98. return editable;
  99. }
  100. @Override
  101. public ConfigBuilder setEditable(boolean editable) {
  102. this.editable = editable;
  103. return this;
  104. }
  105. @Override
  106. public ConfigCategory getOrCreateCategory(Text categoryKey) {
  107. if (categoryMap.containsKey(categoryKey))
  108. return categoryMap.get(categoryKey);
  109. dataMap.put(categoryKey, Lists.newArrayList());
  110. if (fallbackCategory == null)
  111. fallbackCategory = categoryKey;
  112. return categoryMap.computeIfAbsent(categoryKey, key -> new ConfigCategoryImpl(categoryKey, identifier -> {
  113. if (transparentBackground)
  114. throw new IllegalStateException("Cannot set category background if screen is using transparent background.");
  115. categoryBackground.put(categoryKey, identifier);
  116. }, () -> dataMap.get(categoryKey), () -> removeCategory(categoryKey)));
  117. }
  118. @Override
  119. public ConfigBuilder removeCategory(Text category) {
  120. if (dataMap.containsKey(category) && fallbackCategory.equals(category))
  121. fallbackCategory = null;
  122. if (!dataMap.containsKey(category))
  123. throw new NullPointerException("Category doesn't exist!");
  124. dataMap.remove(category);
  125. return this;
  126. }
  127. @Override
  128. public ConfigBuilder removeCategoryIfExists(Text category) {
  129. if (dataMap.containsKey(category) && fallbackCategory.equals(category))
  130. fallbackCategory = null;
  131. dataMap.remove(category);
  132. return this;
  133. }
  134. @Override
  135. public boolean hasCategory(Text category) {
  136. return dataMap.containsKey(category);
  137. }
  138. @Override
  139. public ConfigBuilder setShouldTabsSmoothScroll(boolean shouldTabsSmoothScroll) {
  140. this.tabsSmoothScroll = shouldTabsSmoothScroll;
  141. return this;
  142. }
  143. @Override
  144. public boolean isTabsSmoothScrolling() {
  145. return tabsSmoothScroll;
  146. }
  147. @Override
  148. public ConfigBuilder setShouldListSmoothScroll(boolean shouldListSmoothScroll) {
  149. this.listSmoothScroll = shouldListSmoothScroll;
  150. return this;
  151. }
  152. @Override
  153. public boolean isListSmoothScrolling() {
  154. return listSmoothScroll;
  155. }
  156. @Override
  157. public ConfigBuilder setDoesConfirmSave(boolean confirmSave) {
  158. this.doesConfirmSave = confirmSave;
  159. return this;
  160. }
  161. @Override
  162. public boolean doesConfirmSave() {
  163. return doesConfirmSave;
  164. }
  165. @Override
  166. public Identifier getDefaultBackgroundTexture() {
  167. return defaultBackground;
  168. }
  169. @Override
  170. public ConfigBuilder setDefaultBackgroundTexture(Identifier texture) {
  171. this.defaultBackground = texture;
  172. return this;
  173. }
  174. @Override
  175. public ConfigBuilder setSavingRunnable(Runnable runnable) {
  176. this.savingRunnable = runnable;
  177. return this;
  178. }
  179. @Override
  180. public Consumer<Screen> getAfterInitConsumer() {
  181. return afterInitConsumer;
  182. }
  183. @Override
  184. public Screen build() {
  185. if (dataMap.isEmpty() || fallbackCategory == null)
  186. throw new NullPointerException("There cannot be no categories or fallback category!");
  187. AbstractConfigScreen screen;
  188. if (globalized) {
  189. screen = new GlobalizedClothConfigScreen(parent, title, dataMap, defaultBackground);
  190. } else {
  191. screen = new ClothConfigScreen(parent, title, dataMap, categoryMap, defaultBackground);
  192. }
  193. screen.setSavingRunnable(savingRunnable);
  194. screen.setEditable(editable);
  195. screen.setFallbackCategory(fallbackCategory);
  196. screen.setTransparentBackground(transparentBackground);
  197. screen.setAlwaysShowTabs(alwaysShowTabs);
  198. screen.setConfirmSave(doesConfirmSave);
  199. screen.setAfterInitConsumer(afterInitConsumer);
  200. if (screen instanceof Expandable)
  201. ((Expandable) screen).setExpanded(globalizedExpanded);
  202. if (screen instanceof TabbedConfigScreen)
  203. categoryBackground.forEach(((TabbedConfigScreen) screen)::registerCategoryBackground);
  204. return screen;
  205. }
  206. @Override
  207. public Runnable getSavingRunnable() {
  208. return savingRunnable;
  209. }
  210. }