Project

General

Profile

Download (9.88 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorScreen.java @ 707f79ff

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.bandaged;
21

    
22
import java.util.ArrayList;
23

    
24
import android.app.Activity;
25
import android.graphics.Bitmap;
26
import android.view.View;
27
import android.widget.ImageView;
28
import android.widget.LinearLayout;
29

    
30
import org.distorted.external.RubikFiles;
31
import org.distorted.helpers.TransparentImageButton;
32
import org.distorted.main.R;
33
import org.distorted.main.RubikActivity;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
public class BandagedCreatorScreen
38
{
39
  private TransparentImageButton mBackButton, mResetButton, mDoneButton;
40
  private LinearLayout mObjectView;
41
  private int mNumObjects;
42
  private ArrayList<BandagedCreatorObjectView> mViews;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
  public BandagedCreatorScreen()
47
    {
48
    mNumObjects = 0;
49
    mViews = new ArrayList<>();
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  private void setupBackButton(final BandagedCreatorActivity act)
55
    {
56
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_smallback,R.drawable.ui_medium_smallback, R.drawable.ui_big_smallback, R.drawable.ui_huge_smallback);
57
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
58
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
59

    
60
    mBackButton.setOnClickListener( new View.OnClickListener()
61
      {
62
      @Override
63
      public void onClick(View v)
64
        {
65
        act.finish();
66
        }
67
      });
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  private void setupDoneButton(final BandagedCreatorActivity act)
73
    {
74
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_done,R.drawable.ui_medium_done, R.drawable.ui_big_done, R.drawable.ui_huge_done);
75
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
76
    mDoneButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
77

    
78
    mDoneButton.setOnClickListener( new View.OnClickListener()
79
      {
80
      @Override
81
      public void onClick(View v)
82
        {
83
        BandagedCreatorRenderer renderer = act.getRenderer();
84
        if( !renderer.isBusy() ) renderer.displaySavingDialog();
85
        }
86
      });
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  private void setupResetButton(final BandagedCreatorActivity act)
92
    {
93
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_reset,R.drawable.ui_medium_reset, R.drawable.ui_big_reset, R.drawable.ui_huge_reset);
94
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
95
    mResetButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
96

    
97
    mResetButton.setOnClickListener( new View.OnClickListener()
98
      {
99
      @Override
100
      public void onClick(View v)
101
        {
102
        BandagedCreatorRenderer renderer = act.getRenderer();
103
        if( !renderer.isBusy() ) renderer.setupReset();
104
        }
105
      });
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  void onAttachedToWindow(final BandagedCreatorActivity act)
111
    {
112
    mObjectView = act.findViewById(R.id.bandagedCreatorView);
113

    
114
    int width = act.getScreenWidthInPixels();
115

    
116
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
117
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
118
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
119

    
120
    LinearLayout layoutLeft = new LinearLayout(act);
121
    layoutLeft.setLayoutParams(paramsL);
122
    LinearLayout layoutMid  = new LinearLayout(act);
123
    layoutMid.setLayoutParams(paramsM);
124
    LinearLayout layoutRight= new LinearLayout(act);
125
    layoutRight.setLayoutParams(paramsR);
126

    
127
    setupBackButton(act);
128
    layoutRight.addView(mBackButton);
129
    setupDoneButton(act);
130
    layoutMid.addView(mDoneButton);
131
    setupResetButton(act);
132
    layoutLeft.addView(mResetButton);
133

    
134
    LinearLayout layout = act.findViewById(R.id.lowerBar);
135
    layout.removeAllViews();
136
    layout.addView(layoutLeft);
137
    layout.addView(layoutMid);
138
    layout.addView(layoutRight);
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  boolean objectDoesntExist(String name)
144
    {
145
    for( BandagedCreatorObjectView view : mViews )
146
      {
147
      String viewName = view.getName();
148
      if( viewName.equals(name) ) return false;
149
      }
150

    
151
    return true;
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  void addObjects(BandagedCreatorActivity act, String objectString)
157
    {
158
    if( objectString.length()>0 )
159
      {
160
      String[] objects = objectString.split(" ");
161
      RubikFiles files = RubikFiles.getInstance();
162

    
163
      for(String object : objects)
164
        {
165
        if( objectDoesntExist(object) )
166
          {
167
          addObject(act, object);
168
          Bitmap bmp = files.getIcon(act, object + ".png");
169
          iconCreationDone(act, bmp);
170
          }
171
        }
172
      }
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  void addObject(BandagedCreatorActivity act, String name)
178
    {
179
    BandagedCreatorObjectView view = new BandagedCreatorObjectView(act,name,mNumObjects==0);
180
    LinearLayout pane = view.getPane();
181
    mObjectView.addView(pane);
182
    mNumObjects++;
183
    mViews.add(view);
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  void deleteObject(BandagedCreatorActivity act, String name)
189
    {
190
    for(int v=0; v<mNumObjects; v++)
191
      {
192
      BandagedCreatorObjectView view = mViews.get(v);
193
      String viewName = view.getName();
194

    
195
      if( viewName.equals(name) )
196
        {
197
        LinearLayout pane = view.getPane();
198
        mObjectView.removeView(pane);
199
        mViews.remove(view);
200
        mNumObjects--;
201

    
202
        if( v==0 && mNumObjects>0 )
203
          {
204
          int width = act.getScreenWidthInPixels();
205
          BandagedCreatorObjectView v2 = mViews.get(v);
206
          LinearLayout p2 = v2.getPane();
207
          int margin = (int)(width*RubikActivity.LARGE_MARGIN);;
208
          int w = (int)(width*BandagedCreatorObjectView.RATIO_PANE);
209
          LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( w, LinearLayout.LayoutParams.MATCH_PARENT);
210
          params.bottomMargin = margin;
211
          params.topMargin    = margin;
212
          params.leftMargin   = margin;
213
          params.rightMargin  = margin;
214

    
215
          p2.setLayoutParams(params);
216
          }
217

    
218
        break;
219
        }
220
      }
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  void iconCreationDone(Activity act, Bitmap bmp)
226
    {
227
    int numChildren = mObjectView.getChildCount();
228

    
229
    if( numChildren>0 )
230
      {
231
      LinearLayout pane = (LinearLayout)mObjectView.getChildAt(numChildren-1);
232
      ImageView view = pane.findViewById(R.id.bandagedCreatorObjectIcon);
233

    
234
      if( view!=null )
235
        {
236
        act.runOnUiThread(new Runnable()
237
          {
238
          @Override
239
          public void run()
240
          {
241
          view.setImageBitmap(bmp);
242
          }
243
          });
244
        }
245
      else
246
        {
247
        android.util.Log.e("D", "ImageView not found!");
248
        }
249
      }
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  String generateObjectStrings()
255
    {
256
    String result = "";
257
    int numViews = mViews.size();
258

    
259
    for( int v=0; v<numViews; v++ )
260
      {
261
      BandagedCreatorObjectView view = mViews.get(v);
262
      String name = view.getName();
263

    
264
      if( v>0 ) result += " ";
265
      result += name;
266
      }
267

    
268
    return result;
269
    }
270
}
(4-4/13)