tb_atomic_widgets.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.0)
  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::SetAlpha ( float value )
  45. {
  46. if ( value < 0.0 || value > 1.0 )
  47. {
  48. alpha_ = 1.0;
  49. return;
  50. }
  51. alpha_ = value;
  52. InvalidateSkinStates();
  53. Invalidate();
  54. }
  55. void TBColorWidget::OnPaint(const PaintProps &paint_props)
  56. {
  57. TBRect local_rect = GetRect();
  58. local_rect.x = 0;
  59. local_rect.y = 0;
  60. float old_opacity = g_renderer->GetOpacity();
  61. g_renderer->SetOpacity(alpha_);
  62. g_renderer->DrawRectFill(local_rect, color_);
  63. g_renderer->SetOpacity(old_opacity);
  64. }
  65. void TBColorWidget::OnInflate(const INFLATE_INFO &info)
  66. {
  67. if (const char *colr = info.node->GetValueString("color", nullptr))
  68. SetColor(colr);
  69. TBWidget::OnInflate(info);
  70. }
  71. TB_WIDGET_FACTORY(TBColorWidget, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  72. // == TBColorWheel =======================================
  73. TBColorWheel::TBColorWheel() :
  74. markerx_(128),
  75. markery_(128),
  76. markercolor_(),
  77. hue_(0.0),
  78. saturation_(0.0)
  79. {
  80. }
  81. void TBColorWheel::OnPaint(const PaintProps &paint_props)
  82. {
  83. TBWidget::OnPaint(paint_props); // draw the widget stuff
  84. TBRect local_rect ( 0,0,4,4 ); // AND draw a marker where we clicked.
  85. local_rect.x = markerx_ - 2;
  86. local_rect.y = markery_ - 2;
  87. g_renderer->DrawRect( local_rect, markercolor_);
  88. local_rect.x -= 1;
  89. local_rect.y -= 1;
  90. local_rect.w += 2;
  91. local_rect.h += 2;
  92. g_renderer->DrawRect( local_rect, markercolor_); // draw double box
  93. }
  94. bool TBColorWheel::OnEvent(const TBWidgetEvent &ev)
  95. {
  96. if (ev.target == this && ev.type == EVENT_TYPE_CLICK)
  97. {
  98. SetMarkerX ( ev.target_x );
  99. SetMarkerY ( ev.target_y );
  100. CalcHueSaturation( markerx_, markery_ );
  101. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  102. InvokeEvent(ev);
  103. }
  104. return TBWidget::OnEvent(ev);
  105. }
  106. void TBColorWheel::SetHueSaturation ( float hue, float saturation )
  107. {
  108. // suppose to set the marker position to match HS here
  109. hue_ = hue * 360.0;
  110. saturation_ = saturation * 128.0;
  111. Invalidate();
  112. }
  113. void TBColorWheel::CalcHueSaturation ( int rawx, int rawy )
  114. {
  115. TBRect rect = GetRect();
  116. int centerx = rect.w / 2;
  117. int centery = rect.h / 2;
  118. float X1 = rawx;
  119. float Y1 = rawy;
  120. float X2 = centerx;
  121. float Y2 = centery;
  122. float angle = 0.0;
  123. float xd = X2-X1;
  124. float yd = Y2-Y1;
  125. float dx = sqrt(xd * xd + yd * yd);
  126. // angle in degrees
  127. angle = atan2(Y2 - Y1, X2 - X1) * 180 / 3.14159265358979323846;
  128. if (angle < 0) angle += 360.0;
  129. // if the distance > 128, can we calculate the line point at 128 and set the marker there?
  130. if( dx > 128.0 ) dx = 128.0; // limit value
  131. saturation_ = dx;
  132. hue_ = angle;
  133. }
  134. void TBColorWheel::SetMarkerX ( int value )
  135. {
  136. markerx_ = value;
  137. }
  138. void TBColorWheel::SetMarkerY ( int value )
  139. {
  140. markery_ = value;
  141. }
  142. void TBColorWheel::SetMarkerColor ( const char *name )
  143. {
  144. if ( name )
  145. markercolor_.SetFromString(name, strlen(name));
  146. Invalidate();
  147. }
  148. void TBColorWheel::OnInflate(const INFLATE_INFO &info)
  149. {
  150. if (const char *colr = info.node->GetValueString("color", nullptr))
  151. SetMarkerColor(colr);
  152. TBWidget::OnInflate(info);
  153. }
  154. TB_WIDGET_FACTORY(TBColorWheel, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  155. }; // namespace tb