CSE 203 Closed Lab 18 Instructions
Table of Contents
1. Objectives
- To learn more about how Phrogram reports mouse events and how to
use a home-grown class to avoid missing mouse events in your programs.
2. Set Up
- Two students should work together at one workstation.
- In one student's account, follow the instructions given
below in section 3, Method. Remember, trading roles (driver and
non-driver) for each new session is a very good
idea.
3. Method
- In Closed Lab 15, you completed a given program and
called the
completed result "Bounce_Among_Many_Circles". Start Phrogram, and
open this "Bounce_Among_Many_Circles" file. If you cannot find
this file, please complete the steps called for in Closed Lab 15.
- If you haven't already done so, modify this program so that it
responds to the left mouse button instead of the F2 key and the right
mouse button instead of the F1 key. You can use what you learned
in Closed Lab 11 as a guide.
- Run this program and try to get it to miss
right-mouse-clicks. One way to do this is to request many
stationary circles. If fourteen circles aren't enough, then try
more. See if you can also notice that the program is missing
left-mouse-clicks, too.
- Why are our programs missing mouse clicks? Here is the
model that Phrogram is using to report mouse clicks to us:
Phrogram divides each second on the clock into sixty parts. Let's
call each (1/60)th of a second part an "interval" and each point in
time
that divides one interval from the next a "tick". Let's say that
interval A precedes interval B, which precedes interval C. The
mouse events that Phrogram detects during one interval, it reports to
us through the Mouse properties throughout the next interval.
That is to say, the mouse events Phrogram detects during interval A are
reported to us throughout interval B. Any time during interval B
that our program looks at a Mouse property, our program will report the
same answer for that property, and that answer was determined during
interval A. If no additional mouse events occur during interval
B, then, throughout interval C, the Mouse properties will show no mouse
events. Why can our program miss a mouse click? If the
click occurs during interval A and our program doesn't look at the
appropriate "pressed" property during interval B, then our program will
miss that click.
- There is now a homegrown class, MouseEventRepository, that can
help our programs solve this problem. Right-click on the link HoldsClassMouseEventRepository.zip
and choose "Save Link As..." to save the zip archive containing this
program in the same folder as your
current program. In the Phrogram files section, click the
right-mouse-button on your current program and select "Open Containing
Folder". In this folder, right-click on the zip archive to
extract the Phrogram file into this folder. Double-click on this
Phrogram file to open it in Phrogram. If the preceding doesn't
work well for you, you can find file HoldsClassMouseEventRepository.kpl
in the folder K:\CSE203\My Phrogram Files\OSU\Week06 on the K: (class)
drive.
- Copy all of class MouseEventRepository into your program in the
place after method Main()'s "End Method".
- Read the comments provided in this class to see if they make
sense to you.
- To use this new class you need to define, in your Main method, an
object, called, say, "mer" (short for "mouse event repository"), whose
type is this new type, MouseEventRepository. So, type "Define mer
As MouseEventRepository".
- This object, mer, has two methods, Poll() and Clear().
Poll() means to look at and remember the mouse events that Phrogram is
currently reporting, giving priority to remembering clicks.
Poll() remembers the most recent mouse click (either up or down).
A call of the Clear() method causes Poll() to forget the past and
allows it to remember non-click mouse events, like movement, until
either the program looks at the event or a button is clicked.
Poll() should be called no less often than each (1/60)th of a
second. Because of this restriction, you should type "mer.Poll()"
as a statement inside the "While" loop that begins with "While cn <=
Number_Of_Circles". You should also type "mer.Poll()" before and
after your call to "Delay(1)". Notice that Phrogram's
Intellisense works for class MouseEventRepository even though it is
homegrown. In class MouseEventRepository, you can find comments
inside method Poll(), which describe it.
- Now, mer has a Boolean-valued (true/false) property called
"EventRemembered". If not mer.EventRemembered, then some of mer's
other properties hold meaningless values. On the other hand, if
mer.EventRemembered, then mer's other properties all hold meaningful
values. Consequently, you need to "guard" your mouse-handling
instructions with the mer.EventRemembered condition. Very likely
your section of mouse-handling instructions looks like
If...Then
...
Else If...Then
...
End If
You should surround this sequence of instructions with a guard based on
the condition mer.EventRemembered, like so:
If mer.EventRemembered Then
If...Then
...
Else If...Then
...
End If
End If
Do that now.
- We should also tell our program to forget these mouse events (to
reset mer.EventRemembered to false) after it has handled them. We
do so by calling mer.Clear(). This call should be the last act of
the mouse handling section. Hence, now you should modify what you
did in the previous step by inserting a call to mer.Clear() to get
something that looks like:
If mer.EventRemembered Then
If...Then
...
Else If...Then
...
End If
mer.Clear()
End If
- It's a good idea to call mer.Poll() just prior to the
mouse-handling section. Insert this call now to get something
that looks like:
mer.Poll()
If mer.EventRemembered Then
If...Then
...
Else If...Then
...
End If
mer.Clear()
End If
- The last group of changes you need to make to get this program
not to miss mouse clicks is to change each occurrence of "Mouse" to
"mer". Use the Replace button (
)
to accomplish this task easily.
- Now run the program and try it out. Can you get it to miss
your mouse clicks? I hope not. . . .
- Finally, mer keeps track for you whether each of the three mouse
buttons is currently down. You can modify your program so that
the user can drag the moving circle around using the right mouse
button. All you need to do is replace 'If mer.RightPressed And
mer.Event = "ButtonDown" Then' with 'If mer.RightDown Then'.
- If you think that using class MouseEventRepository might help you
in your
project, think about how you might change your course project to
include its features. If you want to
know more, just ask!
4. Proctor Help
If you have a question or get stuck,
raise your hand and one of the proctors will come by to chat.