tb_atomic_widgets.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. //
  6. // Copyright (c) 2016, THUNDERBEAST GAMES LLC All rights reserved
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #include "tb_widgets_reader.h"
  27. #include "tb_widgets_common.h"
  28. #include "tb_node_tree.h"
  29. #include "tb_system.h"
  30. #include "tb_atomic_widgets.h"
  31. #include <math.h>
  32. namespace tb {
  33. // == TBColorWidget =======================================
  34. TBColorWidget::TBColorWidget() : color_(), alpha_ ( 1.0f)
  35. {
  36. }
  37. void TBColorWidget::SetColor ( const char *name )
  38. {
  39. if ( name )
  40. color_.SetFromString(name, strlen(name));
  41. InvalidateSkinStates();
  42. Invalidate();
  43. }
  44. void TBColorWidget::SetColor(float r, float g, float b, float a)
  45. {
  46. color_.Set(TBColor(r, g, b, a));
  47. InvalidateSkinStates();
  48. Invalidate();
  49. }
  50. void TBColorWidget::SetAlpha ( float value )
  51. {
  52. if ( value < 0.0 || value > 1.0 )
  53. {
  54. alpha_ = 1.0;
  55. return;
  56. }
  57. alpha_ = value;
  58. InvalidateSkinStates();
  59. Invalidate();
  60. }
  61. void TBColorWidget::OnPaint(const PaintProps &paint_props)
  62. {
  63. TBRect local_rect = GetRect();
  64. local_rect.x = 0;
  65. local_rect.y = 0;
  66. float old_opacity = g_renderer->GetOpacity();
  67. g_renderer->SetOpacity(alpha_);
  68. g_renderer->DrawRectFill(local_rect, color_);
  69. g_renderer->SetOpacity(old_opacity);
  70. }
  71. void TBColorWidget::OnInflate(const INFLATE_INFO &info)
  72. {
  73. if (const char *colr = info.node->GetValueString("color", nullptr))
  74. SetColor(colr);
  75. TBWidget::OnInflate(info);
  76. }
  77. TB_WIDGET_FACTORY(TBColorWidget, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  78. // == TBColorWheel =======================================
  79. TBColorWheel::TBColorWheel() :
  80. markerx_(128),
  81. markery_(128),
  82. markercolor_(),
  83. hue_(0.0),
  84. saturation_(0.0)
  85. {
  86. }
  87. void TBColorWheel::OnPaint(const PaintProps &paint_props)
  88. {
  89. TBWidget::OnPaint(paint_props); // draw the widget stuff
  90. TBRect local_rect ( 0,0,4,4 ); // AND draw a marker where we clicked.
  91. local_rect.x = markerx_ - 2;
  92. local_rect.y = markery_ - 2;
  93. g_renderer->DrawRect( local_rect, markercolor_);
  94. local_rect.x -= 1;
  95. local_rect.y -= 1;
  96. local_rect.w += 2;
  97. local_rect.h += 2;
  98. g_renderer->DrawRect( local_rect, markercolor_); // draw double box
  99. }
  100. bool TBColorWheel::OnEvent(const TBWidgetEvent &ev)
  101. {
  102. if (ev.target == this && ev.type == EVENT_TYPE_CLICK)
  103. {
  104. SetMarkerX ( ev.target_x );
  105. SetMarkerY ( ev.target_y );
  106. CalcHueSaturation( markerx_, markery_ );
  107. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  108. InvokeEvent(ev);
  109. }
  110. return TBWidget::OnEvent(ev);
  111. }
  112. void TBColorWheel::SetHueSaturation ( float hue, float saturation )
  113. {
  114. // suppose to set the marker position to match HS here
  115. hue_ = hue * 360.0;
  116. saturation_ = saturation * 128.0;
  117. Invalidate();
  118. }
  119. void TBColorWheel::CalcHueSaturation ( int rawx, int rawy )
  120. {
  121. TBRect rect = GetRect();
  122. int centerx = rect.w / 2;
  123. int centery = rect.h / 2;
  124. float X1 = rawx;
  125. float Y1 = rawy;
  126. float X2 = centerx;
  127. float Y2 = centery;
  128. float angle = 0.0;
  129. float xd = X2-X1;
  130. float yd = Y2-Y1;
  131. float dx = sqrt(xd * xd + yd * yd);
  132. // angle in degrees
  133. angle = atan2(Y2 - Y1, X2 - X1) * 180 / 3.14159265358979323846;
  134. if (angle < 0) angle += 360.0;
  135. // if the distance > 128, can we calculate the line point at 128 and set the marker there?
  136. if( dx > 128.0 ) dx = 128.0; // limit value
  137. saturation_ = dx;
  138. hue_ = angle;
  139. }
  140. void TBColorWheel::SetMarkerX ( int value )
  141. {
  142. markerx_ = value;
  143. }
  144. void TBColorWheel::SetMarkerY ( int value )
  145. {
  146. markery_ = value;
  147. }
  148. void TBColorWheel::SetMarkerColor ( const char *name )
  149. {
  150. if ( name )
  151. markercolor_.SetFromString(name, strlen(name));
  152. Invalidate();
  153. }
  154. void TBColorWheel::OnInflate(const INFLATE_INFO &info)
  155. {
  156. if (const char *colr = info.node->GetValueString("color", nullptr))
  157. SetMarkerColor(colr);
  158. TBWidget::OnInflate(info);
  159. }
  160. TB_WIDGET_FACTORY(TBColorWheel, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  161. // == TBBarGraph =======================================
  162. TBBarGraph::TBBarGraph() : color_(255,255,255,255), m_value (0.0), m_axis(AXIS_X), m_margin(0)
  163. {
  164. SetSkinBg(TBIDC("background_solid"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  165. }
  166. void TBBarGraph::SetColor ( const char *name )
  167. {
  168. if ( name )
  169. color_.SetFromString(name, strlen(name));
  170. InvalidateSkinStates();
  171. Invalidate();
  172. }
  173. void TBBarGraph::SetColor(float r, float g, float b, float a)
  174. {
  175. color_.Set(TBColor(r, g, b, a));
  176. InvalidateSkinStates();
  177. Invalidate();
  178. }
  179. void TBBarGraph::OnPaint(const PaintProps &paint_props)
  180. {
  181. TBRect local_rect = GetRect();
  182. local_rect.x = 0;
  183. local_rect.y = 0;
  184. if ( m_axis == AXIS_X ) // horizontal bar
  185. {
  186. double w1 = (double)local_rect.w * ( m_value / 100.0 );
  187. local_rect.w = (int)w1;
  188. if ( m_margin > 0 && m_margin < (local_rect.h/2)-2)
  189. {
  190. local_rect.h -= (m_margin *2);
  191. local_rect.y += m_margin;
  192. }
  193. }
  194. else if ( m_axis == AXIS_Y ) // vertical bar
  195. {
  196. double h1 = (double)local_rect.h * ( m_value / 100.0 );
  197. local_rect.y = local_rect.h - (int)h1;
  198. local_rect.h = (int)h1;
  199. if ( m_margin > 0 && m_margin < (local_rect.w/2)-2 )
  200. {
  201. local_rect.w -= (m_margin*2);
  202. local_rect.x += m_margin;
  203. }
  204. }
  205. g_renderer->DrawRectFill(local_rect, color_);
  206. }
  207. void TBBarGraph::OnInflate(const INFLATE_INFO &info)
  208. {
  209. if (const char *colr = info.node->GetValueString("color", nullptr))
  210. SetColor(colr);
  211. if ( const char *axis = info.node->GetValueString("axis", "x") )
  212. SetAxis(*axis == 'x' ? AXIS_X : AXIS_Y);
  213. if (info.sync_type == TBValue::TYPE_FLOAT)
  214. SetValueDouble(info.node->GetValueFloat("value", 0));
  215. SetMargin( (unsigned)info.node->GetValueInt("margin", 0 ) );
  216. TBWidget::OnInflate(info);
  217. }
  218. void TBBarGraph::SetValueDouble(double value)
  219. {
  220. value = CLAMP(value, 0.0, 100.0);
  221. if (value == m_value)
  222. return;
  223. m_value = value;
  224. InvalidateSkinStates();
  225. Invalidate();
  226. }
  227. void TBBarGraph::SetAxis(AXIS axis)
  228. {
  229. m_axis = axis;
  230. InvalidateSkinStates();
  231. Invalidate();
  232. }
  233. TB_WIDGET_FACTORY(TBBarGraph, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  234. }; // namespace tb