ClickBarSketch is a poor name but it allows scrolling a sketch using clickbars instead of scrollbars. The advantage is that ClickBars can be expected to work on a touch screen while scrollbars probably will not work on that type of screen. The ScrollSketch class provides the same functionality using regular scrollbars.
|
This is a simple example that illustrates the use of the ClickBarSketch and how to code it. The sketch contains some text, a line, two rectangles, an ellipse, and a button. The demo emphasizes standard Processing capabilities because of the special coding that is needed for them. However, if you scroll down a bit, you will find a Clickable Button. To scroll, just click one of the ClickBar arrow buttons or the panel between them. The code for this example: |
class ClickBarSketch
extends Clickable
Adds a vertical and horizontal ClickBars to the sketch on the right and bottom. The user can use these bars to "scroll" the sketch.
All the Clickable items have been adapted to scroll so normally no extra coding is needed to
allow them to scroll. However, standard Processing items like text, lines and rectangles
need a little extra coding. All the x and y coordinates must be adapted to use the
global sX_ and sY_ methods found in the Clickable package. For example, replace
text("Some text", 20, 40);
with
text("Some text", sX_(20), sY_(40));
If you don't want a Clickable item to scroll, have it call its setScrollable method.
For example,
myButton.setScrollable(false);
For non-Clickable graphics like text, lines and rectangles, just don't use the sX_ and sY_
methods if they are not supposed to scroll.
The ClickBarSketch graphics must be set up carefully. It should be the last item in the ClickableGroup and it must be created in setup() after the size of the sketch is specified. For example,
...
ClickBarSketch sWindow;
Clickable[] clickItems = {... 6 Clickable items ...,
sWindow};
ClickableGroup clickGrp;
void setup() {
size(450, 500);
sWindow = new ClickBarSketch(#E0E0E0, #C0C0C0, #FF0000);
clickItems[6] = sWindow;
// or clickItems[clickItems.length - 1]= sWindow;
clickGrp = new ClickableGroup(clickItems);
...
In addition, in draw(), the ClickBarSketch should be the last thing drawn. This will be true if the SchollSketch item is the last item in the Clickable group.
The Clickable.pde file contains global fields "soX"_and "soY_" which contain the current scrolling offsets. The global methods sX_() and sY_() are intended for the user's program. Every Clickable item has the methods sX() and sY() methods that carry out similar operations except these methods do not adjust the coordinates if the "scrollable" field has been set false.
final int BAR_SIZE = 20 - Width and height of the scroll bars int scrollW, scrollH - The size of virtual window - the maximum values of x and y that can be displayed in the window. (The vitural window is marked "Virtual Window" in the diagram.) int winWidth, winHeight - The width and height of the sketch window. (This window is marked with "win" in the diagram.) int winPaneWidth, winPaneHeigh - The area that actually is is visible and is drawn after the clickbars are inserted. (Marked "winPane" in the diagram.) HoriClickBar hScroll - The horizontal scroll bar. VertClickBar vScroll - The vertical scroll bar. |
| ** ClickBarSketch(int maxWidth, int maxHeight,) color theColor, color theOverColor, color barColor) - Scrolls the sketch window. Vertical and horizontal scroll bars are used. The colors are the standard color, the color used when cursor is over the ClickBar and the color used for the bar in ClickBar. maxWidth and maxHeight are the maximum (virtual) size of the scrolled sketch of which only a portion is shown. maxWidth and maxHeight should be greater than width and height of the sketch for normal scrolling action. Note: In Processing, width and height are undefined until setup is run. Hence this ClickBarSketch, can't be created until after the size is set. Then the new ClickBarSketch must be put in the Clickable array and then the ClicableGroup can be created. |
| ** ClickBarSketch(color theColor,
color theOverColor, color barColor) - Scrolls the sketch window. Vertical and horizontal scroll bars are used. The colors are the standard color, the color used when cursor is over the ClickBar and the color used for the bar in ClickBar. The width and height of the scrolled window is about twice the size of the displayed window. Note: In Processing, width and height are undefined until setup is run. Hence this SchollSketch, can't be created until after the size is set. Then the new ClickBarSketch must be put in the Clickable array and then the ClickableGroup can be created. |
| * void display() - Displays the two ClickBars which do not scroll. |
| ** void setVisible(boolean b) - Sets the scroll bars as being visible true or false. |
| ** void setEnabled(boolean b) - Sets the two scroll bars as being enabled true or false. |
| ** boolean isOver() - Determines if the cursor is over either of the ClickBars and returns true if so. Always returns false if stillSearching_ is false. |
| * boolean checkForClick() - Checks to see if any part of either ClickBar has been clicked. If so, it updates the value of "soX_" or "soY_" as appropriate and sets "clicked" to true. Returns true if either has been clicked. Called directly or indirectly in the mouseReleased() method. |
| ** boolean hasBeenClicked() - Determines if either ClickBar has been clicked. If so, return true after "clicked" is set to false. Called directly or indirectly in by the draw() method. |
| ** int getOffsetX() () - Returns the "soX_" which is to be subtracted from the x coordinate of scrolled things. |
| ** int getOffsetY() - Returns the "soY_" which is to be subtracted from the y coordinate of scrolled things. |
Method use codes:
** This constructor or method is designed to be called in the user sketch.
* This method is normally not called in the user's sketch unless the Clickable item was not been included in a ClickableGroup. ClickableGroup automatically uses the method as needed. Exception: This method may be needed if the user is extending Clickable with a new class.
# This constructor or method is rarely used in the user program. In Java, it would probably marked "private". Clickable, ScrollBar, and some other abstract constructors would never be called directly by the user sketch because they do nothing useful unless the user is extending a class.