virtual i4_bool mouse_drag() { //{{{ Handle Moving if (buttons() & moving) { i4_float theta = m1_render_window->theta + 0.01f*(mouse_x() - last_x()); i4_float phi = m1_render_window->phi - 0.01f*(mouse_y() - last_y()); if (theta<0.0) { theta += i4_pi()*2; } else if (theta>i4_pi()*2) { theta -= i4_pi()*2; } if (phi<0.0) { phi += i4_pi()*2; } else if (phi>i4_pi()*2) { phi -= i4_pi()*2; } m1_render_window->theta = theta; m1_render_window->phi = phi; m1_render_window->recalc_view(); return i4_T; } //}}} //{{{ Handle Zooming if (buttons() & zooming) { //This is not really that time critical, only done several times per second. //otherwise, it would be bad style... m1_render_window->dist *= (i4_float) exp(i4_float(mouse_y() - last_y())/height() * log(4.0f)); m1_render_window->recalc_view(); return i4_T; } //}}} //{{{ Handle Panning if (buttons() & zooming) { i4_float p_x = i4_float(mouse_x() - last_x())/width(); i4_float p_y = i4_float(mouse_y() - last_y())/height(); m1_render_window->pan(p_x,p_y); m1_render_window->recalc_view(); return i4_T; } //}}} return i4_F; }
void TEST_move_mouse() { char *routine = "TEST_move_mouse"; printf(testing, routine); //First we test with no window int before_x = mouse_x(0); int before_y = mouse_y(0); assert(move_mouse(0, 0, 0)); int after_x = mouse_x(0); int after_y = mouse_y(0); assert(after_x == 0 && after_y == 0); //Now we try it with a valid window Window win = active_window(); activate_window(win); assert(move_mouse(10, 10, win)); after_x = mouse_x(win); after_y = mouse_y(win); assert(after_x == 10 && after_y == 10); //Finally, let's make sure it doesn't bomb with an invalid window //The correct action is to leave the mouse where it is assert(move_mouse(20, 20, 20) == FALSE); assert(mouse_x(0) != 20 && mouse_y(0) != 20); move_mouse(before_x, before_y, 0); printf(done, routine); }
BOOL _normalize_wincoords(int *x, int *y, unsigned long win) { if (!_check_init()) { return FALSE; } XWindowAttributes *attribs = calloc(1, sizeof(XWindowAttributes)); if(attribs == NULL) { fprintf(stderr, "Unable to allocate space for XWindowAttributes"); return FALSE; } XSetErrorHandler(_invalid_window_error_handler); int status = XGetWindowAttributes(defaults->display, win, attribs); XSetErrorHandler(NULL); BOOL ret; if (status) { int winx = window_x(win); int winy = window_y(win); *x = winx + *x; *y = winy + *y; logit(LOG_LEVEL_VERBOSE, "Window at %d X %d\n", winx, winy); logit(LOG_LEVEL_VERBOSE, "Relative location %d X %d\n", *x, *y); ret = TRUE; } else { fprintf(stderr, "Window handle %lu appears to be invalid\n", win); fprintf(stderr, "Not moving the mouse\n"); *x = mouse_x(0); *y = mouse_y(0); ret = FALSE; } free(attribs); //In case the window is partially off the screen _normalize_coords(x, y); return ret; }
void event_logic(){ for(ALLEGRO_EVENT event; al_get_next_event(engine_event_queue,&event);){ //Default event handlers switch(event.type){ case ALLEGRO_EVENT_DISPLAY_CLOSE: quit_at_next_frame(); break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: if(event.mouse.button==1){ for(std::list<renderable*>::iterator iter = ENGINE_SCREEN_MANAGER.buttons.begin(); iter != ENGINE_SCREEN_MANAGER.buttons.end(); ++iter){ if( (*iter)->active){ (*iter)->calc_bounds(); if(in_range((*iter)->bounds[0][0],(*iter)->bounds[0][1],mouse_x()) && in_range((*iter)->bounds[1][0],(*iter)->bounds[1][1],mouse_y())){ ((button*)(*iter))->on_click->exec(); } } } } break; case ALLEGRO_EVENT_TIMER: if(event.timer.source==timer) draw_now=true; break; } game_event_hooks(event); } al_rest(0.001); }
virtual i4_bool mouse_down() { ax = mouse_x(); ay = mouse_y(); return i4_F; }
int main() { while ( !button_pressed() ) { setup_demo( buf, synth ); // base frequency and amplitude on mouse position int period = mouse_x() * 100 + 10; int amplitude = mouse_y() * 9 + 1; // generate alternating signs of square wave, spaced by period int time = 0; while ( time < 1000 ) { amplitude = -amplitude; synth.update( time, amplitude ); time += period; } buf.end_frame( 1000 ); show_buffer( buf ); } return 0; }
void particle2_example_logic(){ for(int i = 0; i < 40; ++i){ if(particle2[i]->auto_update){ particle2[i]->x_vel+=(mouse_x()-particle2[i]->x)/(40.0f-0.5f*i)-particle2[i]->x_vel/(20.0f+0.5f*i); particle2[i]->y_vel+=(mouse_y()-particle2[i]->y)/(40.0f-0.5f*i)-particle2[i]->y_vel/(20.0f+0.5f*i); } } }
void image_example_logic(){ for(int i = 0; i < 10; ++i){ if(sprite[i]->auto_update){ sprite[i]->x_vel+=(mouse_x()-sprite[i]->x)/(40.0f-2.0f*i)-sprite[i]->x_vel/(20.0f+2.0f*i); sprite[i]->y_vel+=(mouse_y()-sprite[i]->y)/(40.0f-2.0f*i)-sprite[i]->y_vel/(20.0f+2.0f*i); } } }
void TEST_mouse_y() { char *routine = "TEST_mouse_y"; printf(testing, routine); //There's no way to know if the values we receive are "correct", //so we can only test to make sure it doesn't cause problems //First with no window mouse_y(0); //Then with a valid window Window win = active_window(); mouse_y(win); //Finally with an invalid window - which should return INT_MIN int invalid_y = mouse_y(20); assert(invalid_y == INT_MIN); printf(done, routine); }
int particle2_example_init(){ for(int i = 0; i < 40; ++i){ particle2[i]=new particle_circ(5.0f); particle2[i]->deactivate_at_edge=false; particle2[i]->auto_render=true; particle2[i]->auto_update=true; particle2[i]->tracer=true; int tmp=(rand()%256)|128; particle2[i]->set( double(mouse_x()),double(mouse_y()), al_map_rgba(tmp/2,0,tmp,(rand()%256)|255)); } return 0; }
virtual i4_bool mouse_up() { g1_quad_object_class * obj = m1_info.obj; g1_vert_class * src_vert = obj->get_verts(m1_info.current_animation, m1_info.current_frame); r1_vert v; i4_float tmp; px = mouse_x(); py = mouse_y(); if (ax>px) { tmp=ax; ax=px; px=tmp; } if (ay>py) { tmp=ay; ay=py; py=tmp; } if (!i4_current_app->get_window_manager()->shift_pressed()) { li_call("select_none"); } for (int i=0; i<obj->num_quad; i++) { g1_quad_class * q = 0; q= &(obj->quad[i]); i4_bool out=i4_F; for (int j=0; j<q->num_verts() && !out; j++) { m1_render_window->project_point(src_vert[q->vertex_ref[j]].v, v); if (v.px<ax || v.px>px || v.py<ay || v.py>py) { out = i4_T; } } if (!out) { q->set_flags(g1_quad_class::SELECTED); } } m1_render_window->restore_state(); m1_render_window->request_redraw(i4_F); m1_st_edit->edit_poly_changed(); return i4_T; }
BOOL move_mouse(int newX, int newY, Window win) { int loop = 0; if (!_check_init()) { return FALSE; } BOOL success = TRUE; logit(LOG_LEVEL_VERBOSE, "Window handle: %lu\n", win); if (win) { success = _normalize_wincoords(&newX, &newY, win); if(success == FALSE) { return FALSE; } } else { _normalize_coords(&newX, &newY); } if (defaults->mouse_move_delay == ZERO) { return _do_movement(newX, newY); } else { int oldX = mouse_x(0L); int oldY = mouse_y(0L); while (oldX != newX || oldY != newY) { int movementX, movementY; _gradientMovement(newX, newY, &movementX, &movementY); // 1:1 is a little slow if ((fabs(movementX) == 1 && fabs(movementY) == 1) && (fabs(newX - oldX) > 1 && fabs(newY - oldY) > 1) ) { movementX *= 2; movementY *= 2; } oldX += movementX; oldY += movementY; success &= _do_movement(oldX, oldY); if (!success) { fprintf(stderr, "Simulation of mouse movement failed.\n"); break; } usleep(50 * defaults->mouse_move_delay); loop++; if (loop > 10000) { return FALSE; } //Probably something is wrong } } return success; }
void _gradientMovement(int targX, int targY, int *xResp, int *yResp) { int currX = mouse_x(0L); int currY = mouse_y(0L); int diffX = (int) (fabs(targX - currX)); int diffY = (int) (fabs(targY - currY)); if (diffX == 0 && diffY == 0) { //No movement at all - why are we here? //Theoretically this shouldn't happen, but at least it won't explode fprintf(stderr, "gradientMovement - target and destination" " coordinates are the same\n"); *xResp = 0; *yResp = 0; } else if (diffX == 0) { //Up-down movement only *xResp = 0; *yResp = diffY; } else if (diffY == 0) { //Left-right movement only *xResp = diffX; *yResp = 0; } else { //Want a movement based on ratio if (diffX == diffY) { *xResp = 1; *yResp = 1; } else if (diffX > diffY) { //The following line is cast for clarity sake - I know I want int *xResp = (int) (diffX / diffY); *yResp = 1; } else { *xResp = 1; //The following line is cast for clarity sake - I know I want int *yResp = (int) (diffY / diffX); } } // Multiply by -1 if moving up or left if (targX < currX) { *xResp *= -1; } if (targY < currY) { *yResp *= -1; } }
void IOController::trigger_events(const BBox& playing_area) { // Loop over all possible keys to find active key states for (int i = SDLK_FIRST + 1; i < SDLK_LAST; i++) { IOEventTrigger::trigger_t notrigger = IOEventTrigger::NONE; if (iostate.key_press_states[i]) { // Trigger key press events __trigger_events(notrigger, SDLKey(i), iostate.keymod, false); } else if (iostate.key_down_states[i]) { // Trigger key hold events __trigger_events(notrigger, SDLKey(i), iostate.keymod, true); } } if (playing_area.contains(mouse_x(), mouse_y())) { if (mouse_left_click() || mouse_left_down()) { __trigger_events(IOEventTrigger::MOUSE_LEFT_CLICK, SDLKey(0), iostate.keymod, !mouse_left_click()); } if (mouse_right_click() || mouse_right_down()) { __trigger_events(IOEventTrigger::MOUSE_RIGHT_CLICK, SDLKey(0), iostate.keymod, !mouse_left_click()); } } if (iostate.mouse_middleclick || iostate.mouse_middledown) { __trigger_events(IOEventTrigger::MOUSE_MIDDLE_CLICK, SDLKey(0), iostate.keymod, !iostate.mouse_middleclick); } if (mouse_upwheel()) { __trigger_events(IOEventTrigger::MOUSE_WHEEL_UP, SDLKey(0), iostate.keymod, false); } if (mouse_downwheel()) { __trigger_events(IOEventTrigger::MOUSE_WHEEL_DOWN, SDLKey(0), iostate.keymod, false); } if (mouse_downwheel()) { __trigger_events(IOEventTrigger::MOUSE_WHEEL_DOWN, SDLKey(0), iostate.keymod, false); } }
int delta_mouse_y(void) const noexcept { return mouse_y() - mouse_y(_old); }