/*--------------------------------------------------------------------------*
 Project:
 File: sys_TextWindow.h
 

*--------------------------------------------------------------------------*/
#ifndef _UJI_SYS_TEXT_WINDOW_H_
#define _UJI_SYS_TEXT_WINDOW_H_

#include <nn/gx.h>
#include "sys_Window.h"

namespace uji {
namespace sys {


// テキスト色属性（Color8の定数と対応）
enum ATTR_TEXT_COLOR
{
    ATTR_COLOR_BLACK,
    ATTR_COLOR_WHITE,
    ATTR_COLOR_RED,
    ATTR_COLOR_GREEN,        
    ATTR_COLOR_BLUE,
    ATTR_COLOR_YELLOW,        
    ATTR_COLOR_MAGENTA,
    ATTR_COLOR_CYAN,
    ATTR_COLOR_MAX
};


class TextWindow : public Window
{
public:
    // コンストラクタ・デストラクタ
    TextWindow(s32 width, s32 height, s32 fontHeight);
    virtual ~TextWindow();
        
    // ウインドウ（バッファへ）の文字出力
    void PutString(const char* str);
    void Printf(const char* format, ...);
    void VPrintf(const char* format, std::va_list vlist);

    // テキストカラー
    ATTR_TEXT_COLOR GetTextColor();
    ATTR_TEXT_COLOR SetTextColor(ATTR_TEXT_COLOR attr_color);
    
    // 文字出力位置
    void Gotoxy(s32 x, s32 y);
    Point Getxy();
        
    // テキストその他属性
    // TODO: 

    static const int WINDOW_WIDTH_SPACE     = 2;
    static const int WINDOW_HEIGHT_SPACE    = 2;
    
    static const u8 ATTRIBUTE_COLOR_MASK    = 0x0f;
    static const int ATTRIBUTE_COLOR_SHIFT  = 0;

private:
    virtual void OnDraw(uji::sys::GraphicsDrawing* gfx);
    virtual void OnUpdatePad(const uji::sys::Pad& pad);
    virtual bool OnCreate();
    virtual void OnDestroy();
     
    void ClearBuffer(); 
    Size GetWindowSize();
     
    void DrawLine(uji::sys::GraphicsDrawing* gfx, int line, int y); 
    unsigned long PutChar(const char* str, char* dstPtr, u8* pAttr);
     
    void NextLine();    
    char* GetTextPtr(int line, int xPos);
    u8* GetAttributePtr(int line, int xPos);   
    int GetLineCount();

    static const int m_DrawOffsetX = 4;
    static const int m_DrawOffsetY = 1;    
          
    u8      m_Attribute;            // 文字属性（下位4bit: 色）
    
    s32     m_BufferWidth;          // 横方向のバッファ
    s32     m_BufferHeight;         // 縦方向のバッファ
    u32     m_BufferSize;           // バッファのサイズ
    
    Point   m_CursorXY;             // 現在のテキスト表示位置    
    bool    m_CursorVisible;        // カーソルの可視化フラグ
    
    bool    m_AutoScrollFlag;       // 画面自動でスクロールさせるか
    
    char*   m_TextBuffer;           // テキストバッファのポインタ
    u8*     m_AttributeBuffer;      // 属性バッファのポインタ

    int     mWidth;                 // １ラインの文字数
    int     mHeight;    
    int     mLineMax;               // ウインドウに表示できるライン数

    int     mRingTop;
    int     mPrintTop;
    int     mPrintXPos;

    int     mY;
    s32     mCyChar;                // フォントの高さ
};


}   // usingnamespace sys
}   // usingnamespace uji
  

/* _UJI_SYS_TEXT_WINDOW_H_ */
#endif

