Mercurial > fife-parpg
comparison ext/guichan-0.8.1/src/widgets/imagebutton.cpp @ 89:fa33cda75471
* Reverting back to 2543 as requested by sleek
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sat, 19 Jul 2008 11:38:52 +0000 |
parents | 4a0efb7baf70 |
children |
comparison
equal
deleted
inserted
replaced
88:1c2842ebe393 | 89:fa33cda75471 |
---|---|
1 /* _______ __ __ __ ______ __ __ _______ __ __ | |
2 * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\ | |
3 * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / / | |
4 * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / / | |
5 * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / / | |
6 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ / | |
7 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/ | |
8 * | |
9 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson | |
10 * | |
11 * | |
12 * Per Larsson a.k.a finalman | |
13 * Olof Naessén a.k.a jansem/yakslem | |
14 * | |
15 * Visit: http://guichan.sourceforge.net | |
16 * | |
17 * License: (BSD) | |
18 * Redistribution and use in source and binary forms, with or without | |
19 * modification, are permitted provided that the following conditions | |
20 * are met: | |
21 * 1. Redistributions of source code must retain the above copyright | |
22 * notice, this list of conditions and the following disclaimer. | |
23 * 2. Redistributions in binary form must reproduce the above copyright | |
24 * notice, this list of conditions and the following disclaimer in | |
25 * the documentation and/or other materials provided with the | |
26 * distribution. | |
27 * 3. Neither the name of Guichan nor the names of its contributors may | |
28 * be used to endorse or promote products derived from this software | |
29 * without specific prior written permission. | |
30 * | |
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | |
37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
42 */ | |
43 | |
44 /* | |
45 * For comments regarding functions please see the header file. | |
46 */ | |
47 | |
48 #include "guichan/widgets/imagebutton.hpp" | |
49 | |
50 #include "guichan/graphics.hpp" | |
51 #include "guichan/image.hpp" | |
52 | |
53 namespace gcn | |
54 { | |
55 ImageButton::ImageButton() | |
56 : mImage(0), | |
57 mInternalImage(false) | |
58 { | |
59 setWidth(0); | |
60 setHeight(0); | |
61 } | |
62 | |
63 ImageButton::ImageButton(const std::string& filename) | |
64 : mImage(0), | |
65 mInternalImage(false) | |
66 { | |
67 mImage = Image::load(filename); | |
68 mInternalImage = true; | |
69 setWidth(mImage->getWidth() + mImage->getWidth() / 2); | |
70 setHeight(mImage->getHeight() + mImage->getHeight() / 2); | |
71 } | |
72 | |
73 ImageButton::ImageButton(const Image* image) | |
74 : mImage(image), | |
75 mInternalImage(false) | |
76 { | |
77 setWidth(mImage->getWidth() + mImage->getWidth() / 2); | |
78 setHeight(mImage->getHeight() + mImage->getHeight() / 2); | |
79 } | |
80 | |
81 ImageButton::~ImageButton() | |
82 { | |
83 if (mInternalImage) | |
84 { | |
85 delete mImage; | |
86 } | |
87 } | |
88 | |
89 void ImageButton::setImage(const Image* image) | |
90 { | |
91 if (mInternalImage) | |
92 { | |
93 delete mImage; | |
94 } | |
95 | |
96 mImage = image; | |
97 mInternalImage = false; | |
98 } | |
99 | |
100 const Image* ImageButton::getImage() const | |
101 { | |
102 return mImage; | |
103 } | |
104 | |
105 void ImageButton::draw(Graphics* graphics) | |
106 { | |
107 gcn::Color faceColor = getBaseColor(); | |
108 gcn::Color highlightColor, shadowColor; | |
109 int alpha = getBaseColor().a; | |
110 | |
111 if (isPressed()) | |
112 { | |
113 faceColor = faceColor - 0x303030; | |
114 faceColor.a = alpha; | |
115 highlightColor = faceColor - 0x303030; | |
116 highlightColor.a = alpha; | |
117 shadowColor = faceColor + 0x303030; | |
118 shadowColor.a = alpha; | |
119 } | |
120 else | |
121 { | |
122 highlightColor = faceColor + 0x303030; | |
123 highlightColor.a = alpha; | |
124 shadowColor = faceColor - 0x303030; | |
125 shadowColor.a = alpha; | |
126 } | |
127 | |
128 graphics->setColor(faceColor); | |
129 graphics->fillRectangle(Rectangle(1, | |
130 1, | |
131 getDimension().width - 1, | |
132 getHeight() - 1)); | |
133 | |
134 graphics->setColor(highlightColor); | |
135 graphics->drawLine(0, 0, getWidth() - 1, 0); | |
136 graphics->drawLine(0, 1, 0, getHeight() - 1); | |
137 | |
138 graphics->setColor(shadowColor); | |
139 graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1); | |
140 graphics->drawLine(1, getHeight() - 1, getWidth() - 1, getHeight() - 1); | |
141 | |
142 graphics->setColor(getForegroundColor()); | |
143 | |
144 const int textX = (getWidth() - (mImage ? mImage->getWidth() : 0) ) / 2; | |
145 const int textY = (getHeight() - (mImage ? mImage->getHeight() : 0) ) / 2; | |
146 | |
147 if (isPressed()) | |
148 { | |
149 if(mImage) | |
150 graphics->drawImage(mImage, textX + 1, textY + 1); | |
151 } | |
152 else | |
153 { | |
154 if(mImage) | |
155 graphics->drawImage(mImage, textX, textY); | |
156 | |
157 if (isFocused()) | |
158 { | |
159 graphics->drawRectangle(Rectangle(2, | |
160 2, | |
161 getWidth() - 4, | |
162 getHeight() - 4)); | |
163 } | |
164 } | |
165 } | |
166 } |