<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Basic Tutorial 14: Encapsulation">

<setdocpatch name="14mEncapsulation" patch="14mEncapsulation.maxpat"/>
  <previous name="basicchapter13">Movie Sequencing</previous>
<next name="basicchapter15">Abstractions</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Basics" title="Encapsulation">Patchers inside of patchers</indexinfo>

<h1>Tutorial 14: Encapsulation</h1>

<h2>Introduction</h2>

<p>This tutorial will cover the concept of <i>encapsulation</i> within Max. Encapsulation allows us to place sections of our patcher within their own <i>subpatch</i> using the <o>patcher</o> object.  This lets us hide parts of patcher logic that we no longer need to see, in order to make our projects easier to read and more concise.  We will examine the creation of a <o>patcher</o> that contains objects, as well as using some editing tools to encapsulate parts of a patcher that no longer need to be seen.</p>

<p>Properly encapsulating your Max logic is important for keeping your patch files clean, easy to read and maintainable. Encapsulated logic can be easily edited, and can also be saved as separate files that can be reused in other patches.</p>

<p>To open the tutorial patch, click on the <b>Open Tutorial</b> button in the upper right-hand corner of the documentation window.</p>

<h2>Basic encapsulation</h2>

<p>At the top-left of the tutorial, there are two small patches that do the same thing – they both add <m>5</m> to the incoming <m>number</m>.  The patch on the left does it with a <o>+</o> object, while the patch on the right uses a <o>patcher</o> object.  The <o>patcher</o> object (which can be abbreviated "p", much as <o>trigger</o> can be abbreviated "t") is literally another patch embedded in the current patch.  To see the contents of a <o>patcher</o> object, you can double-click on it (when the patch is locked) to open the encapsulated <o>patcher</o> object's window.</p>

<p>If we double-click on the <o>patcher</o> <m>add5</m> object, a small patcher window shows the contents (in a window labeled <m>add5</m> - the name of the subpatch). It is a simple subpatch, with only three objects.  The object in the middle is obvious – it is the <o>+</o> object that performs the addition. The objects above and below the <o>+</o> object are <o>inlet</o> and <o>outlet</o> objects, respectively. An <o>inlet</o> object causes the enclosing <o>patcher</o> object to have an inlet, while an <o>outlet</o> object causes the enclosing patcher to have an outlet.  In this way, we can make patcher objects that act very much like built-in Max objects.  Multiple <o>inlet</o> and <o>outlet</o> objects will create corresponding inlets and outlets on the enclosing <o>patcher</o> object, arrayed spatially in relation to how they are in the subpatch (e.g. the leftmost <o>inlet</o> object in the subpatch will correspond to the leftmost inlet on the <o>patcher</o>).</p>

<p>When working with <o>inlet</o> and <o>outlet</o> objects, it is useful to add <i>assistance</i> to them; in this way, the enclosing <o>patcher</o> can display helpful information about the type of message that is expected by the inlet, or produced by the outlet. If you select an <o>inlet</o> or <o>outlet</o> and then open the object’s Inspector, you will see that there is a <m>comment</m> field available. Any text that you enter in this field will show up as assistance in the <o>patcher</o> object during patch editing.</p>

<h2>Performing encapsulation</h2>

<p>Creating sub-patchers sometimes needs to happen after the logic is in place; perhaps your patcher has expanded beyond where you originally thought it would go, or you’ve added logic that wasn’t planned, and things are starting to get messy.  In these cases, it would be useful to be able to select a group of objects and quickly turn them into a single encapsulated patcher.  This can be done using the <b>Encapsulate</b> menu item.</p>

<p>Our example patch has an interesting drawing algorithm, where you move a puck around a rectangular area (using an object called the <o>pictslider</o>), and small circles are drawn in the area randomly near the equivalent position in the <o>lcd</o> at the bottom of the patch. If you need to clear the <o>lcd</o>, you can hit the space bar (ASCII character <m>32</m>, captured by the <o>key</o> object and triggered by the <o>select</o> object).  The logic is rather messy, and it doesn’t really help us to see it spread around the screen.  We are going to select a majority of the logic and encapsulate it into a subpatch.</p>

