uk.co.threeonefour.ifictionary.engine.client.pages.level9game.Level9GameActivity.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.threeonefour.ifictionary.engine.client.pages.level9game.Level9GameActivity.java

Source

/**
 * Copyright 2013 Paul Illingworth
 * 
 * Licensed 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 uk.co.threeonefour.ifictionary.engine.client.pages.level9game;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.MethodCallback;

import uk.co.threeonefour.basics.codec.Base64Codec;
import uk.co.threeonefour.ifictionary.api.game.Base64EncodedStr;
import uk.co.threeonefour.ifictionary.api.game.GameInfo;
import uk.co.threeonefour.ifictionary.engine.client.PlaceControllerEx;
import uk.co.threeonefour.ifictionary.engine.client.graphics.Colour;
import uk.co.threeonefour.ifictionary.engine.client.resty.GameService;
import uk.co.threeonefour.level9j.icy.IcyVm;
import uk.co.threeonefour.level9j.vm.FileHandler;
import uk.co.threeonefour.level9j.vm.GameState;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.HTML;
import com.google.inject.Inject;

public class Level9GameActivity extends AbstractActivity {

    private static final Logger LOG = Logger.getLogger(Level9GameActivity.class.getName());

    private final PlaceControllerEx placeController;

    private Level9GameView view;
    // private EventBus eventBus;

    private IcyVm icyVm;

    private final Level9GameView.Presenter viewPresenter;
    private QueuedImageDataGraphicsHandler graphicsHandler;
    private FileHandler fileHandler;

    @Inject
    public Level9GameActivity(Level9GameView v, PlaceControllerEx placeController) {

        this.view = v;
        this.placeController = placeController;

        viewPresenter = new Level9GameView.Presenter() {

            @Override
            public void inputTextChanged(String value) {
                userCommand(value);
            }
        };
    }

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {

        graphicsHandler = new QueuedImageDataGraphicsHandler(view.getCanvas());

        fileHandler = new FileHandler() {

            @Override
            public void saveFile(GameState gameState) throws IOException {
            }

            @Override
            public GameState loadFile() throws IOException {
                return null;
            }

            @Override
            public boolean getGameFile(String newName, int size) {
                return false;
            }

            @Override
            public void setFileNumber(String newName, int size, int n) {
            }
        };

        icyVm = new IcyVm(graphicsHandler, fileHandler);

        // this.eventBus = eventBus;
        panel.setWidget(view.asWidget());

        //        Context2d context = view.getCanvas().getContext2d();
        //        int w = context.getCanvas().getWidth();
        //        int h = context.getCanvas().getHeight();
        //        context.setFillStyle("#" + Colour.BLACK.toHexString());
        //        context.fillRect(0, 0, w, h);

        view.setPresenter(viewPresenter);

        load();
    }

    @Override
    public final void onStop() {

        graphicsHandler = null;
        fileHandler = null;
        icyVm = null;
        view = null;
    }

    protected Level9GamePlace getPlace() {
        return (Level9GamePlace) placeController.getWhere();
    }

    private void load() {

        String path = Window.Location.getPath();
        String gameId = path.substring(path.lastIndexOf('/') + 1);

        GameService.Util.getService().getGame(gameId, new MethodCallback<GameInfo>() {
            @Override
            public void onSuccess(Method method, GameInfo response) {
                // view.getTextPanel().add(new HTML("<h5>" + response.getName() + "</h5>"));
                // view.getTextPanel().add(new HTML("<p>" + response.getDescription() + "</p>"));
                // view.getTextPanel().add(new HTML("<p>" + response.getOwner() + "</p>"));
            }

            @Override
            public void onFailure(Method method, Throwable exception) {
                LOG.log(Level.SEVERE, "Failed to execute method " + method.getClass().getName() + ".", exception);
            }
        });

        GameService.Util.getService().getFile(gameId, new MethodCallback<Base64EncodedStr>() {
            @Override
            public void onSuccess(Method method, Base64EncodedStr response) {
                Base64Codec codec = new Base64Codec();
                byte[] data = codec.decode(response.getBase64());
                icyVm.loadGame(data);

                startGame();
            }

            @Override
            public void onFailure(Method method, Throwable exception) {
                LOG.log(Level.SEVERE, "Failed to execute method " + method.getClass().getName() + ".", exception);
            }
        });

    }

    protected void startGame() {

        Context2d context = view.getCanvas().getContext2d();
        int w = context.getCanvas().getWidth();
        int h = context.getCanvas().getHeight();
        context.setFillStyle("#" + Colour.WHITE.toHexString());
        context.fillRect(0, 0, w, h);

        icyVm.startGame();
        processVmOutput();

        view.getTextBox().setFocus(true);
    }

    private void userCommand(String command) {

        view.getTextPanel().add(new HTML("<b>&gt; " + SafeHtmlUtils.htmlEscape(command) + "</b>"));
        view.getTextBox().setText(null);

        icyVm.execute(command);
        processVmOutput();
    }

    private void processVmOutput() {
        view.getTextPanel().add(new HTML(SafeHtmlUtils.htmlEscape(icyVm.getText())));
        view.getScrollPanel().scrollToBottom();

        graphicsHandler.executeQueuedCommands();
    }

}