Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 244 | chris | 1 | #include "DemoWidget.h" |
| 2 | #include "Res.h" |
||
| 3 | #include "SexyAppFramework/WidgetManager.h" |
||
| 4 | #include "SexyAppFramework/ButtonWidget.h" |
||
| 5 | #include "V12DemoApp.h" |
||
| 6 | |||
| 7 | using namespace Sexy; |
||
| 8 | |||
| 9 | ////////////////////////////////////////////////////////////////////////// |
||
| 10 | ////////////////////////////////////////////////////////////////////////// |
||
| 11 | DemoWidget::DemoWidget() |
||
| 12 | { |
||
| 13 | |||
| 14 | // Just for the heck of it, we'll resize ourselves at instantiation time. |
||
| 15 | int w = 200, h = 200; |
||
| 16 | Resize(gSexyAppBase->mWidth / 2 - w / 2, gSexyAppBase->mHeight / 2 - h / 2, w, h); |
||
| 17 | |||
| 18 | // Previously, it was annoying trying to place widgets on some sort of parent widget, |
||
| 19 | // since there was no notion of parent/child relationship. What you had to do was |
||
| 20 | // override the AddedToManager and RemovedFromManager functions, create and add your |
||
| 21 | // widgets or remove and nuke them, and in the case of AddedToManager, you also had |
||
| 22 | // to then place the widget using global coordinates that had no relation to the coordinates |
||
| 23 | // you set the parent widget at. Not anymore. What we can do now is to dispense with |
||
| 24 | // overriding those methods altogether! So now, in the parent's constructor, we can |
||
| 25 | // create our child widgets, resize them, and place them using relative coordinates. |
||
| 26 | // What this also means is that moving the parent widget around results in moving |
||
| 27 | // ALL the child widgets, which means you no longer have to manually move everything |
||
| 28 | // just because the parent moved. In addition, another nice thing is that you no longer |
||
| 29 | // have to remove your child widgets before deleting them: this is handled automatically |
||
| 30 | // for you. |
||
| 31 | mMoveButton = new ButtonWidget(0, this); |
||
| 32 | mMoveButton->mLabel = _S("MOVE"); |
||
| 33 | mMoveButton->SetFont(FONT_DEFAULT); |
||
| 34 | |||
| 35 | mCloseButton = new ButtonWidget(1, this); |
||
| 36 | mCloseButton->mLabel = _S("CLOSE"); |
||
| 37 | mCloseButton->SetFont(FONT_DEFAULT); |
||
| 38 | |||
| 39 | // VERY IMPORTANT: Notice that we're calling THIS CLASS' (or really, it's parent, WidgetContainer's) |
||
| 40 | // AddWidget method instead of the WidgetManager's method. In order to designate a widget as a child |
||
| 41 | // widget, you have to call the AddWidget method of the class that will be its parent. |
||
| 42 | AddWidget(mMoveButton); |
||
| 43 | AddWidget(mCloseButton); |
||
| 44 | |||
| 45 | mMoveButton->Resize(10, 150, 75, 50); |
||
| 46 | mCloseButton->Resize(115, 150, 75, 50); |
||
| 47 | |||
| 48 | // mPriority determines what..uh..priority...this widget (and thus its children) |
||
| 49 | // draws at when another widget is drawing an overlay (please see Board.cpp |
||
| 50 | // about the overlay stuff if you haven't already, in Board::Draw(...)). |
||
| 51 | // The higher priority widgets get drawn ABOVE (and thus AFTER) widgets |
||
| 52 | // with a lower priority. Thus, by changing the priority of a widget, |
||
| 53 | // you could enforce that it's always drawn above any overlay layer, |
||
| 54 | // or you could do just the opposite. We'll |
||
| 55 | // set it to 1, since in our Board::Draw method, you'll see that we |
||
| 56 | // toggle between drawing the overlay above and below this widget. |
||
| 57 | // Note that widgets by default have priority 0 and dialogs have priority 1. |
||
| 58 | mPriority = 1; |
||
| 59 | |||
| 60 | } |
||
| 61 | |||
| 62 | ////////////////////////////////////////////////////////////////////////// |
||
| 63 | ////////////////////////////////////////////////////////////////////////// |
||
| 64 | DemoWidget::~DemoWidget() |
||
| 65 | { |
||
| 66 | // We need to remove child widgets before deleting them. |
||
| 67 | RemoveAllWidgets(); |
||
| 68 | |||
| 69 | delete mMoveButton; |
||
| 70 | delete mCloseButton; |
||
| 71 | } |
||
| 72 | |||
| 73 | ////////////////////////////////////////////////////////////////////////// |
||
| 74 | ////////////////////////////////////////////////////////////////////////// |
||
| 75 | void DemoWidget::ButtonDepress(int id) |
||
| 76 | { |
||
| 77 | if (id == mMoveButton->mId) |
||
| 78 | { |
||
| 79 | // See how easy it is to move widgets now, using the new hierarchy system? |
||
| 80 | // By calling Move (or Resize as well), we also move ALL our child widgets, |
||
| 81 | // preserving their relative positions. Previously, you would have had to |
||
| 82 | // manually move all child widgets yourself. |
||
| 83 | Move(Rand() % (gSexyAppBase->mWidth - mWidth), Rand() % (gSexyAppBase->mHeight - mHeight)); |
||
| 84 | } |
||
| 85 | else if (id == mCloseButton->mId) |
||
| 86 | { |
||
| 87 | // We'll remove ourselves when the close button is pressed. |
||
| 88 | gSexyAppBase->mWidgetManager->RemoveWidget(this); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | |||
| 93 | ////////////////////////////////////////////////////////////////////////// |
||
| 94 | ////////////////////////////////////////////////////////////////////////// |
||
| 95 | void DemoWidget::Draw(Graphics* g) |
||
| 96 | { |
||
| 97 | g->SetColor(Color(0, 255, 0, 200)); |
||
| 98 | g->FillRect(0, 0, mWidth, mHeight); |
||
| 99 | |||
| 100 | |||
| 101 | } |