<p>There are two <o>comment</o> boxes (“encapsulate from here” and “to here”) that we will use to select our encapsulated logic.  <i>Unlock</i> the patcher, then select (using click-and-drag) all of the objects between and to the right of the comment boxes.  15 objects will be selected.  Select <b>Encapsulate</b> from the <b>Edit</b> menu, and you will see that all of this logic is folded into a single unnamed <o>patcher</o> patcher.  If you lock the patch and double-click the <o>patcher</o> object (or command-double-click in an unlocked patcher), it will open the subpatch and show all of the objects again, tied to the outside world using four <o>inlet</o> objects and one <o>outlet</o> object.  If, for some reason, we needed to reverse this process later, the <b>De-encapsulate</b> command under the <b>Edit</b> menu will unravel the subpatch back into our main patcher, re-connecting everything correctly as it was originally.</p>

<p>It is often useful to name our <o>patcher</o> objects; while this adds no particular value to the <o>patcher</o>, it will help you (and others) understand the logic that is encapsulated into the object.  To name it, just click inside the object, and add a first argument that is the name you’d like to use.  Something like <m>draw_logic</m> would be perfectly useful for this example.</p>

<h2>Creating our own subpatcher</h2>

<p>When you know ahead of time that you want to work within a subpatch, it is easy to create a <o>patcher</o> object and work within its editing window.  At the lower-left is a pair of <o>number</o> boxes connected by – nothing. We will make a subpatch here and use it to convert one value into another.</p>

<p>Create a new, blank object in between the <o>number</o> boxes by parking the mouse there and typing "n". In the new object box, type <m>p mycalc</m> - this will create a new <o>patcher</o> named <m>mycalc</m>.  A new window will appear for editing our new subpatch. Start with an <o>inlet</o> and <o>outlet</o> (both available under the <m>Add Object</m> icon in the top toolbar). Note that adding these to the patcher added and inlet and an outlet to the <o>patcher</o> object in our main window.  Next, connect the <o>inlet</o> directly to the <o>outlet</o> – thereby making a “thru” object that does nothing except pass incoming message to the outlet.  Let’s test this encapsulation in the main patcher window.</p>

<p>Now that our <m>mycalc</m> <o>patcher</o> object has an <o>inlet</o> and <o>outlet</o>, we can connect it to the <o>number</o> boxes.  Connect the top <o>number</o> box to the patcher <o>inlet</o>; connect the <o>patcher</o> outlet to the bottom <o>number</o> box.  Lock the patch, and change the top <o>number</o> box: we should see the bottom <o>number</o> box change to match.  This shouldn’t be a surprise, since our <o>patcher</o> object encapsulates a straight line between the two boxes!</p>

<p>Let’s go back to the <o>patcher</o> editing window and add some logic - if the window has closed, double-click the <o>patcher</o> object to open it up again and unlock it.  Start by selecting and deleting the patch cord connecting the <o>inlet</o> and <o>outlet</o>. Now, let’s add some math: create two new objects, a <o>+</o> <m>50</m> object and a <o>/</o> <m>7</m> object.  Now, connect the <o>inlet</o> to the <o>+</o> object, the <o>+</o> to the <o>/</o> object, and the output of the <o>/</o> object to the <o>outlet</o>.  We’ve changed our <m>mycalc</m> encapsulation from meaningless subpatch into an “(input + 50) / 7” <o>patcher</o>.  The nice thing about doing this within an encapsulation is that we don’t have to change anything in the top-level patcher – all of the connections remain as-is.  Now, at our main patch, changing the top number box will produce the new result without any further changes.</p>

<h2>Summary</h2>

<p>Encapsulation of patching logic accomplishes two things:
	<ol>
<li>it allows us to work at higher levels without having to interact with (or even see) all of our objects; and,</li>
<li>it allows us to change our lower-level logic without having to change anything at the higher-level programming.</li>
</ol>
In working with <o>patcher</o> objects, you can either manually create and populate them, or you can select existing patcher logic and use the <b>Encapsulate</b> menu option to automatically create a subpatch. In either case, you can further explore and change the <o>patcher</o> code without any further changes to the higher level patch.
</p>

<seealsolist>
<seealso name="patcher">Create a sub-patch</seealso>
<seealso name="inlet">Access to a sub-patcher</seealso>
<seealso name="outlet">Communicate from inside a sub-patch</seealso>
<seealso name="pictslider">One- and two-dimensional slider with customizable appearance</seealso>
</seealsolist>

</chapter>
