bool _button::clicked() { if (clickable) { if (click) { if ((!mousedown()) && (mouseover())) { click = false; return true; } else if (!(mouse_b & 1) && (!mouseover())) { click = false; return false; } } else { if (mousedown()) { click = true; } } } return false; }
bool execCommand(Command *cmd) { double x, y; int key; if (strcmp(cmd->cmd, "mm") == 0) { x = strtod(cmd->arg1, NULL); y = strtod(cmd->arg2, NULL); mousemove(x, y); return true; } else if (strcmp(cmd->cmd, "kd") == 0) { key = (int) strtol(cmd->arg1, NULL, 10); keydown(key); } else if (strcmp(cmd->cmd, "ku") == 0) { key = (int)strtol(cmd->arg1, NULL, 10); keyup(key); } else if (strcmp(cmd->cmd, "md") == 0) { mousedown(cmd->arg1[0]); } else if (strcmp(cmd->cmd, "mu") == 0) { mouseup(cmd->arg1[0]); } else if (strcmp(cmd->cmd, "sy") == 0) { scrollY(strtod(cmd->arg1, NULL)); } else if (strcmp(cmd->cmd, "sx") == 0) { scrollX(strtod(cmd->arg1, NULL)); } return false; }
void _button::check() { draw(); if (type == 1) { if (mouseover()) { if (texta < 1.0) { texta += 0.05; } } else { if (texta > 0.5) { texta -= 0.05; } } } if (clickable) { if (mousedown() || rmousedown()) { if (!sound) { play_sound(S_CLICK); sound = true; } } else { sound = false; } } }
// The main Allegro loop, all input handling, animation and drawing is done here _Bool main_loop(void) { // Flag for drawing _Bool needredraw = true; // Declare primitive data types float old_time = 0.0, current_time = 0, dt = 0; while(!data->exit) { if(needredraw && al_event_queue_is_empty(data->queue) && al_event_queue_is_empty(data->queue2)) { // Clear, draw, flip al_clear_to_color(data->background_color); render(); if(get_fps_status()) al_draw_textf(data->font, data->text_color, 0, res_height-30, 0, "fps: %.2f", 1/dt); al_flip_display(); needredraw = false; } // Block until an event enters the queue al_wait_for_event(data->queue, &(data->event)); while(!al_event_queue_is_empty(data->queue2)) { al_get_next_event(data->queue2, &(data->event2)); switch(data->event2.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: { // If x button is pressed on window data->exit = true; data->gamestarted = false; } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: { al_acknowledge_resize(data->event2.display.source); scale(res_width, res_height); } break; case ALLEGRO_EVENT_MOUSE_AXES: { // Stores the mouse's new position and change in position mouseaxes(&(data->event2.mouse)); } case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: { // Stores the mouse button pressed mousedown(&(data->event2.mouse)); } break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: { // Stores the mouse button released mouseup(&(data->event2.mouse)); } break; case ALLEGRO_EVENT_KEY_DOWN: { // Stores keydown keycode into keycode array for processing keydown(&(data->event2.keyboard)); } break; case ALLEGRO_EVENT_KEY_UP: { // Stores keycode into keycode array for processing keyup(&(data->event2.keyboard)); } break; default: break; } } switch (data->event.type) { case ALLEGRO_EVENT_TIMER: { // Determine the change in time between frames, in seconds current_time = al_current_time(); dt = current_time-old_time; // If the computer lags for some reason, don't penalize the player // Cap dt at 0.5 seconds if(dt > 0.25) { dt = 0.25; } // Handle Mouse and Keyboard events and Button Events buttoncheck(&buttonhandler, data); keycheck(&keyhandler, data); mousecheck(&mousehandler, data); keyupdate(); mouseupdate(); // Check if data->quit has been set before updating and drawing if(data->exit) break; // Update the game, always update(); // Skip drawing frames if computer lags if(current_time - data->event.timer.timestamp <= 1.0/al_get_display_refresh_rate(data->display)) { needredraw = true; } // Make the time at this frame the old time, for the next frame old_time = current_time; } break; default: break; } } return true; }
void window::main_loop() { SDL_Event event; while(true) { while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: exit(0); break; default: keydown(event.key.keysym.sym); break; } break; case SDL_KEYUP: keyup(event.key.keysym.sym); break; case SDL_MOUSEBUTTONDOWN: switch(event.button.button) { case SDL_BUTTON_LEFT: { const float x_screen = static_cast<float>(event.button.x); const float y_screen = static_cast<float>(event.button.y); // scaling from <0,width> to <-1,1> const float x_scaled = 2.0 * (x_screen - static_cast<float>(width) / 2.0) / static_cast<float>(width); const float y_scaled = 2.0 * (y_screen - static_cast<float>(height) / 2.0) / static_cast<float>(height); mousedown(x_scaled, y_scaled); } break; case SDL_BUTTON_WHEELUP: mousewheelup(); break; case SDL_BUTTON_WHEELDOWN: mousewheeldown(); break; } break; case SDL_MOUSEBUTTONUP: if(event.button.button == SDL_BUTTON_LEFT) mouseup(); break; case SDL_MOUSEMOTION: { const float x_screen = static_cast<float>(event.motion.x); const float y_screen = static_cast<float>(event.motion.y); // scaling from <0,width> to <-1,1> const float x_scaled = 2.0 * (x_screen - static_cast<float>(width) / 2.0) / static_cast<float>(width); const float y_scaled = 2.0 * (y_screen - static_cast<float>(height) / 2.0) / static_cast<float>(height); mousemotion(x_scaled, y_scaled); } break; case SDL_VIDEORESIZE: handle_resize(event.resize.w, event.resize.h); break; case SDL_QUIT: exit(0); break; } } frame(); } }
void _button::draw() { if ((picture == NULL) || (!picture->IsValid())) { if (!mousedown() && !rmousedown()) { switch(type) { case 0: //standard button if (mouseover()) { buffer_rectrounded(x, y, x + w, y + h, WHITE, 0.7); } else { buffer_rectrounded(x, y, x + w, y + h, WHITE, 0.4); } buffer_textout_ex(normal, text, x + (w / 2) - (textw / 2), y + (h / 2) - 5, BLACK); break; case 1: //larger text, no background buffer_textout_ex(big, text, x, y, color, texta); break; case 2: //colored rectangle buffer_rectfill(x, y, x + w, y + h, color); break; case 3: //large hexagon if (mouseover()) { buffer_draw_sprite(glb.bmp(33), x, y); } break; case 4: //small hexagon if (mouseover()) { buffer_draw_sprite(glb.bmp(34), x, y); } break; case 5: //standard button, but with medium font and a little more opacity if (mouseover()) { buffer_rectrounded(x, y, x + w, y + h, LGREY, 0.9); } else { buffer_rectrounded(x, y, x + w, y + h, WHITE, 0.9); } buffer_textout_ex(med, text, x + (w / 2) - (textw / 2), y + (h / 2) - 9, BLACK); break; case 6: if (mouseover()) { buffer_rectfill(x, y, x + w, y + h, color, 0.3); } break; } } else //if mousedown { switch(type) { case 0: buffer_rectrounded(x, y, x + w, y + h, WHITE, 0.7); buffer_textout_ex(normal, text, x + (w / 2) - (textw / 2) + 1, y + (h / 2) - 4, BLACK); break; case 1: buffer_textout_ex(big, text, x, y, color, texta); break; case 2: buffer_rectfill(x, y, x + w, y + h, color); break; case 3: //large hexagon if (mouseover()) { buffer_draw_sprite(glb.bmp(33), x, y); } break; case 4: //small hexagon if (mouseover()) { buffer_draw_sprite(glb.bmp(34), x, y); } break; case 5: buffer_rectrounded(x, y, x + w, y + h, WHITE, 0.9); buffer_textout_ex(med, text, x + (w / 2) - (textw / 2) + 1, y + (h / 2) - 8, BLACK); break; case 6: buffer_rectfill(x, y, x + w, y + h, color, 0.5); break; } } } else { buffer_blit_clipped(picture, x, y, 0, 0, w, h); } }
void runSimulation(char *fileName){ WorldBoundaryType worldBoundary; //duplicated in GridWorld DevBoundaryType deviceBoundary; //duplicated in GridWorld bool ANIMATE_MOUSE_FLAG=false; bool validCellSelected=false; static BOOL page=false; int mX, mY; float worldX, worldY; worldX=0.0f; worldY=0.0f; int action=-1; //----------------------- CellPosition p; int rowSelected, colSelected; //----------------------- rowSelected=-1; colSelected=-1; int mouseRadius=1; srand(time(NULL)); // Seed the random number generator //Initialise the world boundaries grid_world.initSystemOfCoordinates(); grid_world.loadMapAndDisplay(fileName); grid_world.initialiseMapConnections(); //---------------------------------------------------------------- //LPA* lpa_star = new LpaStar(grid_world.getGridWorldRows(), grid_world.getGridWorldCols()); vertex start = grid_world.getStartVertex(); vertex goal = grid_world.getGoalVertex(); cout << "(start.col = " << start.col << ", start.row = " << start.row << ")" << endl; cout << "(goal.col = " << goal.col << ", goal.row = " << goal.row << ")" << endl; lpa_star->initialise(start.col, start.row, goal.col, goal.row); //lpa_star->copyMazeToDisplayMap(grid_world); //copyMazeToDisplayMap(grid_world, lpa_star); copyDisplayMapToMaze(grid_world, lpa_star); //---------------------------------------------------------------- worldBoundary = grid_world.getWorldBoundary(); deviceBoundary = grid_world.getDeviceBoundary(); GRIDWORLD_ROWS = grid_world.getGridWorldRows(); GRIDWORLD_COLS = grid_world.getGridWorldCols(); //setvisualpage(page); // keep running the program until the ESC key is pressed while((GetAsyncKeyState(VK_ESCAPE)) == 0 ) { setactivepage(page); cleardevice(); action = getKey(); if(SHOW_MAP_DETAILS) grid_world.displayMapWithDetails(); else grid_world.displayMap(); switch(action){ case 1: //Block selected cell if( rowSelected != -1 && colSelected != -1){ grid_world.setMapTypeValue(rowSelected-1, colSelected-1, '1'); grid_world.initialiseMapConnections(); rowSelected=-1; colSelected=-1; } action = -1; break; case 105: grid_world.displayMapWithKeyDetails(); break; case 106: //~ algorithmSelection = ASTAR_ALGORITHM; break; case 107: //~ algorithmSelection = LPASTAR_ALGORITHM; break; case 108: //~ algorithmSelection = DSTAR_ALGORITHM; break; case 15: if( rowSelected != -1 && colSelected != -1){ grid_world.displayVertexConnections(colSelected-1, rowSelected-1); //cout << "display connections" << endl; rowSelected=-1; colSelected=-1; } else { cout << "invalid new START vertex, please select a new START vertex first." << endl; break; } //-------------------------------------------- action = -1; break; case 16: if(grid_world.isGridMapInitialised()){ grid_world.displayMapConnections(); //cout << "display connections" << endl; //~ rowSelected=-1; //~ colSelected=-1; } else { cout << "map has not been initialised yet." << endl; break; } //-------------------------------------------- action = -1; break; case 6: //set cell as new START vertex { //-------------------------------------------- // retrieve current START vertex vertex s = grid_world.getStartVertex(); if( (s.row != -1) && (s.col != -1) ){ //set current START VERTEX to an ordinary TRAVERSABLE CELL grid_world.setMapTypeValue(s.row, s.col, '0'); grid_world.initialiseMapConnections(); //ok, proceed } else { cout << "invalid START vertex" << endl; break; } //-------------------------------------------- //set selected cell as the NEW START VERTEX if( rowSelected != -1 && colSelected != -1){ grid_world.setMapTypeValue(rowSelected-1, colSelected-1, '6'); s.row = rowSelected-1; s.col = colSelected-1; grid_world.setStartVertex(s); rowSelected=-1; colSelected=-1; } else { cout << "invalid new START vertex, please select a new START vertex first." << endl; break; } //-------------------------------------------- action = -1; break; } case 7: //set cell as new GOAL vertex { //-------------------------------------------- // retrieve current GOAL vertex vertex s = grid_world.getGoalVertex(); if( (s.row != -1) && (s.col != -1) ){ //set current GOAL VERTEX to an ordinary TRAVERSABLE CELL grid_world.setMapTypeValue(s.row, s.col, '0'); //ok, proceed } else { cout << "invalid GOAL vertex" << endl; action = -1; break; } //-------------------------------------------- //set selected cell as the NEW GOAL VERTEX if( rowSelected != -1 && colSelected != -1){ grid_world.setMapTypeValue(rowSelected-1, colSelected-1, '7'); s.row = rowSelected-1; s.col = colSelected-1; grid_world.setGoalVertex(s); grid_world.initialiseMapConnections(); rowSelected=-1; colSelected=-1; } else { cout << "invalid new GOAL vertex, please select a new GOAL vertex first." << endl; action = -1; break; } //-------------------------------------------- action = -1; break; } case 109: copyDisplayMapToMaze(grid_world, lpa_star); cout << "copied display map to algorithm's maze" << endl; action = -1; break; case 110: lpa_star->updateHValues(); copyMazeToDisplayMap(grid_world, lpa_star); cout << "copied algorithm's maze to display map" << endl; action = -1; break; case 9: //display g-values only grid_world.displayMapWithSelectedDetails(true, false, false, false); //(bool display_g, bool display_rhs, bool display_h, bool display_key) action = -1; break; case 10: //display h-values only grid_world.displayMapWithSelectedDetails(false, false, true, false); //(bool display_g, bool display_rhs, bool display_h, bool display_key) action = -1; break; case 11: //display key-values only lpa_star->updateAllKeyValues(); copyMazeToDisplayMap(grid_world, lpa_star); grid_world.displayMapWithSelectedDetails(false, false, false, true); //(bool display_g, bool display_rhs, bool display_h, bool display_key) action = -1; break; case 12: //make cell Traversable if( rowSelected != -1 && colSelected != -1){ grid_world.setMapTypeValue(rowSelected-1, colSelected-1, '0'); rowSelected=-1; colSelected=-1; } action = -1; break; case 14: grid_world.displayMapWithPositionDetails(); action = -1; break; //~ default: //Display grid without details //~ grid_world.displayMap(); }; //---------------------------------------------------------------------------------------------------------------- // Mouse handling // if(mousedown()){ ANIMATE_MOUSE_FLAG=true; mX = mousecurrentx(); mY = mousecurrenty(); //if the goal selected is within the playing field boundaries if(mX >= grid_world.getFieldX1() && mX <= grid_world.getGridMaxX() && mY >= grid_world.getFieldY1() && mY <= grid_world.getGridMaxY()){ circle(mX, mY, 3); validCellSelected = true; } else { validCellSelected = false; } } //end of mousedown() //------------------------------------------------------------------------------------------------------------------ ///////////////////////////////////////////////////////////////////////////// if(ANIMATE_MOUSE_FLAG){ //draw Cross-hair to mark Goal setcolor(RED); circle(mX, mY, 20); line(mX,mY-20,mX,mY+20); line(mX-20,mY,mX+20,mY); //end of draw Cross-hair // special effect to display concentric circles locating the target setcolor(YELLOW); if(mouseRadius < 40) { mouseRadius += 1; } circle(mX, mY, mouseRadius); //Sleep(50); if(mouseRadius >= 40) { ANIMATE_MOUSE_FLAG=false; mouseRadius=0; } //end of special effect } ///////////////////////////////////////////////////////////////////////////// char info[80]; float wX, wY; wX = xWorld(worldBoundary,deviceBoundary,mX); wY = yWorld(worldBoundary,deviceBoundary,mY); sprintf(info,"x: %d, y: %d",mX, mY); drawInformationPanel(grid_world.getFieldX2(), grid_world.getFieldY1() + textheight("H")*2, info); sprintf(info,"wX: %3.0f, wY: %3.0f",wX, wY); drawInformationPanel(grid_world.getFieldX2(),grid_world.getFieldY1() + textheight("H")*5, info); ///////////////////////////////////////////////////////////////////////////// //~ CellPosition p; //~ int rowSelected, colSelected; if(validCellSelected) { p=grid_world.getCellPosition_markCell(mX, mY); rowSelected = p.row; colSelected = p.col; sprintf(info,"row: %d, col: %d",rowSelected, colSelected); drawInformationPanel(grid_world.getFieldX2(),grid_world.getFieldY1() + textheight("H")*6, info); } setvisualpage(page); page = !page; //switch to another page } }