示例#1
0
int checkbox_touch_handler(touch_event *ev, void *data)
{
    checkbox *box = (checkbox*)data;

    if(box->touch_id == -1 && (ev->changed & TCHNG_ADDED))
    {
        if(!in_rect(ev->x, ev->y, box->x-TOUCH, box->y-TOUCH, CHECKBOX_SIZE+TOUCH*2, CHECKBOX_SIZE+TOUCH*2))
            return -1;

        box->touch_id = ev->id;
        box->hover = fb_add_rect(box->x-TOUCH, box->y-TOUCH, CHECKBOX_SIZE+TOUCH*2, CHECKBOX_SIZE+TOUCH*2, CLR_SECONDARY);
        fb_draw();
    }

    if(box->touch_id != ev->id)
        return -1;

    if(ev->changed & TCHNG_REMOVED)
    {
        if(in_rect(ev->x, ev->y, box->x-TOUCH, box->y-TOUCH, CHECKBOX_SIZE+TOUCH*2, CHECKBOX_SIZE+TOUCH*2))
        {
            (*box->clicked)(box->selected == NULL);
            checkbox_select(box, (box->selected == NULL));
        }

        fb_rm_rect(box->hover);
        box->hover = NULL;
        box->touch_id = -1;

        fb_draw();
    }
    return 0;
}
示例#2
0
int button_touch_handler(touch_event *ev, void *data)
{
    button *b = (button*)data;

    if(b->flags & BTN_DISABLED)
        return -1;

    if(b->touch_id == -1 && (ev->changed & TCHNG_ADDED) && !ev->consumed)
    {
        if(!in_rect(ev->x, ev->y, b->x, b->y, b->w, b->h))
            return -1;

        b->touch_id = ev->id;
    }

    if(b->touch_id != ev->id)
        return -1;

    if(ev->changed & TCHNG_POS)
        button_set_hover(b, in_rect(ev->x, ev->y, b->x, b->y, b->w, b->h));

    if(ev->changed & TCHNG_REMOVED)
    {
        if((b->flags & BTN_HOVER) && b->clicked)
            (*b->clicked)(b->action);
        button_set_hover(b, 0);
        b->touch_id = -1;
    }

    return 0;
}
示例#3
0
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    long long sq2, sq3, sq4;

    sq2 = in_squares(n, 2);
    sq3 = in_squares(n, 3);
    sq4 = in_squares(n, 4);
    printf("%lld %lld %lld %lld %lld %lld\n", sq2, in_rect(n, 2, sq2), sq3, in_rect(n, 3, sq3), sq4, in_rect(n, 4, sq4));
  }
}
示例#4
0
///Rectangle generator
GridItemPtr Grid::rectangle_generator::operator()()
{
    //Generator preamble
    BEGIN_RESTORE_STATE;
    RESTORE_STATE(state1);
    END_RESTORE_STATE;

    //Generator body
    BEGIN_GENERATOR;
	
    ix0 = limit( (int)floor(x0*grid->cellScale.x), 0, grid->numCols );
    iy0 = limit( (int)floor(y0*grid->cellScale.y), 0, grid->numRows );

    ix1 = limit( (int)ceil(x1*grid->cellScale.x), 0, grid->numCols );
    iy1 = limit( (int)ceil(y1*grid->cellScale.y), 0, grid->numRows );
    //note that local variable are not used, because we are inside pseudo-gnerator
    for (y=iy0; y<=iy1;++y){
	for (x=ix0; x<=ix1;++x){
	    cur_cell = &(grid->cellRef(x,y));
	    i = cur_cell->items.begin();
	    while ( i!=cur_cell->items.end() ){
		if ( in_rect((*i)->getPos(), x0, y0, x1, y1) ){
		    YIELD( *i, state1 );
		}
		++i;
	    }
	}
    }
    END_GENERATOR;
    return GridItemPtr(); //dummy return, just to exit the generator
}
示例#5
0
int pong_touch_handler(touch_event *ev, UNUSED void *data)
{
    int i = 0;
    for(; i < 2; ++i)
    {
        if (paddle_touch_id[i] == -1 && (ev->changed & TCHNG_ADDED) &&
            in_rect(ev->x, ev->y, paddles[i]->x, paddles[i]->y, paddles[i]->w, paddles[i]->h))
        {
            paddle_touch_id[i] = ev->id;
            paddle_last_x[i] = ev->x;
            if(i == L)
                enable_computer = 0;
            return 0;
        }

        if(ev->id != paddle_touch_id[i])
            continue;

        if(ev->changed & TCHNG_REMOVED)
        {
            paddle_touch_id[i] = -1;
            return 0;
        }

        int newX = paddles[i]->x + (ev->x - paddle_last_x[i]);
        paddle_last_x[i] = ev->x;

        if(newX > 0 && newX < (int)fb_width-PADDLE_W)
            paddles[i]->x = newX;
        return 0;
    }
    return -1;
}
示例#6
0
void ui_checkbox::touch_up(touch t) {
   if (in_rect(pos, dim, t.pos)) {
      checked = !checked;
      on_click();
   }
}
示例#7
0
文件: pilot.c 项目: jgraef/aNXT
int main(int argc,char *argv[]) {
  SDL_Event event;
  SDL_Surface *screen;
  SDL_Surface *bg,*left,*enter,*right,*exit,*display_surface;
  SDL_Rect rect_left = {
    .x = 51,
    .y = 172,
    .w = 27,
    .h = 31,
  };
  SDL_Rect rect_enter = {
    .x = 91,
    .y = 173,
    .w = 32,
    .h = 31,
  };
  SDL_Rect rect_right = {
    .x = 136,
    .y = 172,
    .w = 27,
    .h = 31,
  };
  SDL_Rect rect_exit = {
    .x = 91,
    .y = 216,
    .w = 31,
    .h = 20,
  };
  SDL_Rect rect_display = {
    .x = 57,
    .y = 62,
    .w = 100,
    .h = 64,
  };
  int c;
  char *name = NULL;
  int force = 0;

  while ((c = getopt(argc,argv,":hn:f"))!=-1) {
    switch (c) {
      case 'h':
        usage(argv[0],0);
        break;
      case 'f':
        force = 1;
        break;
      case 'n':
        name = optarg;
        break;
      case ':':
        fprintf(stderr,"Option -%c requires an operand\n",optopt);
        usage(argv[0],1);
        break;
      case '?':
        fprintf(stderr,"Unrecognized option: -%c\n", optopt);
        usage(argv[0],1);
        break;
    }
  }

  // init NXT
  nxt_t *nxt = nxt_open(name);
  if (nxt==NULL) {
    fprintf(stderr,"Could not find NXT\n");
    return 1;
  }
  if (nxt_get_connection_type(nxt)==NXT_CON_BT && !force) {
    fprintf(stderr,"Warning! Using NXT Pilot over Bluetooth can make trouble. Are you sure to continue (y/n)[n]: ");
    if (fgetc(stdin)!='y') {
      nxt_close(nxt);
      return 0;
    }
  }
  nxt_display_t *display = nxt_display_open(nxt);
  if (display==NULL) {
    fprintf(stderr,"Could not open display\n");
    nxt_close(nxt);
    return 1;
  }

  // init window
  if (SDL_Init(SDL_INIT_VIDEO)==-1) {
    fprintf(stderr,"Can't init SDL:  %s\n",SDL_GetError());
    return 1;
  }
  atexit(SDL_Quit);
  screen = SDL_SetVideoMode(215,322,32,SDL_HWSURFACE);
  if (screen==NULL) {
    fprintf(stderr,"Can't open window: %s\n",SDL_GetError());
    return 1;
  }
  SDL_WM_SetCaption("NXT Pilot","NXT Pilot");

  // load images
  bg = IMG_ReadXPMFromArray(pilot_bg_xpm);
  left = IMG_ReadXPMFromArray(pilot_left_xpm);
  enter = IMG_ReadXPMFromArray(pilot_enter_xpm);
  right = IMG_ReadXPMFromArray(pilot_right_xpm);
  exit = IMG_ReadXPMFromArray(pilot_exit_xpm);

  // display NXT
  SDL_BlitSurface(bg,NULL,screen,NULL);
  SDL_BlitSurface(left,NULL,screen,&rect_left);
  SDL_BlitSurface(enter,NULL,screen,&rect_enter);
  SDL_BlitSurface(right,NULL,screen,&rect_right);
  SDL_BlitSurface(exit,NULL,screen,&rect_exit);

  int done = 0;
  unsigned int pause = nxt_get_connection_type(nxt)==NXT_CON_BT?500:100;
  while (!done) {
    while (SDL_PollEvent(&event)) {
      switch(event.type) {
        case SDL_QUIT:
          done = 1;
          break;
        case SDL_KEYUP:
          if (event.key.keysym.sym==SDLK_ESCAPE) done = 1;
          else if (event.key.keysym.sym==SDLK_RETURN) nxt_set_button(nxt,NXT_UI_BUTTON_ENTER);
          else if (event.key.keysym.sym==SDLK_BACKSPACE) nxt_set_button(nxt,NXT_UI_BUTTON_EXIT);
          else if (event.key.keysym.sym==SDLK_LEFT) nxt_set_button(nxt,NXT_UI_BUTTON_LEFT);
          else if (event.key.keysym.sym==SDLK_RIGHT) nxt_set_button(nxt,NXT_UI_BUTTON_RIGHT);
          break;
        case SDL_MOUSEBUTTONUP:
          if (in_rect(rect_left,event.button)) nxt_set_button(nxt,NXT_UI_BUTTON_LEFT);
          else if (in_rect(rect_enter,event.button)) nxt_set_button(nxt,NXT_UI_BUTTON_ENTER);
          else if (in_rect(rect_right,event.button)) nxt_set_button(nxt,NXT_UI_BUTTON_RIGHT);
          else if (in_rect(rect_exit,event.button)) nxt_set_button(nxt,NXT_UI_BUTTON_EXIT);
          break;
      }
    }

    display_surface = load_display(display);
    if (display!=NULL) {
      SDL_BlitSurface(display_surface,NULL,screen,&rect_display);
      SDL_UpdateRect(screen,0,0,0,0);
      SDL_FreeSurface(display_surface);
    }

    SDL_Delay(pause);
  }

  // free images
  SDL_FreeSurface(bg);
  SDL_FreeSurface(left);
  SDL_FreeSurface(enter);
  SDL_FreeSurface(right);
  SDL_FreeSurface(exit);

  // close nxt
  int ret = nxt_error(nxt);
  nxt_display_flush(display,1);
  nxt_display_close(display);
  nxt_close(nxt);

  return ret;
}