//---------------------------------------------------//
/*
 *Copyright Bryce Thorup, William Thorup, BitJag 2014
 *
 * Contact: contact@bitjag.com
 *
 *All content developed by BitJag (graphics & Code) is
 *for your personal use only.  Feel free to burn it to a CD, 
 *flash to a SkunkBoard, or use in an emulator.  We 
 *hope you also take the chance to learn from it if needed.
 * 
 *Do not redistribute, duplicate, charge for "handling" 
 *or cost to print, or charge for profit.
 * 
 *If there is someone you know who wants a copy,
 *physical or digital, please direct them to our website.
 *Any other questions about the use of the demo, please
 *shoot us an email.  We will be liberal, promise.
 *
 * http://bitjag.com/
 * 
 *This demo was developed with the Removers Library,
 *which is used under the LGPL licence 2.1, included
 *with the source.  The Removers library is free to 
 *download and use at this location.
 * 
 * http://removers.free.fr/softs/download.php?lang=en
 *
//---------------------------------------------------*/

#include "bj_text.h"

Font* newFont(int height, int* char_widths, int* char_offsets, int graphic_width, phrase* graphic){
  
  Font* font = malloc(sizeof(Font));
  
  font->height = height;
  
  font->kerning = 0;
  
  font->line_spacing = 0;
  
  font->char_widths = char_widths;
  
  font->char_offsets = char_offsets;
  
  screen *char_screen = new_screen();
  
  set_simple_screen(DEPTH16, graphic_width, height, char_screen, graphic);
  
  font->graphic = char_screen;
  
  return font;
  
}

TextBox* newTextBox(char* text, int char_count, Font* font){
  
  TextBox* tb = malloc(sizeof(TextBox));
  
  tb->char_idx = 0;
  
  tb->type_rate = 10;
  
  tb->type_rate_counter = 10;
  
  tb->draw_pos_x = 0;
  
  tb->draw_pos_y = 0;
  
  tb->padding = 0;
  
  tb->char_count = char_count;
  
  tb->text = text;
  
  tb->font = font;
  
  return tb;
  
}

void generateDrawBoxForTextBox(int box_width, int box_height, TextBox* tb){
  
  screen* draw_box = new_screen();
  
  alloc_simple_screen(DEPTH16, box_width, box_height, draw_box);
  
  clear_screen(draw_box);
  
  tb->draw_box = draw_box;
  
}

void drawCharacter(TextBox* tb){
  
  int idx = tb->text[tb->char_idx] - 32;
    
  tb->font->graphic->x = tb->font->char_offsets[idx];
  
  tb->draw_box->x = tb->draw_pos_x;
  
  tb->draw_box->y = tb->draw_pos_y;

  fb2d_copy_straight(tb->font->graphic, tb->draw_box, tb->font->char_widths[idx], tb->font->height, MODE_TRANSPARENT);

  tb->draw_pos_x += tb->font->char_widths[idx] + tb->font->kerning;
  
}

void initTextBox(TextBox* tb){
  
  tb->char_idx = 0;
  
  tb->draw_pos_x = tb->padding;
  
  tb->draw_pos_y = tb->padding;
  
}

int processNewLine(TextBox* tb){
    
  if(tb->text[tb->char_idx] == '^'){
    
    tb->draw_pos_y += (tb->font->height + tb->font->line_spacing);
    
    tb->draw_pos_x = tb->padding;
    
    return 0;
    
  }
  
  return 1;
  
}

void processCharacter(TextBox* tb){
    
  if(tb->text[tb->char_idx] - 32 > -1){
  
    if(processNewLine(tb)){
      
      drawCharacter(tb);
      
    }
    
  }
  
}

int drawTextBoxTyped(TextBox* tb){
  
  if(tb->type_rate_counter > tb->type_rate){
    
    processCharacter(tb);
  
    tb->char_idx++;
    
    tb->type_rate_counter = 0;
    
  }
  
  tb->type_rate_counter++;
  
  if(tb->type_rate_counter >= tb->char_count){
    
    return 1;
    
  }
  
  else{
    
    return 0;
    
  }
  
}

void drawTextBoxAtOnce(TextBox* tb){
  
  initTextBox(tb);
  
  for(tb->char_idx = 0; tb->char_idx < tb->char_count; tb->char_idx++){
    
    processCharacter(tb);
    
  }
  
}
