Fixed CSV Parsing on tools
This commit is contained in:
@ -12,4 +12,5 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
UISprite.cpp
|
||||
UIEmpty.cpp
|
||||
UIGrid.cpp
|
||||
UIMenu.cpp
|
||||
)
|
@ -91,9 +91,7 @@ void UIComponent::updatePositions() {
|
||||
}
|
||||
|
||||
// Fire event
|
||||
eventAlignmentUpdated.invoke(
|
||||
this->width, this->height, this->relativeX, this->relativeY
|
||||
);
|
||||
eventAlignmentUpdated.invoke(this);
|
||||
}
|
||||
|
||||
float_t UIComponent::getWidth() {
|
||||
|
@ -38,7 +38,7 @@ namespace Dawn {
|
||||
UIComponent *parent = nullptr;
|
||||
|
||||
// Events
|
||||
Event<float_t, float_t, float_t, float_t> eventAlignmentUpdated;
|
||||
Event<UIComponent*> eventAlignmentUpdated;
|
||||
|
||||
// I currently don't support rotation or scale. Not because I can't but
|
||||
// because it's basically un-necessary. Unity does support rotation but
|
||||
|
@ -36,7 +36,18 @@ std::vector<struct ShaderPassItem> UIGrid::getSelfPassItems(
|
||||
return std::vector<struct ShaderPassItem>();
|
||||
}
|
||||
|
||||
void UIGrid::onChildAligned(UIComponent *child) {
|
||||
assertNotNull(child);
|
||||
assertMapHasKey(this->gridChildren, child);
|
||||
this->alignChild(child, this->gridChildren[child]);
|
||||
}
|
||||
|
||||
void UIGrid::alignChild(UIComponent *child, struct UIGridPosition pos) {
|
||||
assertNotNull(child);
|
||||
|
||||
// Remove event listener
|
||||
child->eventAlignmentUpdated.addListener(this, &UIGrid::onChildAligned);
|
||||
|
||||
float_t gridX = (this->sizeCol * pos.x) + (this->gutterX * pos.x);
|
||||
float_t gridY = (this->sizeRow * pos.y) + (this->gutterY * pos.y);
|
||||
|
||||
@ -68,6 +79,9 @@ void UIGrid::alignChild(UIComponent *child, struct UIGridPosition pos) {
|
||||
glm::vec4(gridX + x, gridY + y, sizeX, sizeY),
|
||||
0.0f
|
||||
);
|
||||
|
||||
// Re-Add event listener
|
||||
child->eventAlignmentUpdated.addListener(this, &UIGrid::onChildAligned);
|
||||
}
|
||||
|
||||
void UIGrid::setGridSize(
|
||||
@ -79,6 +93,8 @@ void UIGrid::setGridSize(
|
||||
this->gutterX = gutterX;
|
||||
this->gutterY = gutterY;
|
||||
|
||||
// TODO: Need to fix children here.
|
||||
|
||||
this->gridChildren.clear();
|
||||
this->updatePositions();
|
||||
}
|
||||
|
@ -33,6 +33,8 @@ namespace Dawn {
|
||||
*/
|
||||
void alignChild(UIComponent *child, struct UIGridPosition pos);
|
||||
|
||||
void onChildAligned(UIComponent *child);
|
||||
|
||||
protected:
|
||||
void updatePositions() override;
|
||||
std::vector<struct ShaderPassItem> getSelfPassItems(
|
||||
|
96
src/dawn/ui/UIMenu.cpp
Normal file
96
src/dawn/ui/UIMenu.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "UIMenu.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
UIMenu::UIMenu(UICanvas *canvas, int32_t columns, int32_t rows) {
|
||||
assertNotNull(canvas);
|
||||
assertTrue(columns > 0);
|
||||
assertTrue(rows > 0);
|
||||
|
||||
this->canvas = canvas;
|
||||
this->rows = rows;
|
||||
this->columns = columns;
|
||||
this->items = (UIMenuItem**)memoryFillWithZero(sizeof(UIMenuItem*)*columns*rows);
|
||||
}
|
||||
|
||||
void UIMenu::setSize(int32_t cols, int32_t rows) {
|
||||
assertNotNull(this->items);
|
||||
assertTrue(columns > 0);
|
||||
assertTrue(rows > 0);
|
||||
assertTrue(columns != this->columns);
|
||||
assertTrue(rows != this->rows);
|
||||
|
||||
memoryFree(this->items);
|
||||
this->items = (UIMenuItem**)memoryFillWithZero(sizeof(UIMenuItem*)*columns*rows);
|
||||
}
|
||||
|
||||
UIMenuItem * UIMenu::getItem(int32_t x, int32_t y) {
|
||||
return this->items[x * this->columns + y];
|
||||
}
|
||||
|
||||
void UIMenu::setPosition(int32_t x, int32_t y) {
|
||||
assertTrue(x >= 0 && x < this->columns);
|
||||
assertTrue(y >= 0 && y < this->rows);
|
||||
UIMenuItem *item;
|
||||
|
||||
if(this->x != -1) {
|
||||
assertTrue(this->y != -1);
|
||||
item = this->getItem(this->x, this->y);
|
||||
if(item != nullptr) {
|
||||
item->onItemOff();
|
||||
}
|
||||
}
|
||||
|
||||
this->eventCursorChange.invoke(this->x, this->y, x, y);
|
||||
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
item = this->getItem(x, y);
|
||||
if(item != nullptr && item->canBeOvered()) {
|
||||
item->onItemOver();
|
||||
}
|
||||
}
|
||||
|
||||
void UIMenu::moveRelative(int32_t x, int32_t y) {
|
||||
int32_t x2 = this->x + x;
|
||||
if(x2 < 0 || x2 >= this->columns) return;
|
||||
int32_t y2 = this->y + y;
|
||||
if(y2 < 0 || y2 >= this->rows) return;
|
||||
this->setPosition(x2, y2);
|
||||
}
|
||||
|
||||
void UIMenu::setItem(int32_t x, int32_t y, UIMenuItem *item) {
|
||||
assertTrue(x >= 0);
|
||||
assertTrue(y >= 0);
|
||||
assertTrue(x < this->columns);
|
||||
assertTrue(y < this->rows);
|
||||
assertNotNull(item);
|
||||
assertNotNull(this->items);
|
||||
assertNull(this->getItem(x, y));
|
||||
|
||||
this->items[x * this->columns + y] = item;
|
||||
}
|
||||
|
||||
void UIMenu::onInactive() {
|
||||
this->eventMenuInactive.invoke();
|
||||
}
|
||||
|
||||
void UIMenu::onActive() {
|
||||
this->eventMenuActive.invoke();
|
||||
}
|
||||
|
||||
void UIMenu::onTick() {
|
||||
std::cout << "Tick menu" << std::endl;
|
||||
}
|
||||
|
||||
UIMenu::~UIMenu() {
|
||||
if(this->canvas->getCurrentMenu() == this) {
|
||||
this->canvas->setCurrentMenu(nullptr);
|
||||
}
|
||||
memoryFree(this->items);
|
||||
}
|
125
src/dawn/ui/UIMenu.hpp
Normal file
125
src/dawn/ui/UIMenu.hpp
Normal file
@ -0,0 +1,125 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "event/Event.hpp"
|
||||
#include "ui/UIComponent.hpp"
|
||||
#include "util/memory.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class UIMenuItem {
|
||||
public:
|
||||
/**
|
||||
* Called when the item is selected (Accepted) on.
|
||||
*/
|
||||
virtual void onItemSelected() = 0;
|
||||
|
||||
/**
|
||||
* Called when either the mouse or the user controller input is removed
|
||||
* off this item.
|
||||
*/
|
||||
virtual void onItemOver() = 0;
|
||||
|
||||
/**
|
||||
* Called when either the mouth or the user controller input is put over
|
||||
* this item.
|
||||
*/
|
||||
virtual void onItemOff() = 0;
|
||||
|
||||
/**
|
||||
* Returns whether the UI item can be hovered overed or not.
|
||||
* @return Whether it can be overed or not.
|
||||
*/
|
||||
virtual bool_t canBeOvered() = 0;
|
||||
|
||||
/**
|
||||
* Returns whether or not the item can be selected or not.
|
||||
* @return Whether it can be selected or not.
|
||||
*/
|
||||
virtual bool_t canBeSelected() = 0;
|
||||
};
|
||||
|
||||
struct UIMenu {
|
||||
private:
|
||||
UICanvas *canvas;
|
||||
int32_t x = -1;
|
||||
int32_t y = -1;
|
||||
int32_t rows = 1;
|
||||
int32_t columns = 1;
|
||||
UIMenuItem **items = nullptr;
|
||||
|
||||
protected:
|
||||
/** Invoked by UICanvas when this menu has been made inactive. */
|
||||
void onInactive();
|
||||
/** Invoked by UICanvas when this menu has been made active. */
|
||||
void onActive();
|
||||
/** Invoked by UICanvas every tick this menu is active. */
|
||||
void onTick();
|
||||
|
||||
public:
|
||||
Event<> eventMenuActive;
|
||||
Event<> eventMenuInactive;
|
||||
Event<int32_t, int32_t, int32_t, int32_t> eventCursorChange;
|
||||
Event<UIMenuItem*> eventItemSelected;
|
||||
|
||||
/**
|
||||
* Construct a new UI Menu Host.
|
||||
*
|
||||
* @param canvas Canvas that this menu belongs to.
|
||||
* @param columns Iniitial size of the menu X axis.
|
||||
* @param rows Initial size of the menu Y axis.
|
||||
*/
|
||||
UIMenu(UICanvas *canvas, int32_t columns, int32_t rows);
|
||||
|
||||
/**
|
||||
* Sets the size of the UI Menu.
|
||||
*
|
||||
* @param columns How many columns in the menu.
|
||||
* @param rows How many rows in the menu.
|
||||
*/
|
||||
void setSize(int32_t columns, int32_t rows);
|
||||
|
||||
/**
|
||||
* Returns the UI Item at the given position.
|
||||
*
|
||||
* @param x X coordinate of the item to get.
|
||||
* @param y Y coordinate of the item to get.
|
||||
* @return The pointer to the menu item, or null if invalid.
|
||||
*/
|
||||
UIMenuItem * getItem(int32_t x, int32_t y);
|
||||
|
||||
/**
|
||||
* Sets the position of the cursor in the grid.
|
||||
*
|
||||
* @param x X position of the cursor.
|
||||
* @param y Y position of the cursor.
|
||||
*/
|
||||
void setPosition(int32_t x, int32_t y);
|
||||
|
||||
/**
|
||||
* Move the cursor relative to the current position.
|
||||
*
|
||||
* @param x X position to move relative.
|
||||
* @param y Y position to move relative.
|
||||
*/
|
||||
void moveRelative(int32_t x, int32_t y);
|
||||
|
||||
/**
|
||||
* Adds/Sets an item onto the menu.
|
||||
*
|
||||
* @param x X coordinate to set the item.
|
||||
* @param y Y coordinate to set the item.
|
||||
* @param item Item to set.
|
||||
*/
|
||||
void setItem(int32_t x, int32_t y, UIMenuItem *item);
|
||||
|
||||
/**
|
||||
* Cleans up the menu items, doesn't free the children themselves.
|
||||
*/
|
||||
~UIMenu();
|
||||
|
||||
friend class UICanvas;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user