cocos2d-x Label Alignment

ラベルを左揃えにしたいのだが、setHorizontalAlignmentが成功しなかったので、 アンカーポイントでどうにかした。

Cocos2d-x 3.0rc2 - Label alignment not working ? - C++ - Cocos2d-x Forums

f:id:hayateasdf:20171227152333p:plain

util.h

#pragma once

#include "cocos2d.h"

USING_NS_CC;

class util {
public:
  static Vec2 LEFT_TOP() {
    return util::onOrigin(Vec2(0, util::size().height));
  }
  static Vec2 LEFT_BOTTOM() {
    return util::onOrigin(Vec2::ZERO);
  }
  static Vec2 RIGHT_TOP() {
    return util::onOrigin(Vec2(util::size().width, util::size().height));
  }
  static Vec2 RIGHT_BOTTOM() {
    return util::onOrigin(Vec2(util::size().width, 0));
  }
  static Size size() {
    return Director::getInstance()->getVisibleSize();
  }
  static Vec2 origin() {
    return Director::getInstance()->getVisibleOrigin();
  }
  static Vec2 onOrigin(const Vec2& position) {
    return Vec2(position.x + util::origin().x, position.y + util::origin().y);
  }

  enum LabelV {
    Top,
    Bottom,
    Center,
  };
  enum LabelH {
    Left,
    Right,
    Mid
  };

  static Label* label(const std::string& text, float fontSize, const Vec2& position, LabelV v = LabelV::Center, LabelH h = LabelH::Mid) {
    auto label = Label::createWithTTF(text, "fonts/Marker Felt.ttf", fontSize);
    label->setPosition(position);

    auto vertical = [v]() {
      switch (v) {
        case LabelV::Bottom: return 0.0f;
        case LabelV::Top: return 1.0f;
      }
      return 0.5f;
    }

    auto horizon = [h]() {
      switch (h) {
        case LabelH::Left: return 0.0f;
        case LabelH::Right: return 1.0f;
      }
      return 0.5f;
    }

    label->setAnchorPoint(Vec2(horizon(), vertical()));

    return label;
  }
}

HelloWorld.cpp

#include "util.h"

USING_NS_CC;

bool HelloWorld::init() {
  if (!Scene::init()) { return false; }

  auto label = util::label(
    "LEFT TOP",
    24,
    util::LEFT_TOP(),
    util::LabelV::Top,
    util::LabelH::Left);
  addChild(label);

  auto label2 = util::label(
    "LEFT BOTTOM",
    24,
    util::LEFT_BOTTOM(),
    util::LabelV::Bottom,
    util::LabelH::Left);
  addChild(label2);

  auto label3 = util::label(
    "RIGHT TOP",
    24,
    util::RIGHT_TOP(),
    util::LabelV::Top,
    util::LabelH::Right);
  addChild(label3);

  auto label4 = util::label(
    "RIGHT BOTTOM",
    24,
    util::RIGHT_BOTTOM(),
    util::LabelV::Bottom,
    util::LabelH::Right);
  addChild(label4);

  return true;
}




※ onOriginがないとずれる。

f:id:hayateasdf:20171227154347p:plain