#304 GetCharacterIndex 4.x-dev

Sat, 17 Apr 2021 07:36:04 +0300

author
cemkalyoncu
date
Sat, 17 Apr 2021 07:36:04 +0300
branch
4.x-dev
changeset 1653
ee89dacca01d
parent 1652
33efc4141876
child 1654
b4f8b7602951

#304 GetCharacterIndex

Source/Gorgon/Graphics/AdvancedPrinter.h file | annotate | diff | comparison | revisions
Testing/Source/Manual/AdvancedText.cpp file | annotate | diff | comparison | revisions
--- a/Source/Gorgon/Graphics/AdvancedPrinter.h	Fri Apr 16 08:01:01 2021 +0300
+++ b/Source/Gorgon/Graphics/AdvancedPrinter.h	Sat Apr 17 07:36:04 2021 +0300
@@ -2359,8 +2359,6 @@
                     const Geometry::Point &location, const RGBAf &, int
                 ) {
                     if(g != 0xffff) {
-                        if(g == '!')
-                            g = '!';
                         auto p = location + (Geometry::Point)renderer.GetSize(g) + renderer.GetOffset(g);
                         p.Y += renderer.GetBaseLine();
                         
@@ -2378,15 +2376,93 @@
                 }, 
                 [](Byte , const Geometry::Bounds &, const RGBAf &, bool ) {
                 },
-                text, {0,0}, width, true
+                text, {0,0}, width, false
             );
             
             return sz;            
         }
         
-        virtual int GetCharacterIndex(const std::string &text, Geometry::Point location) const override { Utils::NotImplemented(); }
+        virtual int GetCharacterIndex(const std::string &text, Geometry::Point location) const override {
+            int maxdoney = -1;
+            int nearestind = -1;
+            bool done = false;
+            
+            AdvancedOperation(
+                [&](
+                    const GlyphRenderer &renderer, Glyph g, 
+                    const Geometry::Point &l, const RGBAf &, int index
+                ) {
+                    if(done)
+                        return false;
+                    
+                    if(l.Y >= location.Y) {
+                        done = true;
+                        return false;
+                    }
+                    
+                    if(maxdoney < l.Y) {
+                        if(l.X >= location.X+renderer.GetCursorAdvance(g)/2) {
+                            maxdoney = l.Y;
+                        }
+                        else {
+                            nearestind = index;
+                        }
+                    }
+                    
+                    return true;
+                },
+                [](const Geometry::Bounds &, const RGBAf &, int , RGBAf ) {
+                }, 
+                [](int , int , int , int , RGBAf ) {
+                }, 
+                [](Byte , const Geometry::Bounds &, const RGBAf &, bool ) {
+                },
+                text, {0,0}, 0, false
+            );
+            
+            return nearestind;
+        }
         
-        virtual int GetCharacterIndex(const std::string &text, int w, Geometry::Point location, bool wrap = true) const override { Utils::NotImplemented(); }
+        virtual int GetCharacterIndex(const std::string &text, int w, Geometry::Point location, bool wrap = true) const override { 
+            int maxdoney = -1;
+            int nearestind = -1;
+            bool done = false;
+            
+            AdvancedOperation(
+                [&](
+                    const GlyphRenderer &renderer, Glyph g, 
+                    const Geometry::Point &l, const RGBAf &, int index
+                ) {
+                    if(done)
+                        return false;
+                    
+                    if(l.Y >= location.Y) {
+                        done = true;
+                        return false;
+                    }
+                    
+                    if(maxdoney < l.Y) {
+                        if(l.X >= location.X+renderer.GetCursorAdvance(g)/2) {
+                            maxdoney = l.Y;
+                        }
+                        else {
+                            nearestind = index;
+                        }
+                    }
+                    
+                    return true;
+                },
+                [](const Geometry::Bounds &, const RGBAf &, int , RGBAf ) {
+                }, 
+                [](int , int , int , int , RGBAf ) {
+                }, 
+                [](Byte , const Geometry::Bounds &, const RGBAf &, bool ) {
+                },
+                text, {0,0}, w, wrap
+            );
+            
+            return nearestind;
+        }
         
         virtual Geometry::Rectangle GetPosition(const std::string& text, int index) const override { 
             Geometry::Rectangle cur = {std::numeric_limits<int>::min(), std::numeric_limits<int>::min(), 0, 0};
--- a/Testing/Source/Manual/AdvancedText.cpp	Fri Apr 16 08:01:01 2021 +0300
+++ b/Testing/Source/Manual/AdvancedText.cpp	Sat Apr 17 07:36:04 2021 +0300
@@ -21,7 +21,10 @@
     basic_Application<Gorgon::UI::Window> app("generictest", "Test", helptext, 25, 0x10);
 
     Graphics::Layer l;
+    Input::Layer mouse;
     app.wind.Add(l);
+    app.wind.Add(mouse);
+    mouse.Move(25, 25);
     
     using namespace Gorgon::Graphics;
     
@@ -167,6 +170,21 @@
     }
     */
     markings.Prepare();
+    
+    mouse.SetClick([&](Geometry::Point location) {
+        int c = printer.GetCharacterIndex(builder, 150, location);
+        std::cout << c << std::endl;
+        auto rect = printer.GetPosition(builder, 150, c);    
+        rect.X += 25;
+        rect.Y += 25;
+
+        Gorgon::CGI::DrawBounds(
+            markings, 
+            (Geometry::Bounds)rect,
+            1, Gorgon::CGI::SolidFill<>(regioncolor[0])
+        );
+        markings.Prepare();
+    });
 
     while(true) {
         Gorgon::NextFrame();

mercurial