Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogPatternSingle.java @ e9245b7b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import org.distorted.objectlib.main.ObjectControl;
22
import org.distorted.objectlib.patterns.RubikPattern;
23
import org.distorted.objectlib.patterns.RubikPatternList;
24
import org.distorted.objects.RubikObjectList;
25
import org.distorted.patternui.PatternActivity;
26
import org.distorted.patternui.ScreenList;
27
import org.distorted.patternui.ScreenPattern;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class RubikDialogPatternSingle extends RubikDialogAbstract
32
  {
33
  private ExpandableListView mListView;
34
  private int mPatternOrdinal, mPos;
35
  private int mExpandedGroup;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
  @Override
40
  public void onResume()
41
    {
42
    super.onResume();
43

    
44
    Window window = getDialog().getWindow();
45

    
46
    if( window!=null )
47
      {
48
      WindowManager.LayoutParams params = window.getAttributes();
49
      params.width  = (int)Math.min( mHeight*0.65f,mWidth*0.98f );
50
      params.height = (int)Math.min( mHeight*0.80f,mWidth*1.30f );
51
      window.setAttributes(params);
52
      }
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  public int getResource()      { return R.layout.dialog_pattern_tab; }
58
  public int getTitleResource() { return R.string.choose_pattern; }
59
  public boolean hasArgument()  { return true; }
60
  public int getPositive()      { return -1; }
61
  public int getNegative()      { return -1; }
62
  public void positiveAction()  { }
63
  public void negativeAction()  { }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  public void prepareBody(Dialog dialog, View view, FragmentActivity act, float size)
68
    {
69
    int objectOrdinal = RubikObjectList.getOrdinal(mArgument);
70

    
71
    if( objectOrdinal<0 )
72
      {
73
      android.util.Log.e("D", "object "+mArgument+" not found");
74
      return;
75
      }
76

    
77
    mPatternOrdinal = RubikPatternList.getOrdinal(objectOrdinal);
78

    
79
    if( mPatternOrdinal<0 )
80
      {
81
      android.util.Log.e("D", "patterns for object "+mArgument+" not found");
82
      return;
83
      }
84

    
85
    final PatternActivity pact = (PatternActivity)getContext();
86
    int height = pact!=null ? pact.getScreenHeightInPixels() : 100;
87
    final ObjectControl control = pact!=null ? pact.getControl() : null;
88

    
89
    RubikPattern pattern = RubikPattern.getInstance();
90
    mExpandedGroup = pattern.recallExpanded(mPatternOrdinal);
91

    
92
    mListView = view.findViewById(R.id.patternListView);
93
    RubikDialogPatternListAdapter listAdapter = new RubikDialogPatternListAdapter(act,mPatternOrdinal,height);
94
    mListView.setAdapter(listAdapter);
95

    
96
    if( mExpandedGroup>=0 )
97
      {
98
      mListView.expandGroup(mExpandedGroup);
99
      }
100

    
101
    int visible = pattern.recallVisiblePos(mPatternOrdinal);
102
    mListView.setSelectionFromTop(visible,0);
103

    
104

    
105
    mListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener()
106
      {
107
      @Override
108
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
109
        {
110
        RubikPattern pattern = RubikPattern.getInstance();
111
        int[][] moves = pattern.reInitialize(mPatternOrdinal, groupPosition, childPosition);
112
        control.initializeObject(moves);
113

    
114
        ScreenPattern state = (ScreenPattern) ScreenList.PATT.getScreenClass();
115
        state.setPattern(pact, mPatternOrdinal, groupPosition, childPosition);
116

    
117
        rememberState();
118
        dismiss();
119

    
120
        return false;
121
        }
122
      });
123

    
124
    mListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener()
125
      {
126
      @Override
127
      public void onGroupExpand(int groupPosition)
128
        {
129
        if(mExpandedGroup!=-1 && groupPosition!=mExpandedGroup)
130
          {
131
          mListView.collapseGroup(mExpandedGroup);
132
          }
133

    
134
        mExpandedGroup = groupPosition;
135
        }
136
      });
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  private void rememberState()
142
    {
143
    RubikPattern pattern = RubikPattern.getInstance();
144
    pattern.rememberState(mPatternOrdinal,mPos,mListView.getFirstVisiblePosition(),mExpandedGroup);
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  public static String getDialogTag()
150
    {
151
    return "DialogPatternSingle";
152
    }
153
  }
(14-14/32)