DemoButtonGenerator.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. unit DemoButtonGenerator;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Fresnel.DOM, Fresnel.Controls, Fresnel.Classes, fpCSSTree, Fresnel.DemoSlider,
  6. Fresnel.DemoCheckBox;
  7. type
  8. { TDemoButtonGenerator }
  9. TDemoButtonGenerator = class(TDiv)
  10. private
  11. procedure OnPropChange(Sender: TObject);
  12. public
  13. const
  14. BackgroundColor1 ='#79bbff';
  15. BackgroundColor2 ='#378de5';
  16. BorderColor ='#337bc4';
  17. cStyle =
  18. '#ButtonDiv {'+LineEnding
  19. +'background:'+BackgroundColor1+';'+LineEnding
  20. +'border:1px solid '+BorderColor+';'+LineEnding
  21. +'padding:16px 31px;'+LineEnding
  22. +'font-size:15px; font-family:Arial; font-weight:bold;'+LineEnding
  23. +'color:#fff;'+LineEnding
  24. +'}'+LineEnding
  25. +'.CheckBox {'+LineEnding
  26. +' margin: 6px;'+LineEnding
  27. +'}'+LineEnding
  28. +'.Slider {'+LineEnding
  29. +' margin: 6px;'+LineEnding
  30. +' width: 150px;'+LineEnding
  31. +'}'+LineEnding;
  32. var
  33. ButtonDiv: TDiv;
  34. ButtonLabel: TLabel;
  35. FontSizeSlider: TDemoSlider;
  36. PaddingVerticalSlider: TDemoSlider;
  37. GradientChkBox: TDemoCheckBox;
  38. BorderWidthSlider: TDemoSlider;
  39. BorderRadiusDiv: TDiv;
  40. BorderRadiusLabel: TLabel;
  41. BorderRadiusTopLeftChkBox: TDemoCheckBox;
  42. BorderRadiusTopRightChkBox: TDemoCheckBox;
  43. BorderRadiusBottomLeftChkBox: TDemoCheckBox;
  44. BorderRadiusBottomRightChkBox: TDemoCheckBox;
  45. BorderRadiusSlider: TDemoSlider;
  46. TextShadowChkBox: TDemoCheckBox;
  47. TextShadowVertPosSlider: TDemoSlider;
  48. TextShadowHorzPosSlider: TDemoSlider;
  49. TextShadowBlurRadiusSlider: TDemoSlider;
  50. constructor Create(AOwner: TComponent); override;
  51. class function GetCSSTypeStyle: TCSSString; override;
  52. procedure UpdateButton;
  53. end;
  54. implementation
  55. { TDemoButtonGenerator }
  56. procedure TDemoButtonGenerator.OnPropChange(Sender: TObject);
  57. begin
  58. UpdateButton;
  59. end;
  60. constructor TDemoButtonGenerator.Create(AOwner: TComponent);
  61. function AddSlider(aName, aCaption: string; aParent: TFresnelElement = nil): TDemoSlider;
  62. begin
  63. if aParent=nil then aParent:=Self;
  64. Result:=TDemoSlider.Create(Self);
  65. with Result do begin
  66. Name:=aName;
  67. Caption:=aCaption;
  68. ValueFormat:='%.0fpx';
  69. Parent:=aParent;
  70. OnChange:=@OnPropChange;
  71. end;
  72. end;
  73. function AddCheckBox(aName, aCaption: string; aParent: TFresnelElement = nil): TDemoCheckBox;
  74. begin
  75. if aParent=nil then aParent:=Self;
  76. Result:=TDemoCheckBox.Create(Self);
  77. with Result do begin
  78. Name:=aName;
  79. Caption:=aCaption;
  80. Checked:=true;
  81. Parent:=aParent;
  82. OnChange:=@OnPropChange;
  83. end;
  84. end;
  85. begin
  86. inherited Create(AOwner);
  87. // font-size slider
  88. FontSizeSlider:=AddSlider('FontSizeSlider','Font Size:');
  89. with FontSizeSlider do begin
  90. MinPosition:=5;
  91. MaxPosition:=40;
  92. SliderPosition:=12;
  93. end;
  94. // padding vertical slider
  95. PaddingVerticalSlider:=AddSlider('PaddingVerticalSlider','Vertical Padding:');
  96. with PaddingVerticalSlider do begin
  97. MinPosition:=0;
  98. MaxPosition:=50;
  99. SliderPosition:=16;
  100. end;
  101. // gradient
  102. GradientChkBox:=AddCheckBox('GradientChkBox','Gradient');
  103. // border-width slider
  104. BorderWidthSlider:=AddSlider('BorderWidthSlider','Border Width:');
  105. with BorderWidthSlider do begin
  106. MinPosition:=0;
  107. MaxPosition:=10;
  108. SliderPosition:=1;
  109. end;
  110. // border-radius corners: four checkboxes
  111. BorderRadiusDiv:=TDiv.Create(Self);
  112. with BorderRadiusDiv do begin
  113. Name:='BorderRadiusDiv';
  114. Style:='margin: 6px;';
  115. Parent:=Self;
  116. end;
  117. BorderRadiusLabel:=TLabel.Create(Self);
  118. with BorderRadiusLabel do begin
  119. Name:='BorderRadiusLabel';
  120. Caption:='Border Corner Radius:';
  121. Parent:=BorderRadiusDiv;
  122. end;
  123. BorderRadiusTopLeftChkBox:=AddCheckBox('BorderRadiusTopLeftChkBox','Top Left',BorderRadiusDiv);
  124. BorderRadiusTopRightChkBox:=AddCheckBox('BorderRadiusTopRightChkBox','Top Right',BorderRadiusDiv);
  125. BorderRadiusBottomLeftChkBox:=AddCheckBox('BorderRadiusBottomLeftChkBox','Bottom Left',BorderRadiusDiv);
  126. BorderRadiusBottomRightChkBox:=AddCheckBox('BorderRadiusBottomRightChkBox','Bottom Right',BorderRadiusDiv);
  127. // slider for border radius
  128. BorderRadiusSlider:=AddSlider('BorderRadiusSlider','Border Radius:');
  129. with BorderRadiusSlider do begin
  130. MinPosition:=0;
  131. MaxPosition:=50;
  132. SliderPosition:=16;
  133. end;
  134. // text-shadow checkbox
  135. TextShadowChkBox:=AddCheckBox('TextShadowChkBox','Text Shadow');
  136. // slider for text-shadow vertical position
  137. TextShadowVertPosSlider:=AddSlider('TextShadowVertPosSlider','Vertical Position:');
  138. with TextShadowVertPosSlider do begin
  139. MinPosition:=-30;
  140. MaxPosition:=30;
  141. SliderPosition:=1;
  142. end;
  143. // slider for text-shadow horizontal position
  144. TextShadowHorzPosSlider:=AddSlider('TextShadowHorzPosSlider','Horizontal Position:');
  145. with TextShadowHorzPosSlider do begin
  146. MinPosition:=-30;
  147. MaxPosition:=30;
  148. SliderPosition:=1;
  149. end;
  150. // slider for text-shadow horizontal position
  151. TextShadowBlurRadiusSlider:=AddSlider('TextShadowBlurRadiusSlider','Blur Radius:');
  152. with TextShadowBlurRadiusSlider do begin
  153. ValueFormat:='%.1fpx';
  154. MinPosition:=0;
  155. MaxPosition:=5;
  156. SliderPosition:=0;
  157. end;
  158. // the Button
  159. ButtonDiv:=TDiv.Create(Self);
  160. with ButtonDiv do begin
  161. Name:='ButtonDiv';
  162. Parent:=Self;
  163. end;
  164. ButtonLabel:=TLabel.Create(Self);
  165. with ButtonLabel do begin
  166. Name:='ButtonLabel';
  167. Caption:='Button Text';
  168. Parent:=ButtonDiv;
  169. end;
  170. UpdateButton;
  171. end;
  172. class function TDemoButtonGenerator.GetCSSTypeStyle: TCSSString;
  173. begin
  174. Result:=cStyle;
  175. end;
  176. procedure TDemoButtonGenerator.UpdateButton;
  177. var
  178. s, NewStyle, Radius: String;
  179. begin
  180. if ButtonDiv=nil then exit;
  181. NewStyle:=
  182. 'font-size:'+FloatToCSSPx(FontSizeSlider.SliderPosition)+';'
  183. +'padding: '+FloatToCSSPx(PaddingVerticalSlider.SliderPosition)+' 31px;'
  184. +'border-width:'+FloatToCSSPx(BorderWidthSlider.SliderPosition)+';';
  185. if GradientChkBox.Checked then
  186. NewStyle+='background-image:linear-gradient('+BackgroundColor1+','+BackgroundColor2+');';
  187. Radius:=FloatToCSSPx(BorderRadiusSlider.SliderPosition);
  188. s:='';
  189. if BorderRadiusTopLeftChkBox.Checked then
  190. s:=Radius
  191. else
  192. s:='0';
  193. if BorderRadiusTopRightChkBox.Checked then
  194. s+=' '+Radius
  195. else
  196. s+=' 0';
  197. if BorderRadiusBottomLeftChkBox.Checked then
  198. s+=' '+Radius
  199. else
  200. s+=' 0';
  201. if BorderRadiusBottomRightChkBox.Checked then
  202. s+=' '+Radius
  203. else
  204. s+=' 0';
  205. NewStyle+='border-radius:'+s+';';
  206. s:='';
  207. if TextShadowChkBox.Checked then
  208. begin
  209. s:=FloatToCSSStr(TextShadowHorzPosSlider.SliderPosition)
  210. +' '+FloatToCSSStr(TextShadowVertPosSlider.SliderPosition)
  211. +' '+FloatToCSSStr(TextShadowBlurRadiusSlider.SliderPosition)
  212. +' #2f6627';
  213. end;
  214. NewStyle+='text-shadow:'+s+';';
  215. ButtonDiv.Style:=NewStyle;
  216. end;
  217. end.