Skip to content

Commit

Permalink
Added hover tooltips for tile tool, object tool and editor panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Marukyu committed Jun 17, 2016
1 parent 5ffc8a3 commit b40cacd
Show file tree
Hide file tree
Showing 18 changed files with 240 additions and 40 deletions.
5 changes: 5 additions & 0 deletions src/Client/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ Tool * Editor::getTool() const
return tool;
}

sf::Vector2i Editor::getMousePositionTile() const
{
return lastMousePosition;
}

void Editor::resetCamera()
{
float tileSize = TileAppearanceManager::TILE_SIZE;
Expand Down
2 changes: 2 additions & 0 deletions src/Client/Editor/Editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Editor : public gui2::Widget
void setTool(Tool * tool);
Tool * getTool() const;

sf::Vector2i getMousePositionTile() const;

void resetCamera();

bool isVertexRenderable() const override;
Expand Down
102 changes: 72 additions & 30 deletions src/Client/Editor/SelectionPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ gui2::Ptr<SelectionPanel> SelectionPanel::make()
}

SelectionPanel::SelectionPanel() :
mapper([](ID id)
{
static ItemEntry invalidEntry;
return invalidEntry;
}),
texture(nullptr),
wasSelectionChanged(false),
selectionExists(false),
selection(0),
itemCount(0),
itemSize(24.f, 24.f),
scrollVelocity(0)
mapper([](ID id)
{
static ItemEntry invalidEntry;
return invalidEntry;
}),
texture(nullptr),
wasSelectionChanged(false),
selectionExists(false),
selection(0),
hoveredItemExists(false),
hoveredItem(0),
itemCount(0),
itemSize(24.f, 24.f),
scrollVelocity(0)
{
}

Expand Down Expand Up @@ -71,6 +73,16 @@ bool SelectionPanel::hasSelection() const
return selectionExists;
}

SelectionPanel::ID SelectionPanel::getHoveredItem() const
{
return hoveredItem;
}

bool SelectionPanel::hasHoveredItem() const
{
return hoveredItemExists;
}

void SelectionPanel::setMapper(Mapper mapper, std::size_t itemCount)
{
this->mapper = mapper;
Expand Down Expand Up @@ -130,6 +142,16 @@ void SelectionPanel::update()
}
}

bool SelectionPanel::wasChanged()
{
if (wasSelectionChanged)
{
wasSelectionChanged = false;
return true;
}
return false;
}

bool SelectionPanel::isVertexRenderable() const
{
return false;
Expand All @@ -143,7 +165,7 @@ void SelectionPanel::onProcess(const gui2::WidgetEvents& events)
{
scrollVelocity -= events.mouseWheelDelta * 5;
}

if (std::abs(scrollVelocity) > 0.0001f)
{
float newSliderValue = slider->getValue() + scrollVelocity;
Expand All @@ -161,14 +183,26 @@ void SelectionPanel::onProcess(const gui2::WidgetEvents& events)
scrollVelocity *= 0.75f;
}
}

if (isMouseOver())
{
std::pair<bool, ID> itemAtMousePos = getItemAtPosition(events.mousePosition);

hoveredItemExists = itemAtMousePos.first;
hoveredItem = itemAtMousePos.second;
}
else
{
hoveredItemExists = false;
}
}

void SelectionPanel::onDraw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();

sf::Transform origTransform = states.transform;

states.transform.translate(0, -slider->getValue());
states.texture = this->texture;

Expand Down Expand Up @@ -222,17 +256,12 @@ void SelectionPanel::onMouseDown(sf::Vector2f pos, Input button)
{
if (button == sf::Mouse::Left)
{
for (ID i = 0; i < getItemCount(); ++i)
auto itemAtPos = getItemAtPosition(pos);

if (itemAtPos.first && (!hasSelection() || getSelection() != itemAtPos.second))
{
if (getShiftedItemRect(i).contains(pos))
{
if (!hasSelection() || getSelection() != i)
{
setSelection(i);
wasSelectionChanged = true;
}
break;
}
setSelection(itemAtPos.second);
wasSelectionChanged = true;
}
}
}
Expand All @@ -253,12 +282,25 @@ sf::FloatRect SelectionPanel::getShiftedItemRect(ID item) const
return moveRect(getItemRect(item), sf::Vector2f(0, -slider->getValue()));
}

bool SelectionPanel::wasChanged()
sf::FloatRect SelectionPanel::getClickableItemRect(ID item) const
{
if (wasSelectionChanged)
return expandRect(getShiftedItemRect(item), sf::Vector2f(MARGIN / 2, MARGIN / 2));
}

std::pair<bool, SelectionPanel::ID> SelectionPanel::getItemAtPosition(sf::Vector2f pos) const
{
// Optimization.
if (hasHoveredItem() && getClickableItemRect(getHoveredItem()).contains(pos))
{
wasSelectionChanged = false;
return true;
return std::make_pair(true, getHoveredItem());
}
return false;

for (ID i = 0; i < getItemCount(); ++i)
{
if (getClickableItemRect(i).contains(pos))
{
return std::make_pair(true, i);
}
}
return std::make_pair(false, 0);
}
20 changes: 14 additions & 6 deletions src/Client/Editor/SelectionPanel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <cstddef>
#include <functional>
#include <string>
#include <utility>
#include <vector>

namespace sf
Expand Down Expand Up @@ -51,6 +52,9 @@ class SelectionPanel : public gui2::Widget
ID getSelection() const;
bool hasSelection() const;

ID getHoveredItem() const;
bool hasHoveredItem() const;

