]> Pileus Git - grits/blob - src/plugin-ridge.c
90ad4d1bf9898d2b50c50080686505a8b2f5fa4c
[grits] / src / plugin-ridge.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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 #include <config.h>
19 #include <gtk/gtk.h>
20 #include <curl/curl.h>
21 #include <GL/gl.h>
22
23 #include <stdio.h>
24
25 #include "aweather-gui.h"
26 #include "plugin-ridge.h"
27 #include "data.h"
28
29 /****************
30  * GObject code *
31  ****************/
32 /* Plugin init */
33 static void aweather_ridge_plugin_init(AWeatherPluginInterface *iface);
34 static void aweather_ridge_expose(AWeatherPlugin *_ridge);
35 G_DEFINE_TYPE_WITH_CODE(AWeatherRidge, aweather_ridge, G_TYPE_OBJECT,
36                 G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
37                         aweather_ridge_plugin_init));
38 static void aweather_ridge_plugin_init(AWeatherPluginInterface *iface)
39 {
40         g_debug("AWeatherRidge: plugin_init");
41         /* Add methods to the interface */
42         iface->expose = aweather_ridge_expose;
43 }
44 /* Class/Object init */
45 static void aweather_ridge_init(AWeatherRidge *ridge)
46 {
47         g_debug("AWeatherRidge: init");
48         /* Set defaults */
49         ridge->gui = NULL;
50 }
51 static void aweather_ridge_dispose(GObject *gobject)
52 {
53         g_debug("AWeatherRidge: dispose");
54         AWeatherRidge *self = AWEATHER_RIDGE(gobject);
55         /* Drop references */
56         G_OBJECT_CLASS(aweather_ridge_parent_class)->dispose(gobject);
57 }
58 static void aweather_ridge_finalize(GObject *gobject)
59 {
60         g_debug("AWeatherRidge: finalize");
61         AWeatherRidge *self = AWEATHER_RIDGE(gobject);
62         /* Free data */
63         G_OBJECT_CLASS(aweather_ridge_parent_class)->finalize(gobject);
64
65 }
66 static void aweather_ridge_class_init(AWeatherRidgeClass *klass)
67 {
68         g_debug("AWeatherRidge: class_init");
69         GObjectClass *gobject_class = (GObjectClass*)klass;
70         gobject_class->dispose  = aweather_ridge_dispose;
71         gobject_class->finalize = aweather_ridge_finalize;
72 }
73
74 /*********************
75  * Overlay constants *
76  *********************/
77 enum {
78         LAYER_TOPO,
79         LAYER_COUNTY,
80         LAYER_RIVERS,
81         LAYER_HIGHWAYS,
82         LAYER_CITY,
83         LAYER_COUNT
84 };
85
86 typedef struct {
87         gchar *name;
88         gchar *fmt;
89         gboolean enabled;
90         float z;
91         guint tex;
92 } layer_t;
93
94 static layer_t layers[] = {
95         [LAYER_TOPO]     = {"Topo",     "Overlays/Topo/Short/%s_Topo_Short.jpg",         TRUE,  1, 0},
96         [LAYER_COUNTY]   = {"Counties", "Overlays/County/Short/%s_County_Short.gif",     TRUE,  3, 0},
97         [LAYER_RIVERS]   = {"Rivers",   "Overlays/Rivers/Short/%s_Rivers_Short.gif",     FALSE, 4, 0},
98         [LAYER_HIGHWAYS] = {"Highways", "Overlays/Highways/Short/%s_Highways_Short.gif", FALSE, 5, 0},
99         [LAYER_CITY]     = {"Cities",   "Overlays/Cities/Short/%s_City_Short.gif",       TRUE,  6, 0},
100 };
101
102
103 /***********
104  * Helpers *
105  ***********/
106 /*
107  * Load an image into an OpenGL texture
108  * \param  filename  Path to the image file
109  * \return The OpenGL identifier for the texture
110  */
111 void load_texture(AWeatherRidge *self, layer_t *layer, gchar *filename)
112 {
113         aweather_gui_gl_begin(self->gui);
114
115         /* Load image */
116         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
117         if (!pixbuf)
118                 g_warning("Failed to load texture: %s", filename);
119         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
120         int        width  = gdk_pixbuf_get_width(pixbuf);
121         int        height = gdk_pixbuf_get_height(pixbuf);
122         int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
123
124         /* Create Texture */
125         glDeleteTextures(1, &layer->tex);
126         glGenTextures(1, &layer->tex);
127         glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
128
129         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
130         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
131                         format, GL_UNSIGNED_BYTE, pixels);
132         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
133         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
134
135         char *base = g_path_get_basename(filename);
136         g_debug("AWeatherRidge: load_texture - "
137                 "w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
138                 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
139                 base);
140         g_free(base);
141
142         aweather_gui_gl_end(self->gui);
143
144         g_object_unref(pixbuf);
145
146         /* Redraw */
147         aweather_gui_gl_redraw(self->gui);
148 }
149
150
151 /*****************
152  * ASync helpers *
153  *****************/
154 typedef struct {
155         AWeatherRidge *self;
156         layer_t *layer;
157 } cached_t;
158 void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
159 {
160         cached_t *udata = _udata;
161         load_texture(udata->self, udata->layer, filename);
162         g_free(udata);
163 }
164
165 /*************
166  * callbacks *
167  *************/
168 static void on_site_changed(AWeatherView *view, gchar *site, AWeatherRidge *self)
169 {
170         g_debug("AWeatherRidge: on_site_changed - site=%s", site);
171         for (int i = 0; i < LAYER_COUNT; i++) {
172                 gchar *base = "http://radar.weather.gov/ridge/";
173                 gchar *path  = g_strdup_printf(layers[i].fmt, site);
174                 cached_t *udata = g_malloc(sizeof(cached_t));
175                 udata->self  = self;
176                 udata->layer = &layers[i];
177                 cache_file(base, path, AWEATHER_NEVER, cached_cb, udata);
178                 g_free(path);
179         }
180 }
181
182 void toggle_layer(GtkToggleButton *check, AWeatherGui *gui)
183 {
184         layer_t *layer = g_object_get_data(G_OBJECT(check), "layer");
185         layer->enabled = gtk_toggle_button_get_active(check);
186         aweather_gui_gl_redraw(gui);
187 }
188
189 /***********
190  * Methods *
191  ***********/
192 AWeatherRidge *aweather_ridge_new(AWeatherGui *gui)
193 {
194         AWeatherRidge *ridge = g_object_new(AWEATHER_TYPE_RIDGE, NULL);
195         ridge->gui = gui;
196
197         AWeatherView *view    = aweather_gui_get_view(gui);
198         GtkWidget    *drawing = aweather_gui_get_widget(gui, "drawing");
199         GtkWidget    *config  = aweather_gui_get_widget(gui, "tabs");
200
201         /* Add configuration tab */
202         GtkWidget *tab  = gtk_label_new("Ridge");
203         GtkWidget *body = gtk_alignment_new(0.5, 0, 0, 0);
204         GtkWidget *hbox = gtk_hbox_new(FALSE, 10);
205         for (int i = 0; i < LAYER_COUNT; i++) {
206                 GtkWidget *check = gtk_check_button_new_with_label(layers[i].name);
207                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), layers[i].enabled);
208                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, TRUE, 0);
209                 g_object_set_data(G_OBJECT(check), "layer", &layers[i]);
210                 g_signal_connect(check, "toggled", G_CALLBACK(toggle_layer), gui);
211         }
212         gtk_container_add(GTK_CONTAINER(body), hbox);
213         gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab);
214
215         g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed),  ridge);
216
217         return ridge;
218 }
219
220 static void aweather_ridge_expose(AWeatherPlugin *_ridge)
221 {
222         AWeatherRidge *ridge = AWEATHER_RIDGE(_ridge);
223
224         g_debug("AWeatherRidge: expose");
225         glPushMatrix();
226         glEnable(GL_TEXTURE_2D);
227         glColor4f(1,1,1,1);
228
229         for (int i = 0; i < LAYER_COUNT; i++) {
230                 if (!layers[i].enabled)
231                         continue;
232                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
233                 glBegin(GL_POLYGON);
234                 glTexCoord2f(0.0, 0.0); glVertex3f(240*1000*-1.0, 282*1000* 1.0, layers[i].z);
235                 glTexCoord2f(0.0, 1.0); glVertex3f(240*1000*-1.0, 282*1000*-1.0, layers[i].z);
236                 glTexCoord2f(1.0, 1.0); glVertex3f(240*1000* 1.0, 282*1000*-1.0, layers[i].z);
237                 glTexCoord2f(1.0, 0.0); glVertex3f(240*1000* 1.0, 282*1000* 1.0, layers[i].z);
238                 glEnd();
239         }
240
241         glPopMatrix();
242 }