@Override
  public void buttonPressed(ClickableComponent button) {

    // save menu buttons
    if (saveMenuVisible) {
      if (button == confirmeSaveButton) {
        saveLevel(saveLevelName);
        updateLevels();
      }

      if (button == confirmeSaveButton || button == cancelSaveButton) {
        saveMenuVisible = false;
        updateButtons = true;
        saveLevelName = "";
      }

      return;
    }

    // tile buttons
    if (button instanceof LevelEditorButton) {
      LevelEditorButton lb = (LevelEditorButton) button;

      if (selectedButton != null && selectedButton != lb) {
        selectedButton.setActive(false);
        selectedButton = lb;
      }

      return;
    }

    // menu buttons
    if (!clicked) {
      if (button == newButton) {
        newLevel();
      } else if (button == openButton) {
        selectedLevel = (selectedLevel < levels.size() - 1 ? selectedLevel + 1 : 0);
        openLevel(LevelList.getLevels().get(selectedLevel));

      } else if (button == saveButton) {
        saveMenuVisible = true;
        updateButtons = true;
      }

      clicked = true;
    } else {
      clicked = false;
    }
  }
  public MojamComponent() {
    setLocale(new Locale("en"));

    this.setPreferredSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
    this.setMinimumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
    this.setMaximumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));

    this.addMouseMotionListener(this);
    this.addMouseListener(this);

    TitleMenu menu = new TitleMenu(GAME_WIDTH, GAME_HEIGHT);
    addMenu(menu);
    addKeyListener(this);
    addKeyListener(chat);

    instance = this;
    LevelList.createLevelList();
  }
  private void saveLevel(String name) {
    File newLevel = new File(LevelList.getBaseDir(), name + ".bmp");

    BufferedImage image = new BufferedImage(LEVEL_WIDTH, LEVEL_HEIGHT, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < LEVEL_HEIGHT; x++) {
      for (int y = 0; y < LEVEL_WIDTH; y++) {
        image.setRGB(x, y, mapTile[x][y]);
      }
    }

    try {
      ImageIO.write(image, "BMP", newLevel);
    } catch (IOException ioe) {
      System.out.println("Error saving new level: " + ioe);
    }
    removeText(levelName);
    levelName = new Text(1, "+ " + name, 120, 5);
    addText(levelName);
  }
 private void updateLevels() {
   LevelList.resetLevels();
   levels = LevelList.getLevels();
 }