1 //============================================================================
2 //
3 // SSSS tt lll lll
4 // SS SS tt ll ll
5 // SS tttttt eeee ll ll aaaa
6 // SSSS tt ee ee ll ll aa
7 // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
8 // SS SS tt ee ll ll aa aa
9 // SSSS ttt eeeee llll llll aaaaa
10 //
11 // Copyright (c) 1995-2021 by Bradford W. Mott, Stephen Anthony
12 // and the Stella Team
13 //
14 // See the file "License.txt" for information on usage and redistribution of
15 // this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 //============================================================================
17
18 #include "OSystem.hxx"
19 #include "Dialog.hxx"
20 #include "DialogContainer.hxx"
21 #include "Font.hxx"
22 #include "FrameBuffer.hxx"
23 #include "FBSurface.hxx"
24 #include "Widget.hxx"
25
26 #include "ToolTip.hxx"
27
28 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ToolTip(Dialog & dialog,const GUI::Font & font)29 ToolTip::ToolTip(Dialog& dialog, const GUI::Font& font)
30 : myDialog{dialog}
31 {
32 myScale = myDialog.instance().frameBuffer().hidpiScaleFactor();
33
34 setFont(font);
35 }
36
37 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
~ToolTip()38 ToolTip::~ToolTip()
39 {
40 myDialog.instance().frameBuffer().deallocateSurface(mySurface);
41 }
42
43 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
setFont(const GUI::Font & font)44 void ToolTip::setFont(const GUI::Font& font)
45 {
46 myFont = &font;
47
48 const int fontWidth = myFont->getMaxCharWidth(),
49 fontHeight = myFont->getFontHeight();
50
51 myTextXOfs = fontHeight < 24 ? 5 : 8;
52 myTextYOfs = fontHeight < 24 ? 2 : 3;
53 myWidth = fontWidth * MAX_COLUMNS + myTextXOfs * 2;
54 myHeight = fontHeight * MAX_ROWS + myTextYOfs * 2;
55
56 // unallocate
57 myDialog.instance().frameBuffer().deallocateSurface(mySurface);
58 mySurface = nullptr;
59 }
60
61 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
surface()62 const shared_ptr<FBSurface>& ToolTip::surface()
63 {
64 if(mySurface == nullptr)
65 mySurface = myDialog.instance().frameBuffer().allocateSurface(myWidth, myHeight);
66
67 return mySurface;
68 }
69
70 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
request()71 void ToolTip::request()
72 {
73 // Called each frame when a tooltip is wanted
74 if(myFocusWidget && myTimer < DELAY_TIME * RELEASE_SPEED)
75 {
76 const string tip = myFocusWidget->getToolTip(myMousePos);
77
78 if(!tip.empty())
79 {
80 myTipWidget = myFocusWidget;
81 myTimer += RELEASE_SPEED;
82 if(myTimer >= DELAY_TIME * RELEASE_SPEED)
83 show(tip);
84 }
85 }
86 }
87
88 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
update(const Widget * widget,const Common::Point & pos)89 void ToolTip::update(const Widget* widget, const Common::Point& pos)
90 {
91 // Called each mouse move
92 myMousePos = pos;
93 myFocusWidget = widget;
94
95 if(myTipWidget != widget)
96 release(false);
97
98 if(!myTipShown)
99 release(true);
100 else
101 {
102 if(myTipWidget->changedToolTip(myTipPos, myMousePos))
103 {
104 const string tip = myTipWidget->getToolTip(myMousePos);
105
106 if(!tip.empty())
107 show(tip);
108 else
109 release(true);
110 }
111 }
112 }
113
114 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
hide()115 void ToolTip::hide()
116 {
117 if(myTipShown)
118 {
119 myTimer = 0;
120 myTipWidget = myFocusWidget = nullptr;
121 myTipShown = false;
122 myDialog.instance().frameBuffer().setPendingRender();
123 }
124 }
125
126 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
release(bool emptyTip)127 void ToolTip::release(bool emptyTip)
128 {
129 if(myTipShown)
130 {
131 myTipShown = false;
132 myDialog.instance().frameBuffer().setPendingRender();
133 }
134
135 // After displaying a tip, slowly reset the timer to 0
136 // until a new tip is requested
137 if((emptyTip || myTipWidget != myFocusWidget) && myTimer)
138 myTimer--;
139 }
140
141 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
show(const string & tip)142 void ToolTip::show(const string& tip)
143 {
144 myTipPos = myMousePos;
145
146 uInt32 maxWidth = std::min(myWidth - myTextXOfs * 2, uInt32(myFont->getStringWidth(tip)));
147
148 surface()->fillRect(1, 1, maxWidth + myTextXOfs * 2 - 2, myHeight - 2, kWidColor);
149 int lines = std::min(MAX_ROWS,
150 uInt32(surface()->drawString(*myFont, tip, myTextXOfs, myTextYOfs,
151 maxWidth, myHeight - myTextYOfs * 2,
152 kTextColor)));
153 // Calculate maximum width of drawn string lines
154 uInt32 width = 0;
155 string inStr = tip;
156 for(int i = 0; i < lines; ++i)
157 {
158 string leftStr, rightStr;
159
160 surface()->splitString(*myFont, inStr, maxWidth, leftStr, rightStr);
161 width = std::max(width, uInt32(myFont->getStringWidth(leftStr)));
162 inStr = rightStr;
163 }
164 width += myTextXOfs * 2;
165
166 // Calculate and set surface size and position
167 const uInt32 height = std::min(myHeight, myFont->getFontHeight() * lines + myTextYOfs * 2);
168 const uInt32 V_GAP = 1;
169 const uInt32 H_CURSOR = 18;
170 // Note: The rects include HiDPI scaling
171 const Common::Rect imageRect = myDialog.instance().frameBuffer().imageRect();
172 const Common::Rect dialogRect = myDialog.surface().dstRect();
173 // Limit position to app size and adjust accordingly
174 const Int32 xAbs = myTipPos.x + dialogRect.x() / myScale;
175 const uInt32 yAbs = myTipPos.y + dialogRect.y() / myScale;
176 Int32 x = std::min(xAbs, Int32(imageRect.w() / myScale - width));
177 const uInt32 y = (yAbs + height + H_CURSOR > imageRect.h() / myScale)
178 ? yAbs - height - V_GAP
179 : yAbs + H_CURSOR / myScale + V_GAP;
180
181 if(x < 0)
182 {
183 x = 0;
184 width = std::min(width, imageRect.w() / myScale);
185 }
186
187 surface()->setSrcSize(width, height);
188 surface()->setDstSize(width * myScale, height * myScale);
189 surface()->setDstPos(x * myScale, y * myScale);
190 surface()->frameRect(0, 0, width, height, kColor);
191
192 myTipShown = true;
193 myDialog.instance().frameBuffer().setPendingRender();
194 }
195
196 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
render()197 void ToolTip::render()
198 {
199 if(myTipShown)
200 surface()->render();
201 }
202