Ejemplo n.º 1
0
/* Compare grids. */
int
grid_compare(struct grid *ga, struct grid *gb)
{
	struct grid_line	*gla, *glb;
	struct grid_cell	 gca, gcb;
	u_int			 xx, yy;

	if (ga->sx != gb->sx || ga->sy != gb->sy)
		return (1);

	for (yy = 0; yy < ga->sy; yy++) {
		gla = &ga->linedata[yy];
		glb = &gb->linedata[yy];
		if (gla->cellsize != glb->cellsize)
			return (1);
		for (xx = 0; xx < gla->cellsize; xx++) {
			grid_get_cell(ga, xx, yy, &gca);
			grid_get_cell(gb, xx, yy, &gcb);
			if (!grid_cells_equal(&gca, &gcb))
				return (1);
		}
	}

	return (0);
}
Ejemplo n.º 2
0
static void grid_apply_function(grid *grid_ptr, double2 center, double radius,
                                int (*cb_fun)(grid *, grid_callback_data), void *user_data, int n_excluded,
                                const int *excluded_indices)
{
  /*
   * Apply a callback function `cb_fun` to all points with a distance less than `radius` to the point `center`
   * except for those that are contained in `excluded_indices`. The information about the search position and the
   * current point are passed to the callback function using the `grid_callback_data` struct.
   * Returns if all points are processed or the callback function returns a non-zero value.
   */
  int i, j, k, l;
  double2 top_right_position, bottom_left_position;
  int2 top_right_cell, bottom_left_cell;
  grid_callback_data callback_data;
  top_right_position.x = center.x + radius;
  top_right_position.y = center.y + radius;
  bottom_left_position.x = center.x - radius;
  bottom_left_position.y = center.y - radius;
  top_right_cell = grid_get_cell(grid_ptr, &top_right_position);
  bottom_left_cell = grid_get_cell(grid_ptr, &bottom_left_position);

  callback_data.center_point = center;
  callback_data.r = radius;
  callback_data.user_data = user_data;

  for (i = bottom_left_cell.y; i <= top_right_cell.y; i++)
    {
      for (j = bottom_left_cell.x; j <= top_right_cell.x; j++)
        {
          int cell_index = i * grid_ptr->num_cells.x + j;
          for (k = grid_ptr->cell_offsets[cell_index]; k < grid_ptr->cell_offsets[cell_index + 1]; k++)
            {
              for (l = 0; l < n_excluded; l++)
                {
                  if (excluded_indices[l] == k)
                    {
                      break;
                    }
                }
              if (l < n_excluded)
                {
                  continue;
                }
              if (distance_sq(center, grid_ptr->points[k]) >= radius * radius)
                {
                  continue;
                }
              callback_data.current_index = k;
              callback_data.current_point = grid_ptr->points[k];
              if (cb_fun(grid_ptr, callback_data))
                {
                  return;
                }
            }
        }
    }
}
Ejemplo n.º 3
0
void grid_reveal_cell(struct grid_t *grid, int x, int y)
{
	struct cell_t *cell = grid_get_cell(grid, x, y);
	
	if (!cell)
		return;
	
	/* The cell must be unrevealed as a user protection */
	if (cell->state != CELL_UNREVEALED)
		return;
	
	cell->state = CELL_REVEALED;
	
	int bombs = grid_adjacent_bombs(grid, x, y);
	if (bombs > 0)
		return;
	
	/* Uncover adjacent cells recursively */
	
	int dx;
	int dy;
	
	for (dy = -1; dy <= 1; dy++)
	{
		for (dx = -1; dx <= 1; dx++)
		{
			grid_reveal_cell(grid, x + dx, y + dy);
		}
	}
}
Ejemplo n.º 4
0
static int grid_get_cell_number(grid *grid, const double2 *position)
{
  /*
   * Calculate the consecutive cell index of the cell containing the coordinate position.
   */
  int2 c = grid_get_cell(grid, position);
  return c.x + c.y * grid->num_cells.x;
}
Ejemplo n.º 5
0
/* Copy from another screen. */
void
screen_write_copy(struct screen_write_ctx *ctx,
    struct screen *src, u_int px, u_int py, u_int nx, u_int ny)
{
	struct screen		*s = ctx->s;
	struct grid		*gd = src->grid;
	struct grid_line	*gl;
	struct grid_cell	 gc;
	u_int		 	 xx, yy, cx, cy, ax, bx;

