Esempio n. 1
0
  public Editor(final Quantum quantum, final Gui gui) {
    this.gui = gui;
    this.quantum = quantum;

    //
    // add this as a display and mouse listener
    //
    quantum.addDisplayListener(this);
    gui.getCanvas().addMouseListener(this);
    gui.getCanvas().addMouseMotionListener(this);
    gui.getCanvas().addKeyListener(this);

    //
    // create the simulation renderer and the simulation itself
    //

    renderer = new Renderer();
    renderer.setRenderAllPaths(true);
    renderer.setRenderIsStartPlanet(true);
    sim = new Simulation(false);
    renderer.getCamera().setScale(20);

    //
    // create the toolbar
    //
    final ScreenAlignementContainer cont =
        new ScreenAlignementContainer(gui, HorizontalAlignement.LEFT, VerticalAlignement.TOP);
    final ScreenAlignementContainer cont2 =
        new ScreenAlignementContainer(gui, HorizontalAlignement.LEFT, VerticalAlignement.BOTTOM);
    final HorizontalBoxContainer toolbar = new HorizontalBoxContainer(gui);

    Button back = new Button(gui, "back");
    back.setBackgroundColor(new Color(0, 0, 0, 0.7f));
    back.setSize(50, 24);
    back.setClickedListener(
        new ClickedListener() {
          public void clicked(Widget widget) {
            clearSelection();
            gui.remove(cont);
            gui.remove(cont2);
            gui.getCanvas().removeMouseListener(self);
            gui.getCanvas().removeMouseMotionListener(self);
            gui.getCanvas().removeKeyListener(self);
            quantum.removeDisplayListener(self);
            renderer.dispose();
            new StartMenu(quantum, gui);
          }
        });

    Button new_b = new Button(gui, "new");
    new_b.setBackgroundColor(new Color(0, 0, 0, 0.7f));
    new_b.setSize(50, 24);
    new_b.setClickedListener(
        new ClickedListener() {
          public void clicked(Widget widget) {
            clearSelection();
            sim.clear();
          }
        });

    Button open = new Button(gui, "open");
    open.setBackgroundColor(new Color(0, 0, 0, 0.7f));
    open.setSize(50, 24);
    open.setClickedListener(
        new ClickedListener() {
          public void clicked(Widget widget) {

            JFileChooser fc = new JFileChooser(last_directory);
            fc.setFileFilter(
                new FileFilter() {

                  public boolean accept(File pathname) {
                    if (pathname.getPath().endsWith(".map")) return true;
                    else return false;
                  }

                  public String getDescription() {
                    return "Quantum map files (*.map)";
                  }
                });
            if (fc.showOpenDialog(quantum) == JFileChooser.APPROVE_OPTION) {
              clearSelection();
              last_directory = fc.getCurrentDirectory();
              try {
                DataInputStream in = new DataInputStream(new FileInputStream(fc.getSelectedFile()));
                sim = new Simulation(false);
                sim.readState(in);
                in.close();
              } catch (Exception e) {
                Log.println("[Editor] couldn't save file");
                gui.showConfirmDialog("Couldn't save file", "Error");
                sim = new Simulation(false);
              }
            }
          }
        });

    Button save = new Button(gui, "save");
    save.setBackgroundColor(new Color(0, 0, 0, 0.7f));
    save.setSize(50, 24);
    save.setClickedListener(
        new ClickedListener() {

          public void clicked(Widget widget) {
            if (sim.getPlanets().size() == 0) return;

            for (Planet planet : sim.getPlanets())
              if (planet.getReachablePlanets().size() == 0) {
                gui.showConfirmDialog(
                    "All planets must be connected to at least one other planet!", "Error");
                return;
              }

            showSaveDialog();
          }
        });

    Button help = new Button(gui, "Help");
    help.setBackgroundColor(new Color(0, 0, 0, 0.7f));
    help.setSize(50, 24);
    help.setClickedListener(
        new ClickedListener() {
          public void clicked(Widget widget) {
            String help_text =
                "   mouse wheel/key up/down-> zoom\n"
                    + "   right/middle mouse button + drag -> pan\n"
                    + "   middle mouse button/space -> create planet\n"
                    + "   left click -> select planet\n"
                    + "   ctrl + left click -> select another planet\n"
                    + "   left click + drag -> select multiple planets\n"
                    + "\n\nWhen multiple planets are selected, changing the property of one planet automatically changes the property of all other selected planets accordingly\n\n"
                    + "Holding down shift and clicking on two planets will create a path between them or delete an already established path";
            gui.showConfirmDialog(help_text, "Editor Usage", 500);
          }
        });

    toolbar.addWidget(new_b);
    toolbar.addWidget(open);
    toolbar.addWidget(save);
    toolbar.addWidget(help);
    toolbar.addWidget(back);
    toolbar.addWidget(new Spacer(gui, 50, 0));
    cont.addWidget(toolbar);
    gui.add(cont);

    Button triangulate = new Button(gui, "Calculate Paths");
    triangulate.setBackgroundColor(new Color(0, 0, 0, 0.7f));
    triangulate.setSize(120, 24);
    triangulate.setClickedListener(
        new ClickedListener() {

          public void clicked(Widget widget) {
            sim.calculatePaths();
          }
        });

    Button clear = new Button(gui, "Clear Paths");
    clear.setBackgroundColor(new Color(0, 0, 0, 0.7f));
    clear.setSize(120, 24);
    clear.setClickedListener(
        new ClickedListener() {

          public void clicked(Widget widget) {
            for (Planet planet : sim.getPlanets()) planet.getReachablePlanets().clear();
          }
        });

    HorizontalBoxContainer toolbar2 = new HorizontalBoxContainer(gui);
    toolbar2.addWidget(triangulate);
    toolbar2.addWidget(clear);
    cont2.addWidget(toolbar2);
    gui.add(cont2);
  }
