istereo2

view libs/goatkit/theme.cc @ 6:3bccfc7d10fe

goatkit is drawing
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 23 Sep 2015 05:44:58 +0300
parents
children a3c4fcc9f8f3
line source
1 /*
2 GoatKit - a themable/animated widget toolkit for games
3 Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "config.h"
19 #include <stdio.h>
20 #include <vector>
21 #include <string>
22 #include <map>
23 #include <algorithm>
24 #include "theme.h"
25 #include "widget.h"
27 #ifdef WIN32
28 #include <windows.h>
30 static void *dlopen(const char *name, int flags);
31 static void dlclose(void *so);
32 static void *dlsym(void *so, const char *symbol);
33 #else
34 #include <unistd.h>
35 #include <dlfcn.h>
36 #endif
38 #ifdef HAVE_OPENGL_H
39 #include "opengl.h"
41 #else
43 #ifdef __APPLE__
44 #include <OpenGL/gl.h>
45 #else
46 #include <GL/gl.h>
47 #endif
49 #endif /* HAVE_OPENGL_H_ */
51 #ifndef PREFIX
52 #define PREFIX "/usr/local"
53 #endif
55 namespace goatkit {
57 struct ThemeImpl {
58 void *so;
59 WidgetDrawFunc (*lookup_theme_draw_func)(const char*);
60 mutable std::map<std::string, WidgetDrawFunc> func_cache;
61 };
63 Theme *theme;
64 static std::vector<std::string> search_paths;
65 static const char *fallback_paths[] = {
66 PREFIX "/share/goatkit",
67 0
68 };
70 void add_theme_path(const char *path)
71 {
72 if(!path || !*path) return;
74 std::string s = path;
75 int last = s.length() - 1;
76 if(s[last] == '/' || s[last] == '\\') {
77 s.erase(last);
78 }
80 if(std::find(search_paths.begin(), search_paths.end(), s) != search_paths.end()) {
81 return;
82 }
84 search_paths.push_back(s);
85 }
87 Theme::Theme()
88 {
89 impl = new ThemeImpl;
90 impl->so = 0;
91 impl->lookup_theme_draw_func = 0;
92 }
94 Theme::~Theme()
95 {
96 unload();
97 delete impl;
98 }
100 typedef WidgetDrawFunc (*LookupFunc)(const char*);
102 bool Theme::load(const char *name)
103 {
104 unload();
106 std::string fname = std::string(name) + ".gtheme";
107 if(!(impl->so = dlopen(fname.c_str(), RTLD_LAZY))) {
108 for(size_t i=0; i<search_paths.size(); i++) {
109 std::string path = search_paths[i] + "/" + fname;
111 if((impl->so = dlopen(path.c_str(), RTLD_LAZY))) {
112 break;
113 }
114 }
116 // try the fallback paths
117 if(!impl->so) {
118 for(int i=0; fallback_paths[i]; i++) {
119 std::string path = std::string(fallback_paths[i]) + "/" + fname;
121 if((impl->so = dlopen(path.c_str(), RTLD_LAZY))) {
122 break;
123 }
124 }
125 }
127 if(!impl->so) {
128 fprintf(stderr, "%s: failed to load theme plugin: %s\n", __func__, name);
129 return false;
130 }
131 }
133 // loaded the shared object, now get the lookup function
134 impl->lookup_theme_draw_func = (LookupFunc)dlsym(impl->so, "get_widget_func");
135 if(!impl->lookup_theme_draw_func) {
136 fprintf(stderr, "%s: invalid theme plugin %s\n", __func__, name);
137 unload();
138 return false;
139 }
141 return true;
142 }
144 void Theme::unload()
145 {
146 if(impl->so) {
147 dlclose(impl->so);
148 impl->so = 0;
149 }
150 impl->func_cache.clear();
151 }
153 WidgetDrawFunc Theme::get_draw_func(const char *type) const
154 {
155 std::map<std::string, WidgetDrawFunc>::const_iterator it = impl->func_cache.find(type);
156 if(it == impl->func_cache.end()) {
157 // don't have it cached, try to look it up
158 WidgetDrawFunc func;
159 if(impl->lookup_theme_draw_func && (func = impl->lookup_theme_draw_func(type))) {
160 impl->func_cache[type] = func;
161 return func;
162 }
164 // can't look it up, return the default
165 return default_draw_func;
166 }
167 return it->second;
168 }
170 #define LERP(a, b, t) ((a) + ((b) - (a)) * t)
171 #define DEF_TEX_SZ 32
172 void default_draw_func(const Widget *w)
173 {
174 static unsigned int tex;
176 if(!tex) {
177 unsigned char *pixels = new unsigned char[DEF_TEX_SZ * DEF_TEX_SZ * 3];
178 unsigned char *ptr = pixels;
179 for(int i=0; i<DEF_TEX_SZ; i++) {
180 for(int j=0; j<DEF_TEX_SZ; j++) {
181 bool stripe = (((j + i) / 8) & 1) == 1;
182 ptr[0] = ptr[1] = ptr[2] = stripe ? 255 : 0;
183 ptr += 3;
184 }
185 }
187 glGenTextures(1, &tex);
188 glBindTexture(GL_TEXTURE_2D, tex);
189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
191 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, DEF_TEX_SZ, DEF_TEX_SZ, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
192 delete [] pixels;
193 }
195 Vec2 pos = w->get_position();
196 Vec2 sz = w->get_size();
197 float aspect = sz.x / sz.y;
199 #if !defined(GL_ES_VERSION_2_0)
200 glPushAttrib(GL_ENABLE_BIT);
201 glEnable(GL_TEXTURE_2D);
202 #endif
203 glBindTexture(GL_TEXTURE_2D, tex);
205 float offs = w->get_pressed() * 0.1 * sz.y;
206 glMatrixMode(GL_MODELVIEW);
207 glPushMatrix();
208 glTranslatef(offs, -offs, 0);
210 float active = w->get_active();
211 float hover = w->get_under_mouse();
213 float rg = LERP(0.4, 1.0, hover);
214 float b = LERP(rg, 0, active);
215 glColor3f(rg, rg, b);
217 glBegin(GL_QUADS);
218 glTexCoord2f(0, 1);
219 glVertex2f(pos.x, pos.y);
220 glTexCoord2f(aspect, 1);
221 glVertex2f(pos.x + sz.x, pos.y);
222 glTexCoord2f(aspect, 0);
223 glVertex2f(pos.x + sz.x, pos.y + sz.y);
224 glTexCoord2f(0, 0);
225 glVertex2f(pos.x, pos.y + sz.y);
226 glEnd();
228 glPopMatrix();
230 #ifndef GL_ES_VERSION_2_0
231 glPopAttrib();
232 #endif
233 }
235 } // namespace goatkit
237 #ifdef WIN32
238 // XXX untested
239 static void *dlopen(const char *name, int flags)
240 {
241 return LoadLibrary(name);
242 }
244 static void dlclose(void *so)
245 {
246 FreeLibrary(so);
247 }
249 static void *dlsym(void *so, const char *symbol)
250 {
251 return (void*)GetProcAddress(so, symbol);
252 }
253 #endif