Skip to content

Commit

Permalink
Created a method to implement different types of button universally
Browse files Browse the repository at this point in the history
  • Loading branch information
Moksh-Mehta7 committed Sep 17, 2023
1 parent 6ca65c1 commit cd34e9c
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 4 deletions.
7 changes: 7 additions & 0 deletions source/core/assets/configs/text.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"com.badlogic.gdx.graphics.g2d.BitmapFont": {
"default": {
"file": "images/ui/font.fnt"
}
}
}
Binary file added source/core/assets/images/ui/Button_Bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions source/core/assets/images/ui/font.fnt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
info face="Times New Roman" size=-32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=0 aa=1 padding=0,0,0,0 spacing=0,0 outline=0
common lineHeight=37 base=29 scaleW=256 scaleH=128 pages=1 packed=0 alphaChnl=0 redChnl=4 greenChnl=4 blueChnl=4
page id=0 file="font.png"
chars count=95
char id=32 x=0 y=0 width=0 height=0 xoffset=0 yoffset=0 xadvance=8 page=0 chnl=15
char id=33 x=251 y=20 width=4 height=23 xoffset=4 yoffset=6 xadvance=12 page=0 chnl=15
char id=34 x=47 y=96 width=9 height=10 xoffset=2 yoffset=6 xadvance=13 page=0 chnl=15
char id=35 x=151 y=15 width=15 height=23 xoffset=1 yoffset=6 xadvance=17 page=0 chnl=15
char id=36 x=169 y=77 width=13 height=26 xoffset=2 yoffset=5 xadvance=17 page=0 chnl=15
char id=37 x=0 y=82 width=25 height=24 xoffset=1 yoffset=6 xadvance=27 page=0 chnl=15
char id=38 x=21 y=53 width=23 height=24 xoffset=1 yoffset=6 xadvance=25 page=0 chnl=15
char id=39 x=61 y=96 width=3 height=10 xoffset=1 yoffset=6 xadvance=5 page=0 chnl=15
char id=40 x=218 y=37 width=9 height=30 xoffset=1 yoffset=6 xadvance=10 page=0 chnl=15
char id=41 x=225 y=67 width=9 height=30 xoffset=0 yoffset=6 xadvance=10 page=0 chnl=15
char id=42 x=232 y=0 width=11 height=13 xoffset=2 yoffset=6 xadvance=15 page=0 chnl=15
char id=43 x=155 y=105 width=17 height=18 xoffset=1 yoffset=10 xadvance=19 page=0 chnl=15
char id=44 x=56 y=96 width=5 height=9 xoffset=1 yoffset=25 xadvance=8 page=0 chnl=15
char id=45 x=52 y=24 width=8 height=3 xoffset=1 yoffset=20 xadvance=10 page=0 chnl=15
char id=46 x=38 y=77 width=3 height=4 xoffset=2 yoffset=25 xadvance=7 page=0 chnl=15
char id=47 x=198 y=100 width=9 height=23 xoffset=0 yoffset=6 xadvance=9 page=0 chnl=15
char id=48 x=166 y=15 width=14 height=23 xoffset=1 yoffset=6 xadvance=16 page=0 chnl=15
char id=49 x=206 y=15 width=10 height=22 xoffset=3 yoffset=7 xadvance=16 page=0 chnl=15
char id=50 x=141 y=82 width=15 height=23 xoffset=1 yoffset=6 xadvance=16 page=0 chnl=15
char id=51 x=156 y=82 width=13 height=23 xoffset=1 yoffset=6 xadvance=16 page=0 chnl=15
char id=52 x=160 y=38 width=14 height=22 xoffset=1 yoffset=7 xadvance=16 page=0 chnl=15
char id=53 x=184 y=55 width=13 height=22 xoffset=2 yoffset=7 xadvance=16 page=0 chnl=15
char id=54 x=180 y=15 width=13 height=23 xoffset=1 yoffset=6 xadvance=16 page=0 chnl=15
char id=55 x=141 y=105 width=14 height=22 xoffset=1 yoffset=7 xadvance=16 page=0 chnl=15
char id=56 x=186 y=100 width=12 height=23 xoffset=2 yoffset=6 xadvance=16 page=0 chnl=15
char id=57 x=182 y=77 width=13 height=23 xoffset=1 yoffset=6 xadvance=16 page=0 chnl=15
char id=58 x=236 y=13 width=3 height=15 xoffset=3 yoffset=14 xadvance=9 page=0 chnl=15
char id=59 x=251 y=0 width=5 height=20 xoffset=2 yoffset=14 xadvance=9 page=0 chnl=15
char id=60 x=167 y=60 width=17 height=17 xoffset=1 yoffset=10 xadvance=19 page=0 chnl=15
char id=61 x=29 y=22 width=17 height=7 xoffset=1 yoffset=15 xadvance=19 page=0 chnl=15
char id=62 x=174 y=38 width=17 height=17 xoffset=1 yoffset=10 xadvance=19 page=0 chnl=15
char id=63 x=195 y=77 width=12 height=23 xoffset=1 yoffset=6 xadvance=14 page=0 chnl=15
char id=64 x=0 y=0 width=29 height=30 xoffset=1 yoffset=6 xadvance=30 page=0 chnl=15
char id=65 x=0 y=106 width=24 height=22 xoffset=-1 yoffset=7 xadvance=23 page=0 chnl=15
char id=66 x=71 y=95 width=20 height=22 xoffset=0 yoffset=7 xadvance=22 page=0 chnl=15
char id=67 x=70 y=71 width=20 height=24 xoffset=1 yoffset=6 xadvance=21 page=0 chnl=15
char id=68 x=47 y=74 width=23 height=22 xoffset=0 yoffset=7 xadvance=24 page=0 chnl=15
char id=69 x=90 y=71 width=20 height=22 xoffset=0 yoffset=7 xadvance=20 page=0 chnl=15
char id=70 x=111 y=44 width=17 height=22 xoffset=0 yoffset=7 xadvance=18 page=0 chnl=15
char id=71 x=25 y=82 width=22 height=24 xoffset=1 yoffset=6 xadvance=23 page=0 chnl=15
char id=72 x=48 y=106 width=23 height=22 xoffset=0 yoffset=7 xadvance=24 page=0 chnl=15
char id=73 x=216 y=15 width=10 height=22 xoffset=0 yoffset=7 xadvance=11 page=0 chnl=15
char id=74 x=197 y=53 width=12 height=23 xoffset=0 yoffset=7 xadvance=12 page=0 chnl=15
char id=75 x=24 y=106 width=24 height=22 xoffset=0 yoffset=7 xadvance=24 page=0 chnl=15
char id=76 x=91 y=46 width=20 height=22 xoffset=0 yoffset=7 xadvance=20 page=0 chnl=15
char id=77 x=29 y=0 width=28 height=22 xoffset=0 yoffset=7 xadvance=29 page=0 chnl=15
char id=78 x=57 y=0 width=24 height=22 xoffset=-1 yoffset=7 xadvance=23 page=0 chnl=15
char id=79 x=78 y=22 width=21 height=24 xoffset=1 yoffset=6 xadvance=23 page=0 chnl=15
char id=80 x=118 y=22 width=17 height=22 xoffset=0 yoffset=7 xadvance=18 page=0 chnl=15
char id=81 x=0 y=53 width=21 height=29 xoffset=1 yoffset=6 xadvance=23 page=0 chnl=15
char id=82 x=81 y=0 width=22 height=22 xoffset=0 yoffset=7 xadvance=21 page=0 chnl=15
char id=83 x=172 y=103 width=14 height=24 xoffset=2 yoffset=6 xadvance=18 page=0 chnl=15
char id=84 x=103 y=0 width=18 height=22 xoffset=1 yoffset=7 xadvance=19 page=0 chnl=15
char id=85 x=30 y=29 width=24 height=23 xoffset=-1 yoffset=7 xadvance=22 page=0 chnl=15
char id=86 x=44 y=52 width=24 height=22 xoffset=0 yoffset=7 xadvance=24 page=0 chnl=15
char id=87 x=0 y=30 width=30 height=23 xoffset=0 yoffset=7 xadvance=30 page=0 chnl=15
char id=88 x=54 y=27 width=24 height=22 xoffset=0 yoffset=7 xadvance=24 page=0 chnl=15
char id=89 x=68 y=49 width=23 height=22 xoffset=0 yoffset=7 xadvance=23 page=0 chnl=15
char id=90 x=99 y=22 width=19 height=22 xoffset=0 yoffset=7 xadvance=20 page=0 chnl=15
char id=91 x=227 y=30 width=8 height=29 xoffset=3 yoffset=7 xadvance=12 page=0 chnl=15
char id=92 x=209 y=53 width=9 height=23 xoffset=0 yoffset=6 xadvance=9 page=0 chnl=15
char id=93 x=234 y=59 width=8 height=29 xoffset=1 yoffset=7 xadvance=12 page=0 chnl=15
char id=94 x=91 y=116 width=15 height=12 xoffset=0 yoffset=6 xadvance=15 page=0 chnl=15
char id=95 x=52 y=22 width=17 height=2 xoffset=0 yoffset=35 xadvance=17 page=0 chnl=15
char id=96 x=46 y=22 width=6 height=6 xoffset=1 yoffset=6 xadvance=11 page=0 chnl=15
char id=97 x=195 y=0 width=13 height=15 xoffset=1 yoffset=14 xadvance=15 page=0 chnl=15
char id=98 x=128 y=44 width=16 height=23 xoffset=-1 yoffset=6 xadvance=16 page=0 chnl=15
char id=99 x=208 y=0 width=12 height=15 xoffset=1 yoffset=14 xadvance=14 page=0 chnl=15
char id=100 x=135 y=15 width=16 height=23 xoffset=1 yoffset=6 xadvance=16 page=0 chnl=15
char id=101 x=220 y=0 width=12 height=15 xoffset=1 yoffset=14 xadvance=14 page=0 chnl=15
char id=102 x=193 y=15 width=13 height=23 xoffset=1 yoffset=6 xadvance=10 page=0 chnl=15
char id=103 x=152 y=60 width=15 height=22 xoffset=1 yoffset=14 xadvance=16 page=0 chnl=15
char id=104 x=91 y=93 width=17 height=23 xoffset=0 yoffset=6 xadvance=16 page=0 chnl=15
char id=105 x=243 y=0 width=8 height=23 xoffset=0 yoffset=6 xadvance=9 page=0 chnl=15
char id=106 x=229 y=97 width=9 height=30 xoffset=-3 yoffset=6 xadvance=9 page=0 chnl=15
char id=107 x=108 y=93 width=17 height=23 xoffset=0 yoffset=6 xadvance=16 page=0 chnl=15
char id=108 x=235 y=30 width=9 height=23 xoffset=0 yoffset=6 xadvance=9 page=0 chnl=15
char id=109 x=110 y=68 width=25 height=15 xoffset=0 yoffset=14 xadvance=24 page=0 chnl=15
char id=110 x=145 y=0 width=17 height=15 xoffset=0 yoffset=14 xadvance=16 page=0 chnl=15
char id=111 x=191 y=38 width=14 height=15 xoffset=1 yoffset=14 xadvance=16 page=0 chnl=15
char id=112 x=125 y=83 width=16 height=22 xoffset=-1 yoffset=14 xadvance=16 page=0 chnl=15
char id=113 x=125 y=105 width=16 height=22 xoffset=1 yoffset=14 xadvance=16 page=0 chnl=15
char id=114 x=217 y=107 width=12 height=15 xoffset=0 yoffset=14 xadvance=11 page=0 chnl=15
char id=115 x=226 y=15 width=10 height=15 xoffset=2 yoffset=14 xadvance=13 page=0 chnl=15
char id=116 x=207 y=107 width=10 height=20 xoffset=0 yoffset=9 xadvance=10 page=0 chnl=15
char id=117 x=135 y=67 width=17 height=15 xoffset=-1 yoffset=14 xadvance=15 page=0 chnl=15
char id=118 x=162 y=0 width=17 height=15 xoffset=-1 yoffset=14 xadvance=16 page=0 chnl=15
char id=119 x=121 y=0 width=24 height=15 xoffset=0 yoffset=14 xadvance=23 page=0 chnl=15
char id=120 x=179 y=0 width=16 height=15 xoffset=0 yoffset=14 xadvance=16 page=0 chnl=15
char id=121 x=144 y=38 width=16 height=22 xoffset=0 yoffset=14 xadvance=16 page=0 chnl=15
char id=122 x=205 y=38 width=13 height=15 xoffset=1 yoffset=14 xadvance=15 page=0 chnl=15
char id=123 x=207 y=76 width=9 height=31 xoffset=4 yoffset=6 xadvance=15 page=0 chnl=15
char id=124 x=238 y=88 width=2 height=30 xoffset=2 yoffset=6 xadvance=7 page=0 chnl=15
char id=125 x=216 y=76 width=9 height=31 xoffset=3 yoffset=6 xadvance=16 page=0 chnl=15
char id=126 x=21 y=77 width=17 height=5 xoffset=1 yoffset=18 xadvance=18 page=0 chnl=15
Binary file added source/core/assets/images/ui/font.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.ui.ButtonFactory;
import com.csse3200.game.ui.UIComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -58,10 +59,21 @@ private void addActors() {
title.setHeight(Gdx.graphics.getHeight());
title.setPosition(0, 0);

TextButton startBtn = new TextButton("Start", skin);
TextButton loadBtn = new TextButton("Help", skin);
TextButton settingsBtn = new TextButton("Settings", skin);
TextButton exitBtn = new TextButton("Quit", skin);
// Create an instance of the ButtonFactory class
ButtonFactory buttonFactory = new ButtonFactory();

// Create a "Start" TextButton using the default style
TextButton startBtn = buttonFactory.createButton("Start");

// Create a "Help" TextButton using the default style
TextButton loadBtn = buttonFactory.createButton("Help");

// Create a "Settings" TextButton with a custom image
TextButton settingsBtn = buttonFactory.createCustomButton("Settings", "images/ui/Sprites/UI_Glass_Button_Large_Lock_01a2.png");

// Create a "Quit" TextButton with a custom image
TextButton exitBtn = buttonFactory.createCustomButton("Quit", "images/ui/Sprites/UI_Glass_Button_Large_Press_01a2.png");


// Triggers an event when the button is pressed
startBtn.addListener(
Expand Down
108 changes: 108 additions & 0 deletions source/core/src/main/com/csse3200/game/ui/ButtonFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* The ButtonFactory class is responsible for creating different types of TextButtons
* with customizable styles and properties for use in a LibGDX user interface.
*/
package com.csse3200.game.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

/**
* This class provides methods for creating various types of TextButtons with different styles.
*/
public class ButtonFactory {
private Skin defaultSkin;

/**
* Constructs a ButtonFactory and initializes the default skin.
*/
public ButtonFactory() {
defaultSkin = createDefaultSkin();
}

/**
* Creates a default skin for TextButtons using a JSON file for configuration.
*
* @return The default Skin object.
*/
private Skin createDefaultSkin() {
Skin skin = new Skin(Gdx.files.internal("configs/text.json"));

// Load the background image for the default button style
TextureRegionDrawable buttonBackground = new TextureRegionDrawable(
new TextureRegion(new Texture("images/ui/Button_Bg.png"))
);

// Define the button style with the background image
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
style.font = skin.getFont("default");
style.up = new TextureRegionDrawable(new TextureRegion(new Texture("images/ui/Sprites/UI_Glass_Button_Large_Lock_01a1.png"))); // Set the button background to the loaded image

skin.add("default", style);
return skin;
}

/**
* Creates a TextButton with the specified text using the default skin.
*
* @param text The text to display on the button.
* @return The created TextButton.
*/
public TextButton createButton(String text) {
TextButton button = new TextButton(text, defaultSkin);
button.getLabel().setFontScale(0.8f); // Adjust text size
button.pad(10f); // Adjust padding
return button;
}

/**
* Creates a custom TextButton with the specified text and a custom image.
*
* @param text The text to display on the button.
* @param customImagePath The path to the custom image for the button.
* @return The created custom TextButton.
*/
public TextButton createCustomButton(String text, String customImagePath) {
// Create a custom button with a PNG image
Texture customTexture = new Texture(Gdx.files.internal(customImagePath));
TextureRegionDrawable customDrawable = new TextureRegionDrawable(customTexture);

TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
style.font = defaultSkin.getFont("default");
style.up = customDrawable;

TextButton button = new TextButton(text, style);
button.getLabel().setFontScale(0.8f); // Adjust text size
button.pad(10f); // Adjust padding

return button;
}

/**
* Creates a custom TextButton with the specified text and an image from a TextureAtlas.
*
* @param text The text to display on the button.
* @param atlasPath The path to the TextureAtlas containing the button image.
* @return The created custom TextButton.
*/
public TextButton createCustomButtonWithAtlas(String text, String atlasPath) {
// Create a custom button with a TextureAtlas
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal(atlasPath));
Skin customSkin = new Skin(atlas);

TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
style.font = customSkin.getFont("default"); // for custom font if specified in the atlas
style.up = customSkin.getDrawable("button"); // for the button drawable from the atlas

TextButton button = new TextButton(text, style);
button.getLabel().setFontScale(0.8f); // Adjust text size
button.pad(10f); // Adjust padding

return button;
}
}

0 comments on commit cd34e9c

Please sign in to comment.