CheckBox.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. void CheckBox::zero()
  6. {
  7. _focusable =false;
  8. _on =false;
  9. _multi =false;
  10. _func_immediate=false;
  11. _func_user =null;
  12. _func =null;
  13. _lit =0;
  14. }
  15. CheckBox::CheckBox() {zero();}
  16. CheckBox& CheckBox::del()
  17. {
  18. skin.clear();
  19. super::del(); zero(); return T;
  20. }
  21. CheckBox& CheckBox::create(Bool on)
  22. {
  23. del();
  24. T._type =GO_CHECKBOX;
  25. T._visible =true;
  26. T._on =on;
  27. T._rect.max.x= 0.04f;
  28. T._rect.min.y=-0.04f;
  29. return T;
  30. }
  31. CheckBox& CheckBox::create(C CheckBox &src)
  32. {
  33. if(this!=&src)
  34. {
  35. if(!src.is())del();else
  36. {
  37. copyParams(src);
  38. _type =GO_CHECKBOX;
  39. skin =src.skin;
  40. _focusable =src._focusable;
  41. _on =src._on;
  42. _multi =src._multi;
  43. _lit =src._lit;
  44. _func =src._func;
  45. _func_user =src._func_user;
  46. _func_immediate=src._func_immediate;
  47. }
  48. }
  49. return T;
  50. }
  51. CheckBox& CheckBox::toggle(SET_MODE mode)
  52. {
  53. _on^=1; _multi=false;
  54. if(mode!=QUIET)call(mode!=NO_SOUND);
  55. return T;
  56. }
  57. CheckBox& CheckBox::set(Bool on, SET_MODE mode)
  58. {
  59. if(T._on!=on || _multi)
  60. {
  61. T._on=on; _multi=false;
  62. if(mode!=QUIET)call(mode!=NO_SOUND);
  63. }
  64. return T;
  65. }
  66. CheckBox& CheckBox::setMulti()
  67. {
  68. _multi=true;
  69. return T;
  70. }
  71. /******************************************************************************/
  72. CheckBox& CheckBox::func(void (*func)(Ptr), Ptr user, Bool immediate)
  73. {
  74. T._func =func;
  75. T._func_user =user;
  76. T._func_immediate=immediate;
  77. return T;
  78. }
  79. void CheckBox::call(Bool sound)
  80. {
  81. if(sound)Gui.playClickSound();
  82. if(_func)if(_func_immediate)_func(_func_user);else Gui.addFuncCall(_func, _func_user);
  83. }
  84. /******************************************************************************/
  85. CheckBox& CheckBox::focusable(Bool on) {if(_focusable!=on){_focusable=on; if(!on)kbClear();} return T;}
  86. /******************************************************************************/
  87. void CheckBox::update(C GuiPC &gpc)
  88. {
  89. Bool enabled=(T.enabled() && gpc.enabled);
  90. if( enabled)
  91. {
  92. if((Gui.kb()==this && Kb.k(' '))
  93. || (Gui.ms()==this && Ms.bp(0) ))toggle();else // don't check touches if we already toggled it with keyboard/mouse (this is to avoid double presses when using 'SimulateTouches')
  94. REPA(Touches)
  95. {
  96. Touch &touch=Touches[i];
  97. if(touch.guiObj()==this)
  98. if(touch.scrolling() ? touch.rs() : touch.pd())toggle();
  99. }
  100. //AdjustValBool(_lit, Gui.ms()==this && enabled, Gui._time_d_fade_in, Gui._time_d_fade_out);
  101. if(Gui.ms()==this)_lit=1;else MAX(_lit-=Gui._time_d_fade_out, 0);
  102. }
  103. }
  104. void CheckBox::draw(C GuiPC &gpc)
  105. {
  106. if(visible() && gpc.visible)
  107. if(GuiSkin *skin=getSkin())
  108. if(PanelImage *panel_image=(_multi ? skin->checkbox.multi() : T() ? skin->checkbox.on() : skin->checkbox.off()))
  109. {
  110. Rect rect=T.rect()+gpc.offset, ext_rect;
  111. panel_image->extendedRect(rect, ext_rect);
  112. if(Cuts(ext_rect, gpc.clip))
  113. {
  114. D.clip(gpc.clip);
  115. D.alignScreenToPixel(rect);
  116. Bool enabled =(T.enabled() && gpc.enabled);
  117. Color highlight=ColorMulZeroAlpha(skin->mouse_highlight_color, _lit);
  118. panel_image->draw(enabled ? skin->checkbox.normal_color : skin->checkbox.disabled_color, highlight, rect);
  119. if(Gui.kb()==this)Gui.kbLit(this, rect, skin);
  120. }
  121. }
  122. }
  123. /******************************************************************************/
  124. Bool CheckBox::save(File &f, CChar *path)C
  125. {
  126. if(super::save(f, path))
  127. {
  128. f.cmpUIntV(1); // version
  129. f<<_focusable<<_on<<_multi;
  130. f._putAsset(skin.name(path));
  131. return f.ok();
  132. }
  133. return false;
  134. }
  135. Bool CheckBox::load(File &f, CChar *path)
  136. {
  137. del(); if(super::load(f, path))switch(f.decUIntV()) // version
  138. {
  139. case 1:
  140. {
  141. _type=GO_CHECKBOX;
  142. f>>_focusable>>_on>>_multi;
  143. skin.require(f._getAsset(), path);
  144. if(f.ok())return true;
  145. }break;
  146. case 0:
  147. {
  148. _type=GO_CHECKBOX;
  149. f>>_focusable>>_on;
  150. if(f.ok())return true;
  151. }break;
  152. }
  153. del(); return false;
  154. }
  155. /******************************************************************************/
  156. }
  157. /******************************************************************************/