GUIButton.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. #include "GUIButton.h"
  2. #include "GUIWidget.h"
  3. #include "GUISlider.h"
  4. #include "../utils/InterfaceUtils.h"
  5. #include "../../..\common_h/gui/controls/gui_helper.h"
  6. #include "BaseGUICursor.h"
  7. const float press_delay = 0.10f;
  8. const float repeat_delay = 0.01f;
  9. static inline float SC2UIRC(float t)
  10. {
  11. return InterfaceUtils::ScreenCoord2UIRectCoord(t);
  12. }
  13. static inline float SS2UIRS(float t)
  14. {
  15. return InterfaceUtils::ScreenSize2UIRectSize(t);
  16. }
  17. GUIButton::GUIButton(void)
  18. {
  19. m_restart = false;
  20. m_staticWidget = null;
  21. m_selectedWidget = null;
  22. m_normalWidget = null;
  23. m_focusedWidget = null;
  24. m_pressedWidget = null;
  25. m_disabledWidget = null;
  26. m_tooltipWidget = null;
  27. m_wasPressed = false;
  28. m_wasReleased = true;
  29. m_activeWidget = null;
  30. m_doNotCheckFocusChange = false;
  31. m_focusTime = 0.0f;
  32. m_validateHash = -1;
  33. // drawLevel = ML_GUI2;
  34. drawLevel = ML_GUI1;
  35. drawPriority = 0;
  36. m_mouseOver = false;
  37. m_wasPressed = false;
  38. m_wasReleased = false;
  39. m_repeat = false;
  40. m_pressDelay = 0.0f;
  41. }
  42. GUIButton::~GUIButton(void)
  43. {
  44. if( EditMode_IsOn())
  45. {
  46. static ConstString empty("");
  47. if( m_staticWidget ) m_staticWidget->SetParent(empty);
  48. if( m_selectedWidget ) m_selectedWidget->SetParent(empty);
  49. if( m_normalWidget ) m_normalWidget->SetParent(empty);
  50. if( m_focusedWidget ) m_focusedWidget->SetParent(empty);
  51. if( m_pressedWidget ) m_pressedWidget->SetParent(empty);
  52. if( m_disabledWidget ) m_disabledWidget->SetParent(empty);
  53. }
  54. if( BaseGUIElement *parent = GetParent())
  55. parent->UnRegister(this);
  56. }
  57. void GUIButton::Restart()
  58. {
  59. if( HaveFocus())
  60. SetElementInFocus(null);
  61. m_restart = true;
  62. ReCreate();
  63. }
  64. void GUIButton::SkipMOPs(MOPReader& reader)
  65. {
  66. reader.Float(); // width
  67. reader.Float(); // height
  68. m_params.description = reader.LocString();
  69. reader.String().c_str(); // desc object
  70. reader.String().c_str(); // static widget
  71. reader.String().c_str(); // selected widget
  72. reader.String().c_str(); // normal widget
  73. reader.String().c_str(); // focused widget
  74. reader.String().c_str(); // pressed widget
  75. reader.String().c_str(); // disabled widget
  76. reader.Float(); // press x offset
  77. reader.Float(); // press y offset
  78. //// links ////
  79. dword count = reader.Array();
  80. for( dword i = 0 ; i < count ; i++ )
  81. {
  82. reader.String().c_str(); // control alias
  83. reader.String().c_str(); // object
  84. }
  85. //// hotkeys ////
  86. count = reader.Array();
  87. for( dword i = 0 ; i < count ; i++ )
  88. {
  89. reader.String().c_str(); // hotkey alias
  90. reader.Bool(); // on focus only
  91. }
  92. ////////////////
  93. reader.Bool(); // action by release
  94. m_params.buttonEvent.Init(reader);
  95. // m_params.inactiEvent.Init(reader);
  96. reader.Bool(); // autorepeat
  97. reader.Bool(); // autofocus
  98. onGetFocus.Init(reader);
  99. onLosFocus.Init(reader);
  100. reader.Bool(); // initially focused
  101. m_state = stNormal;
  102. reader.Bool(); // focus by press
  103. drawPriority = reader.Long();
  104. Activate(reader.Bool());
  105. nativeShow = reader.Bool();
  106. m_activeWidget = null;
  107. SetState(stNormal);
  108. m_pressed = false;
  109. m_wasPressed = false;
  110. m_wasReleased = false;
  111. m_repeat = false;
  112. m_pressDelay = 0.0f;
  113. }
  114. void GUIButton::ReadMOPs(MOPReader& reader)
  115. {
  116. float width = reader.Float();
  117. float height = reader.Float();
  118. if( !Restricted(preserveSize))
  119. SizeTo(width,height);
  120. m_natSize.w = width;
  121. m_natSize.h = height;
  122. m_params.description = reader.LocString();
  123. m_params.descObject = reader.String();
  124. m_params.staticWidget = reader.String();
  125. m_params.selectedButtonWidget = reader.String();
  126. m_params.normalButtonWidget = reader.String();
  127. m_params.focusButtonWidget = reader.String();
  128. m_params.pressedButtonWidget = reader.String();
  129. m_params.disabledButtonWidget = reader.String();
  130. m_staticWidget = null;
  131. m_pressedWidget = null;
  132. m_focusedWidget = null;
  133. m_disabledWidget = null;
  134. m_selectedWidget = null;
  135. m_normalWidget = null;
  136. m_params.pressXoffset = reader.Float();
  137. m_params.pressYoffset = reader.Float();
  138. m_params.links.DelAll();
  139. dword count = reader.Array();
  140. m_params.links.AddElements(count);
  141. for( dword i = 0 ; i < count ; i++ )
  142. {
  143. Params::NavigationLink & link = m_params.links[i];
  144. link.control = reader.String().c_str();
  145. link.object = reader.String();
  146. }
  147. m_params.hotKeys.DelAll();
  148. count = reader.Array();
  149. Params::HotKey hotkey;
  150. for( dword i = 0 ; i < count ; i++ )
  151. {
  152. hotkey.hotKey = reader.String().c_str();
  153. hotkey.onlyInFocus = reader.Bool();
  154. m_params.hotKeys.Add(hotkey);
  155. }
  156. m_release = reader.Bool();
  157. m_params.buttonEvent.Init(reader);
  158. // m_params.inactiEvent.Init(reader);
  159. m_autorepeat = reader.Bool();
  160. m_autofocus = reader.Bool();
  161. if( m_release ) // в режиме срабатывания по отжатию
  162. {
  163. if( m_autorepeat )
  164. {
  165. LogicDebugError("Режим срабатывания по отжатию, автоповтор невозможен");
  166. m_autorepeat = false; // автоповтор невозможен
  167. }
  168. }
  169. onGetFocus.Init(reader);
  170. onLosFocus.Init(reader);
  171. m_params.initiallyFocused = reader.Bool();
  172. m_state = stNormal;
  173. /* if( m_staticWidget ) m_staticWidget->Show(false);
  174. if( m_pressedWidget ) m_pressedWidget->Show(false);
  175. if( m_focusedWidget ) m_focusedWidget->Show(false);
  176. if( m_disabledWidget ) m_disabledWidget->Show(false);
  177. if( m_selectedWidget ) m_selectedWidget->Show(false);
  178. if( m_normalWidget ) m_normalWidget->Show(false);*/
  179. m_activeWidget = m_normalWidget;
  180. m_focusByPress = reader.Bool();
  181. drawPriority = reader.Long();
  182. Activate(reader.Bool());
  183. // Show (reader.Bool());
  184. nativeShow = reader.Bool();
  185. m_activeWidget = null;
  186. SetState(stNormal);
  187. m_pressed = false;
  188. m_cursor = null;
  189. m_cursorHash = -1;
  190. m_wasPressed = false;
  191. m_wasReleased = false;
  192. m_repeat = false;
  193. m_pressDelay = 0.0f;
  194. /* ////
  195. if( !m_cursor )
  196. m_cursor = (BaseGUICursor *)FindObject(InterfaceUtils::GetCursorName());
  197. m_baseCursor = m_cursor;*/
  198. }
  199. void GUIButton::SetDisabled()
  200. {
  201. /* GUIWidget *p = m_disabledWidget;
  202. if( m_staticWidget && m_staticWidget != p ) m_staticWidget->Show(false);
  203. if( m_normalWidget && m_normalWidget != p ) m_normalWidget->Show(false);
  204. if( m_pressedWidget && m_pressedWidget != p ) m_pressedWidget->Show(false);
  205. if( m_focusedWidget && m_focusedWidget != p ) m_focusedWidget->Show(false);
  206. if( m_disabledWidget ) m_disabledWidget->Show(true);*/
  207. SetState(stDisabled);
  208. GUIWidget *p = m_disabledWidget;
  209. // if( m_activeWidget != p )
  210. {
  211. if( m_activeWidget && m_activeWidget != p )
  212. m_activeWidget->Show(false);
  213. if( p && (p != m_activeWidget || !p->IsShow()))
  214. {
  215. m_activeWidget = p; p->Show(true);
  216. }
  217. }
  218. }
  219. void GUIButton::SetFocused()
  220. {
  221. // if( GetElementInFocus() == this )
  222. // return;
  223. bool newFocus = (GetElementInFocus() != this);
  224. if( newFocus )
  225. {
  226. if( GetElementInFocus())
  227. GetElementInFocus()->OnLooseFocus();
  228. SetElementInFocus(this);
  229. }
  230. /* GUIWidget *p = m_focusedWidget;
  231. if( m_staticWidget && m_staticWidget != p ) m_staticWidget->Show(false);
  232. if( m_normalWidget && m_normalWidget != p ) m_normalWidget->Show(false);
  233. if( m_pressedWidget && m_pressedWidget != p ) m_pressedWidget->Show(false);
  234. if( m_disabledWidget && m_disabledWidget != p ) m_disabledWidget->Show(false);
  235. if( m_focusedWidget ) m_focusedWidget->Show(true);*/
  236. if( m_state != stFocused && newFocus )
  237. {
  238. // api->Trace("ON GET FOCUS: %s",GetObjectID().c_str());
  239. LogicDebug("Button getFocus trigger");
  240. onGetFocus.Activate(Mission(),false);
  241. SetState(stFocused);
  242. if( m_tooltipWidget )
  243. {
  244. const char *param[1];
  245. param[0] = m_params.description;
  246. m_tooltipWidget->Command("SetString",1,param);
  247. m_tooltipWidget->Show(true);
  248. }
  249. /* GUIWidget *p = m_focusedWidget;
  250. // api->Trace("\n %u %u\n",m_activeWidget,p);
  251. if( m_activeWidget != p )
  252. {
  253. if( m_activeWidget )
  254. m_activeWidget->Show(false);
  255. if( p )
  256. {
  257. m_activeWidget = p; p->Show(true);
  258. }
  259. }*/
  260. NotifyParent(this,ChildSelected);
  261. if( m_cursor && !(m_cursor->IsMoved() || m_cursor->IsClicked()))
  262. SetCursor();
  263. }
  264. GUIWidget *p = m_focusedWidget;
  265. // api->Trace("\n %u %u\n",m_activeWidget,p);
  266. // if( m_activeWidget != p )
  267. {
  268. if( m_activeWidget && m_activeWidget != p )
  269. m_activeWidget->Show(false);
  270. if( p && (p != m_activeWidget || !p->IsShow()))
  271. {
  272. m_activeWidget = p; p->Show(true);
  273. }
  274. }
  275. }
  276. void GUIButton::SetNormal(bool reset)
  277. {
  278. if( !IsActive())
  279. {
  280. SetDisabled();
  281. return;
  282. }
  283. GUIWidget *p = m_normalWidget;
  284. //// !!!УБРАТЬ ЭТО ЗЛО К Ч МАТЕРИ!!! ////
  285. if( m_staticWidget && m_staticWidget != p && m_staticWidget->IsShow()) m_staticWidget->Show(false);
  286. if( m_pressedWidget && m_pressedWidget != p && m_pressedWidget->IsShow()) m_pressedWidget->Show(false);
  287. if( m_focusedWidget && m_focusedWidget != p && m_focusedWidget->IsShow()) m_focusedWidget->Show(false);
  288. if( m_disabledWidget && m_disabledWidget != p && m_disabledWidget->IsShow()) m_disabledWidget->Show(false);
  289. if( m_selectedWidget && m_selectedWidget != p && m_selectedWidget->IsShow()) m_selectedWidget->Show(false);
  290. /////////////////////////////////////////
  291. if( reset )
  292. m_activeWidget = null;
  293. SetState(stNormal);
  294. // if( m_activeWidget != p )
  295. {
  296. if( m_activeWidget && m_activeWidget != p && m_activeWidget->IsShow())
  297. m_activeWidget->Show(false);
  298. if( p && (p != m_activeWidget || !p->IsShow()))
  299. {
  300. m_activeWidget = p; p->Show(true);
  301. }
  302. }
  303. }
  304. void GUIButton::SetSelected()
  305. {
  306. SetState(stNormal);
  307. GUIWidget *p = m_selectedWidget;
  308. // if( m_activeWidget != p )
  309. {
  310. if( m_activeWidget && m_activeWidget != p )
  311. m_activeWidget->Show(false);
  312. if( p && (p != m_activeWidget || !p->IsShow()))
  313. {
  314. m_activeWidget = p; p->Show(true);
  315. }
  316. }
  317. }
  318. void GUIButton::SetPressed()
  319. {
  320. /* GUIWidget *p = m_pressedWidget;
  321. if( m_staticWidget && m_staticWidget != p ) m_staticWidget->Show(false);
  322. if( m_normalWidget && m_normalWidget != p ) m_normalWidget->Show(false);
  323. if( m_focusedWidget && m_focusedWidget != p ) m_focusedWidget->Show(false);
  324. if( m_disabledWidget && m_disabledWidget != p ) m_disabledWidget->Show(false);
  325. if( m_pressedWidget )
  326. {
  327. m_pressedWidget->Show(true);
  328. m_pressedWidget->SetFrameOffset(m_params.pressXoffset, m_params.pressYoffset);
  329. }*/
  330. SetState(stPressed);
  331. GUIWidget *p = m_pressedWidget;
  332. // if( m_activeWidget != p )
  333. {
  334. if( m_activeWidget && m_activeWidget != p )
  335. m_activeWidget->Show(false);
  336. if( p && (p != m_activeWidget || !p->IsShow()))
  337. {
  338. m_activeWidget = p; p->Show(true);
  339. }
  340. }
  341. m_pressTime = press_delay;
  342. }
  343. void GUIButton::UnRegister(BaseGUIElement* object)
  344. {
  345. BaseGUIElement::UnRegister(object);
  346. if( object == m_staticWidget ) m_staticWidget = null;
  347. if( object == m_selectedWidget ) m_selectedWidget = null;
  348. if( object == m_normalWidget ) m_normalWidget = null;
  349. if( object == m_focusedWidget ) m_focusedWidget = null;
  350. if( object == m_pressedWidget ) m_pressedWidget = null;
  351. if( object == m_disabledWidget ) m_disabledWidget = null;
  352. if( object == m_tooltipWidget ) m_tooltipWidget = null;
  353. }
  354. void GUIButton::OnHotKey()
  355. {
  356. SetFocused();
  357. SetPressed(); m_wasPressed = true;
  358. }
  359. bool GUIButton::GoToLink(const char *control)
  360. {
  361. for( dword i = 0 ; i < m_params.links.Size() ; i++ )
  362. {
  363. if( string::IsEqual(m_params.links[i].control,control))
  364. {
  365. MissionObject *mo = FindObject(m_params.links[i].object);
  366. if( mo && mo->IsShow())
  367. {
  368. if( mo->Is(InterfaceUtils::GetButtonId()))
  369. {
  370. GUIButton *p = (GUIButton *)mo;
  371. if( p->IsActive() && p->ParentIsActive())
  372. {
  373. p->OnAcquireFocus();
  374. for( unsigned int k = 0 ; k < m_params.hotKeys.Size() ; ++k )
  375. if( string::IsEqual(m_params.links[i].control,m_params.hotKeys[k].hotKey))
  376. OnHotKey(),k = m_params.hotKeys.Size();
  377. return true;
  378. }
  379. else
  380. {
  381. return p->GoToLink(control);
  382. }
  383. }
  384. else
  385. if( mo->Is(InterfaceUtils::GetSliderId()))
  386. {
  387. GUISlider *p = (GUISlider *)mo;
  388. if( p->IsActive() && p->ParentIsActive())
  389. {
  390. p->OnAcquireFocus();
  391. for( unsigned int k = 0 ; k < m_params.hotKeys.Size() ; ++k )
  392. if( string::IsEqual(m_params.links[i].control,m_params.hotKeys[k].hotKey))
  393. OnHotKey(),k = m_params.hotKeys.Size();
  394. return true;
  395. }
  396. else
  397. {
  398. return p->GoToLink(control);
  399. }
  400. }
  401. }
  402. }
  403. }
  404. return false;
  405. }
  406. bool GUIButton::ProcessKeyboard()
  407. {
  408. bool wasKeyControl = false; // было ли управление клавишами
  409. if( EditMode_IsOn() || !IsActive() || !ParentIsActive())
  410. return wasKeyControl;
  411. ControlStateType state;
  412. // проверка хоткеев
  413. if( !m_doNotCheckFocusChange )
  414. {
  415. for( dword i = 0 ; i < m_params.hotKeys.Size() ; i++ )
  416. {
  417. state = Controls().GetControlStateType(m_params.hotKeys[i].hotKey);
  418. if( state == CST_ACTIVATED &&
  419. ((m_params.hotKeys[i].onlyInFocus && HaveFocus()) || !m_params.hotKeys[i].onlyInFocus))
  420. {
  421. // if( IsActive())
  422. // OnHotKey();
  423. if( IsActive())
  424. {
  425. if( m_autofocus || m_focusByPress )
  426. SetFocused();
  427. SetPressed(); m_wasPressed = true;
  428. }
  429. wasKeyControl = true; m_pressed = true;
  430. break;
  431. }
  432. if( state == CST_INACTIVATED )
  433. m_pressed = false;
  434. if( state == CST_ACTIVE && HaveFocus() && m_pressed )
  435. {
  436. if( IsActive())
  437. SetPressed();
  438. wasKeyControl = true;
  439. if( m_autorepeat )
  440. m_pressDelay += api->GetDeltaTime();
  441. }
  442. // else
  443. // m_pressDelay = 0.0f;
  444. }
  445. }
  446. // если в фокусе и фокус получен не в этом кадре - то проверка перехода фокуса на другой элемент
  447. if( HaveFocus() && !m_doNotCheckFocusChange )
  448. {
  449. for( dword i = 0 ; i < m_params.links.Size() ; i++ )
  450. {
  451. if( Controls().GetControlStateType(m_params.links[i].control) == CST_ACTIVATED ||
  452. m_params.links[i].activeTime > 0.2f )
  453. {
  454. MissionObject *mo = FindObject(m_params.links[i].object);
  455. // if( mo && mo->Is(InterfaceUtils::GetBaseId()) && mo->IsShow())
  456. if( mo && mo->IsShow())
  457. {
  458. /* static_cast<BaseGUIElement*>(mo)->OnAcquireFocus();
  459. for( unsigned int k = 0 ; k < m_params.hotKeys.Size() ; ++k )
  460. if( string::IsEqual(m_params.links[i].control,m_params.hotKeys[k].hotKey))
  461. OnHotKey(),k = m_params.hotKeys.Size();
  462. wasKeyControl = true;
  463. return wasKeyControl;*/
  464. if( mo->Is(InterfaceUtils::GetButtonId()))
  465. {
  466. GUIButton *p = (GUIButton *)mo;
  467. if( p->IsActive() && p->ParentIsActive())
  468. {
  469. p->OnAcquireFocus();
  470. for( unsigned int k = 0 ; k < m_params.hotKeys.Size() ; ++k )
  471. if( string::IsEqual(m_params.links[i].control,m_params.hotKeys[k].hotKey))
  472. OnHotKey(),k = m_params.hotKeys.Size();
  473. wasKeyControl = true;
  474. return wasKeyControl;
  475. }
  476. else
  477. {
  478. wasKeyControl = p->GoToLink(m_params.links[i].control);
  479. return wasKeyControl;
  480. }
  481. }
  482. else
  483. if( mo->Is(InterfaceUtils::GetSliderId()))
  484. {
  485. GUISlider *p = (GUISlider *)mo;
  486. if( p->IsActive() && p->ParentIsActive())
  487. {
  488. p->OnAcquireFocus();
  489. for( unsigned int k = 0 ; k < m_params.hotKeys.Size() ; ++k )
  490. if( string::IsEqual(m_params.links[i].control,m_params.hotKeys[k].hotKey))
  491. OnHotKey(),k = m_params.hotKeys.Size();
  492. wasKeyControl = true;
  493. return wasKeyControl;
  494. }
  495. else
  496. {
  497. wasKeyControl = p->GoToLink(m_params.links[i].control);
  498. return wasKeyControl;
  499. }
  500. }
  501. }
  502. }
  503. }
  504. }
  505. m_wasPressed = wasKeyControl;
  506. return wasKeyControl;
  507. }
  508. void GUIButton::ProcessMouse()
  509. {
  510. if( Controls().Locked())
  511. return;
  512. if( EditMode_IsOn() || !IsActive() || !ParentIsActive())
  513. return;
  514. // if( !m_cursor )
  515. // m_cursor = (BaseGUICursor *)FindObject(InterfaceUtils::GetCursorName());
  516. if( !m_cursor || m_cursor->IsHided())
  517. return;
  518. float x,y; m_cursor->GetPosition(x,y);
  519. Rect rect; GetParentClipRect(rect);
  520. if( !PointInRect(x*100,y*100,rect))
  521. return;
  522. x *= Render().GetScreenInfo2D().dwWidth;
  523. y *= Render().GetScreenInfo2D().dwHeight;
  524. // проверка мыши
  525. long mouseX, mouseY;
  526. // mouseX = (long)Controls().GetControlStateFloat("MouseH");
  527. // mouseY = (long)Controls().GetControlStateFloat("MouseV");
  528. mouseX = (long)x;
  529. mouseY = (long)y;
  530. // bool lbPressed = Controls().GetControlStateBool("LeftMouseButton");
  531. ControlStateType state = Controls().GetControlStateType("LeftMouseButton");
  532. Rect r; GetRect(r);
  533. float left = SC2UIRC(GetX()/100.0f)*Render().GetViewport().Width + Render().GetViewport().X;
  534. float right = left + SS2UIRS((r.r - r.l)/100.0f)*Render().GetViewport().Width;
  535. float top = SC2UIRC(GetY()/100.0f)*Render().GetViewport().Height + Render().GetViewport().Y;
  536. float bottom = top + SS2UIRS((r.b - r.t)/100.0f)*Render().GetViewport().Height;
  537. if( mouseX >= left && mouseX <= right &&
  538. mouseY >= top && mouseY <= bottom )
  539. {
  540. // if( IsShow() && IsActive())
  541. // api->Trace("MOUSE OVER: %s",GetObjectID().c_str());
  542. m_mouseOver = true;
  543. if( state == CST_ACTIVATED )
  544. {
  545. m_pressed = true;
  546. if( m_autofocus || m_focusByPress )
  547. SetFocused();
  548. }
  549. if( state == CST_INACTIVATED )
  550. m_pressed = false;
  551. // if( lbPressed )
  552. if( state == CST_ACTIVE && m_pressed )
  553. {
  554. if( IsActive())
  555. SetPressed();
  556. m_wasPressed = true;
  557. if( m_autorepeat )
  558. m_pressDelay += api->GetDeltaTime();
  559. }
  560. else
  561. {
  562. if( m_autofocus && m_cursor && (m_cursor->IsMoved() || m_state != stFocused && m_cursor->IsClicked()))
  563. SetFocused();
  564. if( !m_cursor->IsMoved())
  565. m_mouseOver = false;
  566. m_wasPressed = false;
  567. }
  568. if( m_state == stNormal )
  569. SetSelected();
  570. }
  571. else
  572. {
  573. m_mouseOver = false;
  574. if( IsActive())
  575. {
  576. if( !HaveFocus())
  577. {
  578. SetNormal();
  579. m_wasPressed = false;
  580. }
  581. else
  582. {
  583. SetFocused();
  584. m_wasPressed = false;
  585. }
  586. }
  587. else
  588. {
  589. SetDisabled();
  590. m_wasPressed = false;
  591. }
  592. m_pressed = false;
  593. }
  594. /*
  595. // if( m_state == stNormal )
  596. {
  597. if( m_mouseOver )
  598. SetSelected();
  599. else
  600. SetNormal();
  601. }*/
  602. }
  603. #ifdef not_def
  604. void _cdecl GUIButton::Work(float deltaTime, long level)
  605. {
  606. /* if( !IsActive())
  607. {
  608. SetDisabled();
  609. return;
  610. }*/
  611. ControlStateType state;
  612. for( dword i = 0 ; i < m_params.links.Size() ; i++ )
  613. {
  614. state = Controls().GetControlStateType(m_params.links[i].control);
  615. if( state == CST_ACTIVE ||
  616. state == CST_ACTIVATED )
  617. m_params.links[i].activeTime += deltaTime;
  618. else
  619. m_params.links[i].activeTime = 0.0f;
  620. }
  621. if( !ProcessKeyboard())
  622. ProcessMouse();
  623. // if( !m_wasReleased && !m_wasPressed )
  624. // m_pressed = false;
  625. if( !EditMode_IsOn() && !m_doNotCheckFocusChange )
  626. {
  627. if( m_wasPressed && m_wasReleased )
  628. {
  629. if( IsActive())
  630. {
  631. m_params.buttonEvent.Activate(Mission(),false);
  632. m_pressDelay = 0.0f;
  633. m_repeat = false;
  634. }
  635. else
  636. {
  637. // m_params.inactiEvent.Activate(Mission(),false);
  638. }
  639. }
  640. else
  641. {
  642. if( m_autorepeat )
  643. {
  644. if( m_repeat )
  645. {
  646. if( m_pressDelay > repeat_delay )
  647. {
  648. m_params.buttonEvent.Activate(Mission(),false);
  649. m_pressDelay = 0.0f;
  650. }
  651. }
  652. else
  653. {
  654. if( m_pressDelay > repeat_delay*100 )
  655. {
  656. m_params.buttonEvent.Activate(Mission(),false);
  657. m_pressDelay = 0.0f;
  658. m_repeat = true;
  659. }
  660. }
  661. }
  662. }
  663. }
  664. m_focusTime += deltaTime;
  665. // if( m_focusTime > 0.2f )
  666. if( m_focusTime > 0.0f )
  667. m_doNotCheckFocusChange = false;
  668. m_wasReleased = !m_wasPressed;
  669. Rect rect; GetRect(rect);
  670. float width = rect.r - rect.l;
  671. float height = rect.b - rect.t;
  672. float x = GetX();
  673. float y = GetY();
  674. if( EditMode_IsOn())
  675. m_helperQuad.DrawQuad(x,y,width,height,0,0,0,0,10.5f);
  676. float k = m_pressTime/press_delay;
  677. float x_off = Lerp(0.0f,m_params.pressXoffset,k);
  678. float y_off = Lerp(0.0f,m_params.pressYoffset,k);
  679. x += x_off*m_aspect;
  680. y += y_off;
  681. float r = x + width;
  682. float b = y + height;
  683. Rect clip; GetParentClipRect(clip);
  684. if( !RectInRect(
  685. clip.l,
  686. clip.t,
  687. clip.r,
  688. clip.b,x,y,r,b))
  689. return;
  690. if( !PointInRect(x,y,clip) ||
  691. !PointInRect(r,b,clip))
  692. SetClipRect(&clip);
  693. x += width *0.5f;
  694. y += height*0.5f;
  695. x *= 0.01f;
  696. y *= 0.01f;
  697. switch( m_state )
  698. {
  699. /* case stStatic:
  700. if( m_staticWidget )
  701. m_staticWidget->Draw(x,y,GetAlpha()*m_staticWidget->GetNativeAlpha());
  702. break;*/
  703. case stNormal:
  704. if( IsActive())
  705. {
  706. if( m_mouseOver && m_selectedWidget )
  707. {
  708. if( m_selectedWidget )
  709. m_selectedWidget->Draw(x,y,GetAlpha()*m_selectedWidget->GetNativeAlpha());
  710. }
  711. else
  712. {
  713. if( m_normalWidget )
  714. m_normalWidget->Draw(x,y,GetAlpha()*m_normalWidget->GetNativeAlpha());
  715. }
  716. }
  717. else
  718. {
  719. if( m_disabledWidget )
  720. m_disabledWidget->Draw(x,y,GetAlpha()*m_disabledWidget->GetNativeAlpha());
  721. }
  722. break;
  723. case stFocused:
  724. if( IsActive())
  725. {
  726. if( m_focusedWidget )
  727. m_focusedWidget->Draw(x,y,GetAlpha()*m_focusedWidget->GetNativeAlpha());
  728. }
  729. else
  730. {
  731. if( m_staticWidget )
  732. m_staticWidget->Draw(x,y,GetAlpha()*m_staticWidget->GetNativeAlpha());
  733. }
  734. break;
  735. case stPressed:
  736. if( m_pressedWidget )
  737. m_pressedWidget->Draw(x,y,GetAlpha()*m_pressedWidget->GetNativeAlpha());
  738. break;
  739. /* case stDisabled:
  740. if( m_disabledWidget )
  741. m_disabledWidget->Draw(x,y,GetAlpha()*m_disabledWidget->GetNativeAlpha());
  742. break;*/
  743. }
  744. m_pressTime -= deltaTime;
  745. if( m_pressTime < 0.0f )
  746. {
  747. m_pressTime = 0.0f;
  748. if( !HaveFocus())
  749. SetNormal();
  750. }
  751. SetClipRect(NULL);
  752. }
  753. #endif
  754. void _cdecl GUIButton::Work(float deltaTime, long level)
  755. {
  756. if( EditMode_IsOn() && !EditMode_IsVisible())
  757. return;
  758. Draw();
  759. Update(deltaTime);
  760. }
  761. void GUIButton::Update(float deltaTime)
  762. {
  763. /* if( !IsActive())
  764. {
  765. SetDisabled();
  766. return;
  767. }*/
  768. ControlStateType state;
  769. for( dword i = 0 ; i < m_params.links.Size() ; i++ )
  770. {
  771. state = Controls().GetControlStateType(m_params.links[i].control);
  772. if( state == CST_ACTIVE ||
  773. state == CST_ACTIVATED )
  774. m_params.links[i].activeTime += deltaTime;
  775. else
  776. m_params.links[i].activeTime = 0.0f;
  777. }
  778. if( !ProcessKeyboard())
  779. ProcessMouse();
  780. // if( !m_wasReleased && !m_wasPressed )
  781. // m_pressed = false;
  782. if( !EditMode_IsOn() && !m_doNotCheckFocusChange )
  783. {
  784. bool action = m_release ?
  785. !m_wasPressed && !m_wasReleased : // по отжатию
  786. m_wasPressed && m_wasReleased; // по нажатию
  787. // if( m_wasPressed && m_wasReleased )
  788. if( action )
  789. {
  790. if( IsActive())
  791. {
  792. LogicDebug("Button press trigger");
  793. m_params.buttonEvent.Activate(Mission(),false);
  794. m_pressDelay = 0.0f;
  795. m_repeat = false;
  796. }
  797. else
  798. {
  799. // m_params.inactiEvent.Activate(Mission(),false);
  800. }
  801. }
  802. else
  803. {
  804. if( m_autorepeat )
  805. {
  806. if( m_repeat )
  807. {
  808. if( m_pressDelay > repeat_delay )
  809. {
  810. LogicDebug("Button press trigger");
  811. m_params.buttonEvent.Activate(Mission(),false);
  812. m_pressDelay = 0.0f;
  813. }
  814. }
  815. else
  816. {
  817. if( m_pressDelay > repeat_delay*100 )
  818. {
  819. LogicDebug("Button press trigger");
  820. m_params.buttonEvent.Activate(Mission(),false);
  821. m_pressDelay = 0.0f;
  822. m_repeat = true;
  823. }
  824. }
  825. }
  826. }
  827. }
  828. m_focusTime += deltaTime;
  829. // if( m_focusTime > 0.2f )
  830. if( m_focusTime > 0.0f )
  831. m_doNotCheckFocusChange = false;
  832. m_wasReleased = !m_wasPressed;
  833. if( m_release && m_wasPressed )
  834. ;
  835. else
  836. m_pressTime -= deltaTime;
  837. if( m_pressTime < 0.0f )
  838. {
  839. m_pressTime = 0.0f;
  840. if( !HaveFocus())
  841. {
  842. if( m_mouseOver )
  843. SetSelected();
  844. else
  845. SetNormal();
  846. }
  847. }
  848. float k = m_pressTime/press_delay;
  849. float x_off = Lerp(0.0f,m_params.pressXoffset,k)*m_aspect;
  850. float y_off = Lerp(0.0f,m_params.pressYoffset,k);
  851. if( m_activeWidget )
  852. m_activeWidget->SetFrameOffset(x_off,y_off);
  853. }
  854. void GUIButton::Draw()
  855. {
  856. if( InterfaceUtils::IsHide())
  857. return;
  858. Rect rect; GetRect(rect);
  859. float width = rect.r - rect.l;
  860. float height = rect.b - rect.t;
  861. float x = GetX();
  862. float y = GetY();
  863. if( EditMode_IsOn())
  864. m_helperQuad.DrawQuad(x,y,width,height,0,0,0,0,10.5f);
  865. /* float k = m_pressTime/press_delay;
  866. float x_off = Lerp(0.0f,m_params.pressXoffset,k);
  867. float y_off = Lerp(0.0f,m_params.pressYoffset,k);
  868. // ;
  869. x_off *= m_aspect;*/
  870. /* x += x_off*m_aspect;
  871. y += y_off;
  872. float r = x + width;
  873. float b = y + height;
  874. Rect clip; GetParentClipRect(clip);
  875. if( !RectInRect(
  876. clip.l,
  877. clip.t,
  878. clip.r,
  879. clip.b,x,y,r,b))
  880. return;
  881. if( !PointInRect(x,y,clip) ||
  882. !PointInRect(r,b,clip))
  883. SetClipRect(&clip);
  884. x += width *0.5f;
  885. y += height*0.5f;
  886. x *= 0.01f;
  887. y *= 0.01f;
  888. switch( m_state )
  889. {
  890. // case stStatic:
  891. // if( m_staticWidget )
  892. // m_staticWidget->Draw(x,y,GetAlpha()*m_staticWidget->GetNativeAlpha());
  893. // break;
  894. case stNormal:
  895. if( IsActive())
  896. {
  897. if( m_mouseOver && m_selectedWidget )
  898. {
  899. if( m_selectedWidget )
  900. m_selectedWidget->Draw(x,y,GetAlpha()*m_selectedWidget->GetNativeAlpha());
  901. }
  902. else
  903. {
  904. if( m_normalWidget )
  905. m_normalWidget->Draw(x,y,GetAlpha()*m_normalWidget->GetNativeAlpha());
  906. }
  907. }
  908. else
  909. {
  910. if( m_disabledWidget )
  911. m_disabledWidget->Draw(x,y,GetAlpha()*m_disabledWidget->GetNativeAlpha());
  912. }
  913. break;
  914. case stFocused:
  915. if( IsActive())
  916. {
  917. if( m_focusedWidget )
  918. m_focusedWidget->Draw(x,y,GetAlpha()*m_focusedWidget->GetNativeAlpha());
  919. }
  920. else
  921. {
  922. if( m_staticWidget )
  923. m_staticWidget->Draw(x,y,GetAlpha()*m_staticWidget->GetNativeAlpha());
  924. }
  925. break;
  926. case stPressed:
  927. if( m_pressedWidget )
  928. m_pressedWidget->Draw(x,y,GetAlpha()*m_pressedWidget->GetNativeAlpha());
  929. break;
  930. // case stDisabled:
  931. // if( m_disabledWidget )
  932. // m_disabledWidget->Draw(x,y,GetAlpha()*m_disabledWidget->GetNativeAlpha());
  933. // break;
  934. }
  935. SetClipRect(NULL);*/
  936. }
  937. void GUIButton::OnAcquireFocus()
  938. {
  939. for( dword i = 0 ; i < m_params.links.Size() ; i++ )
  940. m_params.links[i].activeTime = 0.0f;
  941. SetFocused();
  942. m_doNotCheckFocusChange = true;
  943. m_focusTime = 0.0f;
  944. // SetCursor();
  945. }
  946. void GUIButton::OnLooseFocus()
  947. {
  948. if( m_state == stFocused ||
  949. m_state == stPressed )
  950. {
  951. // api->Trace("ON LOS FOCUS: %s",GetObjectID().c_str());
  952. LogicDebug("Button losFocus trigger");
  953. onLosFocus.Activate(Mission(),false);
  954. }
  955. // if( IsShow())
  956. SetNormal();
  957. if( m_tooltipWidget )
  958. {
  959. m_tooltipWidget->Show(false);
  960. }
  961. }
  962. void GUIButton::OnParentNotify(BaseGUIElement::Notification event)
  963. {
  964. BaseGUIElement::OnParentNotify(event);
  965. }
  966. bool GUIButton::Create(MOPReader &reader)
  967. {
  968. if( m_restart )
  969. {
  970. RestartObject(reader);
  971. }
  972. else
  973. {
  974. EditMode_Update(reader);
  975. }
  976. return true;
  977. }
  978. bool GUIButton::RestartObject(MOPReader &reader)
  979. {
  980. BaseGUIElement::SkipMOPs(reader);
  981. SkipMOPs(reader);
  982. // SetUpdate(&GUIButton::InitFunc,ML_FIRST);
  983. SetUpdate(&GUIButton::InitFunc,ML_FIRST + 2);
  984. if( m_initComplete == false )
  985. {
  986. m_restart = false;
  987. }
  988. return true;
  989. }
  990. void GUIButton::InitFunc(float, long)
  991. {
  992. DelUpdate(&GUIButton::InitFunc);
  993. ////////////////////////////////
  994. if( m_restart )
  995. {
  996. m_restart = false;
  997. if( m_staticWidget ) m_staticWidget ->Show(false);
  998. if( m_selectedWidget ) m_selectedWidget->Show(false);
  999. if( m_normalWidget ) m_normalWidget ->Show(false);
  1000. if( m_focusedWidget ) m_focusedWidget ->Show(false);
  1001. if( m_pressedWidget ) m_pressedWidget ->Show(false);
  1002. if( m_disabledWidget ) m_disabledWidget->Show(false);
  1003. if( m_tooltipWidget )
  1004. m_tooltipWidget->Show(false);
  1005. Controls().EnableControlGroup("GUI",true);
  1006. Show(nativeShow);
  1007. return;
  1008. }
  1009. ////////////////////////////////
  1010. m_initComplete = true;
  1011. BaseGUIElement *parent = GetParent();
  1012. if( BaseGUIElement *newParent = FindParent())
  1013. {
  1014. newParent->Register(this);
  1015. }
  1016. else
  1017. {
  1018. if( parent )
  1019. parent->UnRegister(this);
  1020. }
  1021. const ConstString & myName = GetObjectID();
  1022. GUIWidget *mo = null;
  1023. mo = (GUIWidget*)FindObject(m_params.staticWidget);
  1024. if( mo && mo->Is(InterfaceUtils::GetWidgetId()))
  1025. {
  1026. m_staticWidget = (GUIWidget*)mo;
  1027. m_staticWidget->Show(false);
  1028. m_staticWidget->SetShiftAspect(m_shiftAspect);
  1029. m_staticWidget->SetWidthAspect(m_widthAspect);
  1030. m_staticWidget->SetLayout(m_layout);
  1031. m_staticWidget->SetParent(myName);
  1032. m_staticWidget->SetFading(false);
  1033. }
  1034. mo = (GUIWidget*)FindObject(m_params.selectedButtonWidget);
  1035. if( mo && mo->Is(InterfaceUtils::GetWidgetId()))
  1036. {
  1037. m_selectedWidget = (GUIWidget*)mo;
  1038. m_selectedWidget->Show(false);
  1039. m_selectedWidget->SetShiftAspect(m_shiftAspect);
  1040. m_selectedWidget->SetWidthAspect(m_widthAspect);
  1041. m_selectedWidget->SetLayout(m_layout);
  1042. m_selectedWidget->SetParent(myName);
  1043. m_selectedWidget->SetFading(false);
  1044. }
  1045. mo = (GUIWidget*)FindObject(m_params.normalButtonWidget);
  1046. if( mo && mo->Is(InterfaceUtils::GetWidgetId()))
  1047. {
  1048. m_normalWidget = (GUIWidget*)mo;
  1049. m_normalWidget->Show(false);
  1050. m_normalWidget->SetShiftAspect(m_shiftAspect);
  1051. m_normalWidget->SetWidthAspect(m_widthAspect);
  1052. m_normalWidget->SetLayout(m_layout);
  1053. m_normalWidget->SetParent(myName);
  1054. m_normalWidget->SetFading(false);
  1055. }
  1056. mo = (GUIWidget*)FindObject(m_params.focusButtonWidget);
  1057. if( mo && mo->Is(InterfaceUtils::GetWidgetId()))
  1058. {
  1059. m_focusedWidget = (GUIWidget*)mo;
  1060. m_focusedWidget->Show(false);
  1061. m_focusedWidget->SetShiftAspect(m_shiftAspect);
  1062. m_focusedWidget->SetWidthAspect(m_widthAspect);
  1063. m_focusedWidget->SetLayout(m_layout);
  1064. m_focusedWidget->SetParent(myName);
  1065. m_focusedWidget->SetFading(false);
  1066. }
  1067. mo = (GUIWidget*)FindObject(m_params.pressedButtonWidget);
  1068. if( mo && mo->Is(InterfaceUtils::GetWidgetId()))
  1069. {
  1070. m_pressedWidget = (GUIWidget*)mo;
  1071. m_pressedWidget->Show(false);
  1072. m_pressedWidget->SetShiftAspect(m_shiftAspect);
  1073. m_pressedWidget->SetWidthAspect(m_widthAspect);
  1074. m_pressedWidget->SetLayout(m_layout);
  1075. m_pressedWidget->SetParent(myName);
  1076. m_pressedWidget->SetFading(false);
  1077. }
  1078. mo = (GUIWidget*)FindObject(m_params.disabledButtonWidget);
  1079. if( mo && mo->Is(InterfaceUtils::GetWidgetId()))
  1080. {
  1081. m_disabledWidget = (GUIWidget*)mo;
  1082. m_disabledWidget->Show(false);
  1083. m_disabledWidget->SetShiftAspect(m_shiftAspect);
  1084. m_disabledWidget->SetWidthAspect(m_widthAspect);
  1085. m_disabledWidget->SetLayout(m_layout);
  1086. m_disabledWidget->SetParent(myName);
  1087. m_disabledWidget->SetFading(false);
  1088. }
  1089. mo = (GUIWidget*)FindObject(m_params.descObject);
  1090. if( mo && mo->Is(InterfaceUtils::GetWidgetId()))
  1091. {
  1092. m_tooltipWidget = (GUIWidget*)mo;
  1093. m_tooltipWidget->Show(false);
  1094. }
  1095. /*
  1096. // ;
  1097. m_activeWidget = m_normalWidget;*/
  1098. Controls().EnableControlGroup("GUI",true);
  1099. if( !m_cursor )
  1100. m_cursor = (BaseGUICursor *)FindObject(InterfaceUtils::GetCursorName());
  1101. m_baseCursor = m_cursor;
  1102. // Show(IsShow());
  1103. Show(nativeShow);
  1104. }
  1105. bool GUIButton::EditMode_Update(MOPReader &reader)
  1106. {
  1107. BaseGUIElement::EditMode_Update(reader);
  1108. ReadMOPs (reader);
  1109. // SetUpdate(&GUIButton::InitFunc,ML_FIRST);
  1110. SetUpdate(&GUIButton::InitFunc,ML_FIRST + 2);
  1111. m_initComplete = false;
  1112. return true;
  1113. }
  1114. void GUIButton::Show(bool isShow)
  1115. {
  1116. BaseGUIElement::Show(isShow);
  1117. if( EditMode_IsOn())
  1118. DelUpdate(&GUIButton::Work);
  1119. if( IsShow())
  1120. {
  1121. if( m_params.initiallyFocused || HaveFocus())
  1122. {
  1123. SetFocused();
  1124. if( m_cursor && !m_cursor->IsClicked())
  1125. SetCursor();
  1126. }
  1127. else
  1128. SetNormal();
  1129. m_doNotCheckFocusChange = true;
  1130. m_focusTime = 0.0f;
  1131. if( GetAlpha() > 0.0f )
  1132. {
  1133. // SetUpdate(&GUIButton::Work,drawLevel + drawPriority);
  1134. SetUpdate(&GUIButton::Work,drawLevel);
  1135. }
  1136. }
  1137. else
  1138. {
  1139. if( m_activeWidget )
  1140. m_activeWidget->Show(false);
  1141. m_activeWidget = null;
  1142. SetState(stNormal);
  1143. if( HaveFocus())
  1144. SetElementInFocus(null);
  1145. DelUpdate(&GUIButton::Work);
  1146. }
  1147. m_pressed = false;
  1148. m_pressTime = 0.0f;
  1149. }
  1150. void GUIButton::Activate(bool isActive)
  1151. {
  1152. BaseGUIElement::Activate(isActive);
  1153. if( !IsActive() && isActive )
  1154. {
  1155. SetState(stNormal);
  1156. }
  1157. if( isActive == false )
  1158. {
  1159. if( GetElementInFocus() == this )
  1160. {
  1161. OnLooseFocus();
  1162. SetElementInFocus(null);
  1163. }
  1164. SetDisabled();
  1165. }
  1166. }
  1167. void GUIButton::Command(const char *id, dword numParams, const char **params)
  1168. {
  1169. if( string::IsEmpty(id))
  1170. return;
  1171. if( string::IsEqual(id,"SetFocus"))
  1172. {
  1173. if( IsShow() && IsActive())
  1174. SetFocused();
  1175. // if( IsShow() && IsActive())
  1176. // SetCursor();
  1177. LogicDebug("Command GUIButton::<SetFocus>. Button is now focused.");
  1178. }
  1179. else
  1180. if( string::IsEqual(id,"Set"))
  1181. {
  1182. if( numParams < 2 )
  1183. {
  1184. LogicDebugError("Command GUIButton::<Set> error. Too few arguments.");
  1185. return;
  1186. }
  1187. const char *type = params[0];
  1188. const char *name = params[1];
  1189. if( name && type[0] )
  1190. {
  1191. MissionObject *p = FindObject(ConstString(name));
  1192. GUIWidget *widget = NULL;
  1193. if( p && p->Is(InterfaceUtils::GetWidgetId()))
  1194. {
  1195. widget = (GUIWidget *)p;
  1196. widget->Show(false);
  1197. }
  1198. if( widget )
  1199. {
  1200. if( type && type[0] )
  1201. {
  1202. if( string::IsEqual(type,"Static"))
  1203. m_staticWidget = widget;
  1204. else
  1205. if( string::IsEqual(type,"Selected"))
  1206. m_selectedWidget = widget;
  1207. else
  1208. if( string::IsEqual(type,"Normal"))
  1209. m_normalWidget = widget;
  1210. else
  1211. if( string::IsEqual(type,"Focused"))
  1212. m_focusedWidget = widget;
  1213. else
  1214. if( string::IsEqual(type,"Pressed"))
  1215. m_pressedWidget = widget;
  1216. else
  1217. if( string::IsEqual(type,"Disabled"))
  1218. m_disabledWidget = widget;
  1219. else
  1220. {
  1221. LogicDebugError("Command GUIButton::<Set> error. Invalid type name (%s).",type);
  1222. return;
  1223. }
  1224. LogicDebug("Command GUIButton::<Set>. %s widget now is %s.",type,name);
  1225. }
  1226. else
  1227. {
  1228. LogicDebugError("Command GUIButton::<Set> error. Invalid type parameter.");
  1229. }
  1230. }
  1231. else
  1232. {
  1233. LogicDebugError("Command GUIButton::<Set> error. Widget %s not found.",name);
  1234. }
  1235. }
  1236. else
  1237. {
  1238. LogicDebugError("Command GUIButton::<Set> error. Invalid name parameter.");
  1239. }
  1240. }
  1241. else
  1242. {
  1243. BaseGUIElement::Command(id,numParams,params);
  1244. }
  1245. }
  1246. static char GUIDescription[] =
  1247. "GUI Button.\n\n"
  1248. " NOTE: all sizes-positions are relative (not pixel sizes-positions)\n\n"
  1249. "Commands:\n\n"
  1250. " Set <type> <name> - set new widget to use\n\n"
  1251. " type - widget type (static, normal, focused, pressed, disabled)\n"
  1252. " name - widget object name\n\n"
  1253. " SetFocus - set button to focus state";
  1254. MOP_BEGINLISTCG(GUIButton, "GUI Button", '1.00', 200, GUIDescription, "Interface")
  1255. // MOP_GROUPBEG("Wide screen layout")
  1256. // MOP_BOOL("Use shift aspect", false)
  1257. // MOP_BOOL("Use width aspect", true)
  1258. MOP_ENUMBEG("Layout")
  1259. MOP_ENUMELEMENT("Left")
  1260. MOP_ENUMELEMENT("Center")
  1261. MOP_ENUMELEMENT("Right")
  1262. MOP_ENUMEND
  1263. MOP_ENUM("Layout", "Layout")
  1264. // MOP_GROUPEND()
  1265. MOP_FLOAT("X position", 0.0f)
  1266. MOP_FLOAT("Y position", 0.0f)
  1267. MOP_FLOAT("Alpha", 1.0f)
  1268. MOP_STRING("Parent id", "")
  1269. MOP_FLOAT("Width" , 25.0f)
  1270. MOP_FLOAT("Height", 25.0f)
  1271. MOP_LOCSTRING("Tooltip message")
  1272. MOP_STRING("Tooltip widget", "")
  1273. MOP_STRING("Static widget", "")
  1274. MOP_STRING("Selected widget", "")
  1275. MOP_STRING("Normal state widget", "")
  1276. MOP_STRING("Focused state widget", "")
  1277. MOP_STRING("Pressed state widget", "")
  1278. MOP_STRING("Disabled (inactive) state widget", "")
  1279. MOP_FLOAT("X offset on press", 1.0f)
  1280. MOP_FLOAT("Y offset on press", 1.0f)
  1281. MOP_ARRAYBEG("Navigation links", 0, 100)
  1282. MOP_STRING("Control", "")
  1283. MOP_STRING("Object to go to", "")
  1284. MOP_ARRAYEND
  1285. MOP_ARRAYBEG("Hot keys", 0, 10)
  1286. MOP_STRING("Control", "")
  1287. MOP_BOOL("Only in focus", true)
  1288. MOP_ARRAYEND
  1289. MOP_BOOL("Action by release", false)
  1290. MOP_GROUPBEG("Events")
  1291. MOP_MISSIONTRIGGER("")
  1292. MOP_GROUPEND()
  1293. // MOP_MISSIONTRIGGERC("Inactive","")
  1294. MOP_BOOL("Autorepeat", false)
  1295. MOP_BOOL("Autofocus" , true)
  1296. MOP_MISSIONTRIGGERC("GetFocus","")
  1297. MOP_MISSIONTRIGGERC("LosFocus","")
  1298. MOP_BOOL("Initially focused", false)
  1299. MOP_BOOL("Focus by press", true)
  1300. MOP_LONG("Draw priority", 0)
  1301. MOP_BOOL("Active" , true)
  1302. MOP_BOOL("Visible", true)
  1303. MOP_ENDLIST(GUIButton)