User Tools

Site Tools


tutorials:create-groimp-plugin

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorials:create-groimp-plugin [2024/12/06 08:20] – [New file filter] gaetantutorials:create-groimp-plugin [2024/12/06 08:54] (current) gaetan
Line 1: Line 1:
 This tutorial aims at showing how to get started on GroIMP plugin development. It is assumed that you are able to compile the plugin using Maven (either the plugin alone, or the whole project). This tutorial aims at showing how to get started on GroIMP plugin development. It is assumed that you are able to compile the plugin using Maven (either the plugin alone, or the whole project).
-In this tutorial we are going to create a new plugin (from the empty template), add a new MimeType (format of file accepted by GroIMP) for both import and export, and add a menu item that run a specific command.+In this tutorial we are going to create a new plugin (from the empty template), add a new MimeType (format of file accepted by GroIMP) for import, and add a menu item that run a specific command.
  
 See: See:
Line 253: Line 253:
  requires graph;  requires graph;
  requires imp3d;  requires imp3d;
-        requires imp3d;+        requires imp; 
 +        requires utilities;
 </code> </code>
  
Line 327: Line 328:
 <code xml> <code xml>
     <directory name="tutorial">     <directory name="tutorial">
-    <directory name="commands">+    <directory name="tutorial">
  <command name="createRandomCylinders" run="de.grogra.template.tutorial.Commands.randomCylinders"/>  <command name="createRandomCylinders" run="de.grogra.template.tutorial.Commands.randomCylinders"/>
     </directory>     </directory>
Line 336: Line 337:
  <ref name="project">  <ref name="project">
  <insert target="/workbench/menu/src" resolveLinks="false">  <insert target="/workbench/menu/src" resolveLinks="false">
- <link source="/tutorial" />+ <link source="/tutorial/tutorial" />
  </insert>  </insert>
  </ref>  </ref>
Line 343: Line 344:
 </code> </code>
  
-The first '' <directory name="tutorial"> '' creates a directory in the registry, which can be used latter by GroIMP or users. In this directory we create a ''<command />'' tag that point at the command implementation in java.+The tag '' <directory name="tutorial"> '' creates a directory in the registry, which can be used latter by GroIMP or users. In this directory we create a ''<command />'' tag that point at the command implementation in java.
  
 Then, the hooks > complete > project defines what happens when a GroIMP project is open. In this case at ''/workbench/menu/src'' (i.e. the menu bar) we insert the directory previously created. Then, the hooks > complete > project defines what happens when a GroIMP project is open. In this case at ''/workbench/menu/src'' (i.e. the menu bar) we insert the directory previously created.
Line 353: Line 354:
  
 {{:tutorials:tuto_menu.png?400|}} {{:tutorials:tuto_menu.png?400|}}
 +
 +=== Add an option ===
 +
 +The previous command is creating a chain of five cylinders. Instead of using a fixed number we can add a GroIMP option to let the user define the number.
 +
 +Options are defined in the registry. In the ''plugin.xml'' file. We need to modify slightly the directory "tutorial created in the previous section:
 +<code xml>
 +   <directory name="tutorial"  optionCategory="true">
 +    <directory name="tutorial">
 + <command name="createRandomCylinders" run="de.grogra.template.tutorial.Commands.randomCylinders"/>
 +    </directory> 
 +      <command name="options">
 +            <option name="numberOfCylinders" type="java.lang.Integer" value="5"/>
 +      </command>
 +   </directory>
 +</code>
 +
 +The directory is now using the parameter //optionCategory// , which will make it visible in the panel //preferences// of GroIMP. And the tag ''<option />'' is defining a new option with a type and a default value.
 +
 +{{:tutorials:tuto_addoption.png?400|}}
 +
 +Now we change the java code that was creating the Cylinders to use the value of this option. In the file ''Commands.java'', change the method :
 +
 +<code java>
 +
 +import de.grogra.util.Utils;
 +
 +public class Commands {
 +
 +public static void randomCylinders(Item item, Object info, Context ctx) {
 +  Node n = new Node();
 +  Node last = n;
 +
 +  Item ite = Item.resolveItem(item.getRegistry(), "/tutorial/options");
 +  int nbOfCylinders = (int) Utils.get(ite, "numberOfCylinders", 5);
 +
 +  for(int i=0;i<nbOfCylinders;i++) {
 + float length =  (float) (0.5f + Math.random() * (2 - 0.5));
 + float radius = (float) (0.1f + Math.random() * (0.4 - 0.1));
 + Cylinder c = new Cylinder(length, radius);
 + last.addEdgeBitsTo(c, Graph.BRANCH_EDGE, null);
 + last = c;
 +  }
 +  IMP.addNode(null, n, ctx);
 +}
 +}
 +</code>
tutorials/create-groimp-plugin.1733469656.txt.gz · Last modified: 2024/12/06 08:20 by gaetan