Skip to content

Commit

Permalink
Added another spotify overlay layout
Browse files Browse the repository at this point in the history
  • Loading branch information
tttsaurus committed Jan 11, 2025
1 parent f5b5b54 commit 4dd8342
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 18 deletions.
56 changes: 48 additions & 8 deletions src/main/java/com/tttsaurus/ingameinfo/common/api/gui/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,50 @@ public abstract class Element
private final RenderMask mask = new RenderMask(RenderMask.MaskShape.ROUNDED_RECT);

//<editor-fold desc="runtime variables">

// stores width & height when enabled is false
public float cachedWidth = 0f, cachedHeight = 0f;

// stores padding when enabled is false
public Padding cachedPadding = new Padding(0f, 0f, 0f, 0f);

// stores the actual render pos (top-left) and size
public Rect rect = new Rect(0, 0, 0, 0);
public Rect rect = new Rect(0f, 0f, 0f, 0f);

// stores the actual render pos before applying the pivot
public float pivotPosX = 0, pivotPosY = 0;
public float pivotPosX = 0f, pivotPosY = 0f;

// stores the rect of the parent group
public Rect contextRect = new Rect(0, 0, 0, 0);
public Rect contextRect = new Rect(0f, 0f, 0f, 0f);

private boolean needReCalc = false;
//</editor-fold>

@StylePropertyCallback
public void requestReCalc() { needReCalc = true; }

@StyleProperty
@StylePropertyCallback
public void setEnabledCallbackPre(boolean value, CallbackInfo callbackInfo)
{
if (!enabled && value)
{
rect.width = cachedWidth;
rect.height = cachedHeight;
padding.set(cachedPadding.top, cachedPadding.bottom, cachedPadding.left, cachedPadding.right);
requestReCalc();
}
if (enabled && !value)
{
cachedWidth = rect.width;
cachedHeight = rect.height;
rect.width = 0f;
rect.height = 0f;
cachedPadding.set(padding.top, padding.bottom, padding.left, padding.right);
padding.set(0f, 0f, 0f, 0f);
requestReCalc();
}
}
@StyleProperty(setterCallbackPre = "setEnabledCallbackPre")
public boolean enabled = true;

@StylePropertyCallback
Expand Down Expand Up @@ -62,12 +93,21 @@ public void pivotValidation(Pivot value, CallbackInfo callbackInfo)
public Pivot pivot = Pivot.TOP_LEFT;

@StylePropertyCallback
public void paddingValidation(Padding value, CallbackInfo callbackInfo)
public void setPaddingCallbackPre(Padding value, CallbackInfo callbackInfo)
{
if (value == null) callbackInfo.cancel = true;
if (value == null)
{
callbackInfo.cancel = true;
return;
}
if (!enabled)
{
cachedPadding.set(value.top, value.bottom, value.left, value.right);
callbackInfo.cancel = true;
}
}
@StyleProperty(setterCallbackPost = "requestReCalc", setterCallbackPre = "paddingValidation")
public Padding padding = new Padding(0, 0, 0, 0);
@StyleProperty(setterCallbackPost = "requestReCalc", setterCallbackPre = "setPaddingCallbackPre")
public Padding padding = new Padding(0f, 0f, 0f, 0f);

// determines how the background is drawn (optional)
@StylePropertyCallback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public GuiLayout addElement(Element element, List<ElementStyle> styles)
return this;
}

