1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 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.component;
|
21
|
|
22
|
import android.content.Context;
|
23
|
import android.support.annotation.Nullable;
|
24
|
import android.util.AttributeSet;
|
25
|
import android.view.View;
|
26
|
import android.widget.Button;
|
27
|
import android.widget.LinearLayout;
|
28
|
import android.widget.TextView;
|
29
|
|
30
|
import org.distorted.magic.R;
|
31
|
|
32
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
33
|
|
34
|
public class HorizontalNumberPicker extends LinearLayout
|
35
|
{
|
36
|
private TextView mNumber;
|
37
|
private int mMin, mMax;
|
38
|
|
39
|
private class AddHandler implements OnClickListener
|
40
|
{
|
41
|
final int diff;
|
42
|
|
43
|
AddHandler(int diff)
|
44
|
{
|
45
|
this.diff = diff;
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
public void onClick(View v)
|
50
|
{
|
51
|
int newValue = getValue() + diff;
|
52
|
|
53
|
if (newValue < mMin)
|
54
|
{
|
55
|
newValue = mMin;
|
56
|
}
|
57
|
else if (newValue > mMax)
|
58
|
{
|
59
|
newValue = mMax;
|
60
|
}
|
61
|
setValue(newValue);
|
62
|
}
|
63
|
}
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
// PUBLIC API
|
67
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68
|
|
69
|
public HorizontalNumberPicker(Context context, @Nullable AttributeSet attrs)
|
70
|
{
|
71
|
super(context, attrs);
|
72
|
|
73
|
mMin = 0;
|
74
|
mMax = 5;
|
75
|
|
76
|
inflate(context, R.layout.numberpicker, this);
|
77
|
|
78
|
mNumber = findViewById(R.id.textNumber);
|
79
|
|
80
|
final Button btn_less = findViewById(R.id.buttonLess);
|
81
|
btn_less.setOnClickListener(new AddHandler(-1));
|
82
|
|
83
|
final Button btn_more = findViewById(R.id.buttonMore);
|
84
|
btn_more.setOnClickListener(new AddHandler( 1));
|
85
|
}
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
public int getValue()
|
90
|
{
|
91
|
if(mNumber != null)
|
92
|
{
|
93
|
try
|
94
|
{
|
95
|
final String value = mNumber.getText().toString();
|
96
|
return Integer.parseInt(value);
|
97
|
}
|
98
|
catch(NumberFormatException ex)
|
99
|
{
|
100
|
android.util.Log.e("HorizontalNumberPicker", ex.toString());
|
101
|
}
|
102
|
}
|
103
|
return 0;
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
|
108
|
public void setValue(final int value)
|
109
|
{
|
110
|
if (mNumber != null)
|
111
|
{
|
112
|
mNumber.setText(String.valueOf(value));
|
113
|
}
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
|
118
|
public void setMin(int min)
|
119
|
{
|
120
|
mMin = min;
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
public void setMax(int max)
|
126
|
{
|
127
|
mMax = max;
|
128
|
}
|
129
|
}
|