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 that control what is inverted when using complementary mode for background colour.
Next, we can build the pattern selection buttons. If "star" or "regular polygon" is selected, the user can customise the number of vertices that it has, so we need to show SGCLPOptionsPage::polygonSideCountInput. Otherwise there is no need to show the input field and it will be hidden to reveal the "not applicable" text below.
We can write 4 functions, 1 per button, but that is annoying.
Using what we learnt from the last part of this tutorial, we can use SGWButton::clickFunctionWithPointer to get a pointer to the button being clicked and make that selected. The problem with that is we do not know whether to hide or show the input field without keeping track of additional things...
What if there is a way to store non displayed information inside a UI element?
In QWidget, we would subclass the UI element and add our own field. However, this is annoying to do once you have many types of UI elements doing various things.
In QML, we would add a property. Though that sounds like nonsense from JS and not nice at all.
SGEXTN's approach to this issue is attaching a int and a SGXString to every interactive UI element. In this case we can use SGWButton::attachedInt. We can set it to 0 if the pattern does not need a vertex count, and 1 otherwise.
Inside the callback function ⁽㈳㈴㈳㈮㈱㈨㈠㈫ ㈧㈤㈱㈤⁾, we can just check SGWButton::attachedInt to determine what to do. This is simple and does not use JS nonsense.
We then can implement this.
First we declare a function in SGCLPOptionsPage
static void patternSelect(SGWButton* x);
Then when creating the buttons, not only do we set the associated callback function ⁽㈳㈴㈳㈮㈱㈨㈠㈫ ㈧㈤㈱㈤⁾, we also set the SGWButton::attachedInt.
SGCLPOptionsPage::patternCircleButton = new SGWTextButton(bg, "circle", nullptr, 0.0f, 0.5f, 0.0f, 12.0f, 0.5f, -0.5f, 0.0f, 1.0f); (*SGCLPOptionsPage::patternCircleButton).clickFunctionWithPointer = &SGCLPOptionsPage::patternSelect; (*SGCLPOptionsPage::patternCircleButton).attachedInt = 0; SGCLPOptionsPage::patternPolygonButton = new SGWTextButton(bg, "regular polygon", nullptr, 0.5f, 0.0f, 0.0f, 12.0f, 0.5f, -0.5f, 0.0f, 1.0f); (*SGCLPOptionsPage::patternPolygonButton).clickFunctionWithPointer = &SGCLPOptionsPage::patternSelect; (*SGCLPOptionsPage::patternPolygonButton).attachedInt = 1; SGCLPOptionsPage::patternStarButton = new SGWTextButton(bg, "star", nullptr, 0.0f, 0.5f, 0.0f, 13.0f, 0.5f, -0.5f, 0.0f, 1.0f); (*SGCLPOptionsPage::patternStarButton).clickFunctionWithPointer = &SGCLPOptionsPage::patternSelect; (*SGCLPOptionsPage::patternStarButton).attachedInt = 1; SGCLPOptionsPage::patternFractalButton = new SGWTextButton(bg, "fractal", nullptr, 0.5f, 0.0f, 0.0f, 13.0f, 0.5f, -0.5f, 0.0f, 1.0f); (*SGCLPOptionsPage::patternFractalButton).clickFunctionWithPointer = &SGCLPOptionsPage::patternSelect; (*SGCLPOptionsPage::patternFractalButton).attachedInt = 0;
We make sure the default is circle by setting that in SGCLPOptionsPage::reset
SGCLPOptionsPage::patternSelect(SGCLPOptionsPage::patternCircleButton);
Then we finally actually implement the function.
void SGCLPOptionsPage::patternSelect(SGWButton *x){ (*SGCLPOptionsPage::patternCircleButton).setSelected(false); (*SGCLPOptionsPage::patternPolygonButton).setSelected(false); (*SGCLPOptionsPage::patternStarButton).setSelected(false); (*SGCLPOptionsPage::patternFractalButton).setSelected(false); (*x).setSelected(true); if((*x).attachedInt == 0){(*SGCLPOptionsPage::polygonSideCountInput).setItemVisibility(false);} else{(*SGCLPOptionsPage::polygonSideCountInput).setItemVisibility(true);} }
Testing that shows that it works.
See here for the next part of the tutorial.
©2025 05524F.sg (Singapore)