/
GetCharacterAtCoord_example.cpp
193 lines (151 loc) · 6.05 KB
/
GetCharacterAtCoord_example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
////////////////////////////////////////////////////////////////
//
// The MIT License (MIT)
//
// Copyright (c) 2022-2024 M.J.Silk
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
//
// ------------
// INTRODUCTION
// ------------
//
// Displays a single line of text as well as a multi-line text object.
// Whichever character the mouse hovering over, is shown (as well as its index in the text string) and the character is also visually highlighted.
//
//
// --------
// CONTROLS
// --------
//
// MOUSE hover cursor over a text character
// ESC quit
//
//
// ----
// NOTE
// ----
//
// If the window is too large (1600x900) for your resolution, you can uncomment the following define line to halve the window size (to 800x450) and scale the texts to match: #define HALVE_WINDOW_SIZE
//
//
////////////////////////////////////////////////////////////////
#include <iostream>
#include <functional>
#include <SFML/Graphics.hpp>
#include "GetCharacterAtCoord.hpp"
//#define HALVE_WINDOW_SIZE
int main()
{
sf::Vector2u windowSize{ 1600u, 900u };
#ifdef HALVE_WINDOW_SIZE
windowSize /= 2u;
#endif // HALVE_WINDOW_SIZE
sf::Font font;
if (!font.loadFromFile("resources/fonts/arial.ttf"))
{
std::cerr << "ERROR LOADING FONT" << std::endl;
return EXIT_FAILURE;
}
auto setTextOriginToCenter = [](sf::Text& text)
{
const sf::FloatRect textLocalBounds{ text.getLocalBounds() };
text.setOrigin(textLocalBounds.left + (textLocalBounds.width / 2.f), textLocalBounds.top + (textLocalBounds.height / 2.f));
};
sf::Text text;
text.setFont(font);
text.setCharacterSize(64u);
text.setFillColor(sf::Color::White);
text.setString("ABCDEF\nGHIJKLM\nNOPQRST\n\tUVWXYZ\n12345\n67890");
setTextOriginToCenter(text);
text.setPosition(sf::Vector2f(windowSize.x * 0.5f, windowSize.y * 0.6f));
sf::Text textSingleLine = text;
textSingleLine.setString("ABCDEFGHIJKLMNOPQRST\tUVWXYZ1234567890");
setTextOriginToCenter(textSingleLine);
textSingleLine.setPosition(sf::Vector2f(windowSize.x * 0.5f, windowSize.y * 0.25f));
sf::Text textCharacterHighlight = text;
textCharacterHighlight.setString("");
textCharacterHighlight.setOrigin({ 0.f, 0.f });
textCharacterHighlight.setFillColor(sf::Color::Red);
textCharacterHighlight.setOutlineColor(sf::Color::White);
textCharacterHighlight.setOutlineThickness(2.f);
sf::Text feedbackText;
feedbackText.setFont(font);
feedbackText.setCharacterSize(36u);
feedbackText.setFillColor(sf::Color::Green);
feedbackText.setPosition({ 5.f, 5.f });
#ifdef HALVE_WINDOW_SIZE
text.setScale(0.5f, 0.5f);
textSingleLine.setScale(0.5f, 0.5f);
textCharacterHighlight.setScale(0.5f, 0.5f);
feedbackText.setScale(0.5f, 0.5f);
#endif // HALVE_WINDOW_SIZE
sf::RenderWindow window(sf::VideoMode(windowSize.x, windowSize.y), "");
while (window.isOpen())
{
// EVENTS
sf::Event event;
while (window.pollEvent(event))
{
if ((event.type == sf::Event::Closed) || ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
window.close();
}
// UPDATE
const sf::Vector2f mouseCoord{ window.mapPixelToCoords(sf::Mouse::getPosition(window)) };
bool isMouseInsideText{ false };
bool isMouseInsideSingleLineText{ false };
char characterUnderMouse{ '\n' };
char singleLineCharacterUnderMouse{ '\n' };
std::size_t indexOfCharacterUnderMouse{ 0u };
std::size_t indexOfSingleLineCharacterUnderMouse{ 0u };
auto processTextUnderMouse = [mouseCoord, &textCharacterHighlight](bool& isMouseInside, sf::Text& text, char& c, std::size_t& i)
{
isMouseInside = text.getGlobalBounds().contains(mouseCoord);
i = getCharacterIndexAtCoord(text, mouseCoord);
if (i < text.getString().getSize())
{
c = text.getString()[i];
textCharacterHighlight.setString(c);
textCharacterHighlight.setPosition(text.findCharacterPos(i));
}
};
textCharacterHighlight.setString("");
processTextUnderMouse(isMouseInsideText, text, characterUnderMouse, indexOfCharacterUnderMouse);
processTextUnderMouse(isMouseInsideSingleLineText, textSingleLine, singleLineCharacterUnderMouse, indexOfSingleLineCharacterUnderMouse);
std::string feedbackString;
auto composeFeedbackString = [](char c, std::size_t i) { return ((c == '\n') ? "" : "Character Under Mouse: " + ((c == '\t') ? "[TAB]" : "\'" + std::string(1u, c) + "\'") + " (index: " + std::to_string(i) + ")"); };
if (isMouseInsideText)
feedbackString = composeFeedbackString(characterUnderMouse, indexOfCharacterUnderMouse);
else if (isMouseInsideSingleLineText)
feedbackString = composeFeedbackString(singleLineCharacterUnderMouse, indexOfSingleLineCharacterUnderMouse);
feedbackText.setString(feedbackString);
// RENDER
window.clear();
window.draw(textSingleLine);
window.draw(text);
if (isMouseInsideText || isMouseInsideSingleLineText)
window.draw(textCharacterHighlight);
window.draw(feedbackText);
window.display();
}
}