	cx = s->cx;
	cy = s->cy;
	for (yy = py; yy < py + ny; yy++) {
		gl = &gd->linedata[yy];
		if (yy < gd->hsize + gd->sy) {
			/*
			 * Find start and end position and copy between
			 * them. Limit to the real end of the line then use a
			 * clear EOL only if copying to the end, otherwise
			 * could overwrite whatever is there already.
			 */
			if (px > gl->cellsize)
				ax = gl->cellsize;
			else
				ax = px;
			if (px + nx == gd->sx && px + nx > gl->cellsize)
				bx = gl->cellsize;
			else
				bx = px + nx;

			for (xx = ax; xx < bx; xx++) {
				grid_get_cell(gd, xx, yy, &gc);
				screen_write_cell(ctx, &gc);
			}
			if (px + nx == gd->sx && px + nx > gl->cellsize)
				screen_write_clearendofline(ctx);
		} else
			screen_write_clearline(ctx);
		cy++;
		screen_write_cursormove(ctx, cx, cy);
	}
}
Ejemplo n.º 6
0
int grid_get_char(struct grid_t *grid, int x, int y)
{
	struct cell_t *cell = grid_get_cell(grid, x, y);
	assert(cell);
	
	switch (cell->state)
	{
		case CELL_UNREVEALED:
			return ACS_BULLET;
		
		case CELL_REVEALED:
		{
			if (cell->bomb)
			{
				return '*' | A_BOLD | COLOR_PAIR(1);
			}
			
			int bombs = grid_adjacent_bombs(grid, x, y);
			
			if (bombs > 0)
			{
				char c[2];
				snprintf(c, 2, "%d", bombs);
				return c[0];
			}
			else
			{
				return ' ';
			}
		}
		
		case CELL_FLAGGED:
			return 'o' | COLOR_PAIR(2);
		
		case CELL_QUESTION:
			return '?' | COLOR_PAIR(3);
		
		default:
			return 0;
	}
}
Ejemplo n.º 7
0
/* Copy from another screen. */
void
screen_write_copy(struct screen_write_ctx *ctx, struct screen *src, u_int px,
    u_int py, u_int nx, u_int ny)
{
	struct screen		*s = ctx->s;
	struct grid		*gd = src->grid;
	struct grid_cell	 gc;
	u_int		 	 xx, yy, cx, cy;

	cx = s->cx;
	cy = s->cy;

	for (yy = py; yy < py + ny; yy++) {
		for (xx = px; xx < px + nx; xx++) {
			grid_get_cell(gd, xx, yy, &gc);
			screen_write_cell(ctx, &gc);
		}

		cy++;
		screen_write_cursormove(ctx, cx, cy);
	}
}
Ejemplo n.º 8
0
void grid_toggle_question(struct grid_t *grid, int x, int y)
{
	struct cell_t *cell = grid_get_cell(grid, x, y);
	
	if (!cell)
		return;
	
	switch (cell->state)
	{
		case CELL_UNREVEALED:
		case CELL_FLAGGED:
			cell->state = CELL_QUESTION;
			break;
		
		case CELL_QUESTION:
			cell->state = CELL_UNREVEALED;
			break;
		
		default:
			break;
	}
}
Ejemplo n.º 9
0
Archivo: grid.c Proyecto: ershov/tmux
/* Convert cells into a string. */
char *
grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
    struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
{
	struct grid_cell	 gc;
	static struct grid_cell	 lastgc1;
	const char		*data;
	char			*buf, code[128];
	size_t			 len, off, size, codelen;
	u_int			 xx;
	const struct grid_line	*gl;

	if (lastgc != NULL && *lastgc == NULL) {
		memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
		*lastgc = &lastgc1;
	}

	len = 128;
	buf = xmalloc(len);
	off = 0;

	gl = grid_peek_line(gd, py);
	for (xx = px; xx < px + nx; xx++) {
		if (gl == NULL || xx >= gl->cellsize)
			break;
		grid_get_cell(gd, xx, py, &gc);
		if (gc.flags & GRID_FLAG_PADDING)
			continue;

		if (with_codes) {
			grid_string_cells_code(*lastgc, &gc, code, sizeof code,
			    escape_c0);
			codelen = strlen(code);
			memcpy(*lastgc, &gc, sizeof **lastgc);
		} else
			codelen = 0;

		data = gc.data.data;
		size = gc.data.size;
		if (escape_c0 && size == 1 && *data == '\\') {
			data = "\\\\";
			size = 2;
		}

		while (len < off + size + codelen + 1) {
			buf = xreallocarray(buf, 2, len);
			len *= 2;
		}

		if (codelen != 0) {
			memcpy(buf + off, code, codelen);
			off += codelen;
		}
		memcpy(buf + off, data, size);
		off += size;
	}

	if (trim) {
		while (off > 0 && buf[off - 1] == ' ')
			off--;
	}
	buf[off] = '\0';

	return (buf);
}
Ejemplo n.º 10
0
/* Get cell. */
void
grid_view_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
{
	grid_get_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc);
}
Ejemplo n.º 11
0
/* Get cell for writing. */
struct grid_cell *
grid_view_get_cell(struct grid *gd, u_int px, u_int py)
{
	return (grid_get_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py)));
}
Ejemplo n.º 12
0
int grid_is_bomb(struct grid_t *grid, int x, int y)
{
	struct cell_t *cell = grid_get_cell(grid, x, y);
	return cell && (cell->bomb == 1);
}
Ejemplo n.º 13
0
Archivo: grid.c Proyecto: UIKit0/mctrl
static LRESULT CALLBACK
grid_proc(HWND win, UINT msg, WPARAM wp, LPARAM lp)
{
    grid_t* grid = (grid_t*) GetWindowLongPtr(win, 0);

    switch(msg) {
        case WM_PAINT:
            return generic_paint(win, grid->no_redraw,
                                 (grid->style & MC_GS_DOUBLEBUFFER),
                                 grid_paint, grid);

        case WM_PRINTCLIENT:
            return generic_printclient(win, (HDC) wp, grid_paint, grid);

        case WM_NCPAINT:
            return generic_ncpaint(win, grid->theme_listview, (HRGN) wp);

        case WM_ERASEBKGND:
            return generic_erasebkgnd(win, grid->theme_listview, (HDC) wp);

        case MC_GM_GETTABLE:
            return (LRESULT) grid->table;

        case MC_GM_SETTABLE:
            return (grid_set_table(grid, (table_t*) lp) == 0 ? TRUE : FALSE);

        case MC_GM_GETCOLUMNCOUNT:
            return grid->col_count;

        case MC_GM_GETROWCOUNT:
            return grid->row_count;

        case MC_GM_RESIZE:
            return (grid_resize_table(grid, LOWORD(wp), HIWORD(wp)) == 0 ? TRUE : FALSE);

        case MC_GM_CLEAR:
            return (grid_clear(grid, wp) == 0 ? TRUE : FALSE);

        case MC_GM_SETCELLW:
        case MC_GM_SETCELLA:
            return (grid_set_cell(grid, LOWORD(wp), HIWORD(wp), (MC_TABLECELL*)lp,
                                  (msg == MC_GM_SETCELLW)) == 0 ? TRUE : FALSE);

        case MC_GM_GETCELLW:
        case MC_GM_GETCELLA:
            return (grid_get_cell(grid, LOWORD(wp), HIWORD(wp), (MC_TABLECELL*)lp,
                                  (msg == MC_GM_GETCELLW)) == 0 ? TRUE : FALSE);

        case MC_GM_SETGEOMETRY:
            return (grid_set_geometry(grid, (MC_GGEOMETRY*)lp, TRUE) == 0 ? TRUE : FALSE);

        case MC_GM_GETGEOMETRY:
            return (grid_get_geometry(grid, (MC_GGEOMETRY*)lp) == 0 ? TRUE : FALSE);

        case MC_GM_REDRAWCELLS:
            return (grid_redraw_cells(grid, LOWORD(wp), HIWORD(wp),
                                      LOWORD(lp), LOWORD(lp)) == 0 ? TRUE : FALSE);

        case MC_GM_SETCOLUMNWIDTH:
            return (grid_set_col_width(grid, wp, LOWORD(lp)) == 0 ? TRUE : FALSE);

        case MC_GM_GETCOLUMNWIDTH:
            return grid_get_col_width(grid, wp);

        case MC_GM_SETROWHEIGHT:
            return (grid_set_row_height(grid, wp, LOWORD(lp)) == 0 ? TRUE : FALSE);

        case MC_GM_GETROWHEIGHT:
            return grid_get_row_height(grid, wp);

        case WM_SETREDRAW:
            grid->no_redraw = !wp;
            return 0;

        case WM_VSCROLL:
        case WM_HSCROLL:
            grid_scroll(grid, (msg == WM_VSCROLL), LOWORD(wp), 1);
            return 0;

        case WM_MOUSEWHEEL:
        case WM_MOUSEHWHEEL:
            grid_mouse_wheel(grid, (msg == WM_MOUSEWHEEL), (int)(SHORT)HIWORD(wp));
            return 0;

        case WM_SIZE:
            if(!grid->no_redraw) {
                int old_scroll_x = grid->scroll_x;
                int old_scroll_y = grid->scroll_y;

                grid_setup_scrollbars(grid, FALSE);

                if(grid->scroll_x != old_scroll_x || grid->scroll_y != old_scroll_y)
                    InvalidateRect(win, NULL, TRUE);
            }
            return 0;

        case WM_GETFONT:
            return (LRESULT) grid->font;

        case WM_SETFONT:
            grid->font = (HFONT) wp;
            if((BOOL) lp  &&  !grid->no_redraw)
                InvalidateRect(win, NULL, TRUE);
            return 0;

        case WM_STYLECHANGED:
            if(wp == GWL_STYLE)
                grid_style_changed(grid, (STYLESTRUCT*) lp);
            break;

        case WM_THEMECHANGED:
            grid_close_theme(grid);
            grid_open_theme(grid);
            if(!grid->no_redraw)
                RedrawWindow(win, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE);
            return 0;

        case WM_SYSCOLORCHANGE:
            if(!grid->no_redraw)
                RedrawWindow(win, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE);
            return 0;

        case WM_NOTIFYFORMAT:
            if(lp == NF_REQUERY)
                grid_notify_format(grid);
            return (grid->unicode_notifications ? NFR_UNICODE : NFR_ANSI);

        case CCM_SETUNICODEFORMAT:
        {
            BOOL old = grid->unicode_notifications;
            grid->unicode_notifications = (wp != 0);
            return old;
        }

        case CCM_GETUNICODEFORMAT:
            return grid->unicode_notifications;

        case CCM_SETNOTIFYWINDOW:
        {
            HWND old = grid->notify_win;
            grid->notify_win = (wp ? (HWND) wp : GetAncestor(win, GA_PARENT));
            return (LRESULT) old;
        }

        case CCM_SETWINDOWTHEME:
            mcSetWindowTheme(win, (const WCHAR*) lp, NULL);
            return 0;

        case WM_NCCREATE:
            grid = grid_nccreate(win, (CREATESTRUCT*)lp);
            if(MC_ERR(grid == NULL))
                return FALSE;
            SetWindowLongPtr(win, 0, (LONG_PTR)grid);
            return TRUE;

        case WM_CREATE:
            return (grid_create(grid) == 0 ? 0 : -1);

        case WM_DESTROY:
            grid_destroy(grid);
            return 0;

        case WM_NCDESTROY:
            if(grid)
                grid_ncdestroy(grid);
            return 0;
    }

    return DefWindowProc(win, msg, wp, lp);
}