Presets.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*\
  2. |*| Copyright 2015-2016 bill-auger <https://github.com/bill-auger/av-caster/issues>
  3. |*|
  4. |*| This file is part of the AvCaster program.
  5. |*|
  6. |*| AvCaster is free software: you can redistribute it and/or modify
  7. |*| it under the terms of the GNU General Public License version 3
  8. |*| as published by the Free Software Foundation.
  9. |*|
  10. |*| AvCaster is distributed in the hope that it will be useful,
  11. |*| but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. |*| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. |*| GNU General Public License for more details.
  14. |*|
  15. |*| You should have received a copy of the GNU General Public License
  16. |*| along with AvCaster. If not, see <http://www.gnu.org/licenses/>.
  17. \*/
  18. //[Headers] You can add your own extra header files here...
  19. #include "../Controllers/AvCaster.h"
  20. #include "../Trace/TraceConfig.h"
  21. //[/Headers]
  22. #include "Presets.h"
  23. //[MiscUserDefs] You can add your own user definitions and misc code here...
  24. //[/MiscUserDefs]
  25. //==============================================================================
  26. Presets::Presets (MainContent* main_content)
  27. : mainContent(main_content)
  28. {
  29. //[Constructor_pre] You can add your own custom stuff here..
  30. //[/Constructor_pre]
  31. addAndMakeVisible (presetsGroup = new GroupComponent ("presetsGroup",
  32. TRANS("Presets")));
  33. presetsGroup->setColour (GroupComponent::outlineColourId, Colours::white);
  34. presetsGroup->setColour (GroupComponent::textColourId, Colours::white);
  35. addAndMakeVisible (newPresetButton = new TextButton ("newPresetButton"));
  36. newPresetButton->setExplicitFocusOrder (1);
  37. newPresetButton->setButtonText (TRANS("New Preset"));
  38. newPresetButton->addListener (this);
  39. addAndMakeVisible (deletePresetButton = new TextButton ("deletePresetButton"));
  40. deletePresetButton->setExplicitFocusOrder (2);
  41. deletePresetButton->setButtonText (TRANS("Delete Preset"));
  42. deletePresetButton->addListener (this);
  43. addAndMakeVisible (presetLabel = new Label ("presetLabel",
  44. TRANS("Preset:")));
  45. presetLabel->setFont (Font (15.00f, Font::plain));
  46. presetLabel->setJustificationType (Justification::centredLeft);
  47. presetLabel->setEditable (false, false, false);
  48. presetLabel->setColour (Label::textColourId, Colours::white);
  49. presetLabel->setColour (TextEditor::textColourId, Colours::black);
  50. presetLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  51. addAndMakeVisible (presetsCombo = new ComboBox ("presetsCombo"));
  52. presetsCombo->setExplicitFocusOrder (3);
  53. presetsCombo->setEditableText (true);
  54. presetsCombo->setJustificationType (Justification::centredLeft);
  55. presetsCombo->setTextWhenNothingSelected (String());
  56. presetsCombo->setTextWhenNoChoicesAvailable (TRANS("(no choices)"));
  57. presetsCombo->addListener (this);
  58. addAndMakeVisible (configButton = new ImageButton ("configButton"));
  59. configButton->setExplicitFocusOrder (4);
  60. configButton->addListener (this);
  61. configButton->setImages (false, true, true,
  62. ImageCache::getFromMemory (BinaryData::config_png, BinaryData::config_pngSize), 1.000f, Colour (0x00000000),
  63. ImageCache::getFromMemory (BinaryData::confighover_png, BinaryData::confighover_pngSize), 1.000f, Colour (0x00000000),
  64. ImageCache::getFromMemory (BinaryData::configpushed_png, BinaryData::configpushed_pngSize), 1.000f, Colour (0x00000000));
  65. //[UserPreSize]
  66. //[/UserPreSize]
  67. setSize (600, 400);
  68. //[Constructor] You can add your own custom stuff here..
  69. this->mainContent->configureCombobox(this->presetsCombo) ;
  70. //[/Constructor]
  71. }
  72. Presets::~Presets()
  73. {
  74. //[Destructor_pre]. You can add your own custom destruction code here..
  75. //[/Destructor_pre]
  76. presetsGroup = nullptr;
  77. newPresetButton = nullptr;
  78. deletePresetButton = nullptr;
  79. presetLabel = nullptr;
  80. presetsCombo = nullptr;
  81. configButton = nullptr;
  82. //[Destructor]. You can add your own custom destruction code here..
  83. //[/Destructor]
  84. }
  85. //==============================================================================
  86. void Presets::paint (Graphics& g)
  87. {
  88. //[UserPrePaint] Add your own custom painting code here..
  89. //[/UserPrePaint]
  90. g.setColour (Colour (0xff282828));
  91. g.fillRoundedRectangle (20.0f, 22.0f, static_cast<float> (getWidth() - 40), 52.0f, 4.000f);
  92. //[UserPaint] Add your own custom painting code here..
  93. //[/UserPaint]
  94. }
  95. void Presets::resized()
  96. {
  97. //[UserPreResize] Add your own custom resize code here..
  98. //[/UserPreResize]
  99. presetsGroup->setBounds (16, 12, getWidth() - 32, 64);
  100. newPresetButton->setBounds (268, 36, 96, 24);
  101. deletePresetButton->setBounds (388, 36, 96, 24);
  102. presetLabel->setBounds (500, 36, 48, 24);
  103. presetsCombo->setBounds (560, 36, 128, 24);
  104. configButton->setBounds (696, 36, 24, 24);
  105. //[UserResized] Add your own custom resize handling here..
  106. //[/UserResized]
  107. }
  108. void Presets::buttonClicked (Button* buttonThatWasClicked)
  109. {
  110. //[UserbuttonClicked_Pre]
  111. Identifier key ;
  112. var value = var(buttonThatWasClicked->getToggleState()) ;
  113. //[/UserbuttonClicked_Pre]
  114. if (buttonThatWasClicked == newPresetButton)
  115. {
  116. //[UserButtonCode_newPresetButton] -- add your button handler code here..
  117. handleNewButton() ; return ;
  118. //[/UserButtonCode_newPresetButton]
  119. }
  120. else if (buttonThatWasClicked == deletePresetButton)
  121. {
  122. //[UserButtonCode_deletePresetButton] -- add your button handler code here..
  123. handleDeleteButton() ; return ;
  124. //[/UserButtonCode_deletePresetButton]
  125. }
  126. else if (buttonThatWasClicked == configButton)
  127. {
  128. //[UserButtonCode_configButton] -- add your button handler code here..
  129. if (!createOrUpdatePreset()) return ;
  130. key = CONFIG::IS_PENDING_ID ;
  131. value = var(false) ;
  132. //[/UserButtonCode_configButton]
  133. }
  134. //[UserbuttonClicked_Post]
  135. AvCaster::SetValue(key , value) ;
  136. //[/UserbuttonClicked_Post]
  137. }
  138. void Presets::comboBoxChanged (ComboBox* comboBoxThatHasChanged)
  139. {
  140. //[UsercomboBoxChanged_Pre]
  141. //[/UsercomboBoxChanged_Pre]
  142. if (comboBoxThatHasChanged == presetsCombo)
  143. {
  144. //[UserComboBoxCode_presetsCombo] -- add your combo box handling code here..
  145. handlePresetsCombo() ; return ;
  146. //[/UserComboBoxCode_presetsCombo]
  147. }
  148. //[UsercomboBoxChanged_Post]
  149. //[/UsercomboBoxChanged_Post]
  150. }
  151. //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
  152. void Presets::broughtToFront()
  153. {
  154. this->mainContent->loadPresetsCombo(this->presetsCombo) ; setCreatePresetMode(false) ;
  155. }
  156. void Presets::handleNewButton()
  157. {
  158. this->presetsCombo->setText(String::empty , juce::dontSendNotification) ;
  159. this->presetsCombo->grabKeyboardFocus() ;
  160. setCreatePresetMode(true) ;
  161. }
  162. void Presets::handleDeleteButton()
  163. {
  164. if (isCreatePresetMode())
  165. {
  166. setCreatePresetMode(false) ;
  167. this->presetsCombo->setText(AvCaster::GetPresetName() , juce::dontSendNotification) ;
  168. }
  169. else if (AvCaster::IsStaticPreset()) AvCaster::ResetPreset() ;
  170. else AvCaster::DeletePreset() ;
  171. }
  172. bool Presets::createOrUpdatePreset()
  173. {
  174. String preset_name = this->presetsCombo->getText() ;
  175. int option_n = this->presetsCombo->getSelectedItemIndex() ;
  176. String stored_name = AvCaster::GetPresetName() ;
  177. int stored_idx = AvCaster::GetPresetIdx() ;
  178. bool is_create_preset_mode = isCreatePresetMode() ;
  179. bool is_static_preset = AvCaster::IsStaticPreset() ;
  180. bool is_valid_option = !!(~option_n) ;
  181. bool has_selection_changed = option_n != stored_idx ;
  182. bool has_name_changed = !has_selection_changed && preset_name != stored_name ;
  183. bool is_empty_preset_name = preset_name.isEmpty() ;
  184. bool should_rename_preset = !is_static_preset && !is_valid_option &&
  185. has_name_changed && !is_empty_preset_name ;
  186. bool is_name_collision = false ;
  187. // reject duplicate preset name if creating or renaming
  188. for (int preset_n = 0 ; preset_n < this->presetsCombo->getNumItems() ; ++preset_n)
  189. if (this->presetsCombo->getItemText(preset_n) == preset_name) is_name_collision = true ;
  190. DEBUG_TRACE_CREATE_PRESET
  191. // validate new preset name existence
  192. if (is_empty_preset_name) { AvCaster::Warning(GUI::PRESET_NAME_ERROR_MSG) ; return false ; }
  193. if (is_create_preset_mode || should_rename_preset)
  194. {
  195. // validate preset name uniqueness
  196. if (is_name_collision) { AvCaster::Warning(GUI::PRESET_RENAME_ERROR_MSG) ; return false ; }
  197. // create preset
  198. if (is_create_preset_mode) AvCaster::StorePreset (preset_name) ;
  199. // rename preset
  200. else if (should_rename_preset ) AvCaster::RenamePreset(preset_name) ;
  201. }
  202. // update preset
  203. else AvCaster::StorePreset(stored_name) ;
  204. setCreatePresetMode(false) ;
  205. return true ;
  206. }
  207. void Presets::handlePresetsCombo()
  208. {
  209. if (this->deletePresetButton->isDown()) return ; // defer to handleDeleteButton()
  210. // create, rename or update preset or revert
  211. String preset_name = this->presetsCombo->getText() ;
  212. int option_n = this->presetsCombo->getSelectedItemIndex() ;
  213. int stored_option_n = AvCaster::GetPresetIdx() ;
  214. bool should_revert_idx = !createOrUpdatePreset() ;
  215. int current_option_n = (should_revert_idx)? stored_option_n : option_n ;
  216. AvCaster::SetValue(CONFIG::PRESET_ID , current_option_n) ;
  217. }
  218. void Presets::setCreatePresetMode(bool is_pending_new_preset_name)
  219. {
  220. bool is_static_preset = AvCaster::IsStaticPreset() ;
  221. bool should_be_editable = is_pending_new_preset_name || !is_static_preset ;
  222. String button_text = (is_static_preset ) ? GUI::DELETE_BTN_RESET_TEXT :
  223. (is_pending_new_preset_name) ? GUI::DELETE_BTN_CANCEL_TEXT :
  224. GUI::DELETE_BTN_DELETE_TEXT ;
  225. DEBUG_TRACE_SET_CREATE_PRESET_MODE
  226. this->presetsCombo ->setEditableText(should_be_editable) ;
  227. this->deletePresetButton->setButtonText (button_text) ;
  228. if (should_be_editable) this->presetsCombo->showEditor() ;
  229. }
  230. bool Presets::isCreatePresetMode()
  231. {
  232. return this->deletePresetButton->getButtonText() == GUI::DELETE_BTN_CANCEL_TEXT ;
  233. }
  234. //[/MiscUserCode]
  235. //==============================================================================
  236. #if 0
  237. /* -- Projucer information section --
  238. This is where the Projucer stores the metadata that describe this GUI layout, so
  239. make changes in here at your peril!
  240. BEGIN_JUCER_METADATA
  241. <JUCER_COMPONENT documentType="Component" className="Presets" componentName=""
  242. parentClasses="public Component" constructorParams="MainContent* main_content"
  243. variableInitialisers="mainContent(main_content)" snapPixels="8"
  244. snapActive="1" snapShown="1" overlayOpacity="0.330" fixedSize="0"
  245. initialWidth="600" initialHeight="400">
  246. <BACKGROUND backgroundColour="0">
  247. <ROUNDRECT pos="20 22 40M 52" cornerSize="4" fill="solid: ff282828" hasStroke="0"/>
  248. </BACKGROUND>
  249. <GROUPCOMPONENT name="presetsGroup" id="5f4ffe47101cb73b" memberName="presetsGroup"
  250. virtualName="" explicitFocusOrder="0" pos="16 12 32M 64" outlinecol="ffffffff"
  251. textcol="ffffffff" title="Presets"/>
  252. <TEXTBUTTON name="newPresetButton" id="e71f2f34ab7772e6" memberName="newPresetButton"
  253. virtualName="" explicitFocusOrder="1" pos="268 36 96 24" buttonText="New Preset"
  254. connectedEdges="0" needsCallback="1" radioGroupId="0"/>
  255. <TEXTBUTTON name="deletePresetButton" id="693a3f523732acb3" memberName="deletePresetButton"
  256. virtualName="" explicitFocusOrder="2" pos="388 36 96 24" buttonText="Delete Preset"
  257. connectedEdges="0" needsCallback="1" radioGroupId="0"/>
  258. <LABEL name="presetLabel" id="3a60504146c5134" memberName="presetLabel"
  259. virtualName="" explicitFocusOrder="0" pos="500 36 48 24" textCol="ffffffff"
  260. edTextCol="ff000000" edBkgCol="0" labelText="Preset:" editableSingleClick="0"
  261. editableDoubleClick="0" focusDiscardsChanges="0" fontname="Default font"
  262. fontsize="15" bold="0" italic="0" justification="33"/>
  263. <COMBOBOX name="presetsCombo" id="94d77976c2b2f37" memberName="presetsCombo"
  264. virtualName="" explicitFocusOrder="3" pos="560 36 128 24" editable="1"
  265. layout="33" items="" textWhenNonSelected="" textWhenNoItems="(no choices)"/>
  266. <IMAGEBUTTON name="configButton" id="19b48645d13bf310" memberName="configButton"
  267. virtualName="" explicitFocusOrder="4" pos="696 36 24 24" buttonText="configButton"
  268. connectedEdges="0" needsCallback="1" radioGroupId="0" keepProportions="1"
  269. resourceNormal="BinaryData::config_png" opacityNormal="1" colourNormal="0"
  270. resourceOver="BinaryData::confighover_png" opacityOver="1" colourOver="0"
  271. resourceDown="BinaryData::configpushed_png" opacityDown="1" colourDown="0"/>
  272. </JUCER_COMPONENT>
  273. END_JUCER_METADATA
  274. */
  275. #endif
  276. //[EndFile] You can add extra defines here...
  277. //[/EndFile]