Moved most Image creation into GameImage enum.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-01-07 23:45:21 -05:00
parent 8671718ce3
commit f6eac71643
10 changed files with 154 additions and 87 deletions

View File

@ -204,13 +204,100 @@ public enum GameImage {
SCORE_COMMA ("score-comma.png"), SCORE_COMMA ("score-comma.png"),
SCORE_DOT ("score-dot.png"), SCORE_DOT ("score-dot.png"),
SCORE_PERCENT ("score-percent.png"), SCORE_PERCENT ("score-percent.png"),
SCORE_X ("score-x.png"); SCORE_X ("score-x.png"),
// Non-Game Components
MENU_BACK ("menu-back.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h * 0.1f) / img.getHeight());
}
},
MENU_BUTTON_BG ("menu-button-background.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy(w / 2, h / 6);
}
},
MENU_TAB ("selection-tab.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h * 0.033f) / img.getHeight());
}
},
MENU_SEARCH ("search.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy(Utils.FONT_BOLD.getLineHeight() * 2f / img.getHeight());
}
},
MENU_OPTIONS ("options.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy(Utils.FONT_BOLD.getLineHeight() * 2f / img.getHeight());
}
},
MENU_MUSICNOTE ("music-note.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
int r = (int) (Utils.FONT_LARGE.getLineHeight() * 0.75f + Utils.FONT_DEFAULT.getLineHeight());
return img.getScaledCopy(r, r);
}
},
MENU_LOADER ("loader.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
int r = (int) (Utils.FONT_LARGE.getLineHeight() * 0.75f + Utils.FONT_DEFAULT.getLineHeight());
return img.getScaledCopy(r / 48f);
}
},
MENU_LOGO ("logo.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h / 1.2f) / img.getHeight());
}
},
// TODO: scale MENU_PLAY and MENU_EXIT
MENU_PlAY ("menu-play.png", false),
MENU_EXIT ("menu-exit.png", false),
MENU_BUTTON_MID ("button-middle.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy(w / 2, img.getHeight());
}
},
MENU_BUTTON_LEFT ("button-left.png", false),
MENU_BUTTON_RIGHT ("button-right.png", false),
MUSIC_PLAY ("music-play.png", false),
MUSIC_PAUSE ("music-pause.png", false),
MUSIC_NEXT ("music-next.png", false),
MUSIC_PREVIOUS ("music-previous.png", false),
RANKING_RETRY ("ranking-retry.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h * 0.15f) / img.getHeight());
}
},
RANKING_EXIT ("ranking-back.png", false) {
@Override
protected Image process_sub(Image img, int w, int h) {
return img.getScaledCopy((h * 0.15f) / img.getHeight());
}
};
/** /**
* The file name. * The file name.
*/ */
private String filename; private String filename;
/**
* Whether or not the image is related to gameplay.
* Game images are skinnable per beatmap, while other images are not.
*/
private boolean gameImage;
/** /**
* The default image. * The default image.
*/ */
@ -237,13 +324,30 @@ public enum GameImage {
} }
/** /**
* Constructor. * Game image constructor.
* @param filename the image file name * @param filename the image file name
*/ */
GameImage(String filename) { GameImage(String filename) {
this.filename = filename; this.filename = filename;
this.gameImage = true;
} }
/**
* Game image constructor.
* @param filename the image file name
* @param gameImage whether or not the image is related to gameplay
*/
GameImage(String filename, boolean gameImage) {
this.filename = filename;
this.gameImage = gameImage;
}
/**
* Returns whether or not the image is related to gameplay.
* @return true if game image
*/
public boolean isGameImage() { return gameImage; }
/** /**
* Returns the image associated with this resource. * Returns the image associated with this resource.
* The skin image takes priority over the default image. * The skin image takes priority over the default image.

View File

@ -178,12 +178,13 @@ public enum SongSort {
* @param width the container width * @param width the container width
* @param height the container height * @param height the container height
*/ */
public void init(Image img, int width, int height) { public void init(int width, int height) {
Image tab = GameImage.MENU_TAB.getImage();
float buttonX = width * 0.6f; float buttonX = width * 0.6f;
float tabOffset = (width - buttonX - img.getWidth()) / (SIZE - 1); float tabOffset = (width - buttonX - tab.getWidth()) / (SIZE - 1);
this.tab = new MenuButton(img, this.tab = new MenuButton(tab,
(buttonX + (img.getWidth() / 2f)) + (id * tabOffset), (buttonX + (tab.getWidth() / 2f)) + (id * tabOffset),
(height * 0.15f) - (img.getHeight() / 2f) - 2f (height * 0.15f) - (tab.getHeight() / 2f) - 2f
); );
} }

