]> Pileus Git - grits/blob - src/plugins/test.c
Add mouse-over testing to test plugin
[grits] / src / plugins / test.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /**
19  * SECTION:test
20  * @short_description: Testing plugin
21  *
22  * #GritsPluginTest is a testing plugin used during development and as an example
23  * for how to create a plugin.
24  */
25
26 #include <string.h>
27
28 #include <GL/gl.h>
29 #include <GL/glu.h>
30
31 #include <gdk/gdkkeysyms.h>
32 #include <grits.h>
33
34 #include "test.h"
35
36 static void on_poly_enter(GritsPoly *poly)
37 {
38         g_debug("on_poly_enter");
39         poly->color[3] = 0.50;
40         grits_object_queue_draw(GRITS_OBJECT(poly));
41 }
42
43 static void on_poly_leave(GritsPoly *poly)
44 {
45         g_debug("on_poly_leave");
46         poly->color[3] = 0.2;
47         grits_object_queue_draw(GRITS_OBJECT(poly));
48 }
49
50 static void on_poly_button(GritsPoly *poly, GdkEventButton *event)
51 {
52         g_debug("on_poly_button");
53         static int i = 0;
54         gdouble colors[][3] = {
55                 {1, 0, 0}, {1, 1, 0},
56                 {0, 1, 0}, {0, 1, 1},
57                 {0, 0, 1}, {1, 0, 1},
58         };
59         int idx = i++ % G_N_ELEMENTS(colors);
60         memcpy(poly->color, colors[idx], sizeof(gdouble)*3);
61         grits_object_queue_draw(GRITS_OBJECT(poly));
62 }
63
64 static void on_poly_key(GritsPoly *poly, GdkEventKey *event)
65 {
66         g_debug("on_poly_key");
67         gdouble colors[0xff][3] = {
68                 [GDK_r] {1, 0, 0},
69                 [GDK_g] {0, 1, 0},
70                 [GDK_b] {0, 0, 1},
71         };
72         int key = event->keyval;
73         memcpy(poly->color, colors[key], sizeof(gdouble)*3);
74         grits_object_queue_draw(GRITS_OBJECT(poly));
75 }
76
77 static void on_marker_enter(GritsMarker *marker, GritsViewer *viewer)
78 {
79         g_debug("on_marker_enter");
80         GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(viewer));
81         GdkCursor *cursor = gdk_cursor_new(GDK_HAND1);
82         gdk_window_set_cursor(window, cursor);
83 }
84
85 static void on_marker_leave(GritsMarker *marker, GritsViewer *viewer)
86 {
87         g_debug("on_marker_leave");
88         GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(viewer));
89         gdk_window_set_cursor(window, NULL);
90 }
91
92 static void on_marker_button(GritsMarker *marker, GdkEventButton *event)
93 {
94         g_debug("on_marker_button");
95         GtkWidget *dialog = gtk_dialog_new_with_buttons(
96                         "St. Charles!", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
97         gtk_dialog_run(GTK_DIALOG(dialog));
98 }
99
100 /***********
101  * Methods *
102  ***********/
103 void _load_marker(GritsPluginTest *test)
104 {
105         test->marker = grits_marker_new("St. Charles");
106         GRITS_OBJECT(test->marker)->center.lat  =  38.841847;
107         GRITS_OBJECT(test->marker)->center.lon  = -90.491982;
108         GRITS_OBJECT(test->marker)->center.elev =   0.0;
109         GRITS_OBJECT(test->marker)->lod         = EARTH_R;
110         grits_viewer_add(test->viewer, GRITS_OBJECT(test->marker), GRITS_LEVEL_OVERLAY, FALSE);
111         /* These do not work on marker yet */
112         //g_signal_connect(test->marker, "enter",        G_CALLBACK(on_marker_enter),  NULL);
113         //g_signal_connect(test->marker, "leave",        G_CALLBACK(on_marker_leave),  NULL);
114         //g_signal_connect(test->marker, "button-press", G_CALLBACK(on_marker_button), NULL);
115         (void)on_marker_enter;
116         (void)on_marker_leave;
117         (void)on_marker_button;
118 }
119
120 void _load_poly(GritsPluginTest *test)
121 {
122         test->poly = grits_poly_parse("35,-90 35,-110 45,-110 45,-90", "\t", " ", ",");
123         test->poly->color[0]  = test->poly->border[0] = 1;
124         test->poly->color[1]  = test->poly->border[1] = 0;
125         test->poly->color[2]  = test->poly->border[2] = 0;
126         test->poly->color[3]  = 0.2;
127         test->poly->border[3] = 1;
128         test->poly->width     = 10;
129         grits_viewer_add(test->viewer, GRITS_OBJECT(test->poly),  GRITS_LEVEL_OVERLAY, TRUE);
130         g_signal_connect(test->poly, "enter",        G_CALLBACK(on_poly_enter),  NULL);
131         g_signal_connect(test->poly, "leave",        G_CALLBACK(on_poly_leave),  NULL);
132         g_signal_connect(test->poly, "button-press", G_CALLBACK(on_poly_button), NULL);
133         g_signal_connect(test->poly, "key-press",    G_CALLBACK(on_poly_key),    NULL);
134 }
135
136 /**
137  * grits_plugin_test_new:
138  * @viewer: the #GritsViewer to use for drawing
139  *
140  * Create a new instance of the testing plugin.
141  *
142  * Returns: the new #GritsPluginTest
143  */
144 GritsPluginTest *grits_plugin_test_new(GritsViewer *viewer)
145 {
146         g_debug("GritsPluginTest: new");
147         GritsPluginTest *test = g_object_new(GRITS_TYPE_PLUGIN_TEST, NULL);
148         test->viewer = g_object_ref(viewer);
149         _load_marker(test);
150         _load_poly(test);
151         return test;
152 }
153
154
155 /****************
156  * GObject code *
157  ****************/
158 /* Plugin init */
159 static void grits_plugin_test_plugin_init(GritsPluginInterface *iface);
160 G_DEFINE_TYPE_WITH_CODE(GritsPluginTest, grits_plugin_test, G_TYPE_OBJECT,
161                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
162                         grits_plugin_test_plugin_init));
163 static void grits_plugin_test_plugin_init(GritsPluginInterface *iface)
164 {
165         g_debug("GritsPluginTest: plugin_init");
166         /* Add methods to the interface */
167 }
168 /* Class/Object init */
169 static void grits_plugin_test_init(GritsPluginTest *test)
170 {
171         g_debug("GritsPluginTest: init");
172 }
173 static void grits_plugin_test_dispose(GObject *_test)
174 {
175         g_debug("GritsPluginTest: dispose");
176         GritsPluginTest *test = GRITS_PLUGIN_TEST(_test);
177         if (test->viewer) {
178                 grits_viewer_remove(test->viewer, test->marker);
179                 g_object_unref(test->viewer);
180                 test->viewer = NULL;
181         }
182         G_OBJECT_CLASS(grits_plugin_test_parent_class)->dispose(_test);
183 }
184 static void grits_plugin_test_class_init(GritsPluginTestClass *klass)
185 {
186         g_debug("GritsPluginTest: class_init");
187         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
188         gobject_class->dispose = grits_plugin_test_dispose;
189 }