Skip to content

Commit d1503cb

Browse files
committed
update ezgl subtree to add comments and refactor code
libezgl: Updating libs/EXTERNAL/libezgl/ (external git subtree from https://github.com/mariobadr/ezgl.git master)
2 parents 44e89e8 + 5a818f6 commit d1503cb

File tree

6 files changed

+56
-58
lines changed

6 files changed

+56
-58
lines changed

libs/EXTERNAL/libezgl/include/ezgl/application.hpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ class application {
132132
: main_ui_resource(build_ui_from_file ? "main_ui" : "/ezgl/main.ui"), window_identifier("MainWindow"), canvas_identifier("MainCanvas"), application_identifier("ezgl.app"),
133133
setup_callbacks(nullptr)
134134
{
135-
// If the default application identifier is used, uniquify it by appending a time stamp
136-
application_identifier = "ezgl.app.t" + std::to_string(std::time(nullptr));
135+
// Uniquify the application_identifier by appending a time stamp,
136+
// so that each instance of the same program has a different application ID.
137+
// This allows multiple instance of the program to run independelty.
138+
application_identifier += ".t" + std::to_string(std::time(nullptr));
137139
}
138140

139141
/**
@@ -144,9 +146,10 @@ class application {
144146
: main_ui_resource(m_resource), window_identifier(w_identifier), canvas_identifier(c_identifier), application_identifier(a_identifier),
145147
setup_callbacks(s_callbacks)
146148
{
147-
// If the default application identifier is used, uniquify it by appending a time stamp
148-
if (application_identifier == "ezgl.app")
149-
application_identifier = "ezgl.app.t" + std::to_string(std::time(nullptr));
149+
// Uniquify the application_identifier by appending a time stamp,
150+
// so that each instance of the same program has a different application ID.
151+
// This allows multiple instance of the program to run independelty.
152+
application_identifier += ".t" + std::to_string(std::time(nullptr));
150153
}
151154
};
152155

@@ -413,27 +416,13 @@ class application {
413416
key_callback_fn key_press_callback;
414417
};
415418

416-
#ifndef ECE297
417419
/**
418420
* Set the disable_event_loop flag to new_setting
419421
* Call with new_setting == true to make the event_loop immediately return.
420-
* Needed only for auto-marking
421422
*
422423
* @param new_setting The new state of disable_event_loop flag
423424
*/
424425
void set_disable_event_loop(bool new_setting);
425-
#endif
426426
}
427427

428-
#ifdef ECE297
429-
/**
430-
* Set the disable_event_loop flag to new_setting
431-
* Call with new_setting == true to make the event_loop immediately return.
432-
* Needed only for auto-marking
433-
*
434-
* @param new_setting The new state of disable_event_loop flag
435-
*/
436-
void set_disable_event_loop(bool new_setting);
437-
#endif
438-
439428
#endif //EZGL_APPLICATION_HPP

libs/EXTERNAL/libezgl/include/ezgl/graphics.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ class renderer {
425425
* Draw a surface
426426
*
427427
* @param surface The surface to draw
428-
* @param top_left The corner point of the drawn surface.
428+
* @param anchor_point The anchor_point point of the drawn surface.
429429
* @param scale_factor The scaling factor of the drawn surface (optional)
430430
*/
431-
void draw_surface(surface *p_surface, point2d top_left, double scale_factor = 1);
431+
void draw_surface(surface *p_surface, point2d anchor_point, double scale_factor = 1);
432432

433433
/**
434434
* load a png image

libs/EXTERNAL/libezgl/include/ezgl/point.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace ezgl {
2727
class point2d {
2828
public:
2929
/**
30-
* Create a point at (0, 0).
30+
* Default constructor: Create a point at (0, 0).
3131
*/
3232
point2d() : x(0.0), y(0.0)
3333
{

libs/EXTERNAL/libezgl/src/application.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,11 @@
1818

1919
#include "ezgl/application.hpp"
2020

21-
#ifdef ECE297
22-
// A flag to disable event loop (default is false)
23-
bool disable_event_loop = false;
24-
#endif
25-
2621
namespace ezgl {
2722

28-
#ifndef ECE297
2923
// A flag to disable event loop (default is false)
24+
// This allows basic scripted testing even if the GUI is on (return immediately when the event loop is called)
3025
bool disable_event_loop = false;
31-
#endif
3226

3327
void application::startup(GtkApplication *, gpointer user_data)
3428
{
@@ -39,6 +33,8 @@ void application::startup(GtkApplication *, gpointer user_data)
3933

4034
if (!build_ui_from_file) {
4135
// Build the main user interface from the XML resource.
36+
// The XML resource is built from an XML file using the glib-compile-resources tool.
37+
// This adds an extra compilation step, but it embeds the UI description in the executable.
4238
GError *error = nullptr;
4339
if(gtk_builder_add_from_resource(ezgl_app->m_builder, main_ui_resource, &error) == 0) {
4440
g_error("%s.", error->message);
@@ -325,7 +321,9 @@ void application::create_button(const char *button_text,
325321
GtkWidget *new_button = gtk_button_new_with_label(button_text);
326322

327323
// set can_focus property to false
324+
#if GTK_CHECK_VERSION (3, 20, 0)
328325
gtk_widget_set_focus_on_click(new_button, false);
326+
#endif
329327

330328
// connect the buttons clicked event to the callback
331329
if(button_func != NULL) {
@@ -476,17 +474,8 @@ renderer *application::get_renderer()
476474
return cnv->create_animation_renderer();
477475
}
478476

479-
#ifndef ECE297
480-
void set_disable_event_loop(bool new_setting)
481-
{
482-
disable_event_loop = new_setting;
483-
}
484-
#endif
485-
}
486-
487-
#ifdef ECE297
488477
void set_disable_event_loop(bool new_setting)
489478
{
490479
disable_event_loop = new_setting;
491480
}
492-
#endif
481+
}

libs/EXTERNAL/libezgl/src/callback.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,24 @@
2020

2121
namespace ezgl {
2222

23-
// File wide static variables to track whether the mouse button used for
24-
// panning is currently pressed AND the old x and y positions of the mouse pointer
25-
bool panning_mouse_button_pressed = false;
26-
int last_panning_event_time = 0;
27-
double prev_x = 0, prev_y = 0;
23+
/**
24+
* Provides file wide variables to support mouse panning
25+
*/
26+
struct mouse_pan {
27+
/**
28+
* Tracks whether the mouse button used for panning is currently pressed
29+
*/
30+
bool panning_mouse_button_pressed = false;
31+
/**
32+
* Holds the timestamp of the last panning event
33+
*/
34+
int last_panning_event_time = 0;
35+
/**
36+
* The old x and y positions of the mouse pointer
37+
*/
38+
double prev_x = 0;
39+
double prev_y = 0;
40+
} g_mouse_pan;
2841

2942
gboolean press_key(GtkWidget *, GdkEventKey *event, gpointer data)
3043
{
@@ -53,9 +66,9 @@ gboolean press_mouse(GtkWidget *, GdkEventButton *event, gpointer data)
5366

5467
// Check for mouse press to support dragging
5568
if(event->button == PANNING_MOUSE_BUTTON) {
56-
panning_mouse_button_pressed = true;
57-
prev_x = event->x;
58-
prev_y = event->y;
69+
g_mouse_pan.panning_mouse_button_pressed = true;
70+
g_mouse_pan.prev_x = event->x;
71+
g_mouse_pan.prev_y = event->y;
5972
}
6073
// Call the user-defined mouse press callback if defined
6174
// The user-defined callback is called for mouse buttons other than
@@ -82,10 +95,10 @@ gboolean release_mouse(GtkWidget *, GdkEventButton *event, gpointer data)
8295
if(event->type == GDK_BUTTON_RELEASE) {
8396
// Check for mouse release to support dragging
8497
if(event->button == PANNING_MOUSE_BUTTON) {
85-
panning_mouse_button_pressed = false;
98+
g_mouse_pan.panning_mouse_button_pressed = false;
8699

87100
// Call the user-defined mouse press callback for the PANNING_MOUSE_BUTTON button only if no panning occurs
88-
if(event->x == prev_x && event->y == prev_y && application->mouse_press_callback != nullptr) {
101+
if(event->x == g_mouse_pan.prev_x && event->y == g_mouse_pan.prev_y && application->mouse_press_callback != nullptr) {
89102
ezgl::point2d const widget_coordinates(event->x, event->y);
90103

91104
std::string main_canvas_id = application->get_main_canvas_id();
@@ -107,26 +120,26 @@ gboolean move_mouse(GtkWidget *, GdkEventButton *event, gpointer data)
107120
if(event->type == GDK_MOTION_NOTIFY) {
108121

109122
// Check if the mouse button is pressed to support dragging
110-
if(panning_mouse_button_pressed) {
123+
if(g_mouse_pan.panning_mouse_button_pressed) {
111124
// drop this panning event if we have just served another one
112-
if(gtk_get_current_event_time() - last_panning_event_time < 100)
125+
if(gtk_get_current_event_time() - g_mouse_pan.last_panning_event_time < 100)
113126
return true;
114127

115-
last_panning_event_time = gtk_get_current_event_time();
128+
g_mouse_pan.last_panning_event_time = gtk_get_current_event_time();
116129

117130
GdkEventMotion *motion_event = (GdkEventMotion *)event;
118131

119132
std::string main_canvas_id = application->get_main_canvas_id();
120133
auto canvas = application->get_canvas(main_canvas_id);
121134

122135
point2d curr_trans = canvas->get_camera().widget_to_world({motion_event->x, motion_event->y});
123-
point2d prev_trans = canvas->get_camera().widget_to_world({prev_x, prev_y});
136+
point2d prev_trans = canvas->get_camera().widget_to_world({g_mouse_pan.prev_x, g_mouse_pan.prev_y});
124137

125138
double dx = curr_trans.x - prev_trans.x;
126139
double dy = curr_trans.y - prev_trans.y;
127140

128-
prev_x = motion_event->x;
129-
prev_y = motion_event->y;
141+
g_mouse_pan.prev_x = motion_event->x;
142+
g_mouse_pan.prev_y = motion_event->y;
130143

131144
// Flip the delta x to avoid inverted dragging
132145
translate(canvas, -dx, -dy);

libs/EXTERNAL/libezgl/src/graphics.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,14 @@ void renderer::set_text_rotation(double degrees)
304304

305305
void renderer::set_horiz_justification(justification horiz_just)
306306
{
307+
// Ignore illegal values for horizontal justification
307308
if (horiz_just != justification::top && horiz_just != justification::bottom)
308309
horiz_justification = horiz_just;
309310
}
310311

311312
void renderer::set_vert_justification(justification vert_just)
312313
{
314+
// Ignore illegal values for vertical justification
313315
if (vert_just != justification::right && vert_just != justification::left)
314316
vert_justification = vert_just;
315317
}
@@ -713,8 +715,10 @@ void renderer::draw_arc_path(point2d center,
713715
void renderer::draw_surface(surface *p_surface, point2d point, double scale_factor)
714716
{
715717
// Check if the surface is properly created
716-
if(cairo_surface_status(p_surface) != CAIRO_STATUS_SUCCESS)
718+
if(cairo_surface_status(p_surface) != CAIRO_STATUS_SUCCESS) {
719+
g_warning("renderer::draw_surface: Error drawing surface at address %p; surface is not valid.", p_surface);
717720
return;
721+
}
718722

719723
// calculate surface width and height in screen coordinates
720724
double s_width = (double)cairo_image_surface_get_width(p_surface) * scale_factor;
@@ -732,6 +736,9 @@ void renderer::draw_surface(surface *p_surface, point2d point, double scale_fact
732736
top_left.x -= s_width/2;
733737
else if (horiz_justification == justification::right)
734738
top_left.x -= s_width;
739+
// Vertical justifaction is calculated differently based on the current coordinate system
740+
// Since the origin point of screen coordinates is at the top left,
741+
// while the origin point of world coordinates is at the bottom left
735742
if (vert_justification == justification::center)
736743
top_left.y += (current_coordinate_system == WORLD) ? s_height/2 : -s_height/2;
737744
else if (vert_justification == justification::bottom)
@@ -776,10 +783,10 @@ surface *renderer::load_png(const char *file_path)
776783
cairo_status_t status = cairo_surface_status(png_surface);
777784

778785
if (status == CAIRO_STATUS_FILE_NOT_FOUND) {
779-
g_warning("load_png: File %s not found.", file_path);
786+
g_warning("renderer::load_png: File %s not found.", file_path);
780787
}
781788
else if (status != CAIRO_STATUS_SUCCESS) {
782-
g_warning("load_png: Error loading file %s.", file_path);
789+
g_warning("renderer::load_png: Error loading file %s.", file_path);
783790
}
784791

785792
return png_surface;

0 commit comments

Comments
 (0)