Esempio n. 2
0
  public void showSaveDialog() {
    no_input = true;
    VerticalBoxContainer v_box = new VerticalBoxContainer(gui);
    v_box.addWidget(new Label(gui, "Author"));
    v_box.addWidget(new Spacer(gui, 0, 5));
    final TextField author = new TextField(gui);
    if (sim.getAuthor().equals("") == false) author.setEnabled(false);
    author.setText(sim.getAuthor());
    author.setSize(280, 25);
    v_box.addWidget(author);

    v_box.addWidget(new Label(gui, "Map Name"));
    v_box.addWidget(new Spacer(gui, 0, 5));
    final TextField name = new TextField(gui);
    name.setText(sim.getName());
    name.setSize(280, 25);
    v_box.addWidget(name);

    v_box.addWidget(new Label(gui, "Map Description"));
    v_box.addWidget(new Spacer(gui, 0, 5));
    final TextField description = new TextField(gui);
    description.setText(sim.getDescription());
    description.setSize(280, 25);
    v_box.addWidget(description);

    Button ok = new Button(gui, "Ok");
    Button cancel = new Button(gui, "Cancel");
    ok.setSize(70, 25);
    cancel.setSize(70, 25);

    final CustomDialog dialog = new CustomDialog(gui, 300, "Map Information", v_box, ok, cancel);
    gui.showCustomDialog(dialog);

    ok.setClickedListener(
        new ClickedListener() {

          public void clicked(Widget widget) {
            no_input = false;
            gui.removeCustomDialog(dialog);

            if (name.getText().equals("")) {
              gui.showConfirmDialog(
                  "You have to enter a map name!",
                  "Error",
                  new ClickedListener() {

                    public void clicked(Widget widget) {
                      showSaveDialog();
                    }
                  });
              return;
            }

            if (author.getText().equals("")) {
              gui.showConfirmDialog(
                  "You have to enter a map author!",
                  "Error",
                  new ClickedListener() {

                    public void clicked(Widget widget) {
                      showSaveDialog();
                    }
                  });
              return;
            }

            if (description.getText().equals("")) {
              gui.showConfirmDialog(
                  "You have to enter a map description!",
                  "Error",
                  new ClickedListener() {

                    public void clicked(Widget widget) {
                      showSaveDialog();
                    }
                  });
              return;
            }

            sim.setAuthor(author.getText());
            sim.setName(name.getText());
            sim.setDescription(description.getText());

            JFileChooser fc = new JFileChooser(last_directory);
            fc.setFileFilter(
                new FileFilter() {

                  @Override
                  public boolean accept(File pathname) {
                    if (pathname.getPath().endsWith(".map")) return true;
                    else return false;
                  }

                  @Override
                  public String getDescription() {
                    return "Quantum map files (*.map)";
                  }
                });
            if (fc.showSaveDialog(quantum) == JFileChooser.APPROVE_OPTION) {
              last_directory = fc.getCurrentDirectory();
              try {
                DataOutputStream out =
                    new DataOutputStream(new FileOutputStream(fc.getSelectedFile()));
                sim.writeState(out);
                out.close();
              } catch (Exception e) {
                Log.println("[Editor] couldn't save file");
                gui.showConfirmDialog("Couldn't save file", "Error");
              }
            }
          }
        });

    cancel.setClickedListener(
        new ClickedListener() {

          public void clicked(Widget widget) {
            no_input = false;
            gui.removeCustomDialog(dialog);
          }
        });
  }