void setMapper(Mapper mapper, std::size_t itemCount);
const Mapper & getMapper() const;
std::size_t getItemCount() const;
Expand All @@ -62,43 +66,47 @@ class SelectionPanel : public gui2::Widget
const sf::Texture * getTexture() const;

void update();

bool wasChanged();

SelectionPanel();

bool isVertexRenderable() const override;

protected:

virtual void init() override;

private:

void onProcess(const gui2::WidgetEvents & events) override;
void onDraw(sf::RenderTarget & target, sf::RenderStates states) const;
void onResize() override;

void onMouseDown(sf::Vector2f pos, Input button) override;

std::size_t getRowCount() const;
std::size_t getColumnCount() const;

sf::FloatRect getItemRect(ID item) const;
sf::FloatRect getShiftedItemRect(ID item) const;
sf::FloatRect getClickableItemRect(ID item) const;
std::pair<bool, ID> getItemAtPosition(sf::Vector2f pos) const;

Mapper mapper;

const sf::Texture * texture;

bool wasSelectionChanged;
bool selectionExists;
std::size_t selection;
ID selection;
bool hoveredItemExists;
ID hoveredItem;
std::size_t itemCount;
sf::Vector2f itemSize;

std::vector<sf::Vertex> vertices;

gui2::Ptr<gui2::Slider> slider;
float scrollVelocity;
};
Expand Down
5 changes: 5 additions & 0 deletions src/Client/Editor/Tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void Tool::onDrawPreview(sf::RenderTarget& target, sf::RenderStates states, sf::
{
}

std::string Tool::getTooltip() const
{
return "";
}

gui2::Ptr<gui2::Widget> Tool::getSettingsPanel() const
{
return nullptr;
Expand Down
8 changes: 7 additions & 1 deletion src/Client/Editor/Tool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <SFML/Graphics/RenderStates.hpp>
#include <SFML/System/Vector2.hpp>
#include <Shared/Utils/NamedFactory.hpp>
#include <string>

class Level;
class ObjectAppearanceManager;
Expand Down Expand Up @@ -89,6 +90,11 @@ class Tool : public NamedFactory<Tool>
*/
virtual void onDrawPreview(sf::RenderTarget & target, sf::RenderStates states, sf::Vector2i cursorPosition);

/**
* Called every frame to determine the current tooltip/status string to be displayed in the editor.
*/
virtual std::string getTooltip() const;

/**
* Returns the settings panel associated with this tool. This has to be a consistent and unique instance; the return
* value must not change between calls.
Expand All @@ -104,7 +110,7 @@ class Tool : public NamedFactory<Tool>
* Returns the editor information given to this tool.
*/
const EditorData & getEditorData() const;

/**
* Initializes the Named Factory of tools, if it is not initialized yet.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Client/Editor/Tools/ObjectTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ gui2::Ptr<gui2::Widget> ObjectTool::getSettingsPanel() const
return settingsPanel;
}

std::string ObjectTool::getTooltip() const
{
return settingsPanel->getTooltip();
}

const Brush& ObjectTool::getPrimaryBrush() const
{
primaryBrush.setObject(settingsPanel->getSelectedObject());
Expand Down
5 changes: 5 additions & 0 deletions src/Client/Editor/Tools/ObjectTool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class ObjectTool : public BrushTool

virtual gui2::Ptr<gui2::Widget> getSettingsPanel() const override;

/**
* Returns the tooltip for this object tool.
*/
virtual std::string getTooltip() const override;

protected:

virtual const Brush & getPrimaryBrush() const override;
Expand Down
14 changes: 14 additions & 0 deletions src/Client/Editor/Tools/Panels/ObjectToolPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ Brush::ObjectMode ObjectToolPanel::getSecondaryMode() const
return static_cast<Brush::ObjectMode>(secondaryModeMenu->getSelection());
}

std::string ObjectToolPanel::getTooltip() const
{
return hoveredObject.getType() != Object::Type::None ? objectAppearance->getObjectName(hoveredObject) : "";
}

void ObjectToolPanel::init()
{
gui2::Container::init();
Expand Down Expand Up @@ -116,6 +121,15 @@ void ObjectToolPanel::onProcessContainer(gui2::WidgetEvents& events)
propertyPanel->update();
}
}

if (selectionPanel->hasHoveredItem())
{
hoveredObject = Object(objects[selectionPanel->getHoveredItem()]);
}
else
{
hoveredObject.setType(Object::Type::None);
}
}

void ObjectToolPanel::addDefaultPropertiesToObject()
Expand Down
10 changes: 8 additions & 2 deletions src/Client/Editor/Tools/Panels/ObjectToolPanel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ObjectToolPanel : public gui2::Container
public:

static gui2::Ptr<ObjectToolPanel> make(std::vector<Object> objects,
const ObjectAppearanceManager & objectAppearance);
const ObjectAppearanceManager & objectAppearance);

ObjectToolPanel(std::vector<Object> objects, const ObjectAppearanceManager & objectAppearance);
virtual ~ObjectToolPanel();
Expand All @@ -38,8 +38,13 @@ class ObjectToolPanel : public gui2::Container
*/
Brush::ObjectMode getSecondaryMode() const;

/**
* Returns the name of the currently hovered tile.
*/
std::string getTooltip() const;

protected:

virtual void init() override;

private:
Expand All @@ -52,6 +57,7 @@ class ObjectToolPanel : public gui2::Container

std::vector<Object> objects;
Object selectedObject;
Object hoveredObject;

gui2::Ptr<gui2::Dropdown> primaryModeMenu;
gui2::Ptr<gui2::Dropdown> secondaryModeMenu;
Expand Down
Loading

0 comments on commit b40cacd

Please sign in to comment.