1 ///////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2004-2011 by The Allacrost Project
3 //            Copyright (C) 2012-2016 by Bertram (Valyria Tear)
4 //                         All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software
7 // and you may modify it and/or redistribute it under the terms of this license.
8 // See http://www.gnu.org/copyleft/gpl.html for details.
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 /** ****************************************************************************
12 *** \file    message_window.cpp
13 *** \author  Daniel Steuernol steu@allacrost.org
14 *** \author  Andy Gardner chopperdave@allacrost.org
15 *** \author  Nik Nadig (IkarusDowned) nihonnik@gmail.com
16 *** \author  Yohann Ferreira, yohann ferreira orange fr
17 *** \brief   Source file for a common message window.
18 *** ***************************************************************************/
19 
20 #include "message_window.h"
21 
22 #include "engine/video/video.h"
23 
24 namespace vt_common
25 {
26 
MessageWindow(const vt_utils::ustring & message,float x,float y,float w,float h)27 MessageWindow::MessageWindow(const vt_utils::ustring& message, float x, float y, float w, float h)
28 {
29     CreateMessageWindow(x, y, w, h);
30     _textbox.SetDisplayText(message);
31 }
32 
~MessageWindow()33 MessageWindow::~MessageWindow()
34 {
35     MenuWindow::Destroy();
36 }
37 
CreateMessageWindow(float x,float y,float w,float h)38 void MessageWindow::CreateMessageWindow(float x, float y, float w, float h)
39 {
40     // Center the window when it's requested.
41     float start_x = x == -1.0f ? (1024 - w) / 2 : x;
42     float start_y = y == -1.0f ? (768 - h) / 2 : y;
43 
44     MenuWindow::Create(w, h);
45     MenuWindow::SetPosition(start_x, start_y);
46     MenuWindow::Show();
47 
48     _textbox.SetPosition(15.0f, -10.0f);
49     _textbox.SetDimensions(w - 30.0f, h - 20.0f);
50     _textbox.SetTextStyle(vt_video::TextStyle("text22"));
51     _textbox.SetDisplayMode(vt_gui::VIDEO_TEXT_CHAR);
52     _textbox.SetTextAlignment(vt_video::VIDEO_X_LEFT, vt_video::VIDEO_Y_CENTER);
53     _textbox.SetOwner(this);
54 }
55 
Draw()56 void MessageWindow::Draw()
57 {
58     MenuWindow::Draw();
59     _textbox.Draw();
60 }
61 
62 // Class ShortNoticeWindow
63 
ShortNoticeWindow(const vt_utils::ustring & message,const std::string & image_filename,uint32_t display_time,float x,float y)64 ShortNoticeWindow::ShortNoticeWindow(const vt_utils::ustring& message,
65                                        const std::string& image_filename,
66                                        uint32_t display_time,
67                                        float x, float y):
68     _display_time(display_time)
69 {
70     _text_image.SetText(message, vt_video::TextStyle("text36"));
71 
72     // Test whether an image can be shown.
73     _has_image = image_filename.empty() ? false : _icon_image.Load(image_filename);
74 
75     // Determine the width and height of the window based on the text.
76     float width = _text_image.GetWidth() + 30.0f;
77     float height = _text_image.GetHeight() + 30.0f;
78 
79     if (_has_image) {
80 
81         // Adapt the image to the text
82         if (_icon_image.GetHeight() > _text_image.GetHeight())
83             _icon_image.SetHeightKeepRatio(_text_image.GetHeight());
84         else if (_icon_image.GetHeight() < _text_image.GetHeight() / 2.0f)
85             _icon_image.SetHeightKeepRatio(_text_image.GetHeight() / 2.0f);
86 
87         if (_icon_image.GetWidth() > 50.0f)
88             _icon_image.SetWidthKeepRatio(50.0f);
89 
90         // Give some space for the icon
91         width += _icon_image.GetWidth() + 5.0f;
92     }
93 
94     // Center the window when it's requested.
95     float start_x = x == -1.0f ? (1024 - width) / 2 : x;
96     float start_y = y == -1.0f ? (768 - height) / 2 : y;
97 
98     // Create but don't show the window per default.
99     Create(width, height);
100     SetPosition(start_x, start_y);
101     Hide();
102 
103     // Set the icon and text draw positions
104     float initial_width = _has_image ? _icon_image.GetWidth() + 5.0f + _text_image.GetWidth() : _text_image.GetWidth();
105     _text_pos.x = MenuWindow::GetXPosition() + (MenuWindow::GetWidth() - initial_width) / 2.0f;
106     _text_pos.y = MenuWindow::GetYPosition() + (MenuWindow::GetHeight() - _text_image.GetHeight()) / 2.0f;
107 
108     if (_has_image)
109         _text_pos.x += _icon_image.GetWidth() + 5.0f;
110 
111     _icon_pos.x = MenuWindow::GetXPosition() + 15.0f;
112     _icon_pos.y = MenuWindow::GetYPosition() + (MenuWindow::GetHeight() - _icon_image.GetHeight()) / 2.0f;
113 }
114 
~ShortNoticeWindow()115 ShortNoticeWindow::~ShortNoticeWindow()
116 {
117     MenuWindow::Destroy();
118 }
119 
Update(uint32_t update_time)120 void ShortNoticeWindow::Update(uint32_t update_time)
121 {
122     if (_display_time > update_time) {
123         _display_time -= update_time;
124         if (!IsVisible())
125             Show();
126     }
127     else {
128         _display_time = 0;
129         Hide();
130     }
131 }
132 
Draw()133 void ShortNoticeWindow::Draw()
134 {
135     MenuWindow::Draw();
136 
137     vt_video::VideoManager->PushState();
138     vt_video::VideoManager->SetStandardCoordSys();
139     vt_video::VideoManager->SetDrawFlags(vt_video::VIDEO_X_LEFT, vt_video::VIDEO_Y_TOP, vt_video::VIDEO_BLEND, 0);
140 
141     vt_video::VideoManager->Move(_text_pos.x, _text_pos.y);
142     // Determine the text and icon position based on the window current size
143     _text_image.Draw();
144 
145     if (_has_image) {
146         vt_video::VideoManager->Move(_icon_pos.x, _icon_pos.y);
147         _icon_image.Draw();
148     }
149     vt_video::VideoManager->PopState();
150 }
151 
152 } // namespace vt_common
153