Project

General

Profile

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

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

1 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 da56b12f Leszek Koltunski
// Copyright 2022 Leszek Koltunski                                                               //
3 9530f6b0 Leszek Koltunski
//                                                                                               //
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 d3d639b1 Leszek Koltunski
import java.util.ArrayList;
23
24 20b60ad9 Leszek Koltunski
import android.app.Activity;
25 7cb8d4b0 Leszek Koltunski
import android.graphics.Bitmap;
26 9530f6b0 Leszek Koltunski
import android.view.View;
27 7cb8d4b0 Leszek Koltunski
import android.widget.ImageView;
28 9530f6b0 Leszek Koltunski
import android.widget.LinearLayout;
29
30 a41e3c94 Leszek Koltunski
import org.distorted.external.RubikFiles;
31 9530f6b0 Leszek Koltunski
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 e48ad1af Leszek Koltunski
  private LinearLayout mObjectView;
41
  private int mNumObjects;
42 d3d639b1 Leszek Koltunski
  private ArrayList<BandagedCreatorObjectView> mViews;
43 9530f6b0 Leszek Koltunski
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46
  public BandagedCreatorScreen()
47
    {
48 e48ad1af Leszek Koltunski
    mNumObjects = 0;
49 d3d639b1 Leszek Koltunski
    mViews = new ArrayList<>();
50 9530f6b0 Leszek Koltunski
    }
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 f802924b Leszek Koltunski
    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 9530f6b0 Leszek Koltunski
    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 e0b71e6e Leszek Koltunski
        BandagedCreatorRenderer renderer = act.getRenderer();
84 72e386ef Leszek Koltunski
        if( !renderer.isBusy() ) renderer.displaySavingDialog();
85 9530f6b0 Leszek Koltunski
        }
86
      });
87
    }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91
  private void setupResetButton(final BandagedCreatorActivity act)
92
    {
93 1b185a91 Leszek Koltunski
    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 9530f6b0 Leszek Koltunski
    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 50ec342b Leszek Koltunski
        BandagedCreatorRenderer renderer = act.getRenderer();
103 e0b71e6e Leszek Koltunski
        if( !renderer.isBusy() ) renderer.setupReset();
104 9530f6b0 Leszek Koltunski
        }
105
      });
106
    }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
110 bebd7af5 Leszek Koltunski
  void onAttachedToWindow(final BandagedCreatorActivity act)
111 9530f6b0 Leszek Koltunski
    {
112 e48ad1af Leszek Koltunski
    mObjectView = act.findViewById(R.id.bandagedCreatorView);
113
114 9530f6b0 Leszek Koltunski
    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 e48ad1af Leszek Koltunski
141 a41e3c94 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e48ad1af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 d3d639b1 Leszek Koltunski
    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 66cbab36 Leszek Koltunski
      String viewName = view.getName();
194 d3d639b1 Leszek Koltunski
195 66cbab36 Leszek Koltunski
      if( viewName.equals(name) )
196 d3d639b1 Leszek Koltunski
        {
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 66cbab36 Leszek Koltunski
          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 d3d639b1 Leszek Koltunski
215
          p2.setLayoutParams(params);
216
          }
217
218
        break;
219
        }
220
      }
221 e48ad1af Leszek Koltunski
    }
222 7cb8d4b0 Leszek Koltunski
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224
225 20b60ad9 Leszek Koltunski
  void iconCreationDone(Activity act, Bitmap bmp)
226 7cb8d4b0 Leszek Koltunski
    {
227
    int numChildren = mObjectView.getChildCount();
228
229 83e021c5 Leszek Koltunski
    if( numChildren>0 )
230 7cb8d4b0 Leszek Koltunski
      {
231 83e021c5 Leszek Koltunski
      LinearLayout pane = (LinearLayout)mObjectView.getChildAt(numChildren-1);
232
      ImageView view = pane.findViewById(R.id.bandagedCreatorObjectIcon);
233
234
      if( view!=null )
235 20b60ad9 Leszek Koltunski
        {
236 83e021c5 Leszek Koltunski
        act.runOnUiThread(new Runnable()
237
          {
238
          @Override
239
          public void run()
240 20b60ad9 Leszek Koltunski
          {
241
          view.setImageBitmap(bmp);
242
          }
243 83e021c5 Leszek Koltunski
          });
244
        }
245
      else
246
        {
247
        android.util.Log.e("D", "ImageView not found!");
248
        }
249 7cb8d4b0 Leszek Koltunski
      }
250
    }
251 a41e3c94 Leszek Koltunski
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 9530f6b0 Leszek Koltunski
}