1 /**
2  * Code to populate a global Gui object with an example of how to
3  * use actions in Guichan.
4  */
5 
6 #include <guichan.hpp>
7 #include <iostream>
8 #include <sstream>
9 
10 namespace action
11 {
12     gcn::Container* top;
13     gcn::ImageFont* font;
14     gcn::Button* button1;
15     gcn::Button* button2;
16     gcn::Label* label1;
17     gcn::Label* label2;
18 
19     int clickCountButton1 = 0; // Holds clicks for button1
20     int clickCountButton2 = 0; // Holds clicks for button2
21 
22     /*
23      * To be able to recieve actions we must have a class inheriting from
24      * ActionListener and implementing the action function.
25      * When an action is generated in a Widget (i.e when a button is clicked)
26      * that widget calls the action function for all its action listeners.
27      * Below we make a class to be able to recieve our buttons actions.
28      */
29     class ButtonActionListener : public gcn::ActionListener
30     {
31     public:
32         // Implement the action function in ActionListener to recieve actions
33         // The eventId tells us which widget called the action function.
action(const gcn::ActionEvent & actionEvent)34         void action(const gcn::ActionEvent& actionEvent)
35         {
36             std::string str;
37             std::ostringstream os(str);
38 
39             // Here we use the widget pointer to check which widget the action
40             // originated from.
41             if (actionEvent.getSource() == button1)
42             {
43                 clickCountButton1++;
44                 os << "Button1 clicks " << clickCountButton1;
45                 // Change the caption to show number of clicks
46                 label1->setCaption(os.str());
47                 // Adjust the label to fit the new caption
48                 label1->adjustSize();
49             }
50             // Here we use the event id in order to check what action occured.
51             else if (actionEvent.getId() == "button2")
52             {
53                 clickCountButton2++;
54                 os << "Button2 clicks " << clickCountButton2;
55                 // Change the caption to show number of clicks
56                 label2->setCaption(os.str());
57                 // Adjust the label to fit the new caption
58                 label2->adjustSize();
59             }
60         }
61     };
62 
63     ButtonActionListener* buttonActionListener; // A pointer to the above class
64 
65     /**
66      * Initialises the widgets example by populating the global Gui
67      * object.
68      */
init()69     void init()
70     {
71          // We first create a container to be used as the top widget.
72         // The top widget in Guichan can be any kind of widget, but
73         // in order to make the Gui contain more than one widget we
74         // make the top widget a container.
75         top = new gcn::Container();
76          // We set the dimension of the top container to match the screen.
77         top->setDimension(gcn::Rectangle(0, 0, 640, 480));
78         // Finally we pass the top widget to the Gui object.
79         globals::gui->setTop(top);
80 
81         // Now we load the font used in this example.
82         font = new gcn::ImageFont("fixedfont.bmp", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
83         // Widgets may have a global font so we don't need to pass the
84         // font object to every created widget. The global font is static.
85         gcn::Widget::setGlobalFont(font);
86 
87         // Now we create the buttons.
88         button1 = new gcn::Button("Button 1");
89         button2 = new gcn::Button("Button 2");
90         button1->setPosition(120, 230);
91         button2->setPosition(420, 230);
92         // We add the buttons to the top container.
93         top->add(button1);
94         top->add(button2);
95 
96         // Now we create the labels.
97         label1 = new gcn::Label("Button1 clicks 0");
98         label2 = new gcn::Label("Button2 clicks 0");
99         label1->setPosition(100, 200);
100         label2->setPosition(400, 200);
101         // We add the labels to the top container.
102         top->add(label1);
103         top->add(label2);
104 
105         // Now we set the button's action event ids.
106         button1->setActionEventId("button1");
107         button2->setActionEventId("button2");
108 
109         // We make an instance of the ButtonActionListener.
110         buttonActionListener = new ButtonActionListener();
111 
112         // And add the ButtonActionListener as an action listener of the buttons.
113         button1->addActionListener(buttonActionListener);
114         button2->addActionListener(buttonActionListener);
115     }
116 
117     /**
118      * Halts the action example.
119      */
halt()120     void halt()
121     {
122         delete buttonActionListener;
123         delete label1;
124         delete label2;
125         delete button1;
126         delete button2;
127         delete font;
128         delete top;
129     }
130 }
131