gosu 0.10.9.pre1 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gosu/Audio.hpp +35 -66
- data/Gosu/AutoLink.hpp +14 -16
- data/Gosu/Bitmap.hpp +50 -37
- data/Gosu/Buttons.hpp +246 -265
- data/Gosu/Color.hpp +32 -76
- data/Gosu/Directories.hpp +14 -17
- data/Gosu/Font.hpp +28 -34
- data/Gosu/Fwd.hpp +27 -31
- data/Gosu/Gosu.hpp +2 -5
- data/Gosu/Graphics.hpp +31 -48
- data/Gosu/GraphicsBase.hpp +27 -58
- data/Gosu/IO.hpp +44 -56
- data/Gosu/Image.hpp +29 -73
- data/Gosu/ImageData.hpp +13 -17
- data/Gosu/Input.hpp +42 -57
- data/Gosu/Inspection.hpp +2 -6
- data/Gosu/Math.hpp +32 -38
- data/Gosu/Platform.hpp +10 -29
- data/Gosu/Text.hpp +30 -39
- data/Gosu/TextInput.hpp +29 -36
- data/Gosu/Timing.hpp +14 -16
- data/Gosu/Utility.hpp +10 -15
- data/Gosu/Version.hpp +13 -14
- data/Gosu/Window.hpp +53 -68
- data/README.md +23 -11
- data/ext/gosu/extconf.rb +31 -81
- data/lib/gosu/patches.rb +35 -19
- data/lib/gosu/run.rb +13 -4
- data/rdoc/gosu.rb +24 -20
- data/src/ALChannelManagement.hpp +119 -0
- data/src/{Audio/Audio.cpp → Audio.cpp} +177 -211
- data/src/AudioFile.hpp +57 -0
- data/src/AudioToolboxFile.hpp +214 -0
- data/src/Bitmap.cpp +159 -0
- data/src/BitmapIO.cpp +141 -0
- data/src/BlockAllocator.cpp +133 -0
- data/src/{Graphics/BlockAllocator.hpp → BlockAllocator.hpp} +34 -35
- data/src/ClipRectStack.hpp +87 -0
- data/src/{Graphics/Color.cpp → Color.cpp} +30 -28
- data/src/DirectoriesApple.cpp +68 -0
- data/src/DirectoriesUnix.cpp +20 -18
- data/src/DirectoriesWin.cpp +40 -41
- data/src/DrawOp.hpp +168 -0
- data/src/DrawOpQueue.hpp +190 -0
- data/src/FileUnix.cpp +40 -46
- data/src/FileWin.cpp +42 -38
- data/src/Font.cpp +165 -0
- data/src/{Text/FormattedString.hpp → FormattedString.hpp} +114 -114
- data/src/GosuAppDelegate.cpp +30 -0
- data/src/{UIKit/GosuAppDelegate.h → GosuAppDelegate.h} +0 -0
- data/src/{UIKit/GosuGLView.mm → GosuGLView.cpp} +22 -17
- data/src/{UIKit/GosuGLView.h → GosuGLView.h} +0 -0
- data/src/GosuViewController.cpp +231 -0
- data/src/{UIKit/GosuViewController.h → GosuViewController.h} +0 -0
- data/src/Graphics.cpp +464 -0
- data/src/{Graphics/Common.hpp → GraphicsImpl.hpp} +29 -32
- data/src/IO.cpp +17 -16
- data/src/Iconv.hpp +13 -22
- data/src/Image.cpp +142 -0
- data/src/Input.cpp +459 -0
- data/src/InputUIKit.cpp +197 -0
- data/src/Inspection.cpp +4 -5
- data/src/LargeImageData.cpp +151 -0
- data/src/LargeImageData.hpp +43 -0
- data/src/{Graphics/Macro.cpp → Macro.cpp} +77 -78
- data/src/Macro.hpp +30 -0
- data/src/Math.cpp +17 -29
- data/src/{Audio/OggFile.hpp → OggFile.hpp} +19 -24
- data/src/RenderState.hpp +205 -0
- data/src/Resolution.cpp +86 -0
- data/src/ResolutionApple.cpp +25 -0
- data/{ext/gosu/gosu_wrap.cxx → src/RubyGosu.cxx} +2256 -1707
- data/{ext/gosu/gosu_wrap.h → src/RubyGosu.h} +9 -9
- data/src/{Audio/SndFile.hpp → SndFile.hpp} +54 -43
- data/src/TexChunk.cpp +117 -0
- data/src/{Graphics/TexChunk.hpp → TexChunk.hpp} +13 -18
- data/src/Text.cpp +371 -0
- data/src/TextApple.cpp +209 -0
- data/src/TextInput.cpp +278 -0
- data/src/TextTTFWin.cpp +251 -0
- data/src/{Text/TextUnix.cpp → TextUnix.cpp} +96 -92
- data/src/TextWin.cpp +194 -0
- data/src/{Graphics/Texture.cpp → Texture.cpp} +35 -38
- data/src/{Graphics/Texture.hpp → Texture.hpp} +9 -13
- data/src/TimingApple.cpp +11 -7
- data/src/TimingUnix.cpp +13 -7
- data/src/TimingWin.cpp +6 -1
- data/src/{Graphics/Transform.cpp → Transform.cpp} +17 -12
- data/src/{Graphics/TransformStack.hpp → TransformStack.hpp} +24 -25
- data/src/Utility.cpp +29 -70
- data/src/UtilityApple.cpp +52 -0
- data/src/UtilityWin.cpp +7 -4
- data/src/Version.cpp +22 -0
- data/src/WinMain.cpp +30 -33
- data/src/WinUtility.cpp +24 -22
- data/src/WinUtility.hpp +11 -20
- data/src/Window.cpp +142 -112
- data/src/WindowUIKit.cpp +155 -0
- data/src/stb_image.h +384 -173
- data/src/stb_vorbis.c +20 -18
- metadata +60 -62
- data/Gosu/TR1.hpp +0 -56
- data/src/AppleUtility.hpp +0 -66
- data/src/Audio/ALChannelManagement.hpp +0 -114
- data/src/Audio/Audio.mm +0 -1
- data/src/Audio/AudioFile.hpp +0 -53
- data/src/Audio/AudioToolboxFile.hpp +0 -207
- data/src/Bitmap/Bitmap.cpp +0 -183
- data/src/Bitmap/BitmapIO.cpp +0 -176
- data/src/DirectoriesApple.mm +0 -71
- data/src/Graphics/BlockAllocator.cpp +0 -142
- data/src/Graphics/ClipRectStack.hpp +0 -93
- data/src/Graphics/DrawOp.hpp +0 -175
- data/src/Graphics/DrawOpQueue.hpp +0 -188
- data/src/Graphics/Graphics.cpp +0 -478
- data/src/Graphics/Image.cpp +0 -193
- data/src/Graphics/LargeImageData.cpp +0 -133
- data/src/Graphics/LargeImageData.hpp +0 -46
- data/src/Graphics/Macro.hpp +0 -36
- data/src/Graphics/RenderState.hpp +0 -211
- data/src/Graphics/Resolution.cpp +0 -91
- data/src/Graphics/ResolutionApple.mm +0 -19
- data/src/Graphics/TexChunk.cpp +0 -112
- data/src/Input/Input.cpp +0 -463
- data/src/Input/InputUIKit.mm +0 -190
- data/src/Input/TextInput.cpp +0 -261
- data/src/Text/Font.cpp +0 -175
- data/src/Text/Text.cpp +0 -391
- data/src/Text/TextApple.mm +0 -227
- data/src/Text/TextTTFWin.cpp +0 -249
- data/src/Text/TextWin.cpp +0 -186
- data/src/UIKit/GosuAppDelegate.mm +0 -24
- data/src/UIKit/GosuViewController.mm +0 -211
- data/src/UtilityApple.mm +0 -63
- data/src/WindowUIKit.mm +0 -139
data/src/TextApple.cpp
ADDED
@@ -0,0 +1,209 @@
|
|
1
|
+
#include <Gosu/Platform.hpp>
|
2
|
+
#if defined(GOSU_IS_MAC)
|
3
|
+
|
4
|
+
#include <Gosu/Text.hpp>
|
5
|
+
#include <Gosu/Bitmap.hpp>
|
6
|
+
#include <Gosu/Math.hpp>
|
7
|
+
#include <Gosu/Utility.hpp>
|
8
|
+
#include <cmath>
|
9
|
+
#include <map>
|
10
|
+
|
11
|
+
#if defined(GOSU_IS_IPHONE)
|
12
|
+
#import <CoreGraphics/CoreGraphics.h>
|
13
|
+
#import <UIKit/UIKit.h>
|
14
|
+
typedef UIFont AppleFont;
|
15
|
+
#else
|
16
|
+
#import <AppKit/AppKit.h>
|
17
|
+
typedef NSFont AppleFont;
|
18
|
+
#endif
|
19
|
+
|
20
|
+
// If a font is a filename, loads the font and returns its family name that can be used
|
21
|
+
// like any system font. Otherwise, just returns the family name.
|
22
|
+
static std::string normalize_font(const std::string& font_name)
|
23
|
+
{
|
24
|
+
#ifdef GOSU_IS_IPHONE
|
25
|
+
// On iOS, we have no support for loading font files yet. However, if you register your fonts
|
26
|
+
// via your app's Info.plist, you should be able to reference them by name.
|
27
|
+
return font_name;
|
28
|
+
#else
|
29
|
+
static std::map<std::string, std::string> family_of_files;
|
30
|
+
|
31
|
+
// Not a path name: It is already a family name.
|
32
|
+
if (font_name.find("/") == font_name.npos) {
|
33
|
+
return font_name;
|
34
|
+
}
|
35
|
+
|
36
|
+
// Already activated font & extracted family name.
|
37
|
+
if (family_of_files.count(font_name) > 0) {
|
38
|
+
return family_of_files[font_name];
|
39
|
+
}
|
40
|
+
|
41
|
+
NSURL* url = [NSURL URLWithString:[NSString stringWithUTF8String:font_name.c_str()]];
|
42
|
+
if (url == nullptr) {
|
43
|
+
return family_of_files[font_name] = Gosu::default_font_name();
|
44
|
+
}
|
45
|
+
CFURLRef url_ref = (__bridge CFURLRef) url;
|
46
|
+
|
47
|
+
NSArray* descriptors = CFBridgingRelease(CTFontManagerCreateFontDescriptorsFromURL(url_ref));
|
48
|
+
if (descriptors.count < 1 ||
|
49
|
+
!CTFontManagerRegisterFontsForURL(url_ref, kCTFontManagerScopeProcess, nullptr)) {
|
50
|
+
return family_of_files[font_name] = Gosu::default_font_name();
|
51
|
+
}
|
52
|
+
|
53
|
+
CTFontDescriptorRef ref = (__bridge CTFontDescriptorRef) descriptors[0];
|
54
|
+
CFTypeRef family_name_ref = CTFontDescriptorCopyAttribute(ref, kCTFontFamilyNameAttribute);
|
55
|
+
NSString* family_name = CFBridgingRelease(family_name_ref);
|
56
|
+
return family_of_files[font_name] = family_name.UTF8String ?: "";
|
57
|
+
#endif
|
58
|
+
}
|
59
|
+
|
60
|
+
static AppleFont* get_font(std::string font_name, unsigned font_flags, double height)
|
61
|
+
{
|
62
|
+
font_name = normalize_font(font_name);
|
63
|
+
|
64
|
+
static std::map<std::pair<std::string, std::pair<unsigned, double>>, AppleFont*> used_fonts;
|
65
|
+
|
66
|
+
auto key = std::make_pair(font_name, std::make_pair(font_flags, height));
|
67
|
+
|
68
|
+
AppleFont* result = used_fonts[key];
|
69
|
+
if (!result) {
|
70
|
+
NSString* name = [NSString stringWithUTF8String:font_name.c_str()];
|
71
|
+
#ifdef GOSU_IS_IPHONE
|
72
|
+
result = [AppleFont fontWithName:name size:height];
|
73
|
+
#else
|
74
|
+
NSFontDescriptor* desc =
|
75
|
+
[[NSFontDescriptor fontDescriptorWithFontAttributes:nil] fontDescriptorWithFamily:name];
|
76
|
+
result = [NSFont fontWithDescriptor:desc size:height];
|
77
|
+
if (result && (font_flags & Gosu::FF_BOLD)) {
|
78
|
+
result =
|
79
|
+
[[NSFontManager sharedFontManager] convertFont:result toHaveTrait:NSFontBoldTrait];
|
80
|
+
}
|
81
|
+
if (result && (font_flags & Gosu::FF_ITALIC)) {
|
82
|
+
result = [[NSFontManager sharedFontManager] convertFont:result
|
83
|
+
toHaveTrait:NSFontItalicTrait];
|
84
|
+
}
|
85
|
+
#endif
|
86
|
+
if (result == nullptr) {
|
87
|
+
if (font_name != Gosu::default_font_name()) {
|
88
|
+
result = get_font(Gosu::default_font_name(), 0, height);
|
89
|
+
}
|
90
|
+
else {
|
91
|
+
throw std::runtime_error("Cannot load default font");
|
92
|
+
}
|
93
|
+
}
|
94
|
+
used_fonts[key] = result;
|
95
|
+
}
|
96
|
+
return result;
|
97
|
+
}
|
98
|
+
|
99
|
+
std::string Gosu::default_font_name()
|
100
|
+
{
|
101
|
+
return "Arial";
|
102
|
+
}
|
103
|
+
|
104
|
+
#ifndef GOSU_IS_IPHONE
|
105
|
+
static NSDictionary* attribute_dictionary(NSFont* font, unsigned font_flags)
|
106
|
+
{
|
107
|
+
auto underline_style =
|
108
|
+
(font_flags & Gosu::FF_UNDERLINE) ? NSUnderlineStyleSingle : NSUnderlineStyleNone;
|
109
|
+
return @{
|
110
|
+
NSFontAttributeName: font,
|
111
|
+
NSForegroundColorAttributeName: [NSColor whiteColor],
|
112
|
+
NSUnderlineStyleAttributeName: @(underline_style)
|
113
|
+
};
|
114
|
+
}
|
115
|
+
#endif
|
116
|
+
|
117
|
+
unsigned Gosu::text_width(const std::string& text, const std::string& font_name,
|
118
|
+
unsigned font_height, unsigned font_flags)
|
119
|
+
{
|
120
|
+
if (text.find_first_of("\r\n") != text.npos) {
|
121
|
+
throw std::invalid_argument("text_width cannot handle line breaks");
|
122
|
+
}
|
123
|
+
|
124
|
+
AppleFont* font = get_font(font_name, font_flags, font_height);
|
125
|
+
|
126
|
+
// This will, of course, compute a too large size; font_height is in pixels,
|
127
|
+
// the method expects point.
|
128
|
+
NSString* string = [NSString stringWithUTF8String:text.c_str()];
|
129
|
+
#ifndef GOSU_IS_IPHONE
|
130
|
+
NSDictionary* attributes = attribute_dictionary(font, font_flags);
|
131
|
+
NSSize size = [string sizeWithAttributes:attributes];
|
132
|
+
#else
|
133
|
+
CGSize size = [string sizeWithFont:font];
|
134
|
+
#endif
|
135
|
+
|
136
|
+
// Now adjust the scaling...
|
137
|
+
return ceil(size.width / size.height * font_height);
|
138
|
+
}
|
139
|
+
|
140
|
+
void Gosu::draw_text(Bitmap& bitmap, const std::string& text, int x, int y, Color c,
|
141
|
+
const std::string& font_name, unsigned font_height, unsigned font_flags)
|
142
|
+
{
|
143
|
+
if (text.find_first_of("\r\n") != text.npos) {
|
144
|
+
throw std::invalid_argument("the argument to draw_text cannot contain line breaks");
|
145
|
+
}
|
146
|
+
|
147
|
+
AppleFont* font = get_font(font_name, font_flags, font_height);
|
148
|
+
NSString* string = [NSString stringWithUTF8String:text.c_str()];
|
149
|
+
|
150
|
+
#ifndef GOSU_IS_IPHONE
|
151
|
+
NSDictionary* attributes = attribute_dictionary(font, font_flags);
|
152
|
+
NSSize size = [string sizeWithAttributes:attributes];
|
153
|
+
#else
|
154
|
+
CGSize size = [string sizeWithFont:font];
|
155
|
+
#endif
|
156
|
+
|
157
|
+
unsigned width = static_cast<unsigned>(round(size.width / size.height * font_height));
|
158
|
+
|
159
|
+
// Get the width and height of the image
|
160
|
+
Bitmap bmp(width, font_height, 0x00ffffff);
|
161
|
+
|
162
|
+
// Use a temporary context to draw the CGImage to the buffer.
|
163
|
+
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
|
164
|
+
CGContextRef context = CGBitmapContextCreate(bmp.data(), bmp.width(), bmp.height(), 8,
|
165
|
+
bmp.width() * 4, color_space, kCGImageAlphaPremultipliedLast);
|
166
|
+
CGColorSpaceRelease(color_space);
|
167
|
+
#ifdef GOSU_IS_IPHONE
|
168
|
+
CGFloat color[] = { 1.f, 1.f, 1.f, 0.f };
|
169
|
+
CGContextSetStrokeColor(context, color);
|
170
|
+
CGContextSetFillColor(context, color);
|
171
|
+
#endif
|
172
|
+
|
173
|
+
// Use new font with proper size this time.
|
174
|
+
font = get_font(font_name, font_flags, font_height * font_height / size.height);
|
175
|
+
|
176
|
+
#ifdef GOSU_IS_IPHONE
|
177
|
+
CGContextTranslateCTM(context, 0, font_height);
|
178
|
+
CGContextScaleCTM(context, 1, -1);
|
179
|
+
UIGraphicsPushContext(context);
|
180
|
+
[string drawAtPoint:CGPointZero withFont:font];
|
181
|
+
UIGraphicsPopContext();
|
182
|
+
#else
|
183
|
+
NSPoint NSPointZero = { 0, 0 };
|
184
|
+
attributes = attribute_dictionary(font, font_flags);
|
185
|
+
|
186
|
+
[NSGraphicsContext saveGraphicsState];
|
187
|
+
[NSGraphicsContext
|
188
|
+
setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:(void*)context
|
189
|
+
flipped:false]];
|
190
|
+
[string drawAtPoint:NSPointZero withAttributes:attributes];
|
191
|
+
[NSGraphicsContext restoreGraphicsState];
|
192
|
+
#endif
|
193
|
+
CGContextRelease(context);
|
194
|
+
|
195
|
+
int effective_width = Gosu::clamp<int>(width, 0, bitmap.width() - x);
|
196
|
+
int effective_height = Gosu::clamp<int>(font_height, 0, bitmap.height() - y);
|
197
|
+
|
198
|
+
// Now copy the set pixels back.
|
199
|
+
for (int rel_y = 0; rel_y < effective_height; ++rel_y) {
|
200
|
+
for (int rel_x = 0; rel_x < effective_width; ++rel_x) {
|
201
|
+
c.set_alpha(bmp.get_pixel(rel_x, rel_y).alpha());
|
202
|
+
if (c.alpha()) {
|
203
|
+
bitmap.set_pixel(x + rel_x, y + rel_y, c);
|
204
|
+
}
|
205
|
+
}
|
206
|
+
}
|
207
|
+
}
|
208
|
+
|
209
|
+
#endif
|
data/src/TextInput.cpp
ADDED
@@ -0,0 +1,278 @@
|
|
1
|
+
#include <Gosu/Platform.hpp>
|
2
|
+
#if !defined(GOSU_IS_IPHONE)
|
3
|
+
|
4
|
+
#include <Gosu/TextInput.hpp>
|
5
|
+
#include <Gosu/Input.hpp>
|
6
|
+
#include <Gosu/Platform.hpp>
|
7
|
+
#include <SDL.h>
|
8
|
+
#include <cctype>
|
9
|
+
|
10
|
+
struct Gosu::TextInput::Impl
|
11
|
+
{
|
12
|
+
std::string text;
|
13
|
+
|
14
|
+
// This is the current IME composition.
|
15
|
+
// http://wiki.libsdl.org/Tutorials/TextInput#CandidateList
|
16
|
+
std::string composition;
|
17
|
+
|
18
|
+
// Indices into the UTF-8 encoded text.
|
19
|
+
unsigned caret_pos = 0, selection_start = 0;
|
20
|
+
|
21
|
+
// Skip continuation characters, see: https://en.wikipedia.org/wiki/UTF-8#Description
|
22
|
+
// (0xc0 = 11'000000, 0x80 = 10'000000)
|
23
|
+
bool should_skip(char ch)
|
24
|
+
{
|
25
|
+
return (static_cast<unsigned char>(ch) & 0xc0) == 0x80;
|
26
|
+
}
|
27
|
+
|
28
|
+
void insert_text(const std::string& new_text)
|
29
|
+
{
|
30
|
+
// Stop IME composition.
|
31
|
+
composition.clear();
|
32
|
+
|
33
|
+
// Delete (overwrite) previous selection.
|
34
|
+
if (caret_pos != selection_start) {
|
35
|
+
unsigned min = std::min(caret_pos, selection_start);
|
36
|
+
unsigned max = std::max(caret_pos, selection_start);
|
37
|
+
text.erase(text.begin() + min, text.begin() + max);
|
38
|
+
caret_pos = selection_start = min;
|
39
|
+
}
|
40
|
+
|
41
|
+
text.insert(text.begin() + caret_pos, new_text.begin(), new_text.end());
|
42
|
+
caret_pos += new_text.size();
|
43
|
+
selection_start = caret_pos;
|
44
|
+
}
|
45
|
+
|
46
|
+
void move_left(bool modify_selection)
|
47
|
+
{
|
48
|
+
if (caret_pos > 0) {
|
49
|
+
caret_pos -= 1;
|
50
|
+
|
51
|
+
// Skip UTF-8 continuation bytes.
|
52
|
+
while (caret_pos > 0 && should_skip(text[caret_pos])) {
|
53
|
+
caret_pos -= 1;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
if (modify_selection) {
|
58
|
+
selection_start = caret_pos;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
void move_right(bool modify_selection)
|
63
|
+
{
|
64
|
+
if (caret_pos < text.length()) {
|
65
|
+
caret_pos += 1;
|
66
|
+
|
67
|
+
// Skip UTF-8 continuation bytes.
|
68
|
+
while (caret_pos < text.length() && should_skip(text[caret_pos])) {
|
69
|
+
caret_pos += 1;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
if (modify_selection) {
|
74
|
+
selection_start = caret_pos;
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
void move_word_left(bool modify_selection)
|
79
|
+
{
|
80
|
+
while (caret_pos > 0 && std::isspace(text[caret_pos - 1])) {
|
81
|
+
move_left(modify_selection);
|
82
|
+
}
|
83
|
+
while (caret_pos > 0 && !std::isspace(text[caret_pos - 1])) {
|
84
|
+
move_left(modify_selection);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
void move_word_right(bool modify_selection)
|
89
|
+
{
|
90
|
+
while (caret_pos < text.length() && std::isspace(text.at(caret_pos))) {
|
91
|
+
move_right(modify_selection);
|
92
|
+
}
|
93
|
+
while (caret_pos < text.length() && !std::isspace(text.at(caret_pos))) {
|
94
|
+
move_right(modify_selection);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
void move_to_beginning_of_line(bool modify_selection)
|
99
|
+
{
|
100
|
+
caret_pos = 0;
|
101
|
+
|
102
|
+
if (modify_selection) {
|
103
|
+
selection_start = caret_pos;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
void move_to_end_of_line(bool modify_selection)
|
108
|
+
{
|
109
|
+
caret_pos = static_cast<unsigned>(text.length());
|
110
|
+
|
111
|
+
if (modify_selection) {
|
112
|
+
selection_start = caret_pos;
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
void delete_backward()
|
117
|
+
{
|
118
|
+
if (selection_start != caret_pos) {
|
119
|
+
unsigned min = std::min(caret_pos, selection_start);
|
120
|
+
unsigned max = std::max(caret_pos, selection_start);
|
121
|
+
text.erase(text.begin() + min, text.begin() + max);
|
122
|
+
selection_start = caret_pos = min;
|
123
|
+
}
|
124
|
+
else if (caret_pos > 0) {
|
125
|
+
move_left(false);
|
126
|
+
text.erase(text.begin() + caret_pos, text.begin() + selection_start);
|
127
|
+
selection_start = caret_pos;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
|
131
|
+
void delete_forward()
|
132
|
+
{
|
133
|
+
if (selection_start != caret_pos) {
|
134
|
+
unsigned min = std::min(caret_pos, selection_start);
|
135
|
+
unsigned max = std::max(caret_pos, selection_start);
|
136
|
+
text.erase(text.begin() + min, text.begin() + max);
|
137
|
+
selection_start = caret_pos = min;
|
138
|
+
}
|
139
|
+
else if (caret_pos < text.length()) {
|
140
|
+
move_right(false);
|
141
|
+
text.erase(text.begin() + selection_start, text.begin() + caret_pos);
|
142
|
+
caret_pos = selection_start;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
};
|
146
|
+
|
147
|
+
Gosu::TextInput::TextInput()
|
148
|
+
: pimpl(new Impl)
|
149
|
+
{
|
150
|
+
}
|
151
|
+
|
152
|
+
Gosu::TextInput::~TextInput()
|
153
|
+
{
|
154
|
+
}
|
155
|
+
|
156
|
+
std::string Gosu::TextInput::text() const
|
157
|
+
{
|
158
|
+
std::string composed_text = pimpl->text;
|
159
|
+
if (!pimpl->composition.empty()) {
|
160
|
+
composed_text.insert(pimpl->caret_pos, pimpl->composition);
|
161
|
+
}
|
162
|
+
return composed_text;
|
163
|
+
}
|
164
|
+
|
165
|
+
void Gosu::TextInput::set_text(const std::string& text)
|
166
|
+
{
|
167
|
+
pimpl->text = text;
|
168
|
+
pimpl->composition.clear();
|
169
|
+
pimpl->caret_pos = pimpl->selection_start = static_cast<unsigned>(pimpl->text.length());
|
170
|
+
}
|
171
|
+
|
172
|
+
unsigned Gosu::TextInput::caret_pos() const
|
173
|
+
{
|
174
|
+
return static_cast<unsigned>(pimpl->caret_pos);
|
175
|
+
}
|
176
|
+
|
177
|
+
void Gosu::TextInput::set_caret_pos(unsigned pos)
|
178
|
+
{
|
179
|
+
pimpl->caret_pos = pos;
|
180
|
+
}
|
181
|
+
|
182
|
+
unsigned Gosu::TextInput::selection_start() const
|
183
|
+
{
|
184
|
+
return pimpl->selection_start;
|
185
|
+
}
|
186
|
+
|
187
|
+
void Gosu::TextInput::set_selection_start(unsigned pos)
|
188
|
+
{
|
189
|
+
pimpl->selection_start = pos;
|
190
|
+
}
|
191
|
+
|
192
|
+
bool Gosu::TextInput::feed_sdl_event(void* event)
|
193
|
+
{
|
194
|
+
const SDL_Event* e = static_cast<SDL_Event*>(event);
|
195
|
+
|
196
|
+
switch (e->type) {
|
197
|
+
// Direct text input or completed IME composition.
|
198
|
+
case SDL_TEXTINPUT: {
|
199
|
+
pimpl->insert_text(filter(e->text.text));
|
200
|
+
return true;
|
201
|
+
}
|
202
|
+
// IME composition in progress.
|
203
|
+
case SDL_TEXTEDITING: {
|
204
|
+
pimpl->composition = e->edit.text;
|
205
|
+
return true;
|
206
|
+
}
|
207
|
+
// Emulate "standard" Windows/Linux keyboard behavior.
|
208
|
+
case SDL_KEYDOWN: {
|
209
|
+
// ...but not if the IME is currently compositing.
|
210
|
+
if (!pimpl->composition.empty()) return false;
|
211
|
+
|
212
|
+
#ifdef GOSU_IS_MAC
|
213
|
+
bool words = (e->key.keysym.mod & (KMOD_LALT | KMOD_RALT));
|
214
|
+
bool command_down = (e->key.keysym.mod & (KMOD_LGUI | KMOD_RGUI));
|
215
|
+
#else
|
216
|
+
bool words = (e->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL));
|
217
|
+
#endif
|
218
|
+
bool shift_down = (e->key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT));
|
219
|
+
SDL_Keycode key = e->key.keysym.sym;
|
220
|
+
|
221
|
+
switch (key) {
|
222
|
+
case SDLK_LEFT:
|
223
|
+
#ifdef GOSU_IS_MAC
|
224
|
+
if (command_down) {
|
225
|
+
pimpl->move_to_beginning_of_line(!shift_down);
|
226
|
+
return true;
|
227
|
+
}
|
228
|
+
#endif
|
229
|
+
if (words) {
|
230
|
+
pimpl->move_word_left(!shift_down);
|
231
|
+
}
|
232
|
+
else {
|
233
|
+
pimpl->move_left(!shift_down);
|
234
|
+
}
|
235
|
+
return true;
|
236
|
+
case SDLK_RIGHT:
|
237
|
+
#ifdef GOSU_IS_MAC
|
238
|
+
if (command_down) {
|
239
|
+
pimpl->move_to_end_of_line(!shift_down);
|
240
|
+
return true;
|
241
|
+
}
|
242
|
+
#endif
|
243
|
+
if (words) {
|
244
|
+
pimpl->move_word_right(!shift_down);
|
245
|
+
}
|
246
|
+
else {
|
247
|
+
pimpl->move_right(!shift_down);
|
248
|
+
}
|
249
|
+
return true;
|
250
|
+
#ifdef GOSU_IS_MAC
|
251
|
+
case SDLK_UP:
|
252
|
+
#endif
|
253
|
+
case SDLK_HOME:
|
254
|
+
pimpl->move_to_beginning_of_line(!shift_down);
|
255
|
+
return true;
|
256
|
+
#ifdef GOSU_IS_MAC
|
257
|
+
case SDLK_DOWN:
|
258
|
+
#endif
|
259
|
+
case SDLK_END:
|
260
|
+
pimpl->move_to_end_of_line(!shift_down);
|
261
|
+
return true;
|
262
|
+
case SDLK_BACKSPACE:
|
263
|
+
pimpl->delete_backward();
|
264
|
+
return true;
|
265
|
+
case SDLK_DELETE:
|
266
|
+
pimpl->delete_forward();
|
267
|
+
return true;
|
268
|
+
}
|
269
|
+
break;
|
270
|
+
}
|
271
|
+
|
272
|
+
// TODO: Handle copy & paste.
|
273
|
+
}
|
274
|
+
|
275
|
+
return false;
|
276
|
+
}
|
277
|
+
|
278
|
+
#endif
|