checkBoxComponent.cpp

00001 /*
00002   チェックボックスのコンポーネント
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "checkBoxComponent.h"
00008 
00009 
00010 DrawSurface* CheckBoxComponent::check_surface = NULL;
00011 
00012 
00013 CheckBoxComponent::CheckBoxComponent(TTF_Draw& ttf_obj)
00014   : ttf(ttf_obj), now_state(false), length(16), activated(false),
00015     text_color(White), box_back(Black), box_border(White),
00016     box_label(NULL), check_label(NULL), draw_label(NULL),
00017     inner_box(NULL), outer_box(NULL), box(NULL) {
00018   w = 0;
00019   h = 0;
00020   position = VXV::Grid(0, 0);
00021 }
00022 
00023 
00024 CheckBoxComponent::~CheckBoxComponent(void) {
00025   delete box_label;
00026   delete draw_label;
00027   delete check_label;
00028 
00029   delete box;
00030   delete inner_box;
00031   delete outer_box;
00032 }
00033 
00034 
00035 void CheckBoxComponent::setLabel(LabelComponent* label) {
00036   draw_label = label;
00037 }
00038 
00039 
00040 void CheckBoxComponent::setLength(int box_length) {
00041   length = box_length;
00042 }
00043 
00044 
00045 void CheckBoxComponent::setColor(unsigned long text,
00046                                  unsigned long back, unsigned long border) {
00047   text_color = text;
00048   box_back = back;
00049   box_border = border;
00050 }
00051 
00052 
00053 void CheckBoxComponent::activate(void) {
00054   if (activated) {
00055     return;
00056   }
00057 
00058   // チェックサーフェスの作成
00059   if (!check_surface) {
00060     Uint16 check_str[] = { 0x30ec, 0x0000 }; // レ
00061     check_surface = new DrawSurface(ttf.createText(check_str, length *2/3, Red,
00062                                                    Red -0x04000000), true);
00063   }
00064   check_label = new LabelComponent(check_surface);
00065 
00066   // ボックスサーフェスの作成
00067   outer_box = new FillSurface(VXV::Rect(length, length), box_border);
00068   inner_box = new FillSurface(VXV::Rect(length *4/5, length *4/5), box_back);
00069   box = new MultiSurface(VXV::Rect(length, length));
00070   box->addSurface(outer_box);
00071   box->addSurface(inner_box, VXV::Center | VXV::Middle);
00072   box_label = new LabelComponent(box);
00073   w = box->w * 3/2;
00074 
00075   if (draw_label) {
00076     w += draw_label->w;
00077     h += draw_label->h;
00078   }
00079   activated = true;
00080 }
00081 
00082 
00083 bool CheckBoxComponent::draw(unsigned long ticks, const UserInput& ui) {
00084   if (!activated) {
00085     return true;
00086   }
00087 
00088   // 外枠の描画
00089   VXV::Grid box_offset;
00090   if (draw_label) {
00091     box_offset = VXV::Grid(0, (draw_label->h - length)/2);
00092   }
00093   box_label->setPosition(position + box_offset);
00094   box_label->draw(ticks, ui);
00095 
00096   // 枠内がクリックされたかどうか
00097   int mx = ui.mpos.x;
00098   int my = ui.mpos.y;
00099   VXV::Rect judge_rect =
00100     VXV::Rect(position.x + box_offset.x, position.y + box_offset.y,
00101               position.x + box_offset.x + length,
00102               position.y + box_offset.y + length);
00103   if (ui.left_release &&
00104       (mx >= judge_rect.x) && (mx <= judge_rect.w) &&
00105       (my >= judge_rect.y) && (my <= judge_rect.h)) {
00106     now_state = !now_state;
00107   }
00108 
00109   // チェックの描画
00110   if (now_state) {
00111     VXV::Grid check_offset = VXV::Grid((box_label->w - check_label->w)/2,
00112                                        (box_label->h - check_label->h)/2 +1);
00113     VXV::Grid check_pos = position + box_offset + check_offset;
00114     check_label->setPosition(check_pos);
00115     check_label->draw(ticks, ui);
00116 
00117     check_label->setPosition(check_pos + VXV::Grid(1, 0));
00118     check_label->draw(ticks, ui);
00119   }
00120 
00121   // ラベルの描画
00122   if (draw_label) {
00123     draw_label->setPosition(position + VXV::Grid(length *3/2, 0));
00124     draw_label->draw(ticks, ui);
00125   }
00126   return true;
00127 }
00128 
00129 
00130 void CheckBoxComponent::beginFocus(void) {
00131 }
00132 
00133 
00134 void CheckBoxComponent::endFocus(void) {
00135 }
00136 
00137 
00138 bool CheckBoxComponent::getNowChecked(void) {
00139   return now_state;
00140 }
00141 
00142 
00143 void CheckBoxComponent::setNowChecked(bool on) {
00144   now_state = on;
00145 }
00146 

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