// // CSE142, Spring 2000 // Homework 4 // Drawing Program Starter Code // #include "gp142display.h" #include "gp142.h" // For many of the GP142 functions, a line width of // zero means to fill the shape being drawn. The // constant FillWidth is used in function calls when a solid // shape is desired. const int FillWidth = 0; // Sizes of menu and menu items // Top-left coordinates of menu const int MenuTop = (GP142_YMAX - 5); const int MenuLeft = (-GP142_XMAX + 5); // Size of shapes drawn in menu and margin inside menu const int MenuShapeSize = 10; const int MenuShapeMargin = 10; // Height and width of menu items const int MenuItemHeight = (MenuShapeSize + MenuShapeMargin) * 2; const int MenuItemWidth = (MenuShapeSize + MenuShapeMargin) * 2; // Number of menu items const int NumMenuItems = 3; // Possible kinds of mouse clicks enum MouseClickKind { MenuClick, // click inside the menu NewShapeClick // click outside the menu }; //**************************************************************************** // // BUTTONS AND MOUSE CLICKS // //**************************************************************************** // Draw the menu with shapes void draw_shape_menu(GP142Display &display) { // draw rectangles for each menu item for(int i = 0; i < NumMenuItems; i++) { display.drawRectangle(MenuLeft, MenuTop - i * MenuItemHeight, MenuLeft + MenuItemWidth, MenuTop - (i + 1) * MenuItemHeight, Black, 1); } // draw shapes for each menu item int offsetX = MenuLeft + MenuShapeMargin + MenuShapeSize; int offsetY = MenuTop - (MenuShapeMargin + MenuShapeSize); display.drawRectangle(offsetX - MenuShapeSize, offsetY - MenuShapeSize, offsetX + MenuShapeSize, offsetY + MenuShapeSize, Black, FillWidth); offsetY = offsetY - MenuItemHeight; display.drawTriangle(offsetX, offsetY + MenuShapeSize, offsetX - MenuShapeSize, offsetY - MenuShapeSize/2, offsetX + MenuShapeSize, offsetY - MenuShapeSize/2, Black, FillWidth); offsetY = offsetY - MenuItemHeight; display.drawCircle(offsetX, offsetY, MenuShapeSize, Black); } // Classify mouse click at location mouse_x, mouse_y and return its kind MouseClickKind classify_mouse_click(int mouse_x, int mouse_y) { if ((mouse_x > MenuLeft) && (mouse_x < MenuLeft + MenuItemWidth) && (mouse_y < MenuTop) && (mouse_y > (MenuTop - NumMenuItems * MenuItemHeight))) return MenuClick; else return NewShapeClick; } // Process menu item click at location x, y void handle_menu_item(int x, int y) { // number of menu item (counting from top), converted from the y coordinate int menuItemNum = (MenuTop - y)/MenuItemHeight; switch(menuItemNum) { case 0: // square chosen // insert appropriate code here break; case 1: // triangle chosen // insert appropriate code here break; case 2: // circle chosen // insert appropriate code here break; default: break; } } //**************************************************************************** // // MAIN // //**************************************************************************** // * Main program contents: // * - Major program variables that persist throughout execution // * - Event loop that processes GP142 events, // * and updates variables and redraws the window as needed. until the user quits // int main() { // Event handling and menu items: bool quit = false; // = "user has selected quit" char key_pressed; // last keyboard character from key event int mouse_x, mouse_y; // x and y coordinates of latest mouse event MouseClickKind click_kind; // kind of mouse click (menu item or not) GP142Display display; // our GP142 window display.clear (White); display.setAnimation (Run); draw_shape_menu(display); // // * Main event loop: // * ---- ----- ----- // * Wait for the next user action, decode it, and call an // * appropriate function to handle it. Repeat until the // * user selects quit. // while (!quit) { // perform appropriate action depending on kind of event switch (display.getNextEvent(mouse_x, mouse_y, key_pressed)) { case Quit: // Quit selected quit = true; break; case Mouse: // Mouse click. Determine if in a menu menu item // or elsewhere and process accordingly. click_kind = classify_mouse_click(mouse_x, mouse_y); if (click_kind == MenuClick) { handle_menu_item(mouse_x, mouse_y); } else { // mouse click outside the menu // insert appropriate code here } break; default: // all other events we do not need to process break; } // Redraw everything... just the menu for now draw_shape_menu(display); } return 0; }