1 //       _________ __                 __
2 //      /   _____//  |_____________ _/  |______     ____  __ __  ______
3 //      \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
4 //      /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ |
5 //     /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
6 //             \/                  \/          \//_____/            \/
7 //  ______________________                           ______________________
8 //                        T H E   W A R   B E G I N S
9 //         Stratagus - A free fantasy real time strategy game engine
10 //
11 /**@name uibuttons_proc.cpp - The UI buttons processing code. */
12 //
13 //      (c) Copyright 1999-2006 by Andreas Arens, Jimmy Salmon, Nehal Mistry
14 //
15 //      This program is free software; you can redistribute it and/or modify
16 //      it under the terms of the GNU General Public License as published by
17 //      the Free Software Foundation; only version 2 of the License.
18 //
19 //      This program is distributed in the hope that it will be useful,
20 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //      GNU General Public License for more details.
23 //
24 //      You should have received a copy of the GNU General Public License
25 //      along with this program; if not, write to the Free Software
26 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 //      02111-1307, USA.
28 //
29 
30 //@{
31 
32 /*----------------------------------------------------------------------------
33 --  Includes
34 ----------------------------------------------------------------------------*/
35 
36 #include "stratagus.h"
37 #include "ui.h"
38 #include "font.h"
39 #include "menus.h"
40 #include "player.h"
41 #include "video.h"
42 
43 /*----------------------------------------------------------------------------
44 -- Variables
45 ----------------------------------------------------------------------------*/
46 
47 /*----------------------------------------------------------------------------
48 -- UI buttons operation functions
49 ----------------------------------------------------------------------------*/
50 
51 /**
52 **  Draw UI button 'button' on x,y
53 **
54 **  @param style  Button style
55 **  @param flags  State of Button (clicked, mouse over...)
56 **  @param x      X display position
57 **  @param y      Y display position
58 **  @param text   text to print on button
59 */
DrawUIButton(ButtonStyle * style,unsigned flags,int x,int y,const std::string & text,int player)60 void DrawUIButton(ButtonStyle *style, unsigned flags, int x, int y,
61 				  const std::string &text, int player)
62 {
63 	ButtonStyleProperties *p;
64 
65 	if (flags & MI_FLAGS_CLICKED) {
66 		p = &style->Clicked;
67 	} else if (flags & MI_FLAGS_ACTIVE) {
68 		p = &style->Hover;
69 	} else {
70 		p = &style->Default;
71 	}
72 
73 	//
74 	//  Image
75 	//
76 	ButtonStyleProperties *pimage = p;
77 	if (!p->Sprite) {
78 		// No image.  Try hover, selected, then default
79 		if ((flags & MI_FLAGS_ACTIVE) && style->Hover.Sprite) {
80 			pimage = &style->Hover;
81 		} else if (style->Default.Sprite) {
82 			pimage = &style->Default;
83 		}
84 	}
85 	if (pimage->Sprite) {
86 		pimage->Sprite->Load();
87 	}
88 	if (pimage->Sprite) {
89 		CPlayerColorGraphic *colorGraphic = dynamic_cast<CPlayerColorGraphic *>(pimage->Sprite);
90 
91 		if (colorGraphic && player != -1) {
92 			colorGraphic->DrawPlayerColorFrameClip(player, pimage->Frame, x, y);
93 		} else {
94 			pimage->Sprite->DrawFrame(pimage->Frame, x, y);
95 		}
96 	}
97 
98 	//
99 	//  Text
100 	//
101 	if (!text.empty()) {
102 		std::string oldnc;
103 		std::string oldrc;
104 		GetDefaultTextColors(oldnc, oldrc);
105 		CLabel label(*style->Font,
106 					 (!p->TextNormalColor.empty() ? p->TextNormalColor :
107 					  !style->TextNormalColor.empty() ? style->TextNormalColor : oldnc),
108 					 (!p->TextReverseColor.empty() ? p->TextReverseColor :
109 					  !style->TextReverseColor.empty() ? style->TextReverseColor : oldrc));
110 
111 		if (p->TextAlign == TextAlignCenter || p->TextAlign == TextAlignUndefined) {
112 			label.DrawCentered(x + p->TextPos.x, y + p->TextPos.y, text);
113 		} else if (p->TextAlign == TextAlignLeft) {
114 			label.Draw(x + p->TextPos.x, y + p->TextPos.y, text);
115 		} else {
116 			label.Draw(x + p->TextPos.x - style->Font->Width(text), y + p->TextPos.y, text);
117 		}
118 	}
119 
120 	//
121 	//  Border
122 	//
123 	if (!p->BorderColor) {
124 		CColor color(p->BorderColorRGB);
125 		if (p->BorderColorRGB.R > 0 || p->BorderColorRGB.G > 0 || p->BorderColorRGB.B > 0) {
126 			int shift = GameCycle % 0x20;
127 			color.R >>= shift / 2;
128 			color.G >>= shift / 2;
129 			color.B >>= shift / 2;
130 			if (shift >= 0x10) {
131 				color.R = (p->BorderColorRGB.R > 0) << ((shift - 0x10) / 2);
132 				color.G = (p->BorderColorRGB.G > 0) << ((shift - 0x10) / 2);
133 				color.B = (p->BorderColorRGB.B > 0) << ((shift - 0x10) / 2);
134 			}
135 		}
136 		p->BorderColor = Video.MapRGB(TheScreen->format, color);
137 	} else {
138 		p->BorderColor = Video.MapRGB(TheScreen->format, p->BorderColorRGB);
139 	}
140 	if (p->BorderSize) {
141 		for (int i = 0; i < p->BorderSize; ++i) {
142 			Video.DrawRectangleClip(p->BorderColor, x - i, y - i,
143 									style->Width + 2 * i, style->Height + 2 * i);
144 		}
145 	}
146 }
147 
148 //@}
149