Debian Codegraph @

engine/ui/icons.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name icons.cpp - The icons. */
11 //
12 // (c) Copyright 1998-2006 by Lutz Sammer and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "stratagus.h"
40
41 #include <string>
42 #include <vector>
43 #include <map>
44
45 #include "video.h"
46 #include "icons.h"
47 #include "player.h"
48 #include "ui.h"
49 #include "menus.h"
50
51
52 /*----------------------------------------------------------------------------
53 -- Variables
54 ----------------------------------------------------------------------------*/
55
56 static std::vector<CIcon *> AllIcons; /// Vector of all icons.
57 std::map<std::string, CIcon *> Icons; /// Map of ident to icon.
58
59
60 /*----------------------------------------------------------------------------
61 -- Functions
62 ----------------------------------------------------------------------------*/
63
64 /**
65 ** CIcon constructor
66 */
67 CIcon::CIcon(const std::string &ident) : G(NULL), Frame(0), Ident(ident)
68 {
69 }
70
71 /**
72 ** CIcon destructor
73 */
74 CIcon::~CIcon()
75 {
76 CGraphic::Free(this->G);
77 }
78
79 /**
80 ** Create a new icon
81 **
82 ** @param ident Icon identifier
83 **
84 ** @return New icon
85 */
86 CIcon *CIcon::New(const std::string &ident)
87 {
88 CIcon *icon = Icons[ident];
89 if (icon) {
90 return icon;
91 } else {
92 icon = new CIcon(ident);
93 Icons[ident] = icon;
94 AllIcons.push_back(icon);
95 return icon;
96 }
97 }
98
99 /**
100 ** Get an icon
101 **
102 ** @param ident Icon identifier
103 **
104 ** @return The icon
105 */
106 CIcon *CIcon::Get(const std::string &ident)
107 {
108 CIcon *icon = Icons[ident];
109 if (!icon) {
110 DebugPrint("icon not found: %s\n" _C_ ident.c_str());
111 }
112 return icon;
113 }
114
115 /**
116 ** Load the Icon
117 **
118 **
119 */
120 void IconConfig::Load()
121 {
122 Assert(!Name.empty());
123
124 Icon = CIcon::Get(Name);
125 if (!Icon) {
126 fprintf(stderr, "Can't find icon %s\n", Name.c_str());
127 ExitFatal(-1);
128 }
129 };
130
131 /**
132 ** Init the icons.
133 **
134 ** Add the short name and icon aliases to hash table.
135 */
136 void InitIcons(void)
137 {
138 }
139
140 /**
141 ** Load the graphics for the icons.
142 */
143 void LoadIcons(void)
144 {
145 for (size_t i = 0; i < AllIcons.size(); ++i) {
146 CIcon *icon = AllIcons[i];
147 icon->G->Load();
148 ShowLoadProgress("Icons %s", icon->G->File.c_str());
149 if (icon->Frame >= icon->G->NumFrames) {
150 DebugPrint("Invalid icon frame: %s - %d\n" _C_
151 icon->GetIdent().c_str() _C_ icon->Frame);
152 icon->Frame = 0;
153 }
154 }
155 }
156
157 /**
158 ** Clean up memory used by the icons.
159 */
160 void CleanIcons(void)
161 {
162 std::vector<CIcon *>::iterator i;
163 for (i = AllIcons.begin(); i != AllIcons.end(); ++i) {
164 delete *i;
165 }
166 AllIcons.clear();
167 Icons.clear();
168 }
169
170 /**
171 ** Draw icon on x,y.
172 **
173 ** @param player Player pointer used for icon colors
174 ** @param x X display pixel position
175 ** @param y Y display pixel position
176 */
177 void CIcon::DrawIcon(const CPlayer *player, int x, int y) const
178 {
179 CPlayerColorGraphic *g = dynamic_cast<CPlayerColorGraphic *>(this->G);
180 if (g) {
181 g->DrawPlayerColorFrameClip(player->Index, this->Frame, x, y);
182 } else {
183 this->G->DrawFrameClip(this->Frame, x, y);
184 }
185 }
186
187 /**
188 ** Draw unit icon 'icon' with border on x,y
189 **
190 ** @param player Player pointer used for icon colors
191 ** @param style Button style
192 ** @param flags State of icon (clicked, mouse over...)
193 ** @param x X display pixel position
194 ** @param y Y display pixel position
195 ** @param text Optional text to display
196 */
197 void CIcon::DrawUnitIcon(const CPlayer *player, ButtonStyle *style,
198 unsigned flags, int x, int y, const std::string &text) const
199 {
200 static ButtonStyle s;
201 s = *style;
202
203 s.Default.Sprite = s.Hover.Sprite = s.Clicked.Sprite = this->G;
204 s.Default.Frame = s.Hover.Frame = s.Clicked.Frame = this->Frame;
205 if (!(flags & IconSelected) && (flags & IconAutoCast)) {
206 s.Default.BorderColorRGB = UI.ButtonPanel.AutoCastBorderColorRGB;
207 s.Default.BorderColor = 0;
208 }
209 // FIXME: player colors
210 DrawMenuButton(&s, flags, x, y, text);
211 }
212
213 /**
214 ** Register CCL features for icons.
215 */
216 void IconCclRegister(void)
217 {
218 }
219
220 //@}