15
15
import android .view .ViewGroup ;
16
16
import android .widget .ImageView ;
17
17
import android .widget .RelativeLayout ;
18
-
19
18
import com .scrappers .GamePad .R ;
20
-
21
19
import androidx .annotation .NonNull ;
22
20
import androidx .annotation .Nullable ;
23
21
import androidx .appcompat .app .AppCompatActivity ;
24
22
import androidx .core .content .ContextCompat ;
25
23
26
24
public class ControlButtonsView extends RelativeLayout {
27
- public static final int GAMEPAD_BUTTON_X = 'X' ;
28
- public static final int GAMEPAD_BUTTON_Y = 'Y' ;
29
- public static final int GAMEPAD_BUTTON_A = 'A' ;
30
- public static final int GAMEPAD_BUTTON_B = 'B' ;
31
- public static final int DEFAULT_GAMEPAD_DOMAIN =R .drawable .gamepad_domain ;
32
- public static final int DEFAULT_COLOR_STICK_DOMAIN =R .drawable .moving_stick_domain ;
33
- public static final int FLIPPED_COLOR_STICK_DOMAIN =R .drawable .moving_stick_flipped_domain ;
34
- public static final int OPACIFIED_COLOR_STICK_DOMAIN =R .drawable .opacified_domain ;
35
- public static final int NOTHING_IMAGE =R .drawable .nothing ;
36
- public static final int DEFAULT_BUTTONS =R .drawable .moving_stick ;
37
- public static final int CRYSTAL_BUTTONS = R .drawable .crystal_buttons ;
38
- public static final int CRYSTAL_QUADS =R .drawable .crystal_buttons_quad ;
39
- public static final int MATERIALISTIC_BUTTONS =R .drawable .material_buttons ;
40
- public static final int MATERIALISTIC_QUADS =R .drawable .material_buttons_quad ;
41
- public static final int TEAL_HEXAS =R .drawable .teal_hexagons ;
42
- public static final int TRIS_BUTTONS =R .drawable .tris_buttons ;
43
- public static final int X_BUTTON_ALPHA =R .drawable .x_button_alpha ;
44
- public static final int X_BUTTON_OUTLINE =R .drawable .x_button_outline ;
45
- public static final int Y_BUTTON_ALPHA =R .drawable .y_button_alpha ;
46
- public static final int Y_BUTTON_OUTLINE =R .drawable .y_button_outline ;
47
- public static final int A_BUTTON_ALPHA =R .drawable .a_button_alpha ;
48
- public static final int A_BUTTON_OUTLINE =R .drawable .a_button_outline ;
49
- public static final int B_BUTTON_ALPHA =R .drawable .b_button_alpha ;
50
- public static final int B_BUTTON_OUTLINE =R .drawable .b_button_outline ;
51
- public static final int STICK_DASHES =R .drawable .stick_dash ;
25
+ /**
26
+ * Grouping up the ButtonSignatures.
27
+ * @author pavl_g
28
+ */
29
+ public enum ButtonSignature {
30
+ GAMEPAD_BUTTON_X ('X' , "X" ),
31
+ GAMEPAD_BUTTON_Y ('Y' , "Y" ),
32
+ GAMEPAD_BUTTON_A ('A' , "A" ),
33
+ GAMEPAD_BUTTON_B ('B' , "B" );
52
34
35
+ public final int ID ;
36
+ public String NAME ;
53
37
38
+ ButtonSignature (final int ID ){
39
+ this .ID = ID ;
40
+ }
41
+ ButtonSignature (final int ID , final String NAME ){
42
+ this .ID = ID ;
43
+ this .NAME = NAME ;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Grouping up the ButtonIcons.
49
+ * @author pavl_g
50
+ */
51
+ public enum ButtonIcon {
52
+ X_BUTTON_ALPHA (R .drawable .x_button_alpha ), X_BUTTON_OUTLINE (R .drawable .x_button_outline ),
53
+ Y_BUTTON_ALPHA (R .drawable .y_button_alpha ), Y_BUTTON_OUTLINE (R .drawable .y_button_outline ),
54
+ A_BUTTON_ALPHA (R .drawable .a_button_alpha ), A_BUTTON_OUTLINE (R .drawable .a_button_outline ),
55
+ B_BUTTON_ALPHA (R .drawable .b_button_alpha ), B_BUTTON_OUTLINE (R .drawable .b_button_outline );
56
+ public final int ID ;
57
+
58
+ ButtonIcon (final int ID ){
59
+ this .ID = ID ;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Grouping up the ButtonsStyles.
65
+ * @author pavl_g
66
+ */
67
+ public enum ButtonStyle {
68
+ DEFAULT_GAMEPAD_DOMAIN (R .drawable .gamepad_domain ), DEFAULT_COLOR_STICK_DOMAIN (R .drawable .moving_stick_domain ),
69
+ FLIPPED_COLOR_STICK_DOMAIN (R .drawable .moving_stick_flipped_domain ), OPACIFIED_COLOR_STICK_DOMAIN (R .drawable .opacified_domain ),
70
+ NOTHING_IMAGE (R .drawable .nothing ), DEFAULT_BUTTONS (R .drawable .moving_stick ), CRYSTAL_BUTTONS (R .drawable .crystal_buttons ),
71
+ CRYSTAL_QUADS (R .drawable .crystal_buttons_quad ), MATERIALISTIC_BUTTONS (R .drawable .material_buttons ),
72
+ TEAL_HEXAS (R .drawable .teal_hexagons ), TRIS_BUTTONS (R .drawable .tris_buttons ), STICK_DASHES (R .drawable .stick_dash );
73
+
74
+ public final int STYLE ;
75
+
76
+ ButtonStyle (final int STYLE ){
77
+ this .STYLE = STYLE ;
78
+ }
79
+ }
54
80
55
81
public ControlButtonsView (@ NonNull Context context , @ Nullable AttributeSet attrs ) {
56
82
super (context , attrs );
@@ -60,60 +86,63 @@ public ControlButtonsView(@NonNull Context context, @Nullable AttributeSet attrs
60
86
super (context , attrs , defStyleAttr );
61
87
}
62
88
63
- public ImageView addControlButton (String buttonName ,int buttonID , int backgroundDrawable , int drawableIcon ){
89
+ /**
90
+ * Adds a GamePad button & apply the UI.
91
+ * @param button the button to add from the enum #{@link ControlButtonsView.ButtonSignature} : eg : #{@link ButtonSignature#GAMEPAD_BUTTON_X},...etc.
92
+ * @param backgroundDrawable the drawable style, you can get some from enum : #{@link ControlButtonsView.ButtonStyle}
93
+ * @param drawableIcon the button icon, you can get some from enum : #{@link ControlButtonsView.ButtonIcon}
94
+ * @return an imageView instance representing the added button.
95
+ */
96
+ public ImageView addControlButton (@ NonNull ButtonSignature button , int backgroundDrawable , int drawableIcon ){
64
97
ImageView controlButton = new ImageView (this .getContext ());
65
- ((AppCompatActivity )getContext ()).runOnUiThread (()->{
66
- controlButton .setId (buttonID );
67
- ViewGroup .LayoutParams layoutParams = new LayoutParams (this .getLayoutParams ().width /3 , this .getLayoutParams ().height /3 );
68
- controlButton .setLayoutParams (layoutParams );
69
- controlButton .setImageDrawable (ContextCompat .getDrawable (this .getContext (), drawableIcon ));
70
- controlButton .setBackground (ContextCompat .getDrawable (this .getContext (), backgroundDrawable ));
71
- if ( Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ){
72
- controlButton .setTooltipText (buttonName );
73
- }
74
- switch (buttonID ){
75
- case GAMEPAD_BUTTON_X :
98
+ ((AppCompatActivity )getContext ()).runOnUiThread (()->{
99
+ controlButton .setId (button .ID );
100
+ ViewGroup .LayoutParams layoutParams = new LayoutParams (this .getLayoutParams ().width /3 , this .getLayoutParams ().height /3 );
101
+ controlButton .setLayoutParams (layoutParams );
102
+ controlButton .setImageDrawable (ContextCompat .getDrawable (this .getContext (), drawableIcon ));
103
+ controlButton .setBackground (ContextCompat .getDrawable (this .getContext (), backgroundDrawable ));
104
+
105
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ){
106
+ controlButton .setTooltipText (button .NAME );
107
+ }
108
+
109
+ if (button == ButtonSignature .GAMEPAD_BUTTON_X ){
76
110
controlButton .setX (0f );
77
111
controlButton .setY (layoutParams .height );
78
- break ;
79
- case GAMEPAD_BUTTON_B :
80
- controlButton .setX (layoutParams .width *2 );
112
+ }else if (button == ButtonSignature .GAMEPAD_BUTTON_B ){
113
+ controlButton .setX (layoutParams .width * 2 );
81
114
controlButton .setY (layoutParams .height );
82
- break ;
83
-
84
- case GAMEPAD_BUTTON_Y :
115
+ }else if (button == ButtonSignature .GAMEPAD_BUTTON_Y ){
85
116
controlButton .setX (layoutParams .width );
86
117
controlButton .setY (0f );
87
- break ;
88
- case GAMEPAD_BUTTON_A :
118
+ }else if (button == ButtonSignature .GAMEPAD_BUTTON_A ){
89
119
controlButton .setX (layoutParams .width );
90
- controlButton .setY (layoutParams .height *2 );
91
- break ;
92
- }
93
- this .addView (controlButton );
94
- });
120
+ controlButton .setY (layoutParams .height * 2 );
121
+ }
122
+ this .addView (controlButton );
123
+ });
95
124
return controlButton ;
96
125
}
97
- public void setButtonListener (int buttonID , OnClickListener onClickListener ){
98
- ImageView pushButton =findViewById (buttonID );
126
+ public void setButtonListener (@ NonNull ButtonSignature button , OnClickListener onClickListener ){
127
+ ImageView pushButton =findViewById (button . ID );
99
128
pushButton .setOnClickListener (onClickListener );
100
129
}
101
- public void setButtonLongClickListener (int buttonID , OnLongClickListener onLongClickListener ){
102
- ImageView pushButton =findViewById (buttonID );
130
+ public void setButtonLongClickListener (@ NonNull ButtonSignature button , OnLongClickListener onLongClickListener ){
131
+ ImageView pushButton =findViewById (button . ID );
103
132
pushButton .setOnLongClickListener (onLongClickListener );
104
133
}
105
- public void setButtonBackgroundDrawable (int buttonID ,int drawable ){
106
- (findViewById (buttonID )).setBackground (ContextCompat .getDrawable (getContext (),drawable ));
134
+ public void setButtonBackgroundDrawable (@ NonNull ButtonSignature button ,int drawable ){
135
+ (findViewById (button . ID )).setBackground (ContextCompat .getDrawable (getContext (),drawable ));
107
136
}
108
- public void setButtonSrcDrawable (int buttonID , int drawable ){
109
- ((ImageView )findViewById (buttonID )).setImageDrawable (ContextCompat .getDrawable (getContext (),drawable ));
137
+ public void setButtonSrcDrawable (@ NonNull ButtonSignature button , int drawable ){
138
+ ((ImageView )findViewById (button . ID )).setImageDrawable (ContextCompat .getDrawable (getContext (),drawable ));
110
139
}
111
- public void setButtonSrcBitmap (int buttonID , String srcPath ){
112
- ((ImageView )findViewById (buttonID )).setImageBitmap (BitmapFactory .decodeFile (srcPath ));
140
+ public void setButtonSrcBitmap (@ NonNull ButtonSignature button , String srcPath ){
141
+ ((ImageView )findViewById (button . ID )).setImageBitmap (BitmapFactory .decodeFile (srcPath ));
113
142
}
114
- public void setButtonSrcIcon (int buttonID , String srcPath ){
143
+ public void setButtonSrcIcon (@ NonNull ButtonSignature button , String srcPath ){
115
144
if ( Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ){
116
- ((ImageView )findViewById (buttonID )).setImageIcon (Icon .createWithFilePath (srcPath ));
145
+ ((ImageView )findViewById (button . ID )).setImageIcon (Icon .createWithFilePath (srcPath ));
117
146
}
118
147
}
119
148
/**
0 commit comments