1 /** @file ui_main.cpp  Graphical User Interface.
2  *
3  * @authors Copyright © 2003-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
4  * @authors Copyright © 2005-2015 Daniel Swanson <danij@dengine.net>
5  *
6  * @par License
7  * GPL: http://www.gnu.org/licenses/gpl.html
8  *
9  * <small>This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; either version 2 of the License, or (at your
12  * option) any later version. This program is distributed in the hope that it
13  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details. You should have received a copy of the GNU
16  * General Public License along with this program; if not, see:
17  * http://www.gnu.org/licenses</small>
18  */
19 
20 #include "de_base.h"
21 #include "ui/ui_main.h"
22 #include "ui/clientwindow.h"
23 #include "sys_system.h"
24 
25 #include <cmath>
26 #include <de/GLState>
27 #include <de/GLInfo>
28 #include <doomsday/console/cmd.h>
29 #include <doomsday/filesys/fs_util.h>
30 //#include <doomsday/world/Materials>
31 
32 #include "api_fontrender.h"
33 #include "gl/gl_main.h"
34 #include "gl/gl_draw.h"
35 //#include "gl/texturecontent.h"
36 //#include "resource/image.h"
37 #include "render/rend_main.h"
38 #include "render/rend_font.h"
39 //#include "MaterialAnimator"
40 
41 using namespace de;
42 
43 fontid_t fontFixed;
44 
45 /// Modify these colors to change the look of the UI.
46 static ui_color_t ui_colors[NUM_UI_COLORS] = {
47     /* UIC_TEXT */      { .85f, .87f, 1 },
48     /* UIC_TITLE */     { 1, 1, 1 },
49 };
50 
UI_ChooseFixedFont()51 char const *UI_ChooseFixedFont()
52 {
53     if (DENG_GAMEVIEW_WIDTH < 300) return "console11";
54     if (DENG_GAMEVIEW_WIDTH > 768) return "console18";
55     return "console14";
56 }
57 
loadSystemFont(char const * name)58 static AbstractFont *loadSystemFont(char const *name)
59 {
60     DENG2_ASSERT(name != 0 && name[0]);
61 
62     // Compose the resource name.
63     de::Uri uri = de::makeUri("System:").setPath(name);
64 
65     // Compose the resource data path.
66     ddstring_t resourcePath; Str_InitStd(&resourcePath);
67     Str_Appendf(&resourcePath, "}data/Fonts/%s.dfn", name);
68 #if defined(UNIX) && !defined(MACOSX)
69     // Case-sensitive file system.
70     /// @todo Unkludge this: handle in a more generic manner.
71     strlwr(resourcePath.str);
72 #endif
73     F_ExpandBasePath(&resourcePath, &resourcePath);
74 
75     AbstractFont *font = ClientResources::get().newFontFromFile(uri, Str_Text(&resourcePath));
76     if (!font)
77     {
78         App_Error("loadSystemFont: Failed loading font \"%s\".", name);
79     }
80 
81     Str_Free(&resourcePath);
82     return font;
83 }
84 
loadFontIfNeeded(char const * uri,fontid_t * fid)85 static void loadFontIfNeeded(char const *uri, fontid_t *fid)
86 {
87     *fid = NOFONTID;
88     if (uri && uri[0])
89     {
90         try
91         {
92             FontManifest &manifest = ClientResources::get().fontManifest(de::makeUri(uri));
93             if (manifest.hasResource())
94             {
95                 *fid = fontid_t(manifest.uniqueId());
96             }
97         }
98         catch (Resources::MissingResourceManifestError const &)
99         {}
100     }
101 
102     if (*fid == NOFONTID)
103     {
104         *fid = loadSystemFont(uri)->manifest().uniqueId();
105     }
106 }
107 
UI_LoadFonts()108 void UI_LoadFonts()
109 {
110     if (novideo) return;
111 
112     loadFontIfNeeded(UI_ChooseFixedFont(), &fontFixed);
113 }
114 
UI_Color(uint id)115 ui_color_t* UI_Color(uint id)
116 {
117     if (id >= NUM_UI_COLORS)
118         return NULL;
119     return &ui_colors[id];
120 }
121 
UI_MixColors(ui_color_t * a,ui_color_t * b,ui_color_t * dest,float amount)122 void UI_MixColors(ui_color_t* a, ui_color_t* b, ui_color_t* dest, float amount)
123 {
124     dest->red   = (1 - amount) * a->red   + amount * b->red;
125     dest->green = (1 - amount) * a->green + amount * b->green;
126     dest->blue  = (1 - amount) * a->blue  + amount * b->blue;
127 }
128 
UI_SetColorA(ui_color_t * color,float alpha)129 void UI_SetColorA(ui_color_t* color, float alpha)
130 {
131     DGL_Color4f(color->red, color->green, color->blue, alpha);
132 }
133 
UI_SetColor(ui_color_t * color)134 void UI_SetColor(ui_color_t* color)
135 {
136     DGL_Color3f(color->red, color->green, color->blue);
137 }
138 
UI_TextOutEx2(const char * text,const Point2Raw * origin,ui_color_t * color,float alpha,int alignFlags,short textFlags)139 void UI_TextOutEx2(const char* text, const Point2Raw* origin, ui_color_t* color, float alpha,
140     int alignFlags, short textFlags)
141 {
142     assert(origin);
143     //alpha *= uiAlpha;
144     if (alpha <= 0) return;
145     FR_SetColorAndAlpha(color->red, color->green, color->blue, alpha);
146     FR_DrawText3(text, origin, alignFlags, textFlags);
147 }
148 
UI_TextOutEx(const char * text,const Point2Raw * origin,ui_color_t * color,float alpha)149 void UI_TextOutEx(const char* text, const Point2Raw* origin, ui_color_t* color, float alpha)
150 {
151     UI_TextOutEx2(text, origin, color, alpha, DEFAULT_ALIGNFLAGS, DEFAULT_DRAWFLAGS);
152 }
153 
UI_DrawDDBackground(Point2Raw const & origin,Size2Raw const & dimensions,float alpha)154 void UI_DrawDDBackground(Point2Raw const &origin, Size2Raw const &dimensions, float alpha)
155 {
156     //DGL_Disable(DGL_TEXTURE_2D);
157     DGL_PushState();
158 
159     if (alpha < 1.0f)
160     {
161         GL_BlendMode(BM_NORMAL);
162     }
163     else
164     {
165         DGL_Disable(DGL_BLEND);
166     }
167 
168     DGL_Color4f(0, 0, 0, alpha);
169     GL_DrawRect2(origin.x, origin.y, dimensions.width, dimensions.height);
170 
171     DGL_PopState();
172 }
173