static int console_draw_string(ConsoleDrawContext *cdc, const char *str, int str_len,
                               const unsigned char fg[3], const unsigned char bg[3], const unsigned char bg_sel[4])
{
	int tot_lines;            /* total number of lines for wrapping */
	int *offsets;             /* offsets of line beginnings for wrapping */
	int y_next;

	str_len = console_wrap_offsets(str, str_len, cdc->console_width, &tot_lines, &offsets);
	y_next = cdc->xy[1] + cdc->lheight * tot_lines;

	/* just advance the height */
	if (cdc->draw == 0) {
		if (cdc->pos_pick && cdc->mval[1] != INT_MAX && cdc->xy[1] <= cdc->mval[1]) {
			if (y_next >= cdc->mval[1]) {
				int ofs = 0;

				/* wrap */
				if (tot_lines > 1) {
					int iofs = (int)((float)(y_next - cdc->mval[1]) / cdc->lheight);
					ofs += offsets[MIN2(iofs, tot_lines - 1)];
				}

				/* last part */
				ofs += txt_utf8_column_to_offset(str + ofs,
				                                 (int)floor((float)cdc->mval[0] / cdc->cwidth));

				CLAMP(ofs, 0, str_len);
				*cdc->pos_pick += str_len - ofs;
			}
			else
				*cdc->pos_pick += str_len + 1;
		}

		cdc->xy[1] = y_next;
		MEM_freeN(offsets);
		return 1;
	}
	else if (y_next < cdc->ymin) {
		/* have not reached the drawable area so don't break */
		cdc->xy[1] = y_next;

		/* adjust selection even if not drawing */
		if (cdc->sel[0] != cdc->sel[1]) {
			console_step_sel(cdc, -(str_len + 1));
		}

		MEM_freeN(offsets);
		return 1;
	}

	if (tot_lines > 1) { /* wrap? */
		const int initial_offset = offsets[tot_lines - 1];
		size_t len = str_len - initial_offset;
		const char *s = str + initial_offset;
		int i;
		
		int sel_orig[2];
		copy_v2_v2_int(sel_orig, cdc->sel);

		/* invert and swap for wrapping */
		cdc->sel[0] = str_len - sel_orig[1];
		cdc->sel[1] = str_len - sel_orig[0];
		
		if (bg) {
			glColor3ubv(bg);
			glRecti(0, cdc->xy[1], cdc->winx, (cdc->xy[1] + (cdc->lheight * tot_lines)));
		}

		glColor3ubv(fg);

		/* last part needs no clipping */
		BLF_position(cdc->font_id, cdc->xy[0], cdc->lofs + cdc->xy[1], 0);
		BLF_draw_mono(cdc->font_id, s, len, cdc->cwidth);

		if (cdc->sel[0] != cdc->sel[1]) {
			console_step_sel(cdc, -initial_offset);
			// glColor4ub(255, 0, 0, 96); // debug
			console_draw_sel(s, cdc->sel, cdc->xy, len, cdc->cwidth, cdc->lheight, bg_sel);
			glColor3ubv(fg);
		}

		cdc->xy[1] += cdc->lheight;

		for (i = tot_lines - 1; i > 0; i--) {
			len = offsets[i] - offsets[i - 1];
			s = str + offsets[i - 1];

			BLF_position(cdc->font_id, cdc->xy[0], cdc->lofs + cdc->xy[1], 0);
			BLF_draw_mono(cdc->font_id, s, len, cdc->cwidth);
			
			if (cdc->sel[0] != cdc->sel[1]) {
				console_step_sel(cdc, len);
				// glColor4ub(0, 255, 0, 96); // debug
				console_draw_sel(s, cdc->sel, cdc->xy, len, cdc->cwidth, cdc->lheight, bg_sel);
				glColor3ubv(fg);
			}

			cdc->xy[1] += cdc->lheight;
			
			/* check if were out of view bounds */
			if (cdc->xy[1] > cdc->ymax) {
				MEM_freeN(offsets);
				return 0;
			}
		}

		copy_v2_v2_int(cdc->sel, sel_orig);
		console_step_sel(cdc, -(str_len + 1));
	}
	else { /* simple, no wrap */

		if (bg) {
			glColor3ubv(bg);
			glRecti(0, cdc->xy[1], cdc->winx, cdc->xy[1] + cdc->lheight);
		}

		glColor3ubv(fg);

		BLF_position(cdc->font_id, cdc->xy[0], cdc->lofs + cdc->xy[1], 0);
		BLF_draw_mono(cdc->font_id, str, str_len, cdc->cwidth);
		
		if (cdc->sel[0] != cdc->sel[1]) {
			int isel[2];

			isel[0] = str_len - cdc->sel[1];
			isel[1] = str_len - cdc->sel[0];

			// glColor4ub(255, 255, 0, 96); // debug
			console_draw_sel(str, isel, cdc->xy, str_len, cdc->cwidth, cdc->lheight, bg_sel);
			console_step_sel(cdc, -(str_len + 1));
		}

		cdc->xy[1] += cdc->lheight;

		if (cdc->xy[1] > cdc->ymax) {
			MEM_freeN(offsets);
			return 0;
		}
	}

	MEM_freeN(offsets);
	return 1;
}
Example #2
0
static int console_draw_string(ConsoleDrawContext *cdc, const char *str, const int str_len,
                               const unsigned char *fg, const unsigned char *bg)
{
	int rct_ofs = cdc->lheight / 4;
	int tot_lines = (str_len / cdc->console_width) + 1; /* total number of lines for wrapping */
	int y_next = (str_len > cdc->console_width) ? cdc->xy[1] + cdc->lheight * tot_lines : cdc->xy[1] + cdc->lheight;
	const int mono = blf_mono_font;

	/* just advance the height */
	if (cdc->draw == 0) {
		if (cdc->pos_pick && (cdc->mval[1] != INT_MAX)) {
			if (cdc->xy[1] <= cdc->mval[1]) {
				if ((y_next >= cdc->mval[1])) {
					int ofs = (int)floor(((float)cdc->mval[0] / (float)cdc->cwidth));

					/* wrap */
					if (str_len > cdc->console_width)
						ofs += (cdc->console_width * ((int)((((float)(y_next - cdc->mval[1]) / (float)(y_next - cdc->xy[1])) * tot_lines))));
	
					CLAMP(ofs, 0, str_len);
					*cdc->pos_pick += str_len - ofs;
				}
				else
					*cdc->pos_pick += str_len + 1;
			}
		}

		cdc->xy[1] = y_next;
		return 1;
	}
	else if (y_next - cdc->lheight < cdc->ymin) {
		/* have not reached the drawable area so don't break */
		cdc->xy[1] = y_next;

		/* adjust selection even if not drawing */
		if (cdc->sel[0] != cdc->sel[1]) {
			console_step_sel(cdc, -(str_len + 1));
		}

		return 1;
	}

	if (tot_lines > 1) { /* wrap? */
		const int initial_offset = ((tot_lines - 1) * cdc->console_width);
		const char *line_stride = str + initial_offset;  /* advance to the last line and draw it first */
		
		int sel_orig[2];
		copy_v2_v2_int(sel_orig, cdc->sel);

		/* invert and swap for wrapping */
		cdc->sel[0] = str_len - sel_orig[1];
		cdc->sel[1] = str_len - sel_orig[0];
		
		if (bg) {
			glColor3ubv(bg);
			glRecti(0, cdc->xy[1] - rct_ofs, cdc->winx, (cdc->xy[1] + (cdc->lheight * tot_lines)) + rct_ofs);
		}

		glColor3ubv(fg);

		/* last part needs no clipping */
		BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
		BLF_draw(mono, line_stride, str_len - initial_offset);

		if (cdc->sel[0] != cdc->sel[1]) {
			console_step_sel(cdc, -initial_offset);
			// glColor4ub(255, 0, 0, 96); // debug
			console_draw_sel(cdc->sel, cdc->xy, str_len % cdc->console_width, cdc->cwidth, cdc->lheight);
			console_step_sel(cdc, cdc->console_width);
			glColor3ubv(fg);
		}

		cdc->xy[1] += cdc->lheight;

		line_stride -= cdc->console_width;
		
		for (; line_stride >= str; line_stride -= cdc->console_width) {
			BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
			BLF_draw(mono, line_stride, cdc->console_width);
			
			if (cdc->sel[0] != cdc->sel[1]) {
				// glColor4ub(0, 255, 0, 96); // debug
				console_draw_sel(cdc->sel, cdc->xy, cdc->console_width, cdc->cwidth, cdc->lheight);
				console_step_sel(cdc, cdc->console_width);
				glColor3ubv(fg);
			}

			cdc->xy[1] += cdc->lheight;
			
			/* check if were out of view bounds */
			if (cdc->xy[1] > cdc->ymax)
				return 0;
		}

		copy_v2_v2_int(cdc->sel, sel_orig);
		console_step_sel(cdc, -(str_len + 1));
	}
	else { /* simple, no wrap */

		if (bg) {
			glColor3ubv(bg);
			glRecti(0, cdc->xy[1] - rct_ofs, cdc->winx, cdc->xy[1] + cdc->lheight - rct_ofs);
		}

		glColor3ubv(fg);

		BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
		BLF_draw(mono, str, str_len);
		
		if (cdc->sel[0] != cdc->sel[1]) {
			int isel[2];

			isel[0] = str_len - cdc->sel[1];
			isel[1] = str_len - cdc->sel[0];

			// glColor4ub(255, 255, 0, 96); // debug
			console_draw_sel(isel, cdc->xy, str_len, cdc->cwidth, cdc->lheight);
			console_step_sel(cdc, -(str_len + 1));
		}

		cdc->xy[1] += cdc->lheight;

		if (cdc->xy[1] > cdc->ymax)
			return 0;
	}

	return 1;
}