sincerely
Singaporean
If you have not done so, read this full tutorial on how to use SGEXTN to build an application.
See here for the previous part of the tutorial.
In the previous part, we wrote behaviours for the options page buttons controlling background mode.
Next, we will write the behaviour for the "H", "S", and "L" under the "complementary" background colour mode. These simply need to tiggle when clicked. We can write 3 functions for the 3 buttons.
However, there is actually a simpler way. SGWidget ⁽㈳㈴㈳㈮㈱㈨㈠㈫ ㈧㈤㈱㈤⁾ UI elements that are interactive always have 4 versions for every callback function ⁽㈳㈴㈳㈮㈱㈨㈠㈫ ㈧㈤㈱㈤⁾. 1 takes nothing as argument, which is what we have been using so far (SGWButton::clickFunction). The other 3 takes a int (SGWButton::clickFunctionWithInt), a SGXString (SGWButton::clickFunctionWithString), and a pointer to the UI element (SGWButton::clickFunctionWithPointer) respectively.
We can write a single function taking a pointer and simply toggles the selection state of whatever SGWButton that the pointer points to.
First declare the function inside SGCLPOptionsPage
static void backgroundComplementaryChannelToggle(SGWButton* x);
then implement it
void SGCLPOptionsPage::backgroundComplementaryChannelToggle(SGWButton *x){ if((*x).getSelected() == true){(*x).setSelected(false);} else{(*x).setSelected(true);} }
actually make the buttons use it when creating the buttons in SGCLPOptionsPage::initialise
SGCLPOptionsPage::backgroundComplementaryHueButton = new SGWTextButton(bg, "H", nullptr, 0.5f, 0.0f, 0.0f, 8.5f, 1.0f / 6.0f, -1.0f / 6.0f, 0.0f, 1.0f); (*SGCLPOptionsPage::backgroundComplementaryHueButton).clickFunctionWithPointer = &SGCLPOptionsPage::backgroundComplementaryChannelToggle; SGCLPOptionsPage::backgroundComplementarySaturationButton = new SGWTextButton(bg, "S", nullptr, 2.0f / 3.0f, -1.0f / 6.0f, 0.0f, 8.5f, 1.0f / 6.0f, -1.0f / 6.0f, 0.0f, 1.0f); (*SGCLPOptionsPage::backgroundComplementarySaturationButton).clickFunctionWithPointer = &SGCLPOptionsPage::backgroundComplementaryChannelToggle; SGCLPOptionsPage::backgroundComplementaryLightnessButton = new SGWTextButton(bg, "L", nullptr, 5.0f / 6.0f, -1.0f / 3.0f, 0.0f, 8.5f, 1.0f / 6.0f, -1.0f / 6.0f, 0.0f, 1.0f); (*SGCLPOptionsPage::backgroundComplementaryLightnessButton).clickFunctionWithPointer = &SGCLPOptionsPage::backgroundComplementaryChannelToggle;
and set up reasonable default choices in SGCLPOptionsPage::reset
(*SGCLPOptionsPage::backgroundComplementaryHueButton).setSelected(false); (*SGCLPOptionsPage::backgroundComplementarySaturationButton).setSelected(false); (*SGCLPOptionsPage::backgroundComplementaryLightnessButton).setSelected(true);
Testing the code confirms that it works.
See here for the next part of the tutorial.
©2025 05524F.sg (Singapore)