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