project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MapFragment.java
author Wuzzy <Wuzzy2@mail.ru>
Thu, 15 Feb 2018 18:32:28 +0100
changeset 13009 67cc3cac4c8e
parent 10017 de822cd3df3a
permissions -rw-r--r--
TrophyRace: Show fastest team times (instead of clan times)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     1
/*
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
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     8
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    13
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    15
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    16
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import java.io.IOException;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import java.util.Arrays;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import java.util.Collections;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import java.util.List;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import java.util.Random;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import org.hedgewars.hedgeroid.R;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import org.hedgewars.hedgeroid.Datastructures.MapFile;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import org.hedgewars.hedgeroid.Datastructures.MapRecipe;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import org.hedgewars.hedgeroid.frontlib.Frontlib;
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    33
import org.hedgewars.hedgeroid.util.CalmDownHandler;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.content.Context;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import android.graphics.drawable.Drawable;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
import android.os.Bundle;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
import android.support.v4.app.Fragment;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
import android.view.LayoutInflater;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
import android.view.View;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
import android.view.View.OnClickListener;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
import android.view.ViewGroup;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
import android.widget.AdapterView;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
import android.widget.AdapterView.OnItemSelectedListener;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
import android.widget.ArrayAdapter;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
import android.widget.ImageView;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
import android.widget.Spinner;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
import android.widget.TableRow;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
import android.widget.Toast;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    51
/**
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    52
 * Display a map preview, and configuration options for the map.
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
 *
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    54
 * Mostly for layout reasons, this does not include the theme setting, which
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    55
 * (arguably) is more a map setting than a general game setting.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    56
 */
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7572
diff changeset
    57
