GSDP:GAM100/CProcessing/textAlign()

From Inside
Jump to: navigation, search

textAlign()

Description

Sets the alignment for all text draw calls after this function is called. Different alignment types listed below.

Parameters

textAlign(CP_TEXT_ALIGN_HORIZONTAL h, CP_TEXT_ALIGN_VERTICAL v)

  • h - (CP_TEXT_ALIGN_HORIZONTAL) How to align the text horizontally.
  • v - (CP_TEXT_ALIGN_VERTICAL) How to align the text vertically.

Alignment Options

Horizontal

  • CP_ALIGN_H_LEFT - Aligns it so the starting point is to the left of the text and grows towards the right.
  • CP_ALIGN_H_CENTER - Aligns it so the starting point is the center of the text and it grows outward.
  • CP_ALIGN_H_RIGHT - Aligns it so the starting point is to the right of the text and grows towards the left.

Vertical

  • CP_ALIGN_V_TOP - Aligns it so that the starting point is above the first line of text.
  • CP_ALIGN_V_MIDDLE - Aligns it so the starting point is in the center (vertically) of first line of text
  • CP_ALIGN_V_BOTTOM - Aligns it so the starting point is on the bottom of the first line of text.

Example

void align_test_init()
{
  textSize(35);
}

int h_choice = 0;
int v_choice = 0;

const char* h_text[] = { "LEFT ", "CENTER ", "RIGHT " };
const char* v_text[] = { "TOP", "MIDDLE", "BOTTOM" };
void align_text_update()
{
  background(color(255, 255, 255, 255));

  // Draw the crosshairs
  fill(color(0, 0, 0, 255));
  line(canvasWidth / 2 - 75, canvasHeight / 2, canvasWidth / 2 + 75, canvasHeight / 2);
  line(canvasWidth / 2, canvasHeight / 2 - 75, canvasWidth / 2, canvasHeight / 2 + 75);

  // Lets user cycle through alignment choices
  if (keyPressed(KEY_LEFT))
    h_choice++;
  else if (keyPressed(KEY_RIGHT))
    h_choice--;

  if (keyPressed(KEY_DOWN))
    v_choice--;
  else if (keyPressed(KEY_UP))
    v_choice++;

  // Clamp the user choice to the available options
  h_choice = clampInt(h_choice, 0, 2);
  v_choice = clampInt(v_choice, 0, 2);

  // Set the alinment based on the choices
  CP_TEXT_ALIGN_HORIZONTAL h_alignment = CP_ALIGN_H_LEFT;
  switch (h_choice)
  {
  case 0 :
    h_alignment = CP_ALIGN_H_LEFT;
    break;
  case 1 :
    h_alignment = CP_ALIGN_H_CENTER;
    break;
  case 2:
    h_alignment = CP_ALIGN_H_RIGHT;
    break;
  }

  CP_TEXT_ALIGN_VERTICAL v_alignment = CP_ALIGN_V_TOP;
  switch (v_choice)
  {
  case 0:
    v_alignment = CP_ALIGN_V_TOP;
    break;
  case 1:
    v_alignment = CP_ALIGN_V_MIDDLE;
    break;
  case 2:
    v_alignment = CP_ALIGN_V_BOTTOM;
    break;
  }

  textAlign(h_alignment, v_alignment);

  // Create text to draw based on choices
  char align_text[16] = { 0 };
  strcat_s(align_text, 16, h_text[h_choice]);
  strcat_s(align_text, 16, v_text[v_choice]);

  // Draw the text to the screen
  fill(color(255, 0, 0, 255));
  text(align_text, canvasWidth / 2, canvasHeight / 2);

  // Draw text telling user what to do
  textAlign(CP_ALIGN_H_CENTER, CP_ALIGN_V_TOP);
  textBox("Use arrow keys to cycle through text alignments.", 0, 0, canvasWidth);
}

Related

Personal tools
Namespaces

Variants
Actions
Navigation
NameSpaces:>
Tools
Dynamic:>