From bd7dd2074228baf32e217ffe619aa53fe7f78f55 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Tue, 14 Aug 2012 18:05:07 +0100 Subject: [PATCH] Better text selection colour invert, fixes #107 --- src/graphics/OpenGLDrawMethods.inl | 13 +++++++++++++ src/graphics/RasterDrawMethods.inl | 14 ++++++++++++++ src/interface/Label.cpp | 31 ++++++++++++++++++++++++++---- src/interface/Label.h | 1 + 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/graphics/OpenGLDrawMethods.inl b/src/graphics/OpenGLDrawMethods.inl index 8392e289d..ac991657e 100644 --- a/src/graphics/OpenGLDrawMethods.inl +++ b/src/graphics/OpenGLDrawMethods.inl @@ -33,6 +33,13 @@ int PIXELMETHODS_CLASS::drawtext(int x, int y, const char *s, int r, int g, int g = oG; b = oB; } + else if (*s == '\x01') + { + invert = !invert; + r = 255-r; + g = 255-g; + b = 255-b; + } else if (*s == '\b') { if(!s[1]) break; @@ -67,6 +74,12 @@ int PIXELMETHODS_CLASS::drawtext(int x, int y, const char *s, int r, int g, int r = 32; break; } + if(invert) + { + r = 255-r; + g = 255-g; + b = 255-b; + } s++; } else diff --git a/src/graphics/RasterDrawMethods.inl b/src/graphics/RasterDrawMethods.inl index d1236a552..a68feb8c7 100644 --- a/src/graphics/RasterDrawMethods.inl +++ b/src/graphics/RasterDrawMethods.inl @@ -6,6 +6,7 @@ int PIXELMETHODS_CLASS::drawtext(int x, int y, const char *s, int r, int g, int return 0; int width, height; + int invert = 0; int oR = r, oG = g, oB = b; int characterX = x, characterY = y; int startX = characterX; @@ -33,6 +34,13 @@ int PIXELMETHODS_CLASS::drawtext(int x, int y, const char *s, int r, int g, int g = oG; b = oB; } + else if (*s == '\x01') + { + invert = !invert; + r = 255-r; + g = 255-g; + b = 255-b; + } else if (*s == '\b') { if(!s[1]) break; @@ -67,6 +75,12 @@ int PIXELMETHODS_CLASS::drawtext(int x, int y, const char *s, int r, int g, int r = 32; break; } + if(invert) + { + r = 255-r; + g = 255-g; + b = 255-b; + } s++; } else diff --git a/src/interface/Label.cpp b/src/interface/Label.cpp index 976c788b1..d4bd82e6f 100644 --- a/src/interface/Label.cpp +++ b/src/interface/Label.cpp @@ -250,26 +250,49 @@ void Label::updateSelection() selectionLineL = Graphics::PositionAtCharIndex((char*)currentText.c_str(), selectionIndex0, selectionXL, selectionYL); textFragments = std::string(currentText); - textFragments.insert(selectionIndex1, "\x0E"); - textFragments.insert(selectionIndex0, "\x0F\x01\x01\x01"); + //textFragments.insert(selectionIndex1, "\x0E"); + //textFragments.insert(selectionIndex0, "\x0F\x01\x01\x01"); + textFragments.insert(selectionIndex1, "\x01"); + textFragments.insert(selectionIndex0, "\x01"); } else if(selectionIndex0 > selectionIndex1) { selectionLineH = Graphics::PositionAtCharIndex((char*)currentText.c_str(), selectionIndex0, selectionXH, selectionYH); selectionLineL = Graphics::PositionAtCharIndex((char*)currentText.c_str(), selectionIndex1, selectionXL, selectionYL); textFragments = std::string(currentText); - textFragments.insert(selectionIndex0, "\x0E"); - textFragments.insert(selectionIndex1, "\x0F\x01\x01\x01"); + //textFragments.insert(selectionIndex0, "\x0E"); + //textFragments.insert(selectionIndex1, "\x0F\x01\x01\x01"); + textFragments.insert(selectionIndex0, "\x01"); + textFragments.insert(selectionIndex1, "\x01"); } else { selectionXH = -1; selectionXL = -1; textFragments = std::string(currentText); } + + if(displayText.length()) + { + displayText = tDisplayText; + if(selectionIndex1 > selectionIndex0) { + int tSelectionIndex1 = Graphics::CharIndexAtPosition((char*)displayText.c_str(), selectionXH, selectionYH); + int tSelectionIndex0 = Graphics::CharIndexAtPosition((char*)displayText.c_str(), selectionXL, selectionYL); + + displayText.insert(tSelectionIndex1, "\x01"); + displayText.insert(tSelectionIndex0, "\x01"); + } else if(selectionIndex0 > selectionIndex1) { + int tSelectionIndex0 = Graphics::CharIndexAtPosition((char*)displayText.c_str(), selectionXH, selectionYH); + int tSelectionIndex1 = Graphics::CharIndexAtPosition((char*)displayText.c_str(), selectionXL, selectionYL); + + displayText.insert(tSelectionIndex0, "\x01"); + displayText.insert(tSelectionIndex1, "\x01"); + } + } } void Label::SetDisplayText(std::string newText) { displayText = newText; + tDisplayText = displayText; } void Label::Draw(const Point& screenPos) diff --git a/src/interface/Label.h b/src/interface/Label.h index 77c28d66f..f701580a6 100644 --- a/src/interface/Label.h +++ b/src/interface/Label.h @@ -15,6 +15,7 @@ namespace ui std::string textFragments; std::string textLines; std::string displayText; + std::string tDisplayText; std::string text; Colour textColour;