// todo: add inject priority
private void injectStyles(Element element, List<ElementStyle> styles)
{
for (ElementStyle style: styles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class IgiGuiContainer

private boolean initFlag = false;
private boolean isActive = true;
private IFunc<Boolean> exitCallback;
private IFunc<Boolean> exitCallback = () -> { return true; };

//<editor-fold desc="getters">
public boolean getActive() { return isActive; }
Expand Down Expand Up @@ -66,6 +66,7 @@ public void onScaledResolutionResize()
mainGroup.resetRenderInfo();
mainGroup.calcWidthHeight();
mainGroup.calcRenderPos(mainGroup.rect);
mainGroup.finishReCalc();
}
public void onFixedUpdate(double deltaTime)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ public void resetRenderInfo()
public void calcWidthHeight()
{
for (Element element: elements)
{
element.calcWidthHeight();
if (!element.enabled)
{
element.cachedWidth = element.rect.width;
element.cachedHeight = element.rect.height;
element.rect.width = 0f;
element.rect.height = 0f;
}
}
}

@Override
Expand Down Expand Up @@ -60,13 +69,15 @@ public void renderBackground()
@Override
public boolean getNeedReCalc()
{
if (super.getNeedReCalc()) return true;
for (Element element: elements)
if (element.getNeedReCalc()) return true;
return false;
}
@Override
public void finishReCalc()
{
super.finishReCalc();
for (Element element: elements)
element.finishReCalc();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ public Padding(float top, float bottom, float left, float right)
this.left = left;
this.right = right;
}

public void set(float top, float bottom, float left, float right)
{
this.top = top;
this.bottom = bottom;
this.left = left;
this.right = right;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public GuiLayout init(ViewModel<TView> viewModel, String mvvmRegistryName)
return guiLayout;
}

// todo: handle the binding of passiveSync
public <T> void bindReactiveObject(Reactive reactive, ReactiveObject<T> reactiveObject)
{
if (reactive.targetUid().isEmpty()) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ public String getDefaultIxml()
<Def debug = false>
<VerticalGroup padding = {"top": 10, "left": 10}>
<VerticalGroup backgroundStyle = "roundedBoxWithOutline">
<HorizontalGroup padding = {"top": 10, "bottom": 5}>
<HorizontalGroup uid = "albumImageGroup" padding = {"top": 10, "bottom": 5}>
<UrlImage uid = "albumImage" rounded = true width = 40 height = 40 padding = {"left": 10, "right": 10}>
<VerticalGroup uid = "anotherTrackTitleGroup" padding = {"top": 10, "left": 5} enabled = false>
<SlidingText uid = "anotherTrackTitle" width = 40 spareWidth = 20 onDemandSliding = true>
<SlidingText uid = "anotherTrackAuthor" scale = 0.7f width = 30 spareWidth = 20 onDemandSliding = true padding = {"top": 5}>
<Text uid = "anotherTrackTimer" scale = 0.7f alignment = TOP_RIGHT pivot = TOP_RIGHT padding = {"top": 25}>
</Group>
</Group>
<HorizontalGroup padding = {"bottom": 5}>
<SlidingText uid = "trackTitle" width = 40 spareWidth = 20 onDemandSliding = true padding = {"left": 10, "right": 10}>
<HorizontalGroup uid = "trackTitleGroup" padding = {"bottom": 5}>
<SlidingText uid = "trackTitle" width = 40 spareWidth = 20 onDemandSliding = true padding = {"left": 10, "right": 10}>
</Group>
<HorizontalGroup padding = {"bottom": 10}>
<ProgressBar uid = "progressBar" width = 50 height = 3 padding = {"left": 5, "right": 5}>
<HorizontalGroup uid = "progressBarGroup" padding = {"bottom": 10}>
<ProgressBar uid = "progressBar" width = 50 height = 3 padding = {"left": 5, "right": 5}>
</Group>
</Group>
<SimpleButton uid = "editButton" text = "test" enabled = false padding = {"top": 5}>
<SimpleButton uid = "editButton" text = "Switch Layout" padding = {"top": 5} enabled = false>
</Group>
""";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.tttsaurus.ingameinfo.common.api.appcommunication.spotify.SpotifyUserInfo;
import com.tttsaurus.ingameinfo.common.api.appcommunication.spotify.TrackPlaying;
import com.tttsaurus.ingameinfo.common.api.gui.delegate.button.IMouseClickButton;
import com.tttsaurus.ingameinfo.common.api.gui.layout.Padding;
import com.tttsaurus.ingameinfo.common.api.mvvm.binding.Reactive;
import com.tttsaurus.ingameinfo.common.api.mvvm.binding.ReactiveObject;
import com.tttsaurus.ingameinfo.common.api.mvvm.viewmodel.ViewModel;
Expand All @@ -22,24 +23,69 @@

public class SpotifyViewModel extends ViewModel<SpotifyView>
{
// albumImageGroup
@Reactive(targetUid = "albumImageGroup", property = "padding", initiativeSync = true)
public ReactiveObject<Padding> albumImageGroupPadding = new ReactiveObject<>(){};

// albumImage
@Reactive(targetUid = "albumImage", property = "url", initiativeSync = true)
public ReactiveObject<String> albumImageUrl = new ReactiveObject<>(){};

@Reactive(targetUid = "albumImage", property = "width", initiativeSync = true)
public ReactiveObject<Float> albumImageWidth = new ReactiveObject<>(){};

@Reactive(targetUid = "albumImage", property = "height", initiativeSync = true)
public ReactiveObject<Float> albumImageHeight = new ReactiveObject<>(){};

@Reactive(targetUid = "albumImage", property = "padding", initiativeSync = true)
public ReactiveObject<Padding> albumImagePadding = new ReactiveObject<>(){};

// trackTitleGroup
@Reactive(targetUid = "trackTitleGroup", property = "enabled", initiativeSync = true)
public ReactiveObject<Boolean> trackTitleGroupEnabled = new ReactiveObject<>(){};

// trackTitle
@Reactive(targetUid = "trackTitle", property = "text", initiativeSync = true)
public ReactiveObject<String> trackTitleText = new ReactiveObject<>(){};

@Reactive(targetUid = "trackTitle", property = "xShiftSpeed", initiativeSync = true)
public ReactiveObject<Float> trackTitleXShiftSpeed = new ReactiveObject<>(){};

// progressBarGroup
@Reactive(targetUid = "progressBarGroup", property = "padding", initiativeSync = true)
public ReactiveObject<Padding> progressBarGroupPadding = new ReactiveObject<>(){};

// progressBar
@Reactive(targetUid = "progressBar", property = "percentage", initiativeSync = true)
public ReactiveObject<Float> progressBarPercentage = new ReactiveObject<>(){};

@Reactive(targetUid = "progressBar", property = "width", initiativeSync = true)
public ReactiveObject<Float> progressBarWidth = new ReactiveObject<>(){};

// anotherTrackTitleGroup
@Reactive(targetUid = "anotherTrackTitleGroup", property = "enabled", initiativeSync = true)
public ReactiveObject<Boolean> anotherTrackTitleGroupEnabled = new ReactiveObject<>(){};

@Reactive(targetUid = "anotherTrackTitle", property = "text", initiativeSync = true)
public ReactiveObject<String> anotherTrackTitleText = new ReactiveObject<>(){};

@Reactive(targetUid = "anotherTrackTitle", property = "xShiftSpeed", initiativeSync = true)
public ReactiveObject<Float> anotherTrackTitleXShiftSpeed = new ReactiveObject<>(){};

@Reactive(targetUid = "anotherTrackAuthor", property = "text", initiativeSync = true)
public ReactiveObject<String> anotherTrackAuthorText = new ReactiveObject<>(){};

@Reactive(targetUid = "anotherTrackTimer", property = "text", initiativeSync = true)
public ReactiveObject<String> anotherTrackTimerText = new ReactiveObject<>(){};

// editButton
@Reactive(targetUid = "editButton", property = "enabled", initiativeSync = true)
public ReactiveObject<Boolean> editButtonEnabled = new ReactiveObject<>(){};

@Reactive(targetUid = "editButton", property = "addClickListener", initiativeSync = true)
public ReactiveObject<IMouseClickButton> editButtonAddClickListener = new ReactiveObject<>(){};

private boolean extendedLayout = false;
private float durationMs = 0;
private float estimatedProgressMs;
private boolean isPlaying = false;
Expand Down Expand Up @@ -109,12 +155,38 @@ private void refreshTrackInfo()
durationMs = trackPlaying.durationMs;
estimatedProgressMs = trackPlaying.progressMs;
isPlaying = trackPlaying.isPlaying;

anotherTrackTitleXShiftSpeed.set(8f);
if (!anotherTrackTitleText.get().equals(trackPlaying.trackName))
anotherTrackTitleText.set(trackPlaying.trackName);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < trackPlaying.artists.size(); i++)
{
builder.append(trackPlaying.artists.get(i));
if (i != trackPlaying.artists.size() - 1) builder.append(", ");
}
String authors = builder.toString();
if (!anotherTrackAuthorText.get().equals(authors))
anotherTrackAuthorText.set(authors);
anotherTrackTimerText.set(calcTimeText(percentage));
}
}
catch (Exception ignored) { }
return null;
});
}
private String calcTimeText(float percentage)
{
int totalSeconds = (int)durationMs / 1000;
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
String time = String.format("%d:%02d", minutes, seconds);
totalSeconds = (int)(percentage * totalSeconds);
minutes = totalSeconds / 60;
seconds = totalSeconds % 60;
time = String.format("%d:%02d", minutes, seconds) + " / " + time;
return time;
}

@Override
public void start()
Expand All @@ -129,11 +201,42 @@ public void start()

albumImageUrl.set("");
progressBarPercentage.set(0f);
editButtonEnabled.set(false);

trackTitleGroupEnabled.set(true);
albumImageGroupPadding.set(new Padding(10f, 5f, 0f, 0f));
albumImageWidth.set(40f);
albumImageHeight.set(40f);
albumImagePadding.set(new Padding(0f, 0f, 10f, 10f));
progressBarGroupPadding.set(new Padding(0f, 10f, 0f, 0f));
progressBarWidth.set(50f);
anotherTrackTitleGroupEnabled.set(false);

editButtonAddClickListener.set((IMouseClickButton)(() ->
{
if (Minecraft.getMinecraft().player != null)
Minecraft.getMinecraft().player.sendChatMessage("test");
if (extendedLayout)
{
extendedLayout = false;
trackTitleGroupEnabled.set(true);
albumImageGroupPadding.set(new Padding(10f, 5f, 0f, 0f));
albumImageWidth.set(40f);
albumImageHeight.set(40f);
albumImagePadding.set(new Padding(0f, 0f, 10f, 10f));
progressBarGroupPadding.set(new Padding(0f, 10f, 0f, 0f));
progressBarWidth.set(50f);
anotherTrackTitleGroupEnabled.set(false);
}
else
{
extendedLayout = true;
trackTitleGroupEnabled.set(false);
albumImageGroupPadding.set(new Padding(5f, 5f, 0f, 0f));
albumImageWidth.set(60f);
albumImageHeight.set(60f);
albumImagePadding.set(new Padding(0f, 0f, 5f, 5f));
progressBarGroupPadding.set(new Padding(0f, 5f, 0f, 0f));
progressBarWidth.set(115f);
anotherTrackTitleGroupEnabled.set(true);
}
}));

EventCenter.spotifyOverlayEvent.addListener((flag) ->
Expand All @@ -155,6 +258,11 @@ public void start()
trackTitleXShiftSpeed.set(20f);
trackTitleText.set("Please wait... And make sure you play a track on Spotify");

anotherTrackTitleXShiftSpeed.set(20f);
anotherTrackTitleText.set("Please wait... And make sure you play a track on Spotify");
anotherTrackAuthorText.set("");
anotherTrackTimerText.set("");

refreshTokenIfNeeded(() ->
{
try
Expand All @@ -172,6 +280,17 @@ public void start()
durationMs = trackPlaying.durationMs;
estimatedProgressMs = trackPlaying.progressMs;
isPlaying = trackPlaying.isPlaying;

anotherTrackTitleXShiftSpeed.set(8f);
anotherTrackTitleText.set(trackPlaying.trackName);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < trackPlaying.artists.size(); i++)
{
builder.append(trackPlaying.artists.get(i));
if (i != trackPlaying.artists.size() - 1) builder.append(", ");
}
anotherTrackAuthorText.set(builder.toString());
anotherTrackTimerText.set(calcTimeText(percentage));
}
}
catch (Exception ignored) { }
Expand Down Expand Up @@ -271,6 +390,9 @@ public void onFixedUpdate(double deltaTime)
}
float percentage = estimatedProgressMs / durationMs;
progressBarPercentage.set(percentage);
String timeText = calcTimeText(percentage);
if (!anotherTrackTimerText.get().equals(timeText))
anotherTrackTimerText.set(timeText);
}
}
}
Expand Down

0 comments on commit 4dd8342

Please sign in to comment.