From c871980b8f75736e359d2e991c5c34bb7220a96c Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Thu, 11 Jun 2015 10:48:50 -0400 Subject: [PATCH] Implement double-clicking to select words in dialog text fields --- src/dialogxml/field.cpp | 25 ++++++++++++++++++++++++- src/dialogxml/field.hpp | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/dialogxml/field.cpp b/src/dialogxml/field.cpp index fc98a84f..0b913602 100644 --- a/src/dialogxml/field.cpp +++ b/src/dialogxml/field.cpp @@ -118,15 +118,27 @@ void cTextField::set_ip(location clickLoc, int cTextField::* insertionPoint) { } bool cTextField::handleClick(location clickLoc) { - // TODO: Set the insertion point, handle selection, etc if(!haveFocus && parent && !parent->setFocus(this)) return true; haveFocus = true; redraw(); // This ensures the snippets array is populated. + std::string contents = getText(); + bool hadSelection = selectionPoint != insertionPoint; + bool is_double = click_timer.getElapsedTime().asMilliseconds() < 500; + click_timer.restart(); bool is_shift = sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::RShift); set_ip(clickLoc, is_shift ? &cTextField::selectionPoint : &cTextField::insertionPoint); if(!is_shift) selectionPoint = insertionPoint; + if(is_double && !is_shift && !hadSelection) { + cKey key = {true, key_word_right, mod_none}; + if(insertionPoint < contents.size() && contents[insertionPoint] != ' ') + handleInput(key); + key.k = key_word_left; + key.mod += mod_shift; + handleInput(key); + } bool done = false; sf::Event e; + int initial_ip = insertionPoint, initial_sp = selectionPoint; while(!done) { redraw(); if(!inWindow->pollEvent(e)) continue; @@ -136,6 +148,17 @@ bool cTextField::handleClick(location clickLoc) { restore_cursor(); location newLoc(e.mouseMove.x, e.mouseMove.y); set_ip(newLoc, &cTextField::selectionPoint); + if(is_double) { + if(selectionPoint > initial_ip) { + insertionPoint = initial_sp; + while(selectionPoint < contents.length() && contents[selectionPoint] != ' ') + selectionPoint++; + } else { + insertionPoint = initial_ip; + while(selectionPoint > 0 && contents[selectionPoint - 1] != ' ') + selectionPoint--; + } + } } } redraw(); diff --git a/src/dialogxml/field.hpp b/src/dialogxml/field.hpp index 314e2dac..bbf22ccd 100644 --- a/src/dialogxml/field.hpp +++ b/src/dialogxml/field.hpp @@ -79,7 +79,7 @@ private: int selectionPoint; sf::Color color; bool ip_visible; - sf::Clock ip_timer, hist_timer; + sf::Clock ip_timer, hist_timer, click_timer; bool changeMade = true; rectangle text_rect; std::vector snippets;