<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Basic Tutorial 9: Mouse Drawing">

<setdocpatch name="09mMouseDrawing" patch="09mMouseDrawing.maxpat"/>
<previous name="basicchapter08">Keyboard and Mouse Input</previous>
<next name="basicchapter10">Random Drawing</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Basics" title="Mouse Drawing">Introduction to drawing</indexinfo>

<h1>Tutorial 9: Mouse Drawing</h1>

<h2>Introduction</h2>

<p>In the previous tutorials, we've looked at a set of basic objects that we use all the time for constructing patchers in Max.  In this tutorial, we'll use these objects (and a few more) to actually control something (in this case, drawing).  We will look at a few ways to manipulate messages by routing them (using the <o>gate</o> object) and manipulating lists (using the <o>pack</o> and <o>unpack</o> objects).  We will also query the state of the mouse to change the way that our patch works, and we will learn more about getting information on individual objects and changing the appearance of objects and patchcords.  We will use these new objects and techniques to control the <o>lcd</o> object, which allows us to draw simple two-dimensional graphics inside of Max.</p>

<p>One of the most powerful aspects of Max is the ability to create small segments of programming that perform specific functions, and having the ability to route messages to the appropriate portion of a larger patcher. Intra-patch routing is key to that power, and the <o>gate</o> object provides that functionality in a very flexible way.  It is also useful to build complex messages on-the-fly, so we can control complex patch processes with relative ease.</p>

<p>The <o>pack</o> and <o>unpack</o> objects are used to create list messages and to split them into their individual components again. Lists are the key to working with objects that require more complex messages, and it's often necessary to construct and deconstruct lists from smaller components (integer and floating-point numbers, for example).</p>

<p>Finally, we will dig into getting more information on object messages.  We'll also learn how to  color and manipulate patchcords.  Visual cues help us figure out other people’s patch functionality, and make patches more readable when we share our 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>Getting more info</h2>

<p>Before we begin looking at what our tutorial patcher does, let’s take a short diversion to look at some of the ways we can get information about the objects that we have in our patch. Unlock the patcher and move your mouse around the objects in the patch.  You’ve probably noticed that when we place our mouse on an inlet or outlet, a small box is displayed with information about the use of that inlet or outlet. This <i>assistance</i> text is provided as a reminder for the programmer of what that inlet or outlet does in an object. You will also notice that a small circle is displayed around the inlet/outlet.</p>

<p>If we hover the mouse over the left edge of an object, we see a small circle with a triangle. Clicking on this will open the <i>Object Action Menu</i>, which shows (via submenus) the state of the object's attributes, all of the supported incoming messages, as well as shortcuts to view the object’s help file, reference pages and even to search our system for relevant matches in the file browser.  This provides a greater level of detail if we need to know details about the exact use of the object. This information is also available in the Messages and Attributes segments of the circular menu.</p>

<p>There are other options for on-the-fly information. Select <b>Clue Window</b> from the <b>Window</b> menu of Max; a small window will be displayed.  Now, when you hover the mouse over an object, we will see a brief explanation of the function of the object. Using the clue window to view the use of objects is a great way to examine the objects used within a more complex patch.</p>

When you select a new object and begin typing, you will be presented with an auto-completion menu that not only provides names for objects, but can give you some information on their use. Something similar happens when you create a new message box: typing information into it will reveal messages that might be useful for nearby objects in the current patcher.

<h2>More about objects and patchcords</h2>

<p>If you look at the objects in this tutorial patcher, we notice that the <i>patchcords</i> look a little different than the ones we've seen earlier.  First of all, a number of the patchcords have intricate paths made of several <i>segments</i>; in addition, some of them are different colors.  Let’s look at some of the ways we can manipulate the patchcords themselves. Under the <b>Options</b> menu in Max, check the option <b>Segmented patchcords</b>.  This allows us to create patchcords that have bends in them.  We can delete a patchcord by selecting it and hitting the Delete key, or by choosing Delete from the patchcord menu.  Delete one of the patchcords connecting the <o>message</o> boxes to the <o>gate</o> object at the top-left of the patch.</p>

