1 /* Copyright (C) 2016 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
22 #include "CGUIScrollBarVertical.h"
29 AddSetting(GUIST_float
, "buffer_zone");
30 AddSetting(GUIST_CGUIString
, "caption");
31 AddSetting(GUIST_int
, "cell_id");
32 AddSetting(GUIST_bool
, "clip");
33 AddSetting(GUIST_CStrW
, "font");
34 AddSetting(GUIST_bool
, "scrollbar");
35 AddSetting(GUIST_CStr
, "scrollbar_style");
36 AddSetting(GUIST_bool
, "scroll_bottom");
37 AddSetting(GUIST_bool
, "scroll_top");
38 AddSetting(GUIST_CGUISpriteInstance
, "sprite");
39 AddSetting(GUIST_EAlign
, "text_align");
40 AddSetting(GUIST_EVAlign
, "text_valign");
41 AddSetting(GUIST_CColor
, "textcolor");
42 AddSetting(GUIST_CColor
, "textcolor_disabled");
43 AddSetting(GUIST_CStrW
, "tooltip");
44 AddSetting(GUIST_CStr
, "tooltip_style");
47 AddSetting(GUIST_CStrW
, "_icon_tooltip");
48 AddSetting(GUIST_CStr
, "_icon_tooltip_style");
50 //GUI<bool>::SetSetting(this, "ghost", true);
51 GUI
<bool>::SetSetting(this, "scrollbar", false);
52 GUI
<bool>::SetSetting(this, "clip", true);
55 CGUIScrollBarVertical
* bar
= new CGUIScrollBarVertical();
56 bar
->SetRightAligned(true);
60 AddText(new SGUIText());
67 void CText::SetupText()
72 ENSURE(m_GeneratedTexts
.size()>=1);
75 if (GUI
<CStrW
>::GetSetting(this, "font", font
) != PSRETURN_OK
|| font
.empty())
76 // Use the default if none is specified
77 // TODO Gee: (2004-08-14) Don't define standard like this. Do it with the default style.
82 GUI
<CGUIString
>::GetSetting(this, "caption", caption
);
83 GUI
<bool>::GetSetting(this, "scrollbar", scrollbar
);
85 float width
= m_CachedActualSize
.GetWidth();
86 // remove scrollbar if applicable
87 if (scrollbar
&& GetScrollBar(0).GetStyle())
88 width
-= GetScrollBar(0).GetStyle()->m_Width
;
91 float buffer_zone
= 0.f
;
92 GUI
<float>::GetSetting(this, "buffer_zone", buffer_zone
);
93 *m_GeneratedTexts
[0] = GetGUI()->GenerateText(caption
, font
, width
, buffer_zone
, this);
96 CalculateTextPosition(m_CachedActualSize
, m_TextPos
, *m_GeneratedTexts
[0]);
101 bool scroll_top
= false, scroll_bottom
= false;
102 GUI
<bool>::GetSetting(this, "scroll_bottom", scroll_bottom
);
103 GUI
<bool>::GetSetting(this, "scroll_top", scroll_top
);
105 // If we are currently scrolled to the bottom of the text,
106 // then add more lines of text, update the scrollbar so we
107 // stick to the bottom.
108 // (Use 1.5px delta so this triggers the first time caption is set)
110 if (scroll_bottom
&& GetScrollBar(0).GetPos() > GetScrollBar(0).GetMaxPos() - 1.5f
)
113 GetScrollBar(0).SetScrollRange(m_GeneratedTexts
[0]->m_Size
.cy
);
114 GetScrollBar(0).SetScrollSpace(m_CachedActualSize
.GetHeight());
116 GetScrollBar(0).SetX(m_CachedActualSize
.right
);
117 GetScrollBar(0).SetY(m_CachedActualSize
.top
);
118 GetScrollBar(0).SetZ(GetBufferedZ());
119 GetScrollBar(0).SetLength(m_CachedActualSize
.bottom
- m_CachedActualSize
.top
);
122 GetScrollBar(0).SetPos(GetScrollBar(0).GetMaxPos());
124 GetScrollBar(0).SetPos(0.0f
);
128 void CText::HandleMessage(SGUIMessage
& Message
)
130 IGUIScrollBarOwner::HandleMessage(Message
);
131 //IGUITextOwner::HandleMessage(Message); <== placed it after the switch instead!
133 switch (Message
.type
)
135 case GUIM_SETTINGS_UPDATED
:
136 if (Message
.value
== "scrollbar")
140 if (Message
.value
== "scrollbar_style")
142 CStr scrollbar_style
;
143 GUI
<CStr
>::GetSetting(this, Message
.value
, scrollbar_style
);
145 GetScrollBar(0).SetScrollBarStyle(scrollbar_style
);
152 case GUIM_MOUSE_WHEEL_DOWN
:
154 GetScrollBar(0).ScrollPlus();
155 // Since the scroll was changed, let's simulate a mouse movement
156 // to check if scrollbar now is hovered
157 SGUIMessage
msg(GUIM_MOUSE_MOTION
);
161 case GUIM_MOUSE_WHEEL_UP
:
163 GetScrollBar(0).ScrollMinus();
164 // Since the scroll was changed, let's simulate a mouse movement
165 // to check if scrollbar now is hovered
166 SGUIMessage
msg(GUIM_MOUSE_MOTION
);
172 GetScrollBar(0).SetX(m_CachedActualSize
.right
);
173 GetScrollBar(0).SetY(m_CachedActualSize
.top
);
174 GetScrollBar(0).SetZ(GetBufferedZ());
175 GetScrollBar(0).SetLength(m_CachedActualSize
.bottom
- m_CachedActualSize
.top
);
177 CStr scrollbar_style
;
178 GUI
<CStr
>::GetSetting(this, "scrollbar_style", scrollbar_style
);
179 GetScrollBar(0).SetScrollBarStyle(scrollbar_style
);
187 IGUITextOwner::HandleMessage(Message
);
192 float bz
= GetBufferedZ();
194 // First call draw on ScrollBarOwner
196 GUI
<bool>::GetSetting(this, "scrollbar", scrollbar
);
200 IGUIScrollBarOwner::Draw();
205 CGUISpriteInstance
* sprite
;
208 GUI
<CGUISpriteInstance
>::GetSettingPointer(this, "sprite", sprite
);
209 GUI
<int>::GetSetting(this, "cell_id", cell_id
);
210 GUI
<bool>::GetSetting(this, "clip", clip
);
212 GetGUI()->DrawSprite(*sprite
, cell_id
, bz
, m_CachedActualSize
);
216 scroll
= GetScrollBar(0).GetPos();
218 // Clipping area (we'll have to subtract the scrollbar)
222 cliparea
= m_CachedActualSize
;
226 // subtract scrollbar from cliparea
227 if (cliparea
.right
> GetScrollBar(0).GetOuterRect().left
&&
228 cliparea
.right
<= GetScrollBar(0).GetOuterRect().right
)
229 cliparea
.right
= GetScrollBar(0).GetOuterRect().left
;
231 if (cliparea
.left
>= GetScrollBar(0).GetOuterRect().left
&&
232 cliparea
.left
< GetScrollBar(0).GetOuterRect().right
)
233 cliparea
.left
= GetScrollBar(0).GetOuterRect().right
;
238 GUI
<bool>::GetSetting(this, "enabled", enabled
);
241 GUI
<CColor
>::GetSetting(this, enabled
? "textcolor" : "textcolor_disabled", color
);
244 DrawText(0, color
, m_CachedActualSize
.TopLeft() - CPos(0.f
, scroll
), bz
+0.1f
, cliparea
);
246 DrawText(0, color
, m_TextPos
, bz
+0.1f
, cliparea
);
249 bool CText::MouseOverIcon()
251 for (SGUIText
* const& guitext
: m_GeneratedTexts
)
252 for (const SGUIText::SSpriteCall
& spritecall
: guitext
->m_SpriteCalls
)
254 // Check mouse over sprite
255 if (!spritecall
.m_Area
.PointInside(GetMousePos() - m_CachedActualSize
.TopLeft()))
258 // If tooltip exists, set the property
259 if (!spritecall
.m_Tooltip
.empty())
261 SetSetting("_icon_tooltip_style", spritecall
.m_TooltipStyle
);
262 SetSetting("_icon_tooltip", spritecall
.m_Tooltip
);