Skip to content

Commit

Permalink
#27 snippets are fully functional and can be named
Browse files Browse the repository at this point in the history
  • Loading branch information
jkbriant committed Nov 8, 2018
1 parent 8d84f4f commit be07d0f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/computerblocks/Game.java
Expand Up @@ -110,7 +110,7 @@ private synchronized void render() {
display.reset(Color.BACKGROUND);

grid.draw(display, player);
player.draw(display, grid);
player.draw(display, grid, snippetTray);
ui.draw(display, player, grid);
if (player.state == State.MENU) menuController.update(display, player, grid);
if (player.state == State.SNIPPET) snippetTray.update(display, player, grid);
Expand Down
2 changes: 2 additions & 0 deletions src/computerblocks/display/ui/menu/MenuController.java
Expand Up @@ -10,6 +10,8 @@ public class MenuController {
public Menu pauseMenu, saveMenu, settingsMenu, creditsMenu;
public SaveMenu saveMenuElement;

public SnippetTray snippetTray;

public MenuController(Display display) {
init(display);
}
Expand Down
9 changes: 0 additions & 9 deletions src/computerblocks/display/ui/menu/SaveMenu.java
Expand Up @@ -22,27 +22,18 @@ public class SaveMenu {
private int scrollSpeed = 3;

public ArrayList<SaveButton> saves;
public ArrayList<SaveButton> savesRemovalQueue;
private CreateSave saveButton;

public boolean refreshSaveNames = true;

public SaveMenu(Display display) {
saves = new ArrayList<SaveButton>();
savesRemovalQueue = new ArrayList<SaveButton>();
height = display.height * 7 / 10;
width = display.width / 3;
saveButton = new CreateSave(display, this);
}

public void update(Display display, Player player, Grid grid) {

for (SaveButton i : savesRemovalQueue) {
refreshSaveNames = true;
savesRemovalQueue.remove(i);
break;
}

if (refreshSaveNames) {
saves = new ArrayList<SaveButton>();
for (final File fileEntry : new File("../saves/grids").listFiles()) {
Expand Down
12 changes: 12 additions & 0 deletions src/computerblocks/display/ui/menu/SnippetTray.java
Expand Up @@ -21,7 +21,10 @@ public class SnippetTray {
public int scroll;
private int scrollSpeed = 3;

public boolean refreshSaveNames = false;

public ArrayList<SnippetButton> snippets;
public ArrayList<SnippetButton> snippetsRemovalQueue;

private int animationTime = 25;

Expand All @@ -31,6 +34,15 @@ public SnippetTray(Display display) {

public void update(Display display, Player player, Grid grid) {

if (refreshSaveNames) {
snippets = new ArrayList<SnippetButton>();
for (final File fileEntry : new File("../saves/snippets").listFiles()) {
if (!fileEntry.isDirectory()) {
snippets.add(new SnippetButton(display, this, fileEntry.getName().substring(0, fileEntry.getName().length() - 5)));
}
}
}

height = display.height * 8 / 10;
width = display.width / 6;

Expand Down
Expand Up @@ -96,7 +96,7 @@ public void checkPress(Display display, Player player, SaveMenu saveMenu, Grid g
display.draw();

if (player.keyboard.enterDown) {
saveMenu.savesRemovalQueue.add(this);
saveMenu.refreshSaveNames = true;
new File("../saves/grids/" + text + ".snip").delete();
break;
}
Expand Down
56 changes: 53 additions & 3 deletions src/computerblocks/display/ui/menu/elements/SnippetSave.java
Expand Up @@ -20,6 +20,10 @@ public class SnippetSave {
public double x, y, width, height;
private Image icon;

private String font = Fonts.pixelmix;
private int textSize = 20;
private String text = "New Snippet";

public SnippetSave(double x, double y, double width, double height) {
this.x = x;
this.y = y;
Expand Down Expand Up @@ -53,17 +57,63 @@ public boolean pointOver(double x, double y) {
return false;
}

public void checkPress(Display display, Player player) {
public void checkPress(Display display, Player player, SnippetTray snippetTray) {
if (pointOver(player.mouse.position.x, player.mouse.position.y)) {
if (player.mouse.down(Mouse.LEFT)) {
buttonPress(player);
buttonPress(display, player, snippetTray);
}
display.color(new Color(255, 255, 255, 0.2f));
display.rect(x, y, width, height);
}
}

public void buttonPress(Player player) {
public void buttonPress(Display display, Player player, SnippetTray snippetTray) {
String saveName = "";
while (true) {
saveName = player.keyboard.keyStream(saveName, 20);

if (player.keyboard.down(Keyboard.ESC)) break;

display.reset(Color.BACKGROUND);
display.font(font, textSize);
if (!saveName.equals("")) {
if (snippetAlreadyExists(saveName, snippetTray)) {
display.color(Color.INVERTER);
String warningText = "WARINING! - THIS WILL REPLACE PREVIOUS SNIPPET";
display.text(warningText, (display.width / 2) - display.getStringWidth(warningText, font, textSize) / 2, (display.height / 2) + display.getFontHeight(font, textSize) / 2 + display.height / 8);
}
display.color(Color.UI_BORDER);
display.text(saveName, (display.width / 2) - display.getStringWidth(saveName, font, textSize) / 2, (display.height / 2) + display.getFontHeight(font, textSize) / 2);
} else {
display.color(new Color(Color.UI_BORDER, 0.25f));
display.text(text, (display.width / 2) - display.getStringWidth(text, font, textSize) / 2, (display.height / 2) + display.getFontHeight(font, textSize) / 2);
}
display.draw();

if (player.keyboard.enterDown) {
if (!saveName.equals("")) {
player.snippet.saveToFile("../saves/snippets/", saveName);
snippetTray.refreshSaveNames = true;
player.state = State.GAME;
player.placeTime = 0;
break;
} else {
player.snippet.saveToFile("../saves/snippets/", text);
snippetTray.refreshSaveNames = true;
player.state = State.GAME;
player.placeTime = 0;
break;
}
}
}
}

public boolean snippetAlreadyExists(String saveName, SnippetTray snippetTray) {
for (SnippetButton i : snippetTray.snippets) {
if (i.text.toLowerCase().equals(saveName.toLowerCase())) {
return true;
}
}
return false;
}
}
4 changes: 2 additions & 2 deletions src/computerblocks/player/Player.java
Expand Up @@ -51,12 +51,12 @@ public Player(Display display, Game game) {
snipSaveButton = new SnippetSave(display.width - display.width / 25 * 1.2, display.width / 25 * 0.2, display.width / 25, display.width / 25);
}

public void draw(Display display, Grid grid) {
public void draw(Display display, Grid grid, SnippetTray snippetTray) {
if (selection != null) selection.draw(display, grid, this);
if (state == State.PASTE && snippet != null) {
snippet.ghost(display, this, grid.mouseOverBlock(this));
snipSaveButton.draw(display);
snipSaveButton.checkPress(display, this);
snipSaveButton.checkPress(display, this, snippetTray);
}
}

Expand Down

0 comments on commit be07d0f

Please sign in to comment.