当前位置: 首页>>代码示例>>C++>>正文


C++ InputState::getMousePosition方法代码示例

本文整理汇总了C++中InputState::getMousePosition方法的典型用法代码示例。如果您正苦于以下问题:C++ InputState::getMousePosition方法的具体用法?C++ InputState::getMousePosition怎么用?C++ InputState::getMousePosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在InputState的用法示例。


在下文中一共展示了InputState::getMousePosition方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Actionable

void
ButtonController::update(EntityManager &em, Entity &entity,
                         const InputState &input, int now) {
  if (!entity.hasComponent(Transform::TYPE)) {
    LOG2 << "Missing Transform component for ButtonController.\n";
    return;
  }
  const Transform *trans = entity.getComponent<Transform>();

  const BVisual *visu = NULL;
  const Collider *collider = NULL;
  if (entity.hasComponent(Collider::TYPE)) {
    collider = entity.getComponent<Collider>();
  }
  if (entity.hasComponent(BVisual::TYPE)) {
    visu = entity.getComponent<BVisual>();
  }
  if ((collider == NULL) && (visu == NULL)) {
    LOG2 << "Missing Collider or BVisual component for ButtonController.\n";
    return;
  }

  Actionable *actionable = NULL;
  if (!entity.hasComponent(Actionable::TYPE)) {
    actionable = new Actionable();
    entity.addComponent(actionable);
  } else {
    actionable = entity.getComponent<Actionable>();
  }
  
  float minX = trans->getX();
  float minY = trans->getY();
  if ((visu != NULL) && (!visu->isGUI())) {
    const Entity *cameraEntity = em.getFirst(Camera::TYPE);
    if (cameraEntity) {
      if (cameraEntity->hasComponent(Transform::TYPE)) {
        const Transform *camtrans = cameraEntity->getComponent<Transform>();
        const Camera *camera = cameraEntity->getComponent<Camera>();
        minX -= (camtrans->getX() +
                 camera->getOffsetX() - camera->getWidth() / 2);
        minY -= (camtrans->getY() +
                 camera->getOffsetY() - camera->getHeight() / 2);
      } else {
        LOG2 << "Camera entity has no Transform\n";
      }
    }
  }

  if (collider != NULL) {
    minX -= collider->getLeft();
    minY -= collider->getTop();
  } else {
    minX -= visu->getCenterX();
    minY -= visu->getCenterY();
  }

  float maxX = minX;
  float maxY = minY;
  if (collider != NULL) {
    maxX += collider->getLeft() + collider->getRight();
    maxY += collider->getTop() + collider->getBottom();
  } else {
    VisualContext &vc = input.getVisualContext();
    maxX += visu->getWidth(vc);
    maxY += visu->getHeight(vc);
  }
  
  InputState::MousePos mpos = input.getMousePosition();
  const string &prevAction = actionable->getAction();
  const string *action = NULL;
  if ((mpos.x >= minX) && (mpos.x <= maxX) &&
      (mpos.y >= minY) && (mpos.y <= maxY)) {
    // Mouse is over button
    if (input.isButtonDown(InputState::LeftButton)) {
      // and left mouse button is down
      if ((prevAction == ButtonJustDown) || (prevAction == ButtonDown)) {
        action = &ButtonDown;
      } else {
        action = &ButtonJustDown;
      }

    } else if ((prevAction == ButtonJustDown) || (prevAction == ButtonDown)) {
      // mouse button is released over the entity but was previouly down:
      // it's a click
      action = &ButtonClicked;
    }
  }
  
  if (action != NULL) {
    actionable->setAction(*action);
  } else {
    actionable->clearAction();
  }
  
}
开发者ID:slowfrog,项目名称:chickenpix,代码行数:95,代码来源:ButtonController.cpp


注:本文中的InputState::getMousePosition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。