<p>Now, reconnect the <o>message</o> box to the <o>gate</o>, but instead of making a direct connection between the two, make a <i>segmented</i> patchcord by clicking the outlet of the <o>message</o> box and then releasing the mouse; we can add segments by clicking the mouse anywhere while we draw the patchcord; we finish the connection by clicking the inlet of the destination object (the <o>gate</o>).  Whether you use straight or segmented patchcords is largely a matter of personal style, but often you can sometimes make a patch look cleaner by using segmented patchcords.</p>

<p>Max also can help us clean up our patchcords.  Click on the patchcord and hit Shift-Command-Y on the Mac, Ctrl-Shift-A on the PC (alternately, you can select the <b>Route Patch Cords</b> function under the <b>Arrange</b> menu in Max).  Max will segment and reroute the patchcord to avoid overlapping objects. A simple Command-Y gets a cord with a single bend along the shortest path. We can also select several patchcords by holding the Shift key while clicking on patchcords (or the Option key while dragging across cords)- the command sequence will then reroute all of the selected patchcords.  There is no difference whatsoever between a regular and segmented patchcord in terms of how messages are passed between objects.</p>

<h2>Styles</h2>
<p>Finally, let’s look at Max’s ability to change the <i>color</i> of objects and patchcords. We can select an object or patchcord (by clicking on it) then select <b>Format</b> from the upper toolbar.  This will display new toolbar with icons for individual elements of objects&#x2014;background, text, borders, whatever it's got. Selecting the appropriate icon will let you color (for instance) the blink of a button. We've been coloring objects as we introduce them in the tutorials; as you can see, it's a great way to focus attention on important items within our patch. (Note that there is a consistent gold color for the objects in a patch you will be instructed to manipulate.)</p>
<p>
	In addition to carefully coloring each object, you can save the color scheme as a <i>style</i>. The text at the left of the format toolbar is a dropdown menu. If a colored object is selected, you can define its color scheme as a style with a distinct name. These names will then be stored in the patch so you can easily apply them to other object of the same type. You can even save your styles in a library so they will be available in all of your patches. Finally, there are several style sets supplied if you feel the default look is a bit austere.
</p>


<h2>Working with the <o>gate</o> object</h2>

<p>Now lets look at what the patcher does.  In our tutorial patcher, we find two small patches (on the left) and one very large patch.  The patch at the top left is where we will start.</p>

<p>This patch shows the <o>gate</o> object in action.  The <o>gate</o> object is one of the most important objects used for dynamic message routing within Max.  We will start by clicking on the message labeled <m>1</m> (the middle <m>message</m> box), then changing the value of the <o>number</o> box attached the right inlet of the <o>gate</o>. When we do this, we see that the value of the <o>number</o> box is sent out the <i>left</i> outlet of the <o>gate</o> (outlet <m>1</m>). If we click on the <m>message</m>  labeled <m>2</m> and alter the <o>number</o> box again, we see that the changes are now sent out the <i>right</i> outlet of the <o>gate</o> (or outlet <m>2</m>).</p>

<p>As we can see, the number sent into the left inlet determines the routing of the message received in the right inlet; in essence, the left inlet determines which outlet is <i>opened</i>. Now, click on the <o>message</o> labeled <m>0</m> and change the <o>number</o> on the right.  The message never makes it out of the <o>gate</o> – the <m>0</m> message has <i>closed</i> all of the outlets.  This is useful for situations where we want to suppress messages within our patch, for example when we need to selectively enable and disable numbers flowing from one part of our patch to another.</p>

<p>As an exercise, change the <o>gate</o> object to have a <m>4</m> for an argument (it currently has a <m>2</m>).  We see that the object changes – it now has four outlets.  The argument to <o>gate</o> determines the number of outlets that the object has.  If we add a few more messages to the left inlet (by adding a <m>3</m> and <m>4</m> option to support the new gate outlets) and attaching <o>number</o> boxes to the new outlets, we can see how easy it is to expand the routing matrix supported by the <o>gate</o> object.</p>

<h2><o>pack</o>, <o>unpack</o>, <o>prepend</o></h2>