public class MapFragment extends Fragment {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
    private Spinner mapTypeSpinner, mapNameSpinner, templateSpinner, mazeSizeSpinner;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
    private TableRow nameRow, templateRow, mazeSizeRow;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
    private ImageView mapPreview;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
    private List<MapFile> mapFiles;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
    private RoomStateManager stateManager;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
    private Random random = new Random();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
    private CalmDownHandler mapPreviewHandler;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
    /*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
     * Rendering the preview can take a few seconds on Android, so we want to prevent preview
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
     * requests from queueing up if maps are changed quickly. So if there is already a preview
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
     * being generated, we store our latest request in the newPreviewRequest variable instead.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
     * Once the current preview is finished generating it will start on that one.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
    private boolean previewGenerationInProgress;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
    private MapRecipe newPreviewRequest;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
    private MapRecipe currentMap; // kept for reference on every change to find out what changed
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
        View v = inflater.inflate(R.layout.fragment_map, container, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
        final Context appContext = getActivity().getApplicationContext();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
        /*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
         * This handler will start the map preview after none of the map settings
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
         * have been updated for a short time.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
         */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
        mapPreviewHandler = new CalmDownHandler(getActivity().getMainLooper(), new Runnable() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
            public void run() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
                if(!previewGenerationInProgress) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
                    mapPreview.setImageResource(R.drawable.roomlist_preparing);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
                    MapPreviewGenerator.startPreviewGeneration(appContext, stateManager.getMapRecipe(), mapPreviewListener);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
                    previewGenerationInProgress = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
                    newPreviewRequest = stateManager.getMapRecipe();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
        }, 250);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
        nameRow = (TableRow) v.findViewById(R.id.rowMapName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
        templateRow = (TableRow) v.findViewById(R.id.rowTemplateFilter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
        mazeSizeRow = (TableRow) v.findViewById(R.id.rowMazeSize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
        mapPreview = (ImageView) v.findViewById(R.id.mapPreview);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
        mapPreview.setImageDrawable(null);;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
        mapPreview.setOnClickListener(mapClickListener);
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   105
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
            mapFiles = FrontendDataUtils.getMaps(getActivity());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
        } catch (IOException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
            Toast.makeText(getActivity().getApplicationContext(), R.string.error_missing_sdcard_or_files, Toast.LENGTH_LONG).show();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
            getActivity().finish();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
            return null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   112
        Collections.sort(mapFiles, MapFile.MISSIONS_FIRST_NAME_ORDER);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   113
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   114
        List<String> mapNames = MapFile.toDisplayNameList(mapFiles, getResources());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   115
        mapTypeSpinner = prepareSpinner(v, R.id.spinMapType, Arrays.asList(getResources().getStringArray(R.array.map_types)), mapTypeSelectedListener);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   116
        mapNameSpinner = prepareSpinner(v, R.id.spinMapName, mapNames, mapNameSelectedListener);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   117
        templateSpinner = prepareSpinner(v, R.id.spinTemplateFilter, Arrays.asList(getResources().getStringArray(R.array.map_templates)), mapTemplateSelectedListener);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   118
        mazeSizeSpinner = prepareSpinner(v, R.id.spinMazeSize, Arrays.asList(getResources().getStringArray(R.array.map_maze_sizes)), mazeSizeSelectedListener);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   119
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   120
        stateManager.addListener(roomStateChangeListener);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   121
        currentMap = stateManager.getMapRecipe();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   122
        if(currentMap != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   123
            updateDisplay(currentMap);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   124
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   125
        setChiefState(stateManager.getChiefStatus());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   126
        mapPreviewHandler.activity();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   127
        return v;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   128
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   129
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   130
    private static Spinner prepareSpinner(View v, int id, List<String> items, OnItemSelectedListener itemSelectedListener) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   131
        Spinner spinner = (Spinner)v.findViewById(id);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   132
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(v.getContext(), R.layout.listview_item, items);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   133
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   134
        spinner.setAdapter(adapter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   135
        spinner.setOnItemSelectedListener(itemSelectedListener);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   136
        return spinner;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   137
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   138
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   139
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   140
    public void onCreate(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   141
        super.onCreate(savedInstanceState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   142
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   143
            stateManager = ((RoomStateManager.Provider)getActivity()).getRoomStateManager();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   144
        } catch(ClassCastException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   145
            throw new RuntimeException("Hosting activity must implement RoomStateManager.Provider.", e);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   146
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   147
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   148
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   149
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   150
    public void onDestroy() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   151
        super.onDestroy();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   152
        mapPreviewHandler.stop();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   153
        newPreviewRequest = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   154
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   155
        stateManager.removeListener(roomStateChangeListener);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   156
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   157
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   158
    private void setChiefState(boolean chiefState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   159
        mapTypeSpinner.setEnabled(chiefState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   160
        mapNameSpinner.setEnabled(chiefState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   161
        templateSpinner.setEnabled(chiefState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   162
        mazeSizeSpinner.setEnabled(chiefState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   163
        mapPreview.setEnabled(chiefState);
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   164
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   165
        if(chiefState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   166
            sendMapnameAndGenerator();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   167
            stateManager.changeMapTemplate(templateSpinner.getSelectedItemPosition());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   168
            stateManager.changeMazeSize(mazeSizeSpinner.getSelectedItemPosition());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   169
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   170
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   171
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   172
    private void updateDisplay(MapRecipe map) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   173
        nameRow.setVisibility(map.mapgen == Frontlib.MAPGEN_NAMED ? View.VISIBLE : View.GONE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   174
        templateRow.setVisibility(map.mapgen == Frontlib.MAPGEN_REGULAR ? View.VISIBLE : View.GONE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   175
        mazeSizeRow.setVisibility(map.mapgen == Frontlib.MAPGEN_MAZE ? View.VISIBLE : View.GONE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   176
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   177
        mapTypeSpinner.setSelection(map.mapgen);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   178
        int mapPosition = findMapPosition(mapFiles, map.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   179
        if(mapPosition >= 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   180
            mapNameSpinner.setSelection(mapPosition);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   181
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   182
        templateSpinner.setSelection(map.templateFilter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   183
        mazeSizeSpinner.setSelection(map.mazeSize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   184
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   185
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   186
    private static int findMapPosition(List<MapFile> mapFiles, String mapName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   187
        for(int i=0; i<mapFiles.size(); i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   188
            if(mapName.equals(mapFiles.get(i).name)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   189
                return i;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   190
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   191
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   192
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   193
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   194
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   195
    private void sendMapnameAndGenerator() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   196
        int mapType = mapTypeSpinner.getSelectedItemPosition();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   197
        String mapName = mapFiles.get(mapNameSpinner.getSelectedItemPosition()).name;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   198
        stateManager.changeMapNameAndGenerator(MapRecipe.mapnameForGenerator(mapType, mapName));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   199
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   200
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   201
    private final OnItemSelectedListener mapTypeSelectedListener = new OnItemSelectedListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   202
        public void onItemSelected(AdapterView<?> adapter, View v, int position, long arg3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   203
            sendMapnameAndGenerator();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   204
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   205
        public void onNothingSelected(AdapterView<?> arg0) {}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   206
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   207
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   208
    private final OnItemSelectedListener mapNameSelectedListener = new OnItemSelectedListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   209
        public void onItemSelected(AdapterView<?> adapter, View v, int position, long arg3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   210
            sendMapnameAndGenerator();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   211
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   212
        public void onNothingSelected(AdapterView<?> arg0) {}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   213
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   214
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   215
    private final OnItemSelectedListener mapTemplateSelectedListener = new OnItemSelectedListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   216
        public void onItemSelected(AdapterView<?> adapter, View v, int position, long arg3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   217
            stateManager.changeMapTemplate(position);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   218
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   219
        public void onNothingSelected(AdapterView<?> arg0) {}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   220
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   221
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   222
    private final OnItemSelectedListener mazeSizeSelectedListener = new OnItemSelectedListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   223
        public void onItemSelected(AdapterView<?> adapter, View v, int position, long arg3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   224
            stateManager.changeMazeSize(position);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   225
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   226
        public void onNothingSelected(AdapterView<?> arg0) {}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   227
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   228
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   229
    private final OnClickListener mapClickListener = new OnClickListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   230
        public void onClick(View v) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   231
            stateManager.changeMapSeed(MapRecipe.makeRandomSeed());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   232
            if(mapTypeSpinner.getSelectedItemPosition() == Frontlib.MAPGEN_NAMED) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   233
                mapNameSpinner.setSelection(random.nextInt(mapNameSpinner.getCount()));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   234
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   235
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   236
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   237
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   238
    private final RoomStateManager.Listener roomStateChangeListener = new RoomStateManager.ListenerAdapter() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   239
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   240
        public void onChiefStatusChanged(boolean isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   241
            setChiefState(isChief);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   242
        };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   243
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   244
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   245
        public void onMapChanged(MapRecipe recipe) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   246
            // Only trigger a preview update if a relevant field changed (not theme)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   247
            if(currentMap==null
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   248
                    || currentMap.mapgen != recipe.mapgen
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   249
                    || currentMap.mazeSize != recipe.mazeSize
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   250
                    || !currentMap.name.equals(recipe.name)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   251
                    || !currentMap.seed.equals(recipe.seed)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   252
                    || currentMap.templateFilter != recipe.templateFilter
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   253
                    || !Arrays.equals(currentMap.getDrawData(), recipe.getDrawData())) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   254
                mapPreviewHandler.activity();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   255
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   256
            updateDisplay(recipe);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   257
            currentMap = recipe;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   258
        };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   259
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   260
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   261
    private MapPreviewGenerator.Listener mapPreviewListener = new MapPreviewGenerator.Listener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   262
        public void onMapPreviewResult(Drawable preview) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   263
            if(newPreviewRequest != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   264
                MapPreviewGenerator.startPreviewGeneration(getActivity().getApplicationContext(), newPreviewRequest, mapPreviewListener);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   265
                newPreviewRequest = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   266
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   267
                if(mapPreview != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   268
                    mapPreview.setImageDrawable(preview);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   269
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   270
                previewGenerationInProgress = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   271
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   272
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   273
    };
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   274
}