Skip to content

Commit

Permalink
Support multiple banners
Browse files Browse the repository at this point in the history
  • Loading branch information
Reco1I committed Dec 24, 2024
1 parent 271e0f5 commit 47fa7f5
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 99 deletions.
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.2f).then {
textureRegion = banner.image
fadeIn(0.2f)
}
}
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 = 8f // Seconds

}

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

if (roomInviteLink != null) {
LobbyScene.INSTANCE.connectFromLink(roomInviteLink);
return;
} else {
if (willReplay) {
GlobalManager.getInstance().getMainScene().watchReplay(beatmapToAdd);
willReplay = false;
}
}

if (willReplay) {
GlobalManager.getInstance().getMainScene().watchReplay(beatmapToAdd);
willReplay = false;
}
GlobalManager.getInstance().getMainScene().loadBannerSprite();
});
});
}
Expand Down
19 changes: 15 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,20 @@ public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX
hitsound = ResourceManager.getInstance().loadSound("menuhit", "sfx/menuhit.ogg", false);
}

public void loadBannerSprite() {

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

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

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

0 comments on commit 47fa7f5

Please sign in to comment.