00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "3d.h"
00020 #include "input.h"
00021 #include "controlEngine.h"
00022 #include "myList.h"
00023 #include "texture.h"
00024 #include "equations.h"
00025 #include "button.h"
00026
00027 extern ControlEngine ce;
00028
00029 Button::Button()
00030 {
00031
00032 name="Button";
00033 depth_min = DEFAULT_DEPTH_MIN_BUTTON;
00034 depth_max = DEFAULT_DEPTH_MAX_BUTTON;
00035 depth_cur = depth_max;
00036 timeOut = TIME_OUT_PUSH_DOWN_BUTTON;
00037 isPushed = FALSE;
00038 pushLikeMode = FALSE;
00039 allButtonMode = FALSE;
00040 materialOnUp.SetDS(0.6f,0.6f,0.6f);
00041 materialOnDown.SetDS(0.9f,0.9f,0.9f);
00042 materialCurrent = &materialOnUp;
00043
00044
00045 text.SetAlign(align_center,align_center);
00046
00047
00048
00049
00050 text.Setup(ce.fontTexture,ft_texture);
00051 text.material.SetDS(0,0,0);
00052
00053
00054 onControl.mouse.left.down.connect(this,OnPushDown);
00055 onControl.mouse.left.up.connect(this,OnPushUp);
00056 onControl.over.out.connect(this,OnOverOut);
00057 onControl.over.in.connect(this,OnOverIn);
00058 }
00059
00060 Button::~Button()
00061 {
00062 }
00063
00064
00065
00066 void Button::Create(double depth)
00067 {
00068
00069 SetParent();
00070 depth_max = depth;
00071 depth_cur = depth;
00072 faceNode = ce.primitive.AddBox(name,id,&depth_cur,FALSE);
00073 faceNode->Elems(FACE_FRONT)->text=&text;
00074 faceNode->elem->material=*materialCurrent;
00075 }
00076
00077
00078
00079 int Button::OnPushDown(StateMouse *mouse,int face)
00080 {
00081 DebugMouse2("Button::OnPushDown",mouse,face);
00082 ce.cursor.ApplyTypes(cursorPush,cursorArrow);
00083
00084 if (pushLikeMode)
00085 {
00086 SetPushToogle();
00087 onButton.checked(isPushed);
00088 return 0;
00089 }
00090 if (isPushed) timerPushDown.GetDeltaTime(TRUE);
00091 else
00092 {
00093 isPushed=TRUE;
00094 materialCurrent = &materialOnDown;
00095 faceNode->elem->material = *materialCurrent;
00096 BindTimer(timerPushDown,OnTimerPushDown,20);
00097 }
00098 return 0;
00099 }
00100
00101
00102
00103 int Button::OnTimerPushDown(Timer *timer)
00104 {
00105 Debug0Param("Button::OnTimerPushDown()");
00106 double time = timer->GetDeltaTime();
00107 double depth_delta = depth_max-depth_min;
00108
00109 if (time<timeOut) depth_cur=depth_min+Equ_1SURX(1.0-(time/timeOut))*depth_delta;
00110 else
00111 {
00112 depth_cur=depth_min;
00113 timer->Stop();
00114 }
00115 return 0;
00116 }
00117
00118
00119
00120 void Button::SetPush(BOOL enable)
00121 {
00122 isPushed=enable;
00123 depth_cur=isPushed?depth_min:depth_max;
00124 }
00125
00126 void Button::SetPushToogle()
00127 {
00128 SetPush(!isPushed);
00129 }
00130
00132 BOOL Button::GetPush()
00133 {
00134 return isPushed;
00135 }
00136
00137 int Button::OnPushUp(StateMouse *mouse,int face)
00138 {
00139 DebugMouse2("Button::OnPushUp",mouse,face);
00140 ce.cursor.ApplyTypes(cursorArrow,cursorPush);
00141 BOOL isPushedCopy = isPushed;
00142 if (!pushLikeMode) PushUp();
00143 if (isPushedCopy) onButton.click();
00144 return 0;
00145 }
00146
00148 void Button::ModePushLike(BOOL enable)
00149 {
00150 pushLikeMode = enable;
00151 }
00152
00154 void Button::ModeAllButtons(BOOL enable)
00155 {
00156 if (allButtonMode==enable) return;
00157 allButtonMode = enable;
00158 if (enable)
00159 {
00160 onControl.mouse.mid.down.connect(this,OnPushDown);
00161 onControl.mouse.mid.up.connect(this,OnPushUp);
00162 onControl.mouse.right.down.connect(this,OnPushDown);
00163 onControl.mouse.right.up.connect(this,OnPushUp);
00164 }
00165 else
00166 {
00167 onControl.mouse.mid.down.disconnect(this);
00168 onControl.mouse.mid.up.disconnect(this);
00169 onControl.mouse.right.down.disconnect(this);
00170 onControl.mouse.right.up.disconnect(this);
00171 }
00172 }
00173
00178 void Button::PushUp()
00179 {
00180 if (isPushed)
00181 {
00182 materialCurrent = &materialOnUp;
00183 faceNode->elem->material = *materialCurrent;
00184 timerPushDown.Stop();
00185 depth_cur=depth_max;
00186 isPushed=FALSE;
00187 }
00188 }
00189
00190
00191
00192 int Button::OnOverIn(Control *oldControl,int face)
00193 {
00194 Debug2Param("Button::OnOverIn(oldCtrl:%s,face:%d)",(char *)oldControl->name,face);
00195 ce.cursor.ApplyTypes(cursorArrow,cursorPush,cursorNothing,cursorNothing);
00196 return 0;
00197 }
00198
00199 int Button::OnOverOut(Control *newControl,int face)
00200 {
00201 Debug2Param("Button::OnOverOut(newCtrl:%s,face:%d)",(char *)newControl->name,face);
00202 if (!pushLikeMode) PushUp();
00203 return 0;
00204 }
00205
00206
00207
00208 void Button::operator=(char *newCaption)
00209 {
00210 text.SetText(newCaption);
00211 }
00212
00213 void Button::SetText(char *newCaption)
00214 {
00215 text.SetText(newCaption);
00216 }
00217
00218
00219
00220 void Button::ToString(MyString &str)
00221 {
00222 str.AddFormat("name: %s caption:%s",name,caption);
00223 }
00224
00225