scrollAreaComponent.cpp

00001 /*
00002   スクロールするテキスト表示
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 // \Todo 再描画が遅いようなら、Dra+---face はキャッシュ管理する
00008 
00009 #include "scrollAreaComponent.h"
00010 #include "drawSurface.h"
00011 #include "labelComponent.h"
00012 #include "detect_os.h"
00013 
00014 
00015 ScrollAreaComponent::ScrollAreaComponent(TTF_Draw& ttfObj,
00016                                          const VXV::Rect& drawRect)
00017   : ttf(ttfObj), px_size(16), draw_rect(drawRect) {
00018   w = draw_rect.w;
00019   h = draw_rect.h;
00020   position = VXV::Grid(draw_rect.x, draw_rect.y);
00021 
00022   set_color.fore = White;
00023   set_color.back = Gray4;
00024   clear();
00025 }
00026 
00027 
00028 ScrollAreaComponent::~ScrollAreaComponent(void) {
00029 }
00030 
00031 
00032 VXV::Grid ScrollAreaComponent::drawChar(draws_t& dch,
00033                                         const VXV::Grid& position,
00034                                         VXV::Grid& cursor) {
00035   Uint16 ch[] = { dch.ch, 0x0000 };
00036   DrawSurface* surface =
00037     new DrawSurface(ttf.createText(ch, px_size,
00038                                    dch.color.fore, dch.color.back), true);
00039   int width = surface->w;
00040   if (cursor.x + width <= draw_rect.w) {
00041     LabelComponent* label = new LabelComponent(surface);
00042     label->draw(position, VXV::Rect(0, 0, w, h), 1.0, cursor, false);
00043     delete label;
00044   }
00045   delete surface;
00046 
00047   return VXV::Grid(width, 0);
00048 }
00049 
00050 
00051 void ScrollAreaComponent::createEndl(VXV::Grid& cursor, int& first_endl,
00052                                      std::deque<draws_t>::iterator& it) {
00053   if (first_endl == InvalidEndl) {
00054     first_endl = static_cast<int>(it - texts.begin() +1);
00055   }
00056   cursor.x = 0;
00057   cursor.y += px_size;
00058 }
00059 
00060 
00061 bool ScrollAreaComponent::draw(unsigned long ticks, const UserInput& ui) {
00062 
00063   int first_endl = InvalidEndl;
00064   VXV::Grid cursor = VXV::Grid(0, 0);
00065   for (std::deque<draws_t>::iterator it = texts.begin();
00066        it != texts.end(); ++it) {
00067 
00068     // 下段での折り返し
00069     if ((cursor.y + px_size > draw_rect.h) && (first_endl != InvalidEndl)) {
00070       texts.erase(texts.begin(), texts.begin() + first_endl);
00071       break;
00072     }
00073 
00074     switch (it->ch) {
00075     case 0x00:
00076       break;
00077 
00078     case ' ':
00079       cursor.x += px_size >> 1;
00080       break;
00081 
00082       case '\t':
00083         // !!! 未実装
00084         break;
00085 
00086     case '\n':
00087       createEndl(cursor, first_endl, it);
00088       break;
00089 
00090     default:
00091       cursor += drawChar(*it, position, cursor);
00092       if (cursor.x > draw_rect.w) {
00093         createEndl(cursor, first_endl, it);
00094         --it;
00095       }
00096       break;
00097     }
00098   }
00099   return true;
00100 }
00101 
00102 
00103 void ScrollAreaComponent::setPxelSize(int pxelSize) {
00104   px_size = pxelSize;
00105 }
00106 
00107 
00108 void ScrollAreaComponent::setColor(unsigned long color, unsigned long back) {
00109   set_color.fore = color;
00110   set_color.back = back;
00111 }
00112 
00113 
00114 void ScrollAreaComponent::setCh(const Uint16 ch, int width) {
00115   draws_t dch;
00116   dch.ch = ch;
00117   dch.width = width;
00118   dch.color = set_color;
00119   texts.push_back(dch);
00120 }
00121 
00122 
00123 int ScrollAreaComponent::printf(const char *fmt, va_list ap) {
00124   enum { BufferMax = 1024 };
00125   char buffer[BufferMax];
00126 
00127 #ifndef MSC
00128   int n = vsnprintf(buffer, BufferMax, fmt, ap);
00129 #else
00130   int n = _vsnprintf(buffer, BufferMax, fmt, ap);
00131 #endif
00132   va_end(ap);
00133   for (int i = 0; i < n; ++i) {
00134     Uint16 ch = buffer[i];
00135     setCh(ch);
00136   }
00137   return n;
00138 }
00139 
00140 
00141 int ScrollAreaComponent::printf(const char *fmt, ...) {
00142   va_list ap;
00143   va_start(ap, fmt);
00144   return printf(fmt, ap);
00145 }
00146 
00147 
00148 void ScrollAreaComponent::drawUnicode(const Uint16* text) {
00149   // !!! 未実装
00150   // !!! width = 2 で、1文字ずつ送る
00151 }
00152 
00153 
00154 void ScrollAreaComponent::clear(void) {
00155   texts.clear();
00156 }
00157 

Generated on Mon Apr 13 22:52:02 2009 by  doxygen 1.5.7.1