Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogPatternView.java @ eb647d5e

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.dialogs;
21

    
22
import android.content.Context;
23
import androidx.fragment.app.FragmentActivity;
24
import android.util.AttributeSet;
25
import android.view.View;
26
import android.widget.ExpandableListView;
27
import android.widget.FrameLayout;
28

    
29
import org.distorted.objectlib.main.ObjectControl;
30
import org.distorted.objectlib.main.ObjectType;
31

    
32
import org.distorted.main.R;
33
import org.distorted.main.RubikActivity;
34
import org.distorted.patterns.RubikPattern;
35
import org.distorted.patterns.RubikPatternList;
36
import org.distorted.screens.ScreenList;
37
import org.distorted.screens.RubikScreenPattern;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class RubikDialogPatternView extends FrameLayout
42
  {
43
  private ExpandableListView mListView;
44
  private RubikDialogPatternListAdapter mListAdapter;
45
  private RubikDialogPattern mDialog;
46
  private int mTab, mPos;
47
  private int mExpandedGroup;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  public RubikDialogPatternView(Context context, AttributeSet attrs, int defStyle)
52
    {
53
    super(context, attrs, defStyle);
54
    }
55

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

    
58
  public RubikDialogPatternView(Context context, AttributeSet attrs)
59
    {
60
    super(context, attrs);
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  public RubikDialogPatternView(FragmentActivity act, RubikDialogPattern dialog, int position)
66
    {
67
    super(act);
68

    
69
    final RubikActivity ract = (RubikActivity)getContext();
70
    final ObjectControl control = ract.getControl();
71

    
72
    mTab = position;
73
    mDialog = dialog;
74

    
75
    RubikPattern pattern = RubikPattern.getInstance();
76
    mExpandedGroup = pattern.recallExpanded(position);
77

    
78
    View tab = inflate( act, R.layout.dialog_pattern_tab, null);
79

    
80
    mListView = tab.findViewById(R.id.patternListView);
81
    mListAdapter = new RubikDialogPatternListAdapter(act,mTab, ract.getScreenHeightInPixels());
82
    mListView.setAdapter(mListAdapter);
83

    
84
    if( mExpandedGroup>=0 )
85
      {
86
      mListView.expandGroup(mExpandedGroup);
87
      }
88

    
89
    int visible = pattern.recallVisiblePos(mTab);
90
    mListView.setSelectionFromTop(visible,0);
91

    
92
    mListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener()
93
      {
94
      @Override
95
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
96
        {
97
        RubikPattern pattern = RubikPattern.getInstance();
98
        int[][] moves   = pattern.reInitialize(mTab, groupPosition, childPosition);
99
        ObjectType object = RubikPatternList.getObject(mTab);
100
        ract.changeIfDifferent(object,control);
101
        control.initializeObject(moves);
102

    
103
        ScreenList.switchScreen(ract, ScreenList.PATT);
104
        RubikScreenPattern state = (RubikScreenPattern) ScreenList.PATT.getScreenClass();
105
        state.setPattern(ract, mTab, groupPosition, childPosition);
106

    
107
        mDialog.rememberState();
108
        mDialog.dismiss();
109

    
110
        return false;
111
        }
112
      });
113

    
114
    mListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener()
115
      {
116
      @Override
117
      public void onGroupExpand(int groupPosition)
118
        {
119
        if(mExpandedGroup!=-1 && groupPosition!=mExpandedGroup)
120
          {
121
          mListView.collapseGroup(mExpandedGroup);
122
          }
123

    
124
        mExpandedGroup = groupPosition;
125
        }
126
      });
127

    
128
    addView(tab);
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  int getCurrentCategory()
134
    {
135
    return mPos;
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  int getCurrentVisiblePos()
141
    {
142
    return mListView.getFirstVisiblePosition();
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  int getExpanded()
148
    {
149
    return mExpandedGroup;
150
    }
151
  }
(9-9/19)