istereo2

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