Ensemble Graphics Toolkit: Events

Last modified by Microchip on 2024/01/08 15:32

Introduction

In this training, you will learn how to create a button widget and add source code for an event to handle a button click.

Steps:

Prerequisites

You have prepared the Host PC with all the development software tools and Ensemble Graphics Toolkit source code as explained in:

You have installed and prepared the Eclipse IDE for C/C++ Developers as explained in:

Create a New Source File

In this section, you will be creating a new event.cpp source file that you will build in this and later training demonstrating the following classes:

​In this training, you will use EgtProject and its settings you entered in the training: Ensemble Graphics Toolkit: First Application using Eclipse IDE

Start the Eclipse IDE

Eclipse will ask you to select a directory for your workspace. Accept the default directory and click on the Launch button.

Eclipse IDE launcher

Eclipse will launch the previous workspace you configured.

Eclipse Previous Workspace


You will be adding a new source file with the name: event.cpp. If you have a *.cpp source file from a previous Ensemble Graphics Toolkit training, perform the following steps:

Right click in the *.cpp window and select Resource Configurations > Exclude from Build…

The Exclude from build window will open:

Eclipse exclude from build

Select Debug and Release and click on the OK button.

The selected *.cpp will be excluded from the build.


Right click on EgtProject and select Clean Project.

Right click on EgtProject and select New > Source File.

The New Source File window will open:

Event: New Source File


Enter event.cpp into the Source file: text box. Click on the Finish button.

A new source file tab event.cpp is created within EgtProject.


Enter the following source code to the event.cpp window pane:

#include <egt/ui>
#include
<iostream>
using namespace std;
using namespace egt;

int main(int argc, const char ** argv)
{
  Application app;
  TopWindow window;
  Button button(window, "Press Me");
  center(button);

  window.show();
 return app.run();
}

Save your program by selecting File > Save. You may also save by pressing CTRL + S.

You have completed adding a new source file to EgtProject.

Back to Top

Event Handling

In this section, you will modify and build the source code you added in the previous section to create a button widget and an event to handle a button click event.

Modify the event.cpp source code by adding the following lines of code:

#include <egt/ui>
#include <iostream>
using namespace std;
using namespace egt;

int main(int argc, const char ** argv)
{
 Application app;
 TopWindow window;
 Button button(window, "Press Me");
 center(button);

 // event handling
 button.on_event([ & ](Event & event)
  {
     if (event.id() == EventId::pointer_click)
      {
         cout << "clicked!" << endl;
      }
  });

 window.show();
 return app.run();
}

Save your program by selecting File > Save. You may also save by pressing CTRL + S.

To build the project, hover over EgtProject, right-click, and select Build Project from the menu. Or you can click on the Build icon.

The Console window (bottom pane) will display the build progress.


To set run properties, hover over EgtProject, right-click, and select Run As > Run Configurations… from the menu.

In the left-hand pane, select EgtProject Debug (under C/C++ Remote Application).

Event: Debug Configurations


In the Run Configurations window, enter the following in the Remote Absolute File Path for C/C++ Application: text box:

/root/event

​You may see the previous program /root/???? here. Replace it with /root/event.

This is the location of the event.cpp executable will be loaded and run on the target.


Click on the Apply button.

Click on the Run button.

Observe the WVGA Display on the target:

The event.cpp executable is running remotely on the target.

Observe the Press Me button in the center of the WVGA Display. When you click on the button you will see clicked! displayed on the Console.

EGT Display: Press Me

/root/event; exit

clicked!
clicked!
clicked!

Event class

From ~/egt/docs/html/annotated.html, click on the Event class. All the functions, attributes and constructor associated with the class is documented here.

In this training, you used the id() function to get the id of the button click event (pointer_click) and you handled it by printing a message to the console.

Here is a list of some event identifiers:

Event IdentifierDescription
none 
raw_pointer_downRaw pointer event. It's usually preferred to use the normal pointer events instead.
raw_pointer_upRaw pointer event. It's usually preferred to use the normal pointer events instead.
raw_pointer_moveRaw pointer event. It's usually preferred to use the normal pointer events instead.
pointer_clickpointer event.
pointer_dblclickpointer event.
pointer_holdpointer event.
pointer_drag_startpointer event.
pointer_dragpointer event.
pointer_drag_stoppointer event.
keyboard_downkeyboard event.
keyboard_upkeyboard event.
keyboard_repeatkeyboard event.

Widget::on_event()

This function allows you to add an event handler when any widget generates an event. Any number of callbacks can be registered.


To stop the program, press the Stop button (upper left hand, just below the menu bar).

​Before you start another session, be sure to stop the current session.

Summary

In this training, you learned how to create a button widget and add source code for an event to handle a button click.

Back to Top

What’s Next?

There’s plenty more to learn. Here are some additional Ensemble Graphics Toolkit training resources to help you gain more knowledge and skills:

Back to Top