generated from CleanroomMC/TemplateDevEnv
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(cherry picked from commit ee9d2fd)
- Loading branch information
Showing
12 changed files
with
515 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/com/cleanroommc/modularui/api/IThemeApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.cleanroommc.modularui.api; | ||
|
||
import com.cleanroommc.modularui.screen.ModularScreen; | ||
import com.cleanroommc.modularui.theme.ThemeAPI; | ||
import com.cleanroommc.modularui.theme.WidgetTheme; | ||
import com.cleanroommc.modularui.theme.WidgetThemeParser; | ||
import com.cleanroommc.modularui.utils.JsonBuilder; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* An API interface for Themes. | ||
*/ | ||
public interface IThemeApi { | ||
|
||
/** | ||
* @return the default api implementation | ||
*/ | ||
static IThemeApi get() { | ||
return ThemeAPI.INSTANCE; | ||
} | ||
|
||
/** | ||
* @return the absolute fallback theme | ||
*/ | ||
ITheme getDefaultTheme(); | ||
|
||
/** | ||
* Finds a theme for an id | ||
* | ||
* @param id id of the theme | ||
* @return the found theme or {@link #getDefaultTheme()} if no theme was found | ||
*/ | ||
@NotNull | ||
ITheme getTheme(String id); | ||
|
||
/** | ||
* @param id id of the theme | ||
* @return if a theme with the id is registered | ||
*/ | ||
boolean hasTheme(String id); | ||
|
||
/** | ||
* Registers a theme json object. Themes from resource packs always have greater priority. | ||
* | ||
* @param id id of the theme | ||
* @param json theme data | ||
*/ | ||
void registerTheme(String id, JsonBuilder json); | ||
|
||
/** | ||
* Gets all currently from java side registered theme json's for a theme. | ||
* | ||
* @param id id of the theme | ||
* @return all theme json's for a theme. | ||
*/ | ||
List<JsonBuilder> getJavaDefaultThemes(String id); | ||
|
||
/** | ||
* Gets the appropriate theme for a screen. | ||
* | ||
* @param owner owner of the screen | ||
* @param name name of the screen | ||
* @param defaultTheme default theme if no theme was found | ||
* @return the registered theme for the given screen or the given default theme or {@link #getDefaultTheme()} | ||
*/ | ||
ITheme getThemeForScreen(String owner, String name, @Nullable String defaultTheme); | ||
|
||
/** | ||
* Gets the appropriate theme for a screen. | ||
* | ||
* @param screen screen | ||
* @param defaultTheme default theme if no theme was found | ||
* @return the registered theme for the given screen or the given default theme or {@link #getDefaultTheme()} | ||
*/ | ||
default ITheme getThemeForScreen(ModularScreen screen, @Nullable String defaultTheme) { | ||
return getThemeForScreen(screen.getOwner(), screen.getName(), defaultTheme); | ||
} | ||
|
||
/** | ||
* Registers a theme for a screen. Themes from resource packs always have greater priority. | ||
* | ||
* @param owner owner of the screen | ||
* @param name name of the screen | ||
* @param theme theme to register | ||
*/ | ||
default void registerThemeForScreen(String owner, String name, String theme) { | ||
registerThemeForScreen(owner + ":" + name, theme); | ||
} | ||
|
||
/** | ||
* Registers a theme for a screen. Themes from resource packs always have greater priority. | ||
* | ||
* @param screen full screen id | ||
* @param theme theme to register | ||
*/ | ||
void registerThemeForScreen(String screen, String theme); | ||
|
||
/** | ||
* Register a widget theme. | ||
* | ||
* @param id id of the widget theme | ||
* @param defaultTheme the fallback widget theme | ||
* @param parser the widget theme json parser function | ||
*/ | ||
void registerWidgetTheme(String id, WidgetTheme defaultTheme, WidgetThemeParser parser); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/main/java/com/cleanroommc/modularui/theme/ThemeAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.cleanroommc.modularui.theme; | ||
|
||
import com.cleanroommc.modularui.ModularUIConfig; | ||
import com.cleanroommc.modularui.api.ITheme; | ||
import com.cleanroommc.modularui.api.IThemeApi; | ||
import com.cleanroommc.modularui.drawable.GuiTextures; | ||
import com.cleanroommc.modularui.utils.Color; | ||
import com.cleanroommc.modularui.utils.JsonBuilder; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class ThemeAPI implements IThemeApi { | ||
|
||
public static final ThemeAPI INSTANCE = new ThemeAPI(); | ||
public static final String DEFAULT = "DEFAULT"; | ||
public static final ITheme DEFAULT_DEFAULT = new DefaultTheme(INSTANCE); | ||
|
||
private final Map<String, ITheme> THEMES = new Object2ObjectOpenHashMap<>(); | ||
private final Map<String, List<JsonBuilder>> defaultThemes = new Object2ObjectOpenHashMap<>(); | ||
protected final Map<String, WidgetTheme> defaultWidgetThemes = new Object2ObjectOpenHashMap<>(); | ||
protected final Map<String, WidgetThemeParser> widgetThemeFunctions = new Object2ObjectOpenHashMap<>(); | ||
protected final Map<String, String> jsonScreenThemes = new Object2ObjectOpenHashMap<>(); | ||
private final Map<String, String> screenThemes = new Object2ObjectOpenHashMap<>(); | ||
|
||
private ThemeAPI() { | ||
registerWidgetTheme(Theme.PANEL, new WidgetTheme(GuiTextures.BACKGROUND, null, Color.WHITE.normal, 0xFF404040, false), (parent, json, fallback) -> new WidgetTheme(parent, fallback, json)); | ||
registerWidgetTheme(Theme.BUTTON, new WidgetTheme(GuiTextures.BUTTON, null, Color.WHITE.normal, Color.WHITE.normal, true), (parent, json, fallback) -> new WidgetTheme(parent, fallback, json)); | ||
registerWidgetTheme(Theme.ITEM_SLOT, new WidgetSlotTheme(GuiTextures.SLOT, Color.withAlpha(Color.WHITE.normal, 0x80)), (parent, json, fallback) -> new WidgetSlotTheme(parent, fallback, json)); | ||
registerWidgetTheme(Theme.FLUID_SLOT, new WidgetSlotTheme(GuiTextures.SLOT_DARK, Color.withAlpha(Color.WHITE.normal, 0x80)), (parent, json, fallback) -> new WidgetSlotTheme(parent, fallback, json)); | ||
registerWidgetTheme(Theme.TEXT_FIELD, new WidgetTextFieldTheme(0xFF2F72A8), (parent, json, fallback) -> new WidgetTextFieldTheme(parent, fallback, json)); | ||
} | ||
|
||
@Override | ||
public ITheme getDefaultTheme() { | ||
return DEFAULT_DEFAULT; | ||
} | ||
|
||
@Override | ||
public @NotNull ITheme getTheme(String id) { | ||
return THEMES.getOrDefault(id, getDefaultTheme()); | ||
} | ||
|
||
@Override | ||
public boolean hasTheme(String id) { | ||
return THEMES.containsKey(id); | ||
} | ||
|
||
@Override | ||
public void registerTheme(String id, JsonBuilder json) { | ||
getJavaDefaultThemes(id).add(json); | ||
} | ||
|
||
@Override | ||
public List<JsonBuilder> getJavaDefaultThemes(String id) { | ||
return defaultThemes.computeIfAbsent(id, key -> new ArrayList<>()); | ||
} | ||
|
||
@Override | ||
public ITheme getThemeForScreen(String owner, String name, @Nullable String defaultTheme) { | ||
String theme = getThemeIdForScreen(owner, name); | ||
if (theme != null) return getTheme(theme); | ||
if (defaultTheme != null) return getTheme(defaultTheme); | ||
return getTheme(ModularUIConfig.useDarkThemeByDefault ? "vanilla_dark" : "vanilla"); | ||
} | ||
|
||
private String getThemeIdForScreen(String mod, String name) { | ||
String theme = jsonScreenThemes.get(mod + ":" + name); | ||
if (theme != null) return theme; | ||
theme = jsonScreenThemes.get(mod); | ||
if (theme != null) return theme; | ||
theme = screenThemes.get(mod + ":" + name); | ||
return theme; | ||
} | ||
|
||
@Override | ||
public void registerThemeForScreen(String screen, String theme) { | ||
Objects.requireNonNull(screen); | ||
Objects.requireNonNull(theme); | ||
screenThemes.put(screen, theme); | ||
} | ||
|
||
@Override | ||
public void registerWidgetTheme(String id, WidgetTheme defaultTheme, WidgetThemeParser parser) { | ||
if (widgetThemeFunctions.containsKey(id)) { | ||
throw new IllegalStateException(); | ||
} | ||
widgetThemeFunctions.put(id, parser); | ||
defaultWidgetThemes.put(id, defaultTheme); | ||
} | ||
|
||
// Internals | ||
|
||
@ApiStatus.Internal | ||
void registerTheme(ITheme theme) { | ||
if (THEMES.containsKey(theme.getId())) { | ||
throw new IllegalArgumentException("Theme with id " + theme.getId() + " already exists!"); | ||
} | ||
THEMES.put(theme.getId(), theme); | ||
} | ||
|
||
@ApiStatus.Internal | ||
void onReload() { | ||
THEMES.clear(); | ||
jsonScreenThemes.clear(); | ||
registerTheme(DEFAULT_DEFAULT); | ||
} | ||
|
||
public static class DefaultTheme extends AbstractDefaultTheme { | ||
|
||
private final ThemeAPI api; | ||
|
||
private DefaultTheme(ThemeAPI api) { | ||
this.api = api; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return DEFAULT; | ||
} | ||
|
||
@Override | ||
public WidgetTheme getFallback() { | ||
return ThemeManager.defaultdefaultWidgetTheme; | ||
} | ||
|
||
@Override | ||
public WidgetTheme getWidgetTheme(String id) { | ||
return this.api.defaultWidgetThemes.get(id); | ||
} | ||
} | ||
} |
Oops, something went wrong.