Skip to content

Instantly share code, notes, and snippets.

@seinecle
Created March 4, 2018 14:24
Show Gist options
  • Save seinecle/e5c70904cca0caf24ae256c44514baa4 to your computer and use it in GitHub Desktop.
Save seinecle/e5c70904cca0caf24ae256c44514baa4 to your computer and use it in GitHub Desktop.
3 MultiButtons not fully visible
package net.clementlevallois.net.cookieclicker.ultimate;
import com.codename1.charts.util.ColorUtil;
import com.codename1.components.MultiButton;
import com.codename1.components.ScaleImageButton;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Font;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.layouts.FlowLayout;
import com.codename1.ui.layouts.GridLayout;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.table.TableLayout;
import com.codename1.ui.util.Resources;
public class Form1 extends com.codename1.ui.Form {
Resources theme;
int score = 0;
int clickRate = 1;
public Form1() {
theme = Resources.getGlobalResources();
Image grandmaPic = theme.getImage("grandma_50x50.png");
Image grandmaBoostIcon = theme.getImage("oven_30x35.png");
Image farmPic = theme.getImage("farm_50x37.png");
Image farmBoostIcon = theme.getImage("farm_boost_30x23.png");
Image clickPic = theme.getImage("click_50x64.png");
Image clickBoostIcon = theme.getImage("click_30x30.png");
Image cookieCentraPic = theme.getImage("cookie_200x200.png");
Image cookiePressedPic = theme.getImage("cookie_200x200_star.png");
//we put the Form in a Grid layout: 3 rows, to divide the screen in three equal spaces.
GridLayout gridLayoutThreeByOne = new GridLayout(3, 1);
this.setLayout(gridLayoutThreeByOne);
//we don't want the screen to be scrollable: everything should fit in it.
this.setScrollable(false);
//we create a Container that will contain everything in the "North" part of the BorderLayout:
Container northRegion = new Container();
//we choose a Box Y Layout for this container:
northRegion.setLayout(new BorderLayout());
//we create a Container that will contain everything in the "Center" part of the BorderLayout:
Container centerRegion = new Container();
//we choose a Grid Layout for this container:
GridLayout gridLayoutTwoByOne = new GridLayout(3, 1);
centerRegion.setLayout(gridLayoutTwoByOne);
centerRegion.setScrollable(false);
//we create a Container that will contain everything in the "South" part of the BorderLayout:
Container southRegion = new Container();
//we choose a Border Layout for this container:
southRegion.setLayout(new BorderLayout());
// we add the three containers to the Form, which is in a Gridlayout (three rows)
// The northRegion will be in the top row because we add it first,
// The centerRegion will be in the 2nd row because we add it in second
// The southRegion will be in the 3rd row because we add it last.
this.addComponent(northRegion);
this.addComponent(centerRegion);
this.addComponent(southRegion);
// creating the cookie central button
ScaleImageButton cookieCentral = new ScaleImageButton();
cookieCentral.setIcon(cookieCentraPic);
cookieCentral.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
cookieCentral.getPressedStyle().setBgImage(cookiePressedPic);
//creating the score Label
Label scoreLabel = new Label();
scoreLabel.setText("0 cookies");
scoreLabel.getStyle().setAlignment(Component.CENTER); //centering the text
//creating the Label for the score per second
Label scorePerSecond = new Label();
scorePerSecond.setText("0 per second");
scorePerSecond.getStyle().setAlignment(Component.CENTER); //centering the text
//adding the cookie pic and the score to the north region
northRegion.add(BorderLayout.NORTH, scoreLabel);
northRegion.add(BorderLayout.CENTER, cookieCentral);
northRegion.add(BorderLayout.SOUTH, scorePerSecond);
//now dealing with the central region of the screen
//we add a button to buy an autoclicker (speeds up cookie production)
Container autoclickContainer = new Container(new BorderLayout());
autoclickContainer.setScrollable(false);
MultiButton autoclickButton = new MultiButton("Auto click");
autoclickButton.setIcon(clickPic);
autoclickButton.setTextLine2(
"\"Get your fingers some rest\"");
autoclickButton.setTextLine3(
"clicks 1 times every 10 seconds");
autoclickButton.setTextLine4(
"price: 15 cookies. Owned: 0");
autoclickContainer.add(BorderLayout.CENTER, autoclickButton);
//we add a button to buy a grandma (speeds up cookie production)
MultiButton grandmaButton = new MultiButton("Grandma");
grandmaButton.setIcon(grandmaPic);
grandmaButton.setTextLine2(
"\"Bake cookies with love\"");
grandmaButton.setTextLine3(
"each grandma produces 1 cookie per second");
grandmaButton.setTextLine4(
"price: 100 cookies. Owned: 0");
//we add a button to buy a farm (speeds up cookie production)
MultiButton farmButton = new MultiButton("Farm");
farmButton.setIcon(farmPic);
farmButton.setTextLine2(
"\"Harvest cookies every summer!\"");
farmButton.setTextLine3(
"each farm produces 3 cookies per second");
farmButton.setTextLine4(
"price: 500 cookies. Owned: 0");
//we add the three buttons to the central region of the screen
centerRegion.add(autoclickContainer);
centerRegion.add(grandmaButton);
centerRegion.add(farmButton);
//now we manage the south region of our screen, where the bonuses will appear
//A container to include the picture and the text of the click boost
Container clickBoostContainer = new Container(BoxLayout.x());
clickBoostContainer.getStyle()
.setPadding(16, 16, 16, 16);
ScaleImageButton clickBoostButton = new ScaleImageButton();
clickBoostButton.setIcon(clickBoostIcon);
Label clickBoostLegend = new Label("600. Clicks get 1% more of your CpS");
clickBoostLegend.getStyle()
.setFgColor(ColorUtil.rgb(152, 152, 152));
clickBoostLegend.getStyle()
.setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
clickBoostContainer.add(clickBoostButton);
clickBoostContainer.add(clickBoostLegend);
//A container to include the picture and the text of the grandma boost
Container grandmaBoostContainer = new Container(BoxLayout.x());
grandmaBoostContainer.getStyle()
.setPadding(16, 16, 16, 16);
ScaleImageButton grandmaBoostButton = new ScaleImageButton();
grandmaBoostButton.setIcon(grandmaBoostIcon);
Label grandmaBoostLegend = new Label("1000. Grandmas produce x 2");
grandmaBoostLegend.getStyle()
.setFgColor(ColorUtil.rgb(152, 152, 152));
grandmaBoostLegend.getStyle()
.setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
grandmaBoostContainer.add(grandmaBoostButton);
grandmaBoostContainer.add(grandmaBoostLegend);
//A container to include the picture and the text of the farm boost
Container farmBoostContainer = new Container(BoxLayout.x());
farmBoostContainer.getStyle()
.setPadding(16, 16, 16, 16);
ScaleImageButton farmBoostButton = new ScaleImageButton();
farmBoostButton.setIcon(farmBoostIcon);
Label farmBoostLegend = new Label("5000. Farms produce x 2");
farmBoostLegend.getStyle()
.setFgColor(ColorUtil.rgb(152, 152, 152));
farmBoostLegend.getStyle()
.setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
farmBoostContainer.add(farmBoostButton);
farmBoostContainer.add(farmBoostLegend);
//A container to include all boosters, in a top to bottom pile.
Container boostContainer = new Container(BoxLayout.y());
boostContainer.add(clickBoostContainer);
boostContainer.add(grandmaBoostContainer);
boostContainer.add(farmBoostContainer);
//Adding this container to the container "southRegion", in the south position (so that they remain close to the bottom of the screen).
southRegion.add(BorderLayout.SOUTH, boostContainer);
// === lesson 3
cookieCentral.addActionListener(
(event) -> {
score = score + clickRate;
scoreLabel.setText(String.valueOf(score) + " cookies");
}
);
// ====
}
}
@seinecle
Copy link
Author

seinecle commented Mar 4, 2018

See preview: https://imgur.com/a/4s7Pp

-> The three MultiButtons are cropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment