Drawing Inches for our ruler
It's time that we start drawing our actual ruler. The first step is we need to figure out what an inch is at all. As we talked about in Course 2 when we discovered the capabilities class we discovered that we can load up how many pixels are in an inch using the Capabilities.screenDPI property.Now that we know how many pixels are an inch its time for us to draw lines in the intersection point of inches. By the end of this video you will leverage Flashes Drawing API(you can learn more about that here) to draw out the inches of our ruler.
Video Transcript
00:00So, welcome back and it's time for us to create the actual ruler where we would actually count
00:06those inches.
00:07So, to do that, what I want to do next is I want to basically add a few variables that
00:11are going to help us along.
00:12So, I'm just going to go right away, right under our imports, and I'm going to add a
00:15new variable called inch.
00:17As we talked about in previous videos, we could actually get into the actual inch,
00:23how much is an inch using the screen DPI property of the capabilities class.
00:28So, I'm going to import the capabilities class by doing that new capabilities, like
00:33that trick that we showed earlier, and I'm going to load up that screen DPI and put
00:38it into my variable of inch.
00:40Now, I know how much is an inch.
00:43The next thing that I want to define is how big of a line will I draw once I
00:47create that inch.
00:48So, I'm going to set a new variable and I'm going to call that variable full step
00:52size, because that's going to be the full inch.
00:55Whenever we have an inch, what will be that size?
00:57So, that's going to be called full step size, and we're going to define that
01:01size as a number, because it might be a fraction of, it's not going to be a full
01:05integer, because I'm going to go to that inch and let's say I'm going to divide
01:08it by four.
01:09So, I'm going to get whatever number comes out, basically how many pixels are
01:12in that division by four, and that's going to be the size of our step that
01:16we create, all right?
01:17The next step that we're going to do is I'm going to create a new ruler
01:20variable, and this ruler variable is actually going to be a sprite.
01:24Now, the reason why I'm creating a sprite is because a sprite is a display object,
01:28a primitive display object, that enables us to draw it dynamically onto the
01:32stage.
01:33So, to do that, all I'm going to do is I'm going to create a new sprite.
01:37Once I create a new sprite, by default, it's going to position itself in
01:40position 00, and as our application is going to align to the top left, that
01:45works perfectly for us.
01:47So, all I really need to do is go to my stage and add that onto my stage.
01:51So, I'm going to add a child, and add that ruler onto the stage.
01:55So, basically once I created the ruler, all that's left for me to do is to
01:58actually tell the stage that I want to add it onto the stage.
02:02And I could even add it maybe after I do the alignment.
02:04It doesn't really matter at all, but just from a visual aesthetics
02:07perspective, it makes sense that we first are aligning.
02:10It won't really make a difference.
02:11I'm going to put that there in line 13.
02:14All right, so we created the variables that we wanted to create, and
02:17all that's left for us to do is actually start drawing out our ruler.
02:22So, to do that, I'm going to scroll down, and I'm going to create a new
02:24function, and this function is going to draw the ruler.
02:27And as our application is going to rotate each time, what we want to
02:32do is we want to make sure that our ruler will redraw basically every
02:36time, realigning and repositioning the variables based on the drawing
02:40based on that.
02:41So, to do that, I'm going to create a function called refresh ruler.
02:46The refresh ruler doesn't accept any parameters, and the first
02:49step that I'm going to do is I'm going to go right into my ruler and
02:53into my graphics library, and the graphics library enables me to draw
02:57things directly onto the stage.
02:59Now the first step that I want to do is I want to clear it out because
03:01it's going to be called more than once, basically cleaning out anything
03:04that's inside of the ruler.
03:07Once I cleared everything out, it's time for me to start drawing, but
03:09just before I start drawing, I really, oops, ruler, graphics.
03:16Before I start drawing, I want to set the actual, in our case, we're
03:21going to create lines.
03:22So I want to define the line style itself.
03:25Doing it is relatively easy as well.
03:27So I'm going to define my line style, and I'm going to define how
03:30thick I want it to be.
03:31I'm not going to worry about all the different properties.
03:34I'm just going to set it to be three pixels in width.
03:37And I'm going to make sure that its color is going to be black.
03:40To do that, I'm going to set my hex values to be in the red
03:43values at zero, in the RGB, red, green, blue, all at zero, making
03:47sure that I'm going to set it up to be an opaquely black image.
03:52And I'm fine with all the other default properties of our line
03:56style.
03:57By the way, if you want to learn more in-depth about the
03:59graphics API, we have a whole course about the graphics API and
04:03how to draw in it, and you can find it at zero2geek.com.
04:05All right, so with that said, let's continue with the ruler,
04:09and we're not going to learn the in-depth of everything about
04:11the library, but we're going to see how to get it to work.
04:13So once we created the line style, it's time for us to
04:16basically figure out how many inches are in our horizontal line
04:21and how much are there in the vertical, so we can actually
04:24draw them out.
04:25So I'm going to start with the horizontal, or actually
04:28vertical.
04:30So I'm going to start with the x-axis, which is the vertical
04:32axis, and I'm just going to create a new variable to fetch
04:35that information.
04:36I'm going to call it totalInches.
04:39Again, it's going to be an integer, and I'm going to ask
04:42from the capabilities library, and I hate to type each time,
04:47so let me find that.
04:48I'm going to copy and paste instead of having typos.
04:51Capabilities library, we're going to fetch that screen
04:53resolution x, and we're going to divide that by, again,
04:57the capabilities step, and we're going to get the DPI again,
05:01or we could just directly access our inch property
05:04because we saved it as a local variable, so I could just
05:07call the inch property directly.
05:09Once I've done that, and now that I'm actually configured,
05:12I should get the total inches.
05:14Last but not least, I just want to make sure that if it's
05:16a little bit more than an inch, that we get rid of it,
05:18so I'm going to wrap it around an int, basically getting
05:21rid of anything that's beyond the dot, and making sure
05:24that our variable will accept actually an integer
05:26because we don't care about anything beyond that dot.
05:30Once we've done that, we're ready to start drawing,
05:32and let's start drawing the x-axis, which is really
05:34simple.
05:35All we need to do now is start creating a, excuse me,
05:38we want to create a for loop, and we're
05:40going to start with defining a new integer setting
05:44in the value of zero for our i, and we're
05:46going to say that we want to continue that loop as long
05:49as it didn't reach that total inches.
05:52Not only that, we want to step up our i each time by one.
05:56And there we go.
05:57That's all we had to do to really configure our application
05:59and start drawing out the inches.
06:02The next step is to actually draw it out,
06:04so now that we know exactly every step when
06:06we want to draw a new inch, all that's left
06:08is for me to go again to my ruler, dot graphics,
06:12and this time around we're going to call the move to first
06:14because we want to position our element in the right x
06:17position.
06:18How are we going to define that?
06:19Well, we could take our i, which is our current inch
06:22that we're in, and duplicate it
06:24by the amount of inches, which is basically
06:25the amount of pixels per inch.
06:28And our y position, we want to put it at zero
06:30because we want to stay on that horizontal line for us,
06:33and we want to stay on the absolute top at first.
06:36And as the x and y coordinates in flash
06:39are basically the y will grow downwards,
06:41we want to start from zero and then grow it downwards.
06:44So we first want to move to the right point.
06:46We don't want to draw into it.
06:47And once we're in the right point,
06:49once we moved into the right point,
06:51it's time for us to actually draw.
06:53So the way we draw, we use the command line 2.
06:56Now the line 2 works in the exact same way.
06:58So first of all, we don't want our x position
07:00to change at all.
07:01All we want is to grow our element by a bit,
07:04and by how much we want it to grow
07:05by that variable that we created earlier,
07:07which is called full step, full step size.
07:10And all I have to do is just call that full step size.
07:14And that's really all I have to do to get my application.
07:16I'm going to save this for a second.
07:18All I have to do to get my application to run,
07:20and if I run this one more time,
07:21we're going to see that, first of all,
07:22our horizontal line should have lines.
07:25Now, only problem is it's not doing it
07:27because we're never calling it.
07:28So let's make sure that we call it.
07:30So I'm going to call the refresh ruler,
07:32and we're going to call it right at first.
07:34We're going to call it right underneath here
07:36as soon as our application is ready for it.
07:39So I'm going to just refresh the ruler in line 16.
07:42I'm going to save that and run it just one more time.
07:44And this time around, we could see that our inches
07:46are starting one, two, three, four, five, six, seven.
07:49And we know that we have your basically from zero
07:51all the way to six,
07:52and we don't have any more beyond them.
07:55All right, so that's our inches.
07:57The next step is we want to also make sure
07:58that we get our inches also on the vertical line.
08:01Vertical line is just as easy as the first one.
08:03So all I'm going to do is I'm going to grab
08:05everything we created,
08:06because literally we're just going to change around
08:08a couple of stuff inside here.
08:09So I'm going to copy that
08:10and just paste it right underneath.
08:13And first of all, we're not going to redefine
08:14the variable because it's already created.
08:16So we could undo that, the name definition,
08:18and just work with the variable.
08:20And I'm just going to change the,
08:22from screen resolution X to screen resolution Y.
08:25Next again, I'm not going to create a new variable.
08:27I'm just going to define it back to be zero.
08:29And we still want to do the same exact logic here.
08:32The next step here is also,
08:33we want to change the logic here.
08:35And literally all we want to do is we want to flip it around
08:38where we start off from zero.
08:39And we're always stepping ahead in that amount of X.
08:42So I'm basically just flipping it around.
08:43We're turning it around.
08:45Our full step size is going to be onto the X coordinate
08:48and not onto the Y.
08:50So basically we're stepping from basically
08:52from the left to the right.
08:54But we're maintaining the actual position of our Y
08:56once we establish what the position of the Y is.
08:59So it's the exact opposite
09:01of what we've done in the first part.
09:03If I save this and I run this one more time,
09:05we're going to see that now we're going to get our inches
09:07and our ruler throughout our application, which is perfect.
09:09This looks exactly the way I wanted it to look like.
