00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pent_include.h"
00020 #include "ButtonWidget.h"
00021 #include "TextWidget.h"
00022 #include "GameData.h"
00023 #include "ShapeFrame.h"
00024 #include "ShapeArchive.h"
00025 #include "Shape.h"
00026 #include "Mouse.h"
00027 #include "getObject.h"
00028
00029 #include "IDataSource.h"
00030 #include "ODataSource.h"
00031
00032
00033 DEFINE_RUNTIME_CLASSTYPE_CODE(ButtonWidget,Gump);
00034
00035 ButtonWidget::ButtonWidget()
00036 : Gump()
00037 {
00038 }
00039
00040 ButtonWidget::ButtonWidget(int X, int Y, std::string txt, bool gamefont,
00041 int font, uint32 mouseOverBlendCol_,
00042 int w, int h, sint32 layer) :
00043 Gump(X,Y,w,h,0,0,layer), shape_up(0), shape_down(0),
00044 mouseOver(false), origw(w), origh(h)
00045 {
00046 TextWidget* widget = new TextWidget(0,0,txt,gamefont,font,w,h);
00047 textwidget = widget->getObjId();
00048 mouseOverBlendCol = mouseOverBlendCol_;
00049 mouseOver = (mouseOverBlendCol != 0);
00050 }
00051
00052 ButtonWidget::ButtonWidget(int X, int Y, FrameID frame_up, FrameID frame_down,
00053 bool _mouseOver, sint32 layer)
00054 : Gump(X,Y,5,5,0,0,layer), textwidget(0), mouseOver(_mouseOver)
00055 {
00056 shape_up = GameData::get_instance()->getShape(frame_up);
00057 shape_down = GameData::get_instance()->getShape(frame_down);
00058 framenum_up = frame_up.framenum;
00059 framenum_down = frame_down.framenum;
00060 }
00061
00062
00063 ButtonWidget::~ButtonWidget(void)
00064 {
00065 }
00066
00067 void ButtonWidget::InitGump(Gump* newparent, bool take_focus)
00068 {
00069 Gump::InitGump(newparent, take_focus);
00070
00071 if (textwidget != 0) {
00072 Gump* widget = getGump(textwidget);
00073 assert(widget);
00074 widget->InitGump(this);
00075 widget->GetDims(dims);
00076 widget->Move(0,dims.y);
00077 } else {
00078 assert(shape_up != 0);
00079 assert(shape_down != 0);
00080
00081 shape = shape_up;
00082 framenum = framenum_up;
00083
00084 ShapeFrame* sf = shape->getFrame(framenum);
00085 assert(sf);
00086 dims.w = sf->width;
00087 dims.h = sf->height;
00088 }
00089 }
00090
00091 int ButtonWidget::getVlead()
00092 {
00093 if (textwidget != 0) {
00094 Gump* widget = getGump(textwidget);
00095 TextWidget* textwidget = p_dynamic_cast<TextWidget*>(widget);
00096 assert(textwidget);
00097 return textwidget->getVlead();
00098 } else {
00099 return 0;
00100 }
00101 }
00102
00103 bool ButtonWidget::PointOnGump(int mx, int my)
00104 {
00105
00106
00107
00108
00109
00110
00111 int gx = mx, gy = my;
00112 ParentToGump(gx,gy);
00113
00114 return dims.InRect(gx,gy);
00115 }
00116
00117 Gump *ButtonWidget::OnMouseDown(int button, int mx, int my)
00118 {
00119 Gump *ret = Gump::OnMouseDown(button,mx,my);
00120 if (ret) return ret;
00121 if (button == BUTTON_LEFT)
00122 {
00123
00124 if (!mouseOver) {
00125 shape = shape_down;
00126 framenum = framenum_down;
00127 }
00128 return this;
00129 }
00130 return 0;
00131 }
00132
00133 uint16 ButtonWidget::TraceObjId(int mx, int my)
00134 {
00135 if (PointOnGump(mx, my))
00136 return getObjId();
00137 else
00138 return 0;
00139 }
00140
00141
00142 void ButtonWidget::OnMouseUp(int button, int mx, int my)
00143 {
00144 if (button == BUTTON_LEFT) {
00145 if (!mouseOver) {
00146 shape = shape_up;
00147 framenum = framenum_up;
00148 }
00149 parent->ChildNotify(this,BUTTON_UP);
00150 }
00151 }
00152
00153 void ButtonWidget::OnMouseClick(int button, int mx, int my)
00154 {
00155 int gx = mx, gy = my;
00156 if (PointOnGump(gx, gy))
00157 parent->ChildNotify(this,BUTTON_CLICK);
00158 }
00159
00160 void ButtonWidget::OnMouseDouble(int button, int mx, int my)
00161 {
00162 parent->ChildNotify(this,BUTTON_DOUBLE);
00163 }
00164
00165 void ButtonWidget::OnMouseOver()
00166 {
00167 if (mouseOver) {
00168 if (textwidget) {
00169 Gump* widget = getGump(textwidget);
00170 TextWidget* textwidget = p_dynamic_cast<TextWidget*>(widget);
00171 assert(textwidget);
00172 textwidget->setBlendColour(mouseOverBlendCol);
00173 } else {
00174 shape = shape_down;
00175 framenum = framenum_down;
00176 }
00177 }
00178 }
00179
00180 void ButtonWidget::OnMouseLeft()
00181 {
00182 if (mouseOver) {
00183 if (textwidget) {
00184 Gump* widget = getGump(textwidget);
00185 TextWidget* textwidget = p_dynamic_cast<TextWidget*>(widget);
00186 assert(textwidget);
00187 textwidget->setBlendColour(0);
00188 } else {
00189 shape = shape_up;
00190 framenum = framenum_up;
00191 }
00192 }
00193 }
00194
00195 void ButtonWidget::saveData(ODataSource* ods)
00196 {
00197
00198 int w=0, h=0;
00199 if (textwidget != 0) {
00200 w = dims.w;
00201 h = dims.h;
00202 dims.w = origw;
00203 dims.h = origh;
00204 }
00205
00206 Gump::saveData(ods);
00207
00208
00209 if (textwidget != 0) {
00210 dims.w = w;
00211 dims.h = h;
00212 }
00213
00214 uint16 flex = 0;
00215 uint32 shapenum = 0;
00216 if (shape_up)
00217 {
00218 shape_up->getShapeId(flex, shapenum);
00219 }
00220 ods->write2(flex);
00221 ods->write4(shapenum);
00222 ods->write4(framenum_up);
00223
00224 flex = 0;
00225 shapenum = 0;
00226 if (shape_down)
00227 {
00228 shape_down->getShapeId(flex, shapenum);
00229 }
00230 ods->write2(flex);
00231 ods->write4(shapenum);
00232 ods->write4(framenum_down);
00233 ods->write2(textwidget);
00234 ods->write4(mouseOverBlendCol);
00235
00236 uint8 m = (mouseOver ? 1 : 0);
00237 ods->write1(m);
00238 }
00239
00240 bool ButtonWidget::loadData(IDataSource* ids, uint32 version)
00241 {
00242 if (!Gump::loadData(ids, version)) return false;
00243
00244 shape_up = 0;
00245 ShapeArchive * flex = GameData::get_instance()->getShapeFlex(ids->read2());
00246 uint32 shapenum = ids->read4();
00247 if (flex)
00248 {
00249 shape_up = flex->getShape(shapenum);
00250 }
00251 framenum_up = ids->read4();
00252
00253 shape_down = 0;
00254 flex = GameData::get_instance()->getShapeFlex(ids->read2());
00255 shapenum = ids->read4();
00256 if (flex)
00257 {
00258 shape_down = flex->getShape(shapenum);
00259 }
00260 framenum_down = ids->read4();
00261 textwidget = ids->read2();
00262 mouseOverBlendCol = ids->read4();
00263 mouseOver = (ids->read1() != 0);
00264
00265
00266 if (textwidget != 0) {
00267 Gump* widget = getGump(textwidget);
00268 widget->GetDims(dims);
00269 widget->Move(0,dims.y);
00270 }
00271
00272 return true;
00273 }
00274