Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogPattern.java @ 740fade2

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.RubikObject;
25
import org.distorted.objects.RubikObjectList;
26
import org.distorted.patternui.PatternActivity;
27
import org.distorted.patternui.ScreenList;
28
import org.distorted.patternui.ScreenPattern;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

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

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

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

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

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

    
78
    RubikObject object = RubikObjectList.getObject(objectOrdinal);
79
    int objectIndex = object==null ? -1 : object.getIndex();
80

    
81
    mPatternOrdinal = RubikPatternList.getOrdinal(objectIndex);
82

    
83
    if( mPatternOrdinal<0 )
84
      {
85
      android.util.Log.e("D", "patterns for object "+mArgument+" not found");
86
      return;
87
      }
88

    
89
    android.util.Log.e("D", "prepareBody: object "+mArgument);
90

    
91
    final PatternActivity pact = (PatternActivity)getContext();
92
    int width = pact!=null ? pact.getScreenWidthInPixels() : 100;
93
    final ObjectControl control = pact!=null ? pact.getControl() : null;
94

    
95
    RubikPattern pattern = RubikPattern.getInstance();
96
    mExpandedGroup = pattern.recallExpanded(mPatternOrdinal);
97

    
98
    mListView = view.findViewById(R.id.patternListView);
99
    RubikDialogPatternListAdapter listAdapter = new RubikDialogPatternListAdapter(act,mPatternOrdinal,width);
100
    mListView.setAdapter(listAdapter);
101

    
102
    if( mExpandedGroup>=0 ) mListView.expandGroup(mExpandedGroup);
103

    
104
    int visible = pattern.recallVisiblePos(mPatternOrdinal);
105
    mListView.setSelectionFromTop(visible,0);
106

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

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

    
119
        rememberState();
120
        dismiss();
121

    
122
        return false;
123
        }
124
      });
125

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

    
136
        mExpandedGroup = groupPosition;
137
        }
138
      });
139
    }
140

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

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  public static String getDialogTag()
152
    {
153
    return "DialogPatternSingle";
154
    }
155
  }
(12-12/25)