From 8d96a2e68a0cc929e8429ab0848f5a5a633d31bd Mon Sep 17 00:00:00 2001 From: Roman Sorokin Date: Fri, 25 Jul 2014 18:18:00 +0300 Subject: [PATCH] code style improvements --- drape_frontend/path_text_shape.cpp | 187 +++++++++++++++-------------- drape_frontend/path_text_shape.hpp | 92 ++++---------- 2 files changed, 119 insertions(+), 160 deletions(-) diff --git a/drape_frontend/path_text_shape.cpp b/drape_frontend/path_text_shape.cpp index ea6305ac31..8ae6d8dabd 100644 --- a/drape_frontend/path_text_shape.cpp +++ b/drape_frontend/path_text_shape.cpp @@ -23,26 +23,6 @@ using m2::PointF; namespace { static float const realFontSize = 20.0f; - float angelFromDir(float x, float y) - { - float const gip = sqrtf(x*x + y*y); - float const cosa = x / gip; - if(y > 0) - return acosf(cosa) * 180.0f / M_PI; - else - return 360.0f - acosf(cosa) * 180.0f / M_PI; - } - - struct ColorF - { - ColorF() {} - ColorF(float r, float g, float b, float a):m_r(r), m_g(g), m_b(b), m_a(a){} - - float m_r; - float m_g; - float m_b; - float m_a; - }; struct TexCoord { @@ -61,7 +41,7 @@ namespace struct Position { Position() {} - Position(float x, float y):m_x(x), m_y(y){} + Position(float x, float y) : m_x(x), m_y(y){} float m_x; float m_y; @@ -78,8 +58,8 @@ namespace }; } -PathTextShape::PathTextShape(vector const & path, PathTextViewParams const & params): - m_params(params) +PathTextShape::PathTextShape(vector const & path, PathTextViewParams const & params) + : m_params(params) { m_path.FromArray(path); } @@ -91,15 +71,11 @@ void PathTextShape::Draw(RefPointer batcher, RefPointer buffers; + vector buffers(1); float const needOutline = m_params.m_TextFont.m_needOutline; - Spline::iterator itr; - itr.Attach(m_path); - itr.Step(0.0f); - int textureSet; - for (int i = 0 ; i < cnt ; i++) + for (int i = 0; i < cnt; i++) { TextureSetHolder::GlyphRegion region; textures->GetGlyphRegion(text[i], region); @@ -112,16 +88,15 @@ void PathTextShape::Draw(RefPointer batcher, RefPointer batcher, RefPointer batcher, RefPointer batcher, RefPointer mutator) const { - float entireLength = m_params.m_OffsetStart * scaleFactor; + float entireLength = m_params.m_OffsetStart * m_scaleFactor; float const fontSize = m_params.m_TextFont.m_size; int const cnt = m_infos.size(); @@ -242,9 +194,9 @@ void PathTextHandle::GetAttributeMutation(RefPointer mut Spline::iterator itr; itr.Attach(m_path); - itr.Step(m_params.m_OffsetStart * scaleFactor); + itr.Step(m_params.m_OffsetStart * m_scaleFactor); - for (int i = 0 ; i < cnt ; i++) + for (int i = 0; i < cnt; i++) { float xOffset = m_infos[i].m_xOffset; float yOffset = m_infos[i].m_yOffset; @@ -258,22 +210,23 @@ void PathTextHandle::GetAttributeMutation(RefPointer mut halfWidth *= aspect; halfHeight *= aspect; - PointF const pos = itr.pos; - PointF const oldDir = itr.dir; + PointF const pos = itr.m_pos; + PointF const oldDir = itr.m_dir; - advance *= scaleFactor; + advance *= m_scaleFactor; + ASSERT_NOT_EQUAL(advance, 0.0, ()); entireLength += advance; - if(entireLength >= m_params.m_OffsetEnd * scaleFactor) + if(entireLength >= m_params.m_OffsetEnd * m_scaleFactor) return; - int const index1 = itr.index; + int const index1 = itr.m_index; itr.Step(advance); - int const index2 = itr.index; + int const index2 = itr.m_index; PointF dir; if (index1 != index2 && index2) { - PointF const newDir = itr.dir; - PointF const newPos = m_path.position[index2]; - float const smoothFactor = (newPos - itr.pos).Length() / advance; + PointF const newDir = itr.m_dir; + PointF const newPos = m_path.m_position[index2]; + float const smoothFactor = (newPos - itr.m_pos).Length() / advance; dir = oldDir * (1.0f - smoothFactor) + newDir * smoothFactor; } else @@ -281,35 +234,35 @@ void PathTextHandle::GetAttributeMutation(RefPointer mut dir = oldDir; } - float const angle = angelFromDir(dir.x, dir.y) * M_PI / 180.0f; + float const angle = atan2(dir.y, dir.x); float const cosa = cosf(angle); float const sina = sinf(angle); yOffset += halfHeight; - xOffset -= halfWidth; - float const x1old = halfWidth - xOffset; + xOffset += halfWidth; + float const x1old = halfWidth + xOffset; float const y1old = -halfHeight + yOffset; - float const x2old = -halfWidth - xOffset; + float const x2old = -halfWidth + xOffset; float const y2old = -halfHeight + yOffset; - float const x3old = -halfWidth - xOffset; + float const x3old = -halfWidth + xOffset; float const y3old = halfHeight + yOffset; - float const x4old = halfWidth - xOffset; + float const x4old = halfWidth + xOffset; float const y4old = halfHeight + yOffset; - float x1 = (x1old * cosa - y1old * sina) * scaleFactor + pos.x; - float y1 = (x1old * sina + y1old * cosa) * scaleFactor + pos.y; + float x1 = (x1old * cosa - y1old * sina) * m_scaleFactor + pos.x; + float y1 = (x1old * sina + y1old * cosa) * m_scaleFactor + pos.y; - float x2 = (x2old * cosa - y2old * sina) * scaleFactor + pos.x; - float y2 = (x2old * sina + y2old * cosa) * scaleFactor + pos.y; + float x2 = (x2old * cosa - y2old * sina) * m_scaleFactor + pos.x; + float y2 = (x2old * sina + y2old * cosa) * m_scaleFactor + pos.y; - float x3 = (x3old * cosa - y3old * sina) * scaleFactor + pos.x; - float y3 = (x3old * sina + y3old * cosa) * scaleFactor + pos.y; + float x3 = (x3old * cosa - y3old * sina) * m_scaleFactor + pos.x; + float y3 = (x3old * sina + y3old * cosa) * m_scaleFactor + pos.y; - float x4 = (x4old * cosa - y4old * sina) * scaleFactor + pos.x; - float y4 = (x4old * sina + y4old * cosa) * scaleFactor + pos.y; + float x4 = (x4old * cosa - y4old * sina) * m_scaleFactor + pos.x; + float y4 = (x4old * sina + y4old * cosa) * m_scaleFactor + pos.y; int index = i * 6; positions[index++] = Position(x2, y2); @@ -328,4 +281,54 @@ void PathTextHandle::GetAttributeMutation(RefPointer mut mutator->AddMutation(node.first, mutateNode); } +void Spline::FromArray(vector const & path) +{ + m_position = vector(path.begin(), path.end() - 1); + int cnt = m_position.size(); + m_direction = vector(cnt); + m_length = vector(cnt); + + for(int i = 0; i < cnt; ++i) + { + m_direction[i] = path[i+1] - path[i]; + m_length[i] = m_direction[i].Length(); + m_direction[i] = m_direction[i].Normalize(); + m_lengthAll += m_length[i]; + } +} + +Spline const & Spline::operator = (Spline const & spl) +{ + if(&spl != this) + { + m_lengthAll = spl.m_lengthAll; + m_position = spl.m_position; + m_direction = spl.m_direction; + m_length = spl.m_length; + } + return *this; +} + +void Spline::iterator::Attach(Spline const & S) +{ + m_spl = &S; + m_index = 0; + m_dist = 0; + m_dir = m_spl->m_direction[m_index]; + m_pos = m_spl->m_position[m_index] + m_dir * m_dist; +} + +void Spline::iterator::Step(float speed) +{ + m_dist += speed; + while(m_dist > m_spl->m_length[m_index]) + { + m_dist -= m_spl->m_length[m_index]; + m_index++; + m_index %= m_spl->m_position.size(); + } + m_dir = m_spl->m_direction[m_index]; + m_pos = m_spl->m_position[m_index] + m_dir * m_dist; +} + } diff --git a/drape_frontend/path_text_shape.hpp b/drape_frontend/path_text_shape.hpp index 267128b738..7bf5b0562b 100644 --- a/drape_frontend/path_text_shape.hpp +++ b/drape_frontend/path_text_shape.hpp @@ -16,8 +16,8 @@ using m2::PointF; struct LetterInfo { - LetterInfo(float xOff, float yOff, float adv, float hw, float hh): - m_xOffset(xOff), m_yOffset(yOff), m_advance(adv), + LetterInfo(float xOff, float yOff, float adv, float hw, float hh) + : m_xOffset(xOff), m_yOffset(yOff), m_advance(adv), m_halfWidth(hw), m_halfHeight(hh){} LetterInfo(){} @@ -32,72 +32,30 @@ struct LetterInfo class Spline { public: - float length_all; - vector position; - vector direction; - vector length; + Spline(){} + void FromArray(vector const & path); + Spline const & operator = (Spline const & spl); public: - Spline(){} - - void FromArray(vector const & path) - { - position = vector(path.begin(), path.end() - 1); - int cnt = position.size(); - direction = vector(cnt); - length = vector(cnt); - - for(int i = 0; i < cnt; ++i) - { - direction[i] = path[i+1] - path[i]; - length[i] = direction[i].Length(); - direction[i] = direction[i].Normalize(); - length_all += length[i]; - } - } - - Spline const & operator = (Spline const & spl) - { - if(&spl != this) - { - length_all = spl.length_all; - position = spl.position; - direction = spl.direction; - length = spl.length; - } - return *this; - } + float m_lengthAll; + vector m_position; + vector m_direction; + vector m_length; class iterator { - private: - Spline const * spl; - float dist; public: - int index; - PointF pos; - PointF dir; - iterator(){} - void Attach(Spline const & S) - { - spl = &S; - index = 0; - dist = 0; - dir = spl->direction[index]; - pos = spl->position[index] + dir * dist; - } - void Step(float speed) - { - dist += speed; - while(dist > spl->length[index]) - { - dist -= spl->length[index]; - index++; - index %= spl->position.size(); - } - dir = spl->direction[index]; - pos = spl->position[index] + dir * dist; - } + int m_index; + PointF m_pos; + PointF m_dir; + iterator() + : m_spl(NULL), m_index(0), m_dist(0), + m_pos(PointF()), m_dir(PointF()) {} + void Attach(Spline const & S); + void Step(float speed); + private: + Spline const * m_spl; + float m_dist; }; }; @@ -118,17 +76,15 @@ public: static const uint8_t DirectionAttributeID = 1; PathTextHandle(Spline const & spl, PathTextViewParams const & params, vector const & info) : OverlayHandle(FeatureID(), dp::Center, 0.0f), - m_params(params), - m_path(spl), - m_infos(info) + m_params(params), m_path(spl), m_infos(info) { SetIsVisible(true); - scaleFactor = 1.0f; + m_scaleFactor = 1.0f; } virtual void Update(ScreenBase const & screen) { - scaleFactor = screen.GetScale(); + m_scaleFactor = screen.GetScale(); } virtual m2::RectD GetPixelRect(ScreenBase const & screen) const { return m2::RectD(); } @@ -138,7 +94,7 @@ private: PathTextViewParams m_params; Spline m_path; vector m_infos; - float scaleFactor; + float m_scaleFactor; }; } // namespace df