MessageBox.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/gui/MessageBox.hpp"
30 
31 namespace nom {
32  namespace ui {
33 
34 void MessageBox::initialize ( void )
35 {
36 NOM_LOG_TRACE ( NOM );
37 
38  this->enabled = true;
39  this->label_alignment = IFont::TextAlignment::MiddleLeft;
40 }
41 
42 MessageBox::MessageBox ( void )
43 {
44  this->initialize();
45 }
46 
47 MessageBox::MessageBox (
48  int32 x, int32 y, int32 width, int32 height,
49  enum FrameStyle style,
50  const Gradient& background
51  )
52 {
53  int32 padding = 1;
54  this->initialize();
55 
56  // init geometry coords w/ arguments list
57  this->coords = Coords ( x, y, width, height );
58 
59  Color gradient_color[2];
60  gradient_color[0] = background.getStartColor();
61  gradient_color[1] = background.getEndColor();
62 
63  this->drawable.push_back ( Gradient::SharedPtr ( new Gradient ( gradient_color, Coords ( this->coords.x, this->coords.y, this->coords.width, this->coords.height ), 0, 0, background.getFillDirection() ) ) );
64 
65  if ( style == FrameStyle::Gray )
66  {
67  this->drawable.push_back ( GrayFrame::SharedPtr ( new GrayFrame ( x, y, width, height, padding ) ) );
68  }
69 }
70 
71 MessageBox::MessageBox (
72  int32 x, int32 y, int32 width, int32 height,
73  GrayFrame::SharedPtr style,
74  Gradient::SharedPtr background
75  )
76 {
77  int32 padding = 1;
78 
79  this->initialize();
80 
81  // init geometry coords w/ arguments list
82  this->coords = Coords ( x, y, width, height );
83 
84  Color gradient_color[2];
85  gradient_color[0] = background->getStartColor();
86  gradient_color[1] = background->getEndColor();
87  this->drawable.push_back ( Gradient::SharedPtr ( new Gradient ( gradient_color, Coords ( this->coords.x, this->coords.y, this->coords.width, this->coords.height ), 0, 0, background->getFillDirection() ) ) );
88 
89  if ( style != nullptr )
90  {
91  style->setPosition ( x, y );
92  style->setSize ( width, height, padding );
93  this->drawable.push_back ( style );
94  }
95  else // default frame style
96  {
97  this->drawable.push_back ( GrayFrame::SharedPtr ( new GrayFrame ( x, y, width, height, padding ) ) );
98  }
99 }
100 
101 MessageBox::~MessageBox ( void )
102 {
103 NOM_LOG_TRACE ( NOM );
104 }
105 
106 bool MessageBox::isEnabled ( void ) const
107 {
108  if ( ! this->enabled ) return false;
109 
110  return true;
111 }
112 
113 const std::string MessageBox::getWindowTitle ( void )
114 {
115  if ( this->label ) return this->window_title->getText();
116 
117  return "\0";
118 }
119 
120 const std::string MessageBox::getLabel ( void )
121 {
122  if ( this->label ) return this->label->getText();
123 
124  return "\0";
125 }
126 
127 void MessageBox::disable ( void )
128 {
129  this->enabled = false;
130 }
131 
132 void MessageBox::enable ( void )
133 {
134  this->enabled = true;
135 }
136 
137 const Point2i MessageBox::size ( void ) const
138 {
139  return Point2i ( this->coords.w, this->coords.h );
140 }
141 
142 const Point2i MessageBox::position ( void ) const
143 {
144  return Point2i ( this->coords.x, this->coords.y );
145 }
146 
147 void MessageBox::setWindowTitleFont ( const IFont::RawPtr font )
148 {
149  this->window_title = IFont::SharedPtr ( font->clone() );
150 
151  // This positions the title text of the message box on top of the second "top"
152  // bordering color of GrayFrame, commented as "top1".
153  //
154  // The original coords.x value was + 8, but I think + 4 looks best.
155  this->window_title->setPosition ( Coords ( this->coords.x + 4, this->coords.y, this->coords.width, this->coords.height ) );
156 
157  this->window_title->setText ( "INFO." );
158  this->window_title->setTextJustification ( IFont::TextAlignment::MiddleLeft );
159 
160  this->drawable.push_back ( IDrawable::SharedPtr ( this->window_title ) );
161 }
162 
163 void MessageBox::setLabelFont ( const IFont::RawPtr font )
164 {
165  this->label = IFont::SharedPtr ( font->clone() );
166  this->label->setPosition ( this->coords );
167 
168  this->drawable.push_back ( IDrawable::SharedPtr ( this->label ) );
169 }
170 
171 void MessageBox::setLabelPosition ( const Coords& pos )
172 {
173  if ( ! this->label ) return;
174 
175  this->label->setPosition ( pos );
176 }
177 
178 void MessageBox::setLabelTextAlignment ( IFont::TextAlignment alignment )
179 {
180  this->label_alignment = alignment;
181 }
182 
183 void MessageBox::setWindowTitle ( const std::string& text )
184 {
185  std::string text_buffer = text;
186 
187  if ( ! this->window_title ) return;
188 
189  // I LIKE TO YELL AT YOU IN ALL CAPS
190  std::transform ( text_buffer.begin(), text_buffer.end(), text_buffer.begin(), ::toupper);
191 
192  this->window_title->setText ( text_buffer );
193 }
194 
195 void MessageBox::setLabel ( const std::string& text )
196 {
197  if ( ! this->label ) return;
198 
199  this->label->setText ( text );
200 }
201 
202 void MessageBox::update ( void )
203 {
204  for ( auto it = this->drawable.begin(); it != this->drawable.end(); ++it )
205  {
206  IDrawable::SharedPtr obj = *it;
207  obj->update();
208  }
209 
210  if ( ! this->label ) return;
211 
212  switch ( this->label_alignment )
213  {
214  default:
215  case IFont::TextAlignment::MiddleLeft: // TODO
216  {
217  this->label->setTextJustification ( IFont::TextAlignment::MiddleLeft );
218  }
219  break;
220 
221  case IFont::TextAlignment::MiddleCenter:
222  {
223  this->label->setTextJustification ( IFont::TextAlignment::MiddleCenter );
224  }
225  break;
226 
227  case IFont::TextAlignment::MiddleRight: // TODO
228  {
229  this->label->setTextJustification ( IFont::TextAlignment::MiddleRight );
230  }
231  break;
232  } // end switch
233 }
234 
235 void MessageBox::draw ( RenderTarget target ) const
236 {
237  if ( this->isEnabled() == false ) return;
238 
239  for ( auto it = this->drawable.begin(); it != this->drawable.end(); ++it )
240  {
241  IDrawable::SharedPtr obj = *it;
242  obj->draw ( target );
243  }
244 }
245 
246 
247  } // namespace ui
248 } // namespace nom
std::shared_ptr< IDrawable > SharedPtr
std::shared_ptr definition type at your leisure, sir!
Definition: IDrawable.hpp:49
void draw(RenderTarget target) const
Render the object onto the primary video buffer – your visible screen.
Definition: MessageBox.cpp:235
int32 & w
Reference alias for width instance variable.
Definition: Coords.hpp:103