From 338ed9dd49d0a99e1ea0f77bbb9c6b6169d12b60 Mon Sep 17 00:00:00 2001
From: David Capello <davidcapello@gmail.com>
Date: Mon, 13 Nov 2017 21:08:15 -0300
Subject: [PATCH] Fix SelectBoxState when tiled mode is enabled

---
 src/app/ui/editor/select_box_state.cpp | 47 +++++++++++++++++++-------
 1 file changed, 35 insertions(+), 12 deletions(-)

diff --git a/src/app/ui/editor/select_box_state.cpp b/src/app/ui/editor/select_box_state.cpp
index 3e16ded46..2f8e9d75c 100644
--- a/src/app/ui/editor/select_box_state.cpp
+++ b/src/app/ui/editor/select_box_state.cpp
@@ -84,7 +84,9 @@ void SelectBoxState::onBeforePopState(Editor* editor)
 bool SelectBoxState::onMouseDown(Editor* editor, MouseMessage* msg)
 {
   if (msg->left() || msg->right()) {
-    m_startingPos = editor->screenToEditor(msg->position());
+    m_startingPos =
+      editor->screenToEditor(msg->position())
+      - editor->mainTilePosition();
 
     if (hasFlag(Flags::Rulers)) {
       m_rulersDragAlign = hitTestRulers(editor, msg->position(), true);
@@ -130,7 +132,10 @@ bool SelectBoxState::onMouseMove(Editor* editor, MouseMessage* msg)
   updateContextBar();
 
   if (hasFlag(Flags::Rulers) && !m_movingRulers.empty()) {
-    gfx::Point newPos = editor->screenToEditor(msg->position());
+    gfx::Point newPos =
+      editor->screenToEditor(msg->position())
+      - editor->mainTilePosition();
+
     gfx::Point delta = newPos - m_startingPos;
 
     for (int i : m_movingRulers) {
@@ -147,7 +152,9 @@ bool SelectBoxState::onMouseMove(Editor* editor, MouseMessage* msg)
 
   if (hasFlag(Flags::QuickBox) && m_selectingBox) {
     gfx::Point p1 = m_startingPos;
-    gfx::Point p2 = editor->screenToEditor(msg->position());
+    gfx::Point p2 =
+      editor->screenToEditor(msg->position())
+      - editor->mainTilePosition();
 
     if (p2.x < p1.x) std::swap(p1.x, p2.x);
     if (p2.y < p1.y) std::swap(p1.y, p2.y);
@@ -257,7 +264,10 @@ void SelectBoxState::postRenderDecorator(EditorPostRender* render)
   // Paint a grid generated by the box
   gfx::Color rulerColor = skin::SkinTheme::instance()->colors.selectBoxRuler();
   gfx::Color gridColor = skin::SkinTheme::instance()->colors.selectBoxGrid();
+  gfx::Point mainOffset = editor->mainTilePosition();
   gfx::Rect boxBounds = getBoxBounds();
+  boxBounds.offset(mainOffset);
+  sp.offset(mainOffset);
 
   if (hasFlag(Flags::Grid)) {
     if (boxBounds.w > 0) {
@@ -288,13 +298,17 @@ void SelectBoxState::postRenderDecorator(EditorPostRender* render)
     for (Rulers::iterator it = m_rulers.begin(), end = m_rulers.end(); it != end; ++it) {
       switch (it->orientation()) {
 
-        case Ruler::Horizontal:
-          render->drawLine(vp.x, it->position(), vp.x+vp.w-1, it->position(), rulerColor);
+        case Ruler::Horizontal: {
+          const int y = it->position()+mainOffset.y;
+          render->drawLine(vp.x, y, vp.x+vp.w-1, y, rulerColor);
           break;
+        }
 
-        case Ruler::Vertical:
-          render->drawLine(it->position(), vp.y, it->position(), vp.y+vp.h-1, rulerColor);
+        case Ruler::Vertical: {
+          const int x = it->position()+mainOffset.x;
+          render->drawLine(x, vp.y, x, vp.y+vp.h-1, rulerColor);
           break;
+        }
       }
     }
   }
@@ -339,7 +353,9 @@ int SelectBoxState::hitTestRulers(Editor* editor,
 
   // Check moving all rulers at the same time
   if (align == 0 && !hasFlag(Flags::QuickBox)) {
-    if (editor->editorToScreen(getBoxBounds()).contains(mousePos)) {
+    if (editor->editorToScreen(
+          getBoxBounds().offset(editor->mainTilePosition()))
+        .contains(mousePos)) {
       align = LEFT | TOP | RIGHT | BOTTOM;
       if (updateMovingRulers) {
         // Add all rulers
@@ -355,12 +371,19 @@ int SelectBoxState::hitTestRulers(Editor* editor,
 bool SelectBoxState::hitTestRuler(Editor* editor, const Ruler& ruler,
                                   const gfx::Point& mousePos)
 {
-  gfx::Point pt = editor->editorToScreen(
-    gfx::Point(ruler.position(), ruler.position()));
+  gfx::Point pt = editor->mainTilePosition();
+  pt = editor->editorToScreen(
+    pt + gfx::Point(ruler.position(), ruler.position()));
 
   switch (ruler.orientation()) {
-    case Ruler::Horizontal: return (mousePos.y >= pt.y-2*guiscale() && mousePos.y <= pt.y+2*guiscale());
-    case Ruler::Vertical:   return (mousePos.x >= pt.x-2*guiscale() && mousePos.x <= pt.x+2*guiscale());
+
+    case Ruler::Horizontal:
+      return (mousePos.y >= pt.y-2*guiscale() &&
+              mousePos.y <= pt.y+2*guiscale());
+
+    case Ruler::Vertical:
+      return (mousePos.x >= pt.x-2*guiscale() &&
+              mousePos.x <= pt.x+2*guiscale());
   }
 
   return false;