Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include "../JuceLibraryCode/JuceHeader.h"
  2. #include "InputForm.h"
  3.  
  4. InputForm::InputForm()
  5. {
  6. addAndMakeVisible(m_usernameLabel);
  7. m_usernameLabel.setText("Username:", dontSendNotification);
  8. m_usernameLabel.attachToComponent(&m_usernameInput, true);
  9. m_usernameLabel.setColour(Label::textColourId, Colours::orange);
  10. m_usernameLabel.setJustificationType(Justification::right);
  11.  
  12. addAndMakeVisible(m_passwordLabel);
  13. m_passwordLabel.setText("Password:", dontSendNotification);
  14. m_passwordLabel.attachToComponent(&m_passwordInput, true);
  15. m_passwordLabel.setColour(Label::textColourId, Colours::orange);
  16. m_passwordLabel.setJustificationType(Justification::right);
  17.  
  18. addAndMakeVisible(m_usernameInput);
  19. m_usernameInput.setEditable(true);
  20. m_usernameInput.setColour(Label::backgroundColourId, Colours::darkblue);
  21. m_usernameInput.setJustificationType(Justification::left);
  22.  
  23. addAndMakeVisible(m_passwordInput);
  24. m_passwordInput.setEditable(true);
  25. m_passwordInput.setColour(Label::backgroundColourId, Colours::darkblue);
  26. m_passwordInput.setJustificationType(Justification::left);
  27.  
  28. m_clearActive = false;
  29. m_submitActive = false;
  30.  
  31. m_usernameInput.addListener(this);
  32. m_passwordInput.addListener(this);
  33. }
  34.  
  35. InputForm::~InputForm()
  36. {
  37. m_usernameInput.removeListener(this);
  38. m_passwordInput.removeListener(this);
  39. }
  40.  
  41. void InputForm::paint (Graphics& g)
  42. {
  43.  
  44. g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId)); // clear the background
  45.  
  46. g.setColour (Colours::grey);
  47. g.drawRect (getLocalBounds(), 1);
  48.  
  49. }
  50.  
  51. void InputForm::resized()
  52. {
  53. auto marginX = 10;
  54. auto marginY = 10;
  55. auto localBounds = getLocalBounds();
  56.  
  57. auto usernameRect = localBounds.removeFromTop(50);
  58. m_usernameLabel.setBounds(usernameRect.removeFromLeft(usernameRect.getWidth() / 5).reduced(marginX, marginY));
  59. m_usernameInput.setBounds(usernameRect.reduced(marginX, marginY));
  60.  
  61. auto passwordRect = localBounds.removeFromTop(50);
  62. m_passwordLabel.setBounds(passwordRect.removeFromLeft(passwordRect.getWidth() / 5).reduced(marginX));
  63. m_passwordInput.setBounds(passwordRect.reduced(marginX, marginY));
  64. }
  65.  
  66.  
  67. void InputForm::addListener(Listener* listenerToAdd)
  68. {
  69. m_listeners.add(listenerToAdd);
  70. }
  71.  
  72. void InputForm::removeListener(Listener* listenerToRemove)
  73. {
  74. jassert(m_listeners.contains(listenerToRemove));
  75. m_listeners.remove(listenerToRemove);
  76. }
  77.  
  78. void InputForm::clearActivate(bool isActive)
  79. {
  80. m_listeners.call([this, isActive](Listener& l) { l.clearActivated(this, isActive); });
  81. }
  82.  
  83. void InputForm::submitActivate(bool isActive)
  84. {
  85. m_listeners.call([this, isActive](Listener& l) { l.submitActivated(this, isActive); });
  86. }
  87.  
  88. void InputForm::labelTextChanged(Label *labelThatHasChanged)
  89. {
  90. bool atLeastOneTextFilled = false;
  91.  
  92. if (labelThatHasChanged == &m_usernameInput) {
  93. atLeastOneTextFilled = !m_usernameInput.getText(false).isEmpty();
  94. }
  95. else if (labelThatHasChanged == &m_passwordInput) {
  96. atLeastOneTextFilled = !m_passwordInput.getText(false).isEmpty();
  97. }
  98.  
  99. if (!m_clearActive && atLeastOneTextFilled) {
  100. m_clearActive = true;
  101. clearActivate(true);
  102. }
  103. else if (m_clearActive && !atLeastOneTextFilled) {
  104. m_clearActive = false;
  105. clearActivate(false);
  106. }
  107.  
  108. bool allTextFilled = !m_usernameInput.getText(true).isEmpty() && !m_passwordInput.getText(true).isEmpty();
  109. if (!m_submitActive && allTextFilled) {
  110. m_submitActive = true;
  111. submitActivate(true);
  112. }
  113. else if (m_submitActive && !allTextFilled) {
  114. m_submitActive = false;
  115. submitActivate(false);
  116. }
  117. }
  118.  
  119. void InputForm::submit()
  120. {
  121. // do nothing
  122. }
  123. void InputForm::clear()
  124. {
  125. m_usernameInput.setText("", NotificationType::sendNotification);
  126. m_passwordInput.setText("", NotificationType::sendNotification);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement