Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / DialogPattern.java @ d7f9a1a7

1 e9245b7b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.dialogs;
11
12
import android.app.Dialog;
13
import android.view.View;
14
import android.view.Window;
15
import android.view.WindowManager;
16
import android.widget.ExpandableListView;
17
18
import androidx.fragment.app.FragmentActivity;
19
20
import org.distorted.main.R;
21 302600e5 leszek
import org.distorted.objectlib.helpers.ObjectMove;
22 e9245b7b leszek
import org.distorted.objectlib.main.ObjectControl;
23
import org.distorted.objectlib.patterns.RubikPattern;
24
import org.distorted.objectlib.patterns.RubikPatternList;
25 740fade2 leszek
import org.distorted.objects.RubikObject;
26
import org.distorted.objects.RubikObjectList;
27 7e9d918b leszek
import org.distorted.patterns.PatternActivity;
28
import org.distorted.patterns.ScreenList;
29
import org.distorted.patterns.ScreenPattern;
30 e9245b7b leszek
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32
33 8477cf44 leszek
public class DialogPattern extends DialogAbstract
34 e9245b7b leszek
  {
35
  private ExpandableListView mListView;
36
  private int mPatternOrdinal, mPos;
37
  private int mExpandedGroup;
38
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41
  @Override
42
  public void onResume()
43
    {
44
    super.onResume();
45
46
    Window window = getDialog().getWindow();
47
48
    if( window!=null )
49
      {
50
      WindowManager.LayoutParams params = window.getAttributes();
51
      params.width  = (int)Math.min( mHeight*0.65f,mWidth*0.98f );
52 017f24b3 leszek
      //params.height = (int)Math.min( mHeight*0.80f,mWidth*1.30f );
53 e9245b7b leszek
      window.setAttributes(params);
54
      }
55
    }
56
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 017f24b3 leszek
  public int getResource()      { return R.layout.dialog_pattern_single; }
60 e9245b7b leszek
  public int getTitleResource() { return R.string.choose_pattern; }
61
  public boolean hasArgument()  { return true; }
62
  public int getPositive()      { return -1; }
63
  public int getNegative()      { return -1; }
64
  public void positiveAction()  { }
65
  public void negativeAction()  { }
66
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69
  public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
70
    {
71 c02235d5 leszek
    int objectOrdinal = Integer.parseInt(mArgument);
72 e9245b7b leszek
73
    if( objectOrdinal<0 )
74
      {
75
      android.util.Log.e("D", "object "+mArgument+" not found");
76
      return;
77
      }
78
79 740fade2 leszek
    RubikObject object = RubikObjectList.getObject(objectOrdinal);
80
    int objectIndex = object==null ? -1 : object.getIndex();
81
82
    mPatternOrdinal = RubikPatternList.getOrdinal(objectIndex);
83 e9245b7b leszek
84
    if( mPatternOrdinal<0 )
85
      {
86
      android.util.Log.e("D", "patterns for object "+mArgument+" not found");
87
      return;
88
      }
89
90
    final PatternActivity pact = (PatternActivity)getContext();
91 337f4660 leszek
    int width = pact!=null ? pact.getScreenWidthInPixels() : 100;
92 e9245b7b leszek
    final ObjectControl control = pact!=null ? pact.getControl() : null;
93
94
    RubikPattern pattern = RubikPattern.getInstance();
95
    mExpandedGroup = pattern.recallExpanded(mPatternOrdinal);
96
97
    mListView = view.findViewById(R.id.patternListView);
98 8477cf44 leszek
    DialogPatternListAdapter listAdapter = new DialogPatternListAdapter(act,mPatternOrdinal,width);
99 e9245b7b leszek
    mListView.setAdapter(listAdapter);
100
101 017f24b3 leszek
    if( mExpandedGroup>=0 ) mListView.expandGroup(mExpandedGroup);
102 e9245b7b leszek
103
    int visible = pattern.recallVisiblePos(mPatternOrdinal);
104
    mListView.setSelectionFromTop(visible,0);
105
106
    mListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener()
107
      {
108
      @Override
109
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
110
        {
111
        RubikPattern pattern = RubikPattern.getInstance();
112 302600e5 leszek
        ObjectMove[] moves = pattern.reInitialize(mPatternOrdinal, groupPosition, childPosition);
113 017f24b3 leszek
        if( control!=null ) control.initializeObject(moves);
114 e9245b7b leszek
115
        ScreenPattern state = (ScreenPattern) ScreenList.PATT.getScreenClass();
116
        state.setPattern(pact, mPatternOrdinal, groupPosition, childPosition);
117
118
        rememberState();
119
        dismiss();
120
121
        return false;
122
        }
123
      });
124
125
    mListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener()
126
      {
127
      @Override
128
      public void onGroupExpand(int groupPosition)
129
        {
130
        if(mExpandedGroup!=-1 && groupPosition!=mExpandedGroup)
131
          {
132
          mListView.collapseGroup(mExpandedGroup);
133
          }
134
135
        mExpandedGroup = groupPosition;
136
        }
137
      });
138
    }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142
  private void rememberState()
143
    {
144
    RubikPattern pattern = RubikPattern.getInstance();
145
    pattern.rememberState(mPatternOrdinal,mPos,mListView.getFirstVisiblePosition(),mExpandedGroup);
146
    }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150
  public static String getDialogTag()
151
    {
152
    return "DialogPatternSingle";
153
    }
154
  }