1 /*
2  * See Licensing and Copyright notice in naev.h
3  */
4 
5 /**
6  * @file image.c
7  *
8  * @brief Image widget.
9  */
10 
11 
12 #include "tk/toolkit_priv.h"
13 
14 
15 static void img_render( Widget* img, double bx, double by );
16 
17 
18 /**
19  * @brief Adds an image widget to the window.
20  *
21  * Position origin is 0,0 at bottom left.  If you use negative X or Y
22  *  positions.  They actually count from the opposite side in.
23  *
24  *    @param wid ID of the window to add the widget to.
25  *    @param x X position within the window to use.
26  *    @param y Y position within the window to use.
27  *    @param name Name of the widget to use internally.
28  *    @param image Image to use.
29  *    @param border Whether to use a border.
30  */
window_addImage(const unsigned int wid,const int x,const int y,const int w,const int h,char * name,glTexture * image,int border)31 void window_addImage( const unsigned int wid,
32                       const int x, const int y,
33                       const int w, const int h,
34                       char* name, glTexture* image, int border )
35 {
36    Window *wdw = window_wget(wid);
37    Widget *wgt = window_newWidget(wdw, name);
38    if (wgt == NULL)
39       return;
40 
41    /* generic */
42    wgt->type   = WIDGET_IMAGE;
43 
44    /* specific */
45    wgt->render          = img_render;
46    wgt->dat.img.image   = image;
47    wgt->dat.img.border  = border;
48    wgt->dat.img.colour  = cWhite; /* normal colour */
49 
50    /* position/size */
51    wgt->w = (w > 0) ? w : ((image==NULL) ? 0 : wgt->dat.img.image->sw);
52    wgt->h = (h > 0) ? h : ((image==NULL) ? 0 : wgt->dat.img.image->sh);
53    toolkit_setPos( wdw, wgt, x, y );
54 }
55 
56 
57 /**
58  * @brief Renders a image widget.
59  *
60  *    @param img Image widget to render.
61  *    @param bx Base X position.
62  *    @param by Base Y position.
63  */
img_render(Widget * img,double bx,double by)64 static void img_render( Widget* img, double bx, double by )
65 {
66    double x,y;
67    double w,h;
68 
69    /* Values. */
70    x = bx + img->x;
71    y = by + img->y;
72    w = img->w;
73    h = img->h;
74 
75    /*
76     * image
77     */
78    if (img->dat.img.image != NULL) {
79       gl_blitScale( img->dat.img.image, x, y,
80             w, h, &img->dat.img.colour );
81    }
82 
83    if (img->dat.img.border) {
84       /* inner outline (outwards) */
85       toolkit_drawOutline( x, y+1, w-1,
86          h-1, 1., toolkit_colLight, toolkit_col );
87       /* outer outline */
88       toolkit_drawOutline( x, y+1, w-1,
89             h-1, 2., toolkit_colDark, NULL );
90    }
91 }
92 
93 
94 /**
95  * @brief Gets the image from an image widget
96  *
97  *    @param wid ID of the window to get widget from.
98  *    @param name Name of the widget.
99  */
window_getImage(const unsigned int wid,char * name)100 glTexture* window_getImage( const unsigned int wid, char* name )
101 {
102    Widget *wgt;
103 
104    /* Get the widget. */
105    wgt = window_getwgt(wid,name);
106    if (wgt == NULL)
107       return NULL;
108 
109    /* Check the type. */
110    if (wgt->type != WIDGET_IMAGE) {
111       WARN("Trying to get image from non-image widget '%s'.", name);
112       return NULL;
113    }
114 
115    /* Get the value. */
116    return (wgt) ? wgt->dat.img.image : NULL;
117 }
118 
119 
120 /**
121  * Modifies an existing image's image.
122  *
123  *    @param wid ID of the window to get widget from.
124  *    @param name Name of the widget to modify image of.
125  *    @param w New width to set, 0 uses image, -1 doesn't change and >0 sets directly.
126  *    @param w New height to set, 0 uses image, -1 doesn't change and >0 sets directly.
127  *    @param image New image to set.
128  */
window_modifyImage(const unsigned int wid,char * name,glTexture * image,int w,int h)129 void window_modifyImage( const unsigned int wid,
130       char* name, glTexture* image, int w, int h )
131 {
132    Widget *wgt;
133 
134    /* Get the widget. */
135    wgt = window_getwgt(wid,name);
136    if (wgt == NULL)
137       return;
138 
139    /* Check the type. */
140    if (wgt->type != WIDGET_IMAGE) {
141       WARN("Not modifying image on non-image widget '%s'.", name);
142       return;
143    }
144 
145    /* Set the image. */
146    wgt->dat.img.image = image;
147 
148    /* Adjust size. */
149    if (w >= 0)
150       wgt->w = (w > 0) ? w : ((image==NULL) ? 0 : wgt->dat.img.image->sw);
151    if (h >= 0)
152       wgt->h = (h > 0) ? h : ((image==NULL) ? 0 : wgt->dat.img.image->sh);
153 }
154 
155 
156 /**
157  * Modifies an existing image's colour.
158  *
159  *    @param wid ID of the window to get widget from.
160  *    @param name Name of the widget to modify image colour of.
161  *    @param colour New colour to use.
162  */
window_imgColour(const unsigned int wid,char * name,const glColour * colour)163 void window_imgColour( const unsigned int wid,
164       char* name, const glColour* colour )
165 {
166    Widget *wgt;
167 
168    /* Get the widget. */
169    wgt = window_getwgt(wid,name);
170    if (wgt == NULL)
171       return;
172 
173    /* Check the type. */
174    if (wgt->type != WIDGET_IMAGE) {
175       WARN("Not modifying image on non-image widget '%s'.", name);
176       return;
177    }
178 
179    /* Set the colour. */
180    wgt->dat.img.colour = *colour;
181 }
182 
183