Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple banners #476

Merged
merged 5 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 0 additions & 90 deletions src/com/reco1l/osu/BannerLoader.kt

This file was deleted.

148 changes: 148 additions & 0 deletions src/com/reco1l/osu/Banners.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package com.reco1l.osu

import android.content.Intent
import android.content.Intent.ACTION_VIEW
import android.net.Uri
import android.util.Log
import com.reco1l.andengine.sprite.ExtendedSprite
import com.reco1l.framework.net.JsonArrayRequest
import com.reco1l.framework.net.WebRequest
import com.reco1l.toolkt.data.writeToFile
import org.anddev.andengine.input.touch.TouchEvent
import org.anddev.andengine.opengl.texture.region.TextureRegion
import ru.nsu.ccfit.zuev.osu.Config
import ru.nsu.ccfit.zuev.osu.GlobalManager
import ru.nsu.ccfit.zuev.osu.ResourceManager
import ru.nsu.ccfit.zuev.osu.online.OnlineManager
import java.io.File

object BannerLoader {

@JvmStatic
fun loadBannerSprite(): BannerSprite? {

val banners = mutableListOf<Banner>()

try {

val bannersFolder = File(Config.getCachePath(), "banners").apply {
delete()
mkdirs()
}

JsonArrayRequest(OnlineManager.endpoint + "game/banner.php").use { request ->
request.execute()

for (i in 0 until request.json.length()) {

try {
val banner = request.json.getJSONObject(i)

val url = banner.getString("Url")
val imageLink = banner.getString("ImageLink")

WebRequest(imageLink).use { imageRequest ->

val bannerFile = File(bannersFolder, "${i}.png")
bannerFile.createNewFile()

imageRequest.execute()
imageRequest.responseBody.byteStream().writeToFile(bannerFile)

ResourceManager.getInstance().loadHighQualityFile("banner@${i}", bannerFile)
}

banners.add(Banner(url, ResourceManager.getInstance().getTexture("banner@${i}")))

} catch (e: Exception) {
Log.e("BannerManager", "Failed to get banner at index ${i}.", e)
}
}
}

} catch (e: Exception) {
Log.e("BannerManager", "Failed to get banners from server.", e)
}

if (banners.isEmpty()) {
return null
}

return BannerSprite(banners)
}

}

data class Banner(
val url: String,
val image: TextureRegion,
)

class BannerSprite(private val banners: List<Banner>) : ExtendedSprite() {


private var currentBannerIndex = 0

private var elapsedTimeSinceLastChange = 0f


init {
textureRegion = banners[currentBannerIndex].image
alpha = 0f
fadeIn(0.75f)
}


override fun onManagedUpdate(pSecondsElapsed: Float) {

if (elapsedTimeSinceLastChange > BANNER_DURATION) {
elapsedTimeSinceLastChange %= BANNER_DURATION

currentBannerIndex++
currentBannerIndex %= banners.size

val banner = banners[currentBannerIndex]

fadeOut(0.5f).then {
textureRegion = banner.image
fadeIn(0.5f)
}
}
elapsedTimeSinceLastChange += pSecondsElapsed

super.onManagedUpdate(pSecondsElapsed)
}

override fun onAreaTouched(event: TouchEvent, localX: Float, localY: Float): Boolean {

elapsedTimeSinceLastChange = 0f

if (event.isActionDown) {
clearEntityModifiers()
scaleTo(0.95f, 0.1f)
}

if (event.isActionUp || event.isActionCancel || event.isActionOutside) {
clearEntityModifiers()
scaleTo(1f, 0.1f)

val banner = banners[currentBannerIndex]

if (event.isActionUp && banner.url.isNotBlank()) {
val uri = Uri.parse(banner.url)
val intent = Intent(ACTION_VIEW, uri)
GlobalManager.getInstance().mainActivity.startActivity(intent)
}
}

return true
}


companion object {

private const val BANNER_DURATION = 10f // Seconds

}

}
7 changes: 3 additions & 4 deletions src/ru/nsu/ccfit/zuev/osu/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,12 @@ public void onLoadComplete() {

if (roomInviteLink != null) {
LobbyScene.INSTANCE.connectFromLink(roomInviteLink);
return;
}

if (willReplay) {
} else if (willReplay) {
GlobalManager.getInstance().getMainScene().watchReplay(beatmapToAdd);
willReplay = false;
}

GlobalManager.getInstance().getMainScene().loadBannerSprite();
});
});
}
Expand Down
20 changes: 16 additions & 4 deletions src/ru/nsu/ccfit/zuev/osu/MainScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.reco1l.andengine.shape.RoundedBox;
import com.reco1l.andengine.sprite.ExtendedSprite;
import com.reco1l.osu.BannerLoader;
import com.reco1l.osu.BannerSprite;
import com.reco1l.osu.data.BeatmapInfo;
import com.reco1l.osu.Execution;
import com.reco1l.osu.ui.entity.MainMenu;
Expand Down Expand Up @@ -457,10 +458,6 @@ public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX
scene.registerTouchArea(music_next);
scene.setTouchAreaBindingEnabled(true);

if (Config.isStayOnline()) {
BannerLoader.loadBanner(scene);
}

if (BuildConfig.DEBUG) {
ResourceManager.getInstance().loadHighQualityAsset("dev-build-overlay", "dev-build-overlay.png");

Expand Down Expand Up @@ -491,6 +488,21 @@ public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX
hitsound = ResourceManager.getInstance().loadSound("menuhit", "sfx/menuhit.ogg", false);
}

public void loadBannerSprite() {

if (!Config.isStayOnline()) {
return;
}

BannerSprite sprite = BannerLoader.loadBannerSprite();
if (sprite != null) {
sprite.setPosition(Config.getRES_WIDTH() - 10f, Config.getRES_HEIGHT() - 10f);
sprite.setOrigin(Anchor.BottomRight);
scene.attachChild(sprite);
scene.registerTouchArea(sprite);
}
}

private void createOnlinePanel(Scene scene) {
Config.loadOnlineConfig(context);
OnlineManager.getInstance().init();
Expand Down
Loading