<p>The next part of patch that we will look at is just below the <o>gate</o> – it uses the <o>unpack</o> and <o>pack</o> objects to split and reassemble <i>lists</i> of numbers, and the <o>prepend</o> object to place a <i>message selector</i> (a symbol) in front of the list. The two <o>message</o> boxes contain two-number lists; when we click on them, we see the two <o>number</o> boxes connected to the <o>unpack</o> object display the individual numbers within the list.  The <o>unpack</o> object has taken the incoming list and split it into individual numeric messages.  Since lists are often used to create complex messages, we often use the <o>unpack</o> object to unwrap these lists into its individual components (called <i>atoms</i>).</p>

<p>After the <o>number</o> boxes display the messages, they send out their contents into the <o>pack</o> object below, which reverses the process by combining them back into lists that are sent to the <o>prepend</o> object and displayed in the Max Console (via the <o>print</o> object.  Notice that only one message is displayed in the Max Console, even though the <o>pack</o> object received two messages (one from each <o>number</o> box).  This is because the <o>pack</o> object (like most other objects in Max) will only produce a message when the leftmost inlet receives a message.  The patch editing environment provides a clue for this: if we unlock the patch and hover the cursor over the right inlet, we see that it displays a blue circle – meaning that it is a <i>cold</i> (non-message producing) inlet.  Hover over the left inlet and we see it circled in red; it is a <i>hot</i> inlet, and will cause the object to generate output when it receives a message.  We can confirm this by typing other numbers into the <o>number</o> boxes that are sandwiched in between the <o>unpack</o> and <o>pack</o> - only the left-hand <o>number</o> box will cause a list to come out of the <o>pack</o> object.</p>

<p>The arguments to <o>pack</o> and <o>unpack</o> determine both the number of inlets and outlets, respectively, the object will have, but also the <i>type</i> of message that can be sent into those inlets.  In our case here, both of these objects are working with two-element lists made up of <i>integer</i> numbers (signified by the use of <m>0</m> as an initial argument).  If the list we were making required floating-point numbers, we could have used <m>0.</m> (or any other initial floating-point value) as an argument; similarly, if a symbol (a word) was needed in the list, we could have used the letter <m>s</m> (or any symbol); types of data can be mixed and matched within a list.</p>

<p>Notice that the <o>prepend</o> object places the word <m>thelist</m> in front of the list of numbers coming out of the <o>unpack</o> - this <i>symbol</i> can be any word we like, but in many cases in Max we use <o>prepend</o> to place a <i>message selector</i> at the start of a list before sending it into an object that has a special use for that message.</p>

<h2>The <o>lcd</o> object</h2>

<p>Now it’s time to attack the big patch to the right. Let’s see what it does by turning on the <o>metro</o> object at the top of the patch by clicking the <o>toggle</o> object to <m>1</m> (bright X).</p>

<p>The large rectangular object at the right of the patch is an <o>lcd</o> (<i>liquid crystal display</i>) object: we can use this object to draw pixels, lines, shapes, text, and other simple two-dimensional vector graphics. If you drag the mouse in this area while holding down the mouse button, we see that it draws lines wherever you go.  Release the mouse button, and the drawing will stop.</p>

<p>If you hold down the shift key, option/alt key or a combination of both, the <o>lcd</o> object draws different shapes under the mouse rather than lines. There are three colored <o>message</o> boxes above the <o>lcd</o> that can be used to change the foreground color of the drawing (using the <m>frgb</m> message), and another <m>message</m> box (to the right) that will send the <m>clear</m> message to the <o>lcd</o>, erasing its screen.</p>

<p>One of the key sections of this patch is the handling of the shift and option/alt keys; this is done by the <o>modifiers</o> object, which is polled using the <o>metro</o>, and reports the current setting of the various modifier keys on your keyboard. In this case, we use the outlets that report the shift and option/alt keys; we <i>multiply</i> the option key's state by <m>2</m> and <i>add</i> it to the state of the shift key.  The result is that shift alone will give us the value <m>1</m>, option/alt alone will give the value <m>2</m>, and the combination will give us <m>3</m>.  If no modifier keys are depressed, the value will be <m>0</m>.  This type of patch circuit is perfect for controlling the state of the <o>gate</o> object below.</p>

<p>In addition to providing a drawing surface, the <o>lcd</o> object can give us the current location of the mouse in a manner analogous to the <o>mousestate</o> object, only relative to the position of the <o>lcd</o> in the patch.  Sending the message <m>idle 1</m> to the object allows us to get mouse coordinates regardless of whether or not the mouse is actually clicked inside the <o>lcd</o> (we enabled idle mousing when we turned on the <o>toggle</o> box, above).</p>

<p>These coordinates are provided by the second outlet of the <o>lcd</o> object – we can see the values (a list of two numbers that represent the X and Y coordinates within the LCD object) in the number boxes at the bottom, after they've been split apart by the <o>unpack</o> object.  This list is also sent to the right inlet of the <o>gate</o> object; we’ve colored that patchcord red to make it easy to see.</p>

<p>The objects named <m>p</m> are <o>patcher</o> objects. These contain subpatches that generate the shapes shown in the <o>lcd</o>. By routing the mouse coordinates to different <o>patcher</o>s, the <o>gate</o> object now controls what type of drawing will occur.  If no modifier keys are depressed, the gate receives a <m>0</m> and none of the drawing routines are used, though we can still click-drag inside the <o>lcd</o> to draw lines.  If shift, option/alt, or both are depressed, the <o>gate</o> routes the mouse location to the appropriate seubpatch.</p>
<bullet>Double-click on a <o>patcher</o> (p) object to see what's inside. Note that the patchcords leading in and out of the subpatch are terminated by objets with arrows on them. These are <o>inlet</o> (above, bringing messages in) and <o>outlet</o> (below, sending messaged out) objects</bullet>

<p>In each of these subpatches, the X and Y locations are run through <o>unpack</o> objects to get the coordinates individually.  They are then used to create a properly sized shape.  In the <m>rectangles</m> (shift) case, a value of <m>10</m> is subtracted and added to both coordinates and sent into a <o>pack</o> object in the order:</p>

<codeexample>X-10 Y-10 X+10 Y+10</codeexample>

<p>This list of four values is then sent to a <o>prepend</o>, which places a symbol at the front of the list.  The <o>lcd</o> object supports a number of drawing commands which do different things; in this case, we're instructing our <o>lcd</o> to draw rectangular frames by providing it with a <m>framerect</m> message followed by four arguments - the left, top, right, and bottom boundaries of the rectangle we'd like to draw.</p>

<p>Looking at the other two pieces of drawing code, we can see that they work in similar ways.  The middle outlet of the <o>gate</o> object (responding to the option/alt key) draws oval-shaped frames (the <m>frameoval</m> message) that are 10x10 pixels in size (notice the <o>+</o> and <link name="-" type="refpage">-</link> objects).  The third drawing alternative (for when both shift and option/alt are held down) draws filled in rectangles with rounded corners (<m>paintroundrect</m>).  In addition to the coordinates of the rectangle, two additional <o>number</o> boxes are sent into the <o>pack</o>object - these control how rounded the corners are.</p>

<p>When the coordinates are created and determined (using the math objects and <o>pack</o>) and given a message selector to determine the drawing routine (using the <o>prepend</o> object), the message is sent back into the <o>lcd</o> object.  In order to easily see this message routing, we’ve colored the drawing command patchcords in blue.  The frgb <o>message</o> boxes are also colored to match the color it will tell the <o>lcd</o> to draw with.  These are examples of using object and patch cord coloring to make the function of a patch more obvious.</p>

<h2>Summary</h2>

<p>There have been several different (but important) topics covered in this tutorial. We see how the <o>gate</o> object can be used to activate message passing to sections of your patch. We’ve also seen how to use the <o>unpack</o>, <o>pack</o>, and <o>prepend</o> objects to build and parse complex messages. We’ve also seen how to color and manipulate patchcords and objects to help make patches and message routing more easily visible.  Finally, we've seen how to use the <o>lcd</o> object for simple drawing.</p>

<seealsolist>
<seealso name="gate">Traffic control</seealso>
<seealso name="pack">Make numbers or symbols into a list</seealso>
<seealso name="unpack">Send list elements out separate outlets</seealso>
<seealso name="prepend">Insert a list or message before another list or message</seealso>
<seealso name="lcd">Draw shapes, lines, and text into a box in a patcher window</seealso>
</seealsolist>

</chapter>
