-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreateEditorScreen.pde
More file actions
135 lines (121 loc) · 5.08 KB
/
Copy pathCreateEditorScreen.pde
File metadata and controls
135 lines (121 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
public class CreateEditorScreen extends Screen implements ClickListener{
private final ScreenDeleguate mScreenDeleguate;
private final DataDeleguate mData;
private final TextureDeleguate mTextures;
private TextField mWidthTextField;
private TextField mHeightTextField;
private TextField mNameTextField;
private TextButton mBackButton;
private TextButton mCreateButton;
private String mErrorMessage;
CreateEditorScreen(ScreenDeleguate screenDeleguate, DataDeleguate dataDeleguate, TextureDeleguate textures){
mScreenDeleguate = screenDeleguate;
mData = dataDeleguate;
mTextures = textures;
mErrorMessage = "";
mBackButton = new TextButton(width/2 - 150, height - 50, "Retour", 36, mTextures.mLeftSelector);
mCreateButton = new TextButton(width/2 + 150, height - 50, "Créer", 36, mTextures.mLeftSelector);
mWidthTextField = new TextField(width/2 - 100, height/2 - 105, 200, 50, 2);
mHeightTextField = new TextField(width/2 - 100, height/2 + 25, 200, 50, 2);
mNameTextField = new TextField(width/2 - 170, height/2 + 155, 340, 50, 8);
// J'enregistre l'objet courant comme écouteur
mBackButton.addListener(this);
mCreateButton.addListener(this);
// Je paramètre les champs de texte
mWidthTextField.setFilter(TextField.ONLY_NUMBERS);
mHeightTextField.setFilter(TextField.ONLY_NUMBERS);
mNameTextField.setFilter(TextField.NUMBERS_AND_CHARACTERS);
mWidthTextField.setActive(true);
// J'actualise le curseur
mouseMoved();
}
public void drawScreen(){
background(mTextures.mBackgroundColor);
mTextures.drawTitle();
drawBody();
drawErrorMessage();
}
private void drawBody(){
textFont(mTextures.mFont, 36);
textAlign(CENTER, CENTER);
text("Largeur du niveau", width/2, height/2 - 130);
mWidthTextField.drawTextField();
text("hauteur du niveau", width/2, height/2);
mHeightTextField.drawTextField();
text("Nom du niveau", width/2, height/2 + 130);
mNameTextField.drawTextField();
mBackButton.drawButton();
mCreateButton.drawButton();
}
private void drawErrorMessage(){
fill(mTextures.mErrorColor);
textAlign(CENTER, CENTER);
textFont(mTextures.mFont, 18);
text(mErrorMessage, width/2, height - 100);
}
// Méthode appelée si un bouton que l'instance écoute est appuyé
public void onClick(Button src){
if(src == mBackButton){
// Le bouton Retour est cliqué, on affiche le menu
mScreenDeleguate.setMenuScreen();
} else if(src == mCreateButton){
// Le bouton créer est cliqué, on créé le niveau et on lance la page de création
// Il faut vérifier que le nom choisis n'existe pas déja
if(mWidthTextField.getText().length() == 0 || mHeightTextField.getText().length() == 0 || mNameTextField.getText().length() == 0){
mErrorMessage = "Tu dois remplir tout les champs";
return;
}
int levelWidth = Integer.parseInt(mWidthTextField.getText());
int levelHeight = Integer.parseInt(mHeightTextField.getText());
if(levelWidth < 2 || levelWidth > 25 || levelHeight < 2 || levelHeight > 25){
mErrorMessage = "La taille du niveau doit etre entre 2 et 25";
return;
}
for(Level level : mData.mLevels){
if(level.getname().toLowerCase().equals(mNameTextField.getText().toLowerCase())){
// Le nom choisis existe déja, afficher un message d'erreur;
mErrorMessage = "Le nom choisis existe déja";
return;
}
}
Level level = new Level(levelWidth, levelHeight, mNameTextField.getText());
mScreenDeleguate.setSetupEditorScreen(level);
}
// La page est changée, j'enlève les listeners
mBackButton.removeListener(this);
mCreateButton.removeListener(this);
}
public void mouseClicked(){
// prévient les champs de texte et boutons
mWidthTextField.isClick();
mHeightTextField.isClick();
mNameTextField.isClick();
mCreateButton.isClick();
mBackButton.isClick();
}
public void keyPressed(){
// Une touche est préssée, je préviens les champs de texte
mWidthTextField.aKeyWasPressed(key);
mHeightTextField.aKeyWasPressed(key);
mNameTextField.aKeyWasPressed(key);
}
public void mouseMoved(){
// La souris bouge, je préviens mes boutons et mes champs de texte
// Si la souris n'est sur aucun, je met le curseur par défaut
if(!mWidthTextField.isMouseOnIt()
&& !mHeightTextField.isMouseOnIt()
&& !mNameTextField.isMouseOnIt()
&& !mBackButton.isMouseOnIt()
&& !mCreateButton.isMouseOnIt()){
cursor(ARROW);
}
}
public void sizeChanged(){
// La taille de la fenetre change, je recalcule les positions des boutons et champs de texte
mWidthTextField.setPosition(width/2 - 100, height/2 - 105);
mHeightTextField.setPosition(width/2 - 100, height/2 + 25);
mNameTextField.setPosition(width/2 - 170, height/2 + 155);
mBackButton.setPosition(width/2 - 150, height - 50);
mCreateButton.setPosition(width/2 + 150, height - 50);
}
}