project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/LocalRoomActivity.java
author koda
Wed, 10 Jan 2018 15:27:47 +0100
changeset 12889 8f99199b33ea
parent 10017 de822cd3df3a
permissions -rw-r--r--
merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
/*
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
 * modify it under the terms of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
 * of the License, or (at your option) any later version.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
 * GNU General Public License for more details.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import java.util.ArrayList;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import java.util.Map;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import java.util.Set;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import java.util.TreeSet;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import org.hedgewars.hedgeroid.Datastructures.GameConfig;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import org.hedgewars.hedgeroid.Datastructures.Team;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import org.hedgewars.hedgeroid.netplay.Netplay;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import org.hedgewars.hedgeroid.util.UiUtils;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.content.Intent;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.os.Bundle;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.support.v4.app.FragmentActivity;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import android.view.View;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
import android.view.View.OnClickListener;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
import android.widget.Button;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
import android.widget.LinearLayout;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
import android.widget.TabHost;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
import android.widget.Toast;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
/**
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    44
 * This activity is used to set up and start a local game.
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
public class LocalRoomActivity extends FragmentActivity implements RoomStateManager.Provider, TeamAddDialog.Listener {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    47
    private TabHost tabHost;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    48
    private RoomStateManager stateManager;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    49
    private Button startButton;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    50
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
    @Override
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
    protected void onCreate(Bundle icicle) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
        super.onCreate(icicle);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
        // TODO find a better central location / way to set up the default scheme and weaponset
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
        Netplay netplay = Netplay.getAppInstance(getApplicationContext());
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
        stateManager = new LocalRoomStateManager(netplay.defaultScheme, netplay.defaultWeaponset);
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    57
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
        setContentView(R.layout.activity_localroom);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
        startButton = (Button)findViewById(R.id.startGame);
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    60
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
        startButton.setOnClickListener(startButtonClickListener);
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    62
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
        // Set up a tabbed UI for medium and small screens
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
        if(tabHost != null) {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    66
            tabHost.setup();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    67
            tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL);
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    69
            tabHost.addTab(tabHost.newTabSpec("map").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.room_tab_map, 0)).setContent(R.id.mapFragment));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    70
            tabHost.addTab(tabHost.newTabSpec("settings").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.room_tab_settings, 0)).setContent(R.id.settingsFragment));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    71
            tabHost.addTab(tabHost.newTabSpec("teams").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.room_tab_teams, 0)).setContent(R.id.teamlistContainer));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    72
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    73
            if (icicle != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    74
                tabHost.setCurrentTabByTag(icicle.getString("currentTab"));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    75
            }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
        }
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
    }
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    78
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
    @Override
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
    protected void onSaveInstanceState(Bundle icicle) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
        super.onSaveInstanceState(icicle);
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
        if(tabHost != null) {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    83
            icicle.putString("currentTab", tabHost.getCurrentTabTag());
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
        }
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
    }
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    86
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    87
    public void onTeamAddDialogSubmitted(Team newTeam) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    88
        stateManager.requestAddTeam(newTeam, TeamInGame.getUnusedOrRandomColorIndex(stateManager.getTeams().values()));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    89
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    90
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    91
    public RoomStateManager getRoomStateManager() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    92
        return stateManager;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    93
    }
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    95
    private final OnClickListener startButtonClickListener = new OnClickListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    96
        public void onClick(View v) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    97
            Map<String, TeamInGame> teams = stateManager.getTeams();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    98
            Set<Integer> clanColors = new TreeSet<Integer>();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    99
            for(TeamInGame t : teams.values()) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   100
                clanColors.add(t.ingameAttribs.colorIndex);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   101
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   102
            if(clanColors.size()<2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   103
                if(tabHost != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   104
                    tabHost.setCurrentTabByTag("teams");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   105
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   106
                int errortext = teams.size()<2 ? R.string.not_enough_teams : R.string.not_enough_clans;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   107
                Toast.makeText(getApplicationContext(), errortext, Toast.LENGTH_SHORT).show();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   108
                return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   109
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   110
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   111
            SDLActivity.startNetgame = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   112
            SDLActivity.startConfig = new GameConfig(
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   113
                    stateManager.getGameStyle(),
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   114
                    stateManager.getScheme(),
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   115
                    stateManager.getMapRecipe(),
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   116
                    new ArrayList<TeamInGame>(stateManager.getTeams().values()),
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   117
                    stateManager.getWeaponset());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   118
            startActivity(new Intent(LocalRoomActivity.this, SDLActivity.class));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   119
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   120
    };
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
}