View File

@ -88,11 +88,6 @@ public class Utils {
*/ */
private static MenuButton backButton; private static MenuButton backButton;
/**
* Tab image (shared by other states).
*/
private static Image tab;
/** /**
* Cursor image and trail. * Cursor image and trail.
*/ */
@ -188,20 +183,6 @@ public class Utils {
Log.error("Failed to load fonts.", e); Log.error("Failed to load fonts.", e);
} }
// tab image
tab = new Image("selection-tab.png");
float tabScale = (height * 0.033f) / tab.getHeight();
tab = tab.getScaledCopy(tabScale);
// back button
Image back = new Image("menu-back.png");
float scale = (height * 0.1f) / back.getHeight();
back = back.getScaledCopy(scale);
backButton = new MenuButton(back,
back.getWidth() / 2f,
height - (back.getHeight() / 2f));
backButton.setHoverDir(MenuButton.Expand.UP_RIGHT);
// set default game images // set default game images
GameImage.init(width, height); GameImage.init(width, height);
for (GameImage img : GameImage.values()) { for (GameImage img : GameImage.values()) {
@ -215,13 +196,15 @@ public class Utils {
// initialize sorts // initialize sorts
for (SongSort sort : SongSort.values()) for (SongSort sort : SongSort.values())
sort.init(tab, width, height); sort.init(width, height);
}
/** // back button
* Returns the 'selection-tab' image. Image back = GameImage.MENU_BACK.getImage();
*/ backButton = new MenuButton(back,
public static Image getTabImage() { return tab; } back.getWidth() / 2f,
height - (back.getHeight() / 2f));
backButton.setHoverDir(MenuButton.Expand.UP_RIGHT);
}
/** /**
* Returns the 'menu-back' MenuButton. * Returns the 'menu-back' MenuButton.

View File

@ -858,7 +858,7 @@ public class Game extends BasicGameState {
// set images // set images
File parent = osu.getFile().getParentFile(); File parent = osu.getFile().getParentFile();
for (GameImage img : GameImage.values()) { for (GameImage img : GameImage.values()) {
if (img.setSkinImage(parent)) if (img.isGameImage() && img.setSkinImage(parent))
img.process(); img.process();
} }

View File

@ -18,6 +18,7 @@
package itdelatrisu.opsu.states; package itdelatrisu.opsu.states;
import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.GameMod; import itdelatrisu.opsu.GameMod;
import itdelatrisu.opsu.GameScore; import itdelatrisu.opsu.GameScore;
import itdelatrisu.opsu.MenuButton; import itdelatrisu.opsu.MenuButton;
@ -80,11 +81,8 @@ public class GameRanking extends BasicGameState {
int height = container.getHeight(); int height = container.getHeight();
// buttons // buttons
Image retry = new Image("ranking-retry.png"); Image retry = GameImage.RANKING_RETRY.getImage();
Image exit = new Image("ranking-back.png"); Image exit = GameImage.RANKING_EXIT.getImage();
float scale = (height * 0.15f) / retry.getHeight();
retry = retry.getScaledCopy(scale);
exit = exit.getScaledCopy(scale);
retryButton = new MenuButton(retry, retryButton = new MenuButton(retry,
width - (retry.getWidth() / 2f), width - (retry.getWidth() / 2f),
(height * 0.97f) - (exit.getHeight() * 1.5f) (height * 0.97f) - (exit.getHeight() * 1.5f)

View File

@ -18,6 +18,7 @@
package itdelatrisu.opsu.states; package itdelatrisu.opsu.states;
import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.MenuButton; import itdelatrisu.opsu.MenuButton;
import itdelatrisu.opsu.Opsu; import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuFile; import itdelatrisu.opsu.OsuFile;
@ -125,14 +126,15 @@ public class MainMenu extends BasicGameState {
int height = container.getHeight(); int height = container.getHeight();
// initialize buttons // initialize buttons
// TODO: clean this up, scale in GameImage.process_sub()
Image logoImg = new Image("logo.png"); Image logoImg = new Image("logo.png");
float buttonScale = (height / 1.2f) / logoImg.getHeight(); float buttonScale = (height / 1.2f) / logoImg.getHeight();
Image logoImgScaled = logoImg.getScaledCopy(buttonScale); Image logoImgScaled = GameImage.MENU_LOGO.getImage();
logo = new MenuButton(logoImgScaled, width / 2f, height / 2f); logo = new MenuButton(logoImgScaled, width / 2f, height / 2f);
logo.setHoverScale(1.05f); logo.setHoverScale(1.05f);
Image playImg = new Image("menu-play.png"); Image playImg = GameImage.MENU_PlAY.getImage();
Image exitImg = new Image("menu-exit.png"); Image exitImg = GameImage.MENU_EXIT.getImage();
playImg = playImg.getScaledCopy((logoImg.getWidth() * 0.83f) / playImg.getWidth()); playImg = playImg.getScaledCopy((logoImg.getWidth() * 0.83f) / playImg.getWidth());
exitImg = exitImg.getScaledCopy((logoImg.getWidth() * 0.66f) / exitImg.getWidth()); exitImg = exitImg.getScaledCopy((logoImg.getWidth() * 0.66f) / exitImg.getWidth());
float exitOffset = (playImg.getWidth() - exitImg.getWidth()) / 3f; float exitOffset = (playImg.getWidth() - exitImg.getWidth()) / 3f;
@ -148,10 +150,10 @@ public class MainMenu extends BasicGameState {
// initialize music buttons // initialize music buttons
int musicWidth = 48; int musicWidth = 48;
int musicHeight = 30; int musicHeight = 30;
musicPlay = new MenuButton(new Image("music-play.png"), width - (2 * musicWidth), musicHeight); musicPlay = new MenuButton(GameImage.MUSIC_PLAY.getImage(), width - (2 * musicWidth), musicHeight);
musicPause = new MenuButton(new Image("music-pause.png"), width - (2 * musicWidth), musicHeight); musicPause = new MenuButton(GameImage.MUSIC_PAUSE.getImage(), width - (2 * musicWidth), musicHeight);
musicNext = new MenuButton(new Image("music-next.png"), width - musicWidth, musicHeight); musicNext = new MenuButton(GameImage.MUSIC_NEXT.getImage(), width - musicWidth, musicHeight);
musicPrevious = new MenuButton(new Image("music-previous.png"), width - (3 * musicWidth), musicHeight); musicPrevious = new MenuButton(GameImage.MUSIC_PREVIOUS.getImage(), width - (3 * musicWidth), musicHeight);
musicPlay.setHoverScale(1.5f); musicPlay.setHoverScale(1.5f);
musicPause.setHoverScale(1.5f); musicPause.setHoverScale(1.5f);
musicNext.setHoverScale(1.5f); musicNext.setHoverScale(1.5f);

View File

@ -18,6 +18,7 @@
package itdelatrisu.opsu.states; package itdelatrisu.opsu.states;
import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.MenuButton; import itdelatrisu.opsu.MenuButton;
import itdelatrisu.opsu.Opsu; import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.Utils; import itdelatrisu.opsu.Utils;
@ -72,10 +73,9 @@ public class MainMenuExit extends BasicGameState {
centerOffset = width / 18f; centerOffset = width / 18f;
// initialize buttons // initialize buttons
Image button = new Image("button-middle.png"); Image button = GameImage.MENU_BUTTON_MID.getImage();
Image buttonL = new Image("button-left.png"); Image buttonL = GameImage.MENU_BUTTON_LEFT.getImage();
Image buttonR = new Image("button-right.png"); Image buttonR = GameImage.MENU_BUTTON_RIGHT.getImage();
button = button.getScaledCopy(width / 2, button.getHeight());
yesButton = new MenuButton(button, buttonL, buttonR, yesButton = new MenuButton(button, buttonL, buttonR,
width / 2f + centerOffset, height * 0.2f width / 2f + centerOffset, height * 0.2f
); );

View File

@ -18,6 +18,7 @@
package itdelatrisu.opsu.states; package itdelatrisu.opsu.states;
import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.GameMod; import itdelatrisu.opsu.GameMod;
import itdelatrisu.opsu.MenuButton; import itdelatrisu.opsu.MenuButton;
import itdelatrisu.opsu.Opsu; import itdelatrisu.opsu.Opsu;
@ -434,7 +435,7 @@ public class Options extends BasicGameState {
offsetY = (int) (((height * 0.8f) - textY) / maxOptionsScreen); offsetY = (int) (((height * 0.8f) - textY) / maxOptionsScreen);
// option tabs // option tabs
Image tab = Utils.getTabImage(); Image tab = GameImage.MENU_TAB.getImage();
int subtextWidth = Utils.FONT_DEFAULT.getWidth("Click or drag an option to change it."); int subtextWidth = Utils.FONT_DEFAULT.getWidth("Click or drag an option to change it.");
float tabX = (width / 50) + (tab.getWidth() / 2f); float tabX = (width / 50) + (tab.getWidth() / 2f);
float tabY = 15 + Utils.FONT_XLARGE.getLineHeight() + (tab.getHeight() / 2f); float tabY = 15 + Utils.FONT_XLARGE.getLineHeight() + (tab.getHeight() / 2f);

View File

@ -18,6 +18,7 @@
package itdelatrisu.opsu.states; package itdelatrisu.opsu.states;
import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.MenuButton; import itdelatrisu.opsu.MenuButton;
import itdelatrisu.opsu.Opsu; import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuFile; import itdelatrisu.opsu.OsuFile;
@ -112,16 +113,6 @@ public class SongMenu extends BasicGameState {
*/ */
private String searchResultString; private String searchResultString;
/**
* Search icon.
*/
private Image searchIcon;
/**
* Music note icon.
*/
private Image musicNote;
/** /**
* Loader animation. * Loader animation.
*/ */
@ -148,7 +139,7 @@ public class SongMenu extends BasicGameState {
int height = container.getHeight(); int height = container.getHeight();
// song button background & graphics context // song button background & graphics context
Image menuBackground = new Image("menu-button-background.png").getScaledCopy(width / 2, height / 6); Image menuBackground = GameImage.MENU_BUTTON_BG.getImage();
OsuGroupNode.setBackground(menuBackground); OsuGroupNode.setBackground(menuBackground);
// song button coordinates // song button coordinates
@ -161,12 +152,8 @@ public class SongMenu extends BasicGameState {
// search // search
searchTimer = 0; searchTimer = 0;
searchResultString = "Type to search!"; searchResultString = "Type to search!";
Image searchIcon = GameImage.MENU_SEARCH.getImage();
searchIcon = new Image("search.png"); Image tab = GameImage.MENU_TAB.getImage();
float iconScale = Utils.FONT_BOLD.getLineHeight() * 2f / searchIcon.getHeight();
searchIcon = searchIcon.getScaledCopy(iconScale);
Image tab = Utils.getTabImage();
search = new TextField( search = new TextField(
container, Utils.FONT_DEFAULT, container, Utils.FONT_DEFAULT,
(int) buttonX + (tab.getWidth() / 2) + searchIcon.getWidth(), (int) buttonX + (tab.getWidth() / 2) + searchIcon.getWidth(),
@ -180,19 +167,13 @@ public class SongMenu extends BasicGameState {
search.setMaxLength(60); search.setMaxLength(60);
// options button // options button
Image optionsIcon = new Image("options.png").getScaledCopy(iconScale); Image optionsIcon = GameImage.MENU_OPTIONS.getImage();
optionsButton = new MenuButton(optionsIcon, search.getX() - (optionsIcon.getWidth() * 1.5f), search.getY()); optionsButton = new MenuButton(optionsIcon, search.getX() - (optionsIcon.getWidth() * 1.5f), search.getY());
optionsButton.setHoverScale(1.75f); optionsButton.setHoverScale(1.75f);
// music note
int musicNoteDim = (int) (Utils.FONT_LARGE.getLineHeight() * 0.75f + Utils.FONT_DEFAULT.getLineHeight());
musicNote = new Image("music-note.png").getScaledCopy(musicNoteDim, musicNoteDim);
// loader // loader
SpriteSheet spr = new SpriteSheet( int loaderDim = GameImage.MENU_MUSICNOTE.getImage().getWidth();
new Image("loader.png").getScaledCopy(musicNoteDim / 48f), SpriteSheet spr = new SpriteSheet(GameImage.MENU_LOADER.getImage(), loaderDim, loaderDim);
musicNoteDim, musicNoteDim
);
loader = new Animation(spr, 50); loader = new Animation(spr, 50);
} }
@ -219,6 +200,7 @@ public class SongMenu extends BasicGameState {
// header // header
if (focusNode != null) { if (focusNode != null) {
Image musicNote = GameImage.MENU_MUSICNOTE.getImage();
if (MusicController.isTrackLoading()) if (MusicController.isTrackLoading())
loader.draw(); loader.draw();
else else
@ -254,6 +236,7 @@ public class SongMenu extends BasicGameState {
SongSort.drawAll(); SongSort.drawAll();
// search // search
Image searchIcon = GameImage.MENU_SEARCH.getImage();
Utils.FONT_BOLD.drawString( Utils.FONT_BOLD.drawString(
search.getX(), search.getY() - Utils.FONT_BOLD.getLineHeight(), search.getX(), search.getY() - Utils.FONT_BOLD.getLineHeight(),
searchResultString, Color.white searchResultString, Color.white

View File

@ -18,6 +18,7 @@
package itdelatrisu.opsu.states; package itdelatrisu.opsu.states;
import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.Opsu; import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuParser; import itdelatrisu.opsu.OsuParser;
import itdelatrisu.opsu.OszUnpacker; import itdelatrisu.opsu.OszUnpacker;
@ -42,11 +43,6 @@ import org.newdawn.slick.state.StateBasedGame;
* Loads game resources and enters "Main Menu" state. * Loads game resources and enters "Main Menu" state.
*/ */
public class Splash extends BasicGameState { public class Splash extends BasicGameState {
/**
* Logo image.
*/
private Image logo;
/** /**
* Whether or not loading has completed. * Whether or not loading has completed.
*/ */
@ -66,19 +62,17 @@ public class Splash extends BasicGameState {
throws SlickException { throws SlickException {
this.container = container; this.container = container;
logo = new Image("logo.png");
logo = logo.getScaledCopy((container.getHeight() / 1.2f) / logo.getHeight());
logo.setAlpha(0f);
// load Utils class first (needed in other 'init' methods) // load Utils class first (needed in other 'init' methods)
Utils.init(container, game); Utils.init(container, game);
GameImage.MENU_LOGO.getImage().setAlpha(0f);
} }
@Override @Override
public void render(GameContainer container, StateBasedGame game, Graphics g) public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException { throws SlickException {
g.setBackground(Color.black); g.setBackground(Color.black);
logo.drawCentered(container.getWidth() / 2, container.getHeight() / 2); GameImage.MENU_LOGO.getImage().drawCentered(container.getWidth() / 2, container.getHeight() / 2);
// display progress // display progress
String unpackedFile = OszUnpacker.getCurrentFileName(); String unpackedFile = OszUnpacker.getCurrentFileName();
@ -131,9 +125,10 @@ public class Splash extends BasicGameState {
} }
// fade in logo // fade in logo
Image logo = GameImage.MENU_LOGO.getImage();
float alpha = logo.getAlpha(); float alpha = logo.getAlpha();
if (alpha < 1f) if (alpha < 1f)
logo.setAlpha(alpha + (delta / 400f)); logo.setAlpha(alpha + (delta / 500f));
// change states when loading complete // change states when loading complete
if (finished && alpha >= 1f) { if (finished && alpha >= 1f) {