com.gamemaker.screens.MenuScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.gamemaker.screens.MenuScreen.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.gamemaker.screens;

import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.gamemaker.ERhelpers.AssetLoader;
import com.gamemaker.ERhelpers.Button;

/**
 * Created by wqc on 16/7/25.
 */
public class MenuScreen implements Screen {
    private Game game;
    private ShapeRenderer shapeRenderer;
    private OrthographicCamera cam;
    private SpriteBatch batcher;//this is for drawing pictures for u
    private TextureRegion menuBG, menuTitle1, menuTitle2, menuStart;
    private TextureRegion startButton_on, startButton_off, aboutButton_on, aboutButton_off;
    public Button button, startBtn, aboutBtn;
    public float x, y, width, height;
    public Rectangle bounds;

    public TextureRegion buttonUp;
    public TextureRegion buttonDown;

    public MenuScreen(Game game) {
        this.game = game;
        cam = new OrthographicCamera();
        cam.setToOrtho(true, 1280, 720);
        batcher = new SpriteBatch();
        batcher.setProjectionMatrix(cam.combined);
        shapeRenderer = new ShapeRenderer();
        shapeRenderer.setProjectionMatrix(cam.combined);
        menuBG = AssetLoader.mapBg;
        menuTitle1 = AssetLoader.textureMenuTitle1;
        menuTitle2 = AssetLoader.textureMenuTitle2;
        startButton_on = AssetLoader.startBtn_on;
        startButton_off = AssetLoader.startBtn_off;
        aboutButton_on = AssetLoader.aboutBtn_on;
        aboutButton_off = AssetLoader.aboutBtn_off;
        startBtn = new Button(400, 400, 150, 100, startButton_on, startButton_off);
        aboutBtn = new Button(700, 400, 150, 100, aboutButton_on, aboutButton_off);
    }

    @Override
    public void show() {

    }

    public void update(float deltaTime) {
        //        if (Gdx.input.justTouched()){
        //            game.setScreen(new IntroScreen(game));
        //        }
    }

    public void draw(float deltaTime) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        batcher.begin();
        //batcher.disableBlending();
        batcher.draw(menuBG, 0, 0, 1280, 720);
        batcher.draw(menuTitle1, 295, 100, 650, 150);
        //batcher.draw(menuStart,520,340,200,200);
        //batcher.draw(menuTitle2,500,400,230,75);//half size of original image

        drawButton();

        batcher.end();
    }

    public void drawButton() {

        startBtn.isTouchDown(Gdx.input.getX(), Gdx.input.getY());
        aboutBtn.isTouchDown(Gdx.input.getX(), Gdx.input.getY());

        if (startBtn.getIsPressed()) {
            batcher.draw(startBtn.buttonDown, startBtn.x, startBtn.y, startBtn.width, startBtn.height);
            if (Gdx.input.justTouched()) {
                AssetLoader.clickButton.play();
                game.setScreen(new IntroScreen(game));
            }
        } else {
            batcher.draw(startBtn.buttonUp, startBtn.x, startBtn.y, startBtn.width, startBtn.height);

        }
        if (aboutBtn.getIsPressed()) {
            batcher.draw(aboutBtn.buttonDown, aboutBtn.x, aboutBtn.y, aboutBtn.width, aboutBtn.height);
            if (Gdx.input.justTouched()) {
                AssetLoader.clickButton.play();
                game.setScreen(new AboutScreen(game));
            }

        } else {
            batcher.draw(aboutBtn.buttonUp, aboutBtn.x, aboutBtn.y, aboutBtn.width, aboutBtn.height);
        }

        startBtn.isTouchUp(Gdx.input.getX(), Gdx.input.getY());
        aboutBtn.isTouchUp(Gdx.input.getX(), Gdx.input.getY());

    }

    @Override
    public void render(float delta) {
        update(delta);
        draw(delta);
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }

}