Cell.h:
#pragma once
#include "UltraEngine.h"
#include "Table.h"
using namespace UltraEngine;
class Cell : public TextField
{
protected:
Cell();
public:
static std::shared_ptr<Cell> create(const int x, const int y, const int width, const int height, shared_ptr<Table> table, bool isHeader = false);
void MouseEnter(const int x, const int y);
void MouseLeave(const int x, const int y);
Vec4 highlightColor = Vec4(0.3f, 0.3f, 0.3f, 1.0f);
Vec4 backgroundColor = Vec4(0.15f, 0.15f, 0.15f, 1.0f);
};
Cell.cpp:
#include "UltraEngine.h"
#include "Cell.h"
Cell::Cell()
{
}
std::shared_ptr<Cell> Cell::create(const int x, const int y, const int width, const int height, shared_ptr<Table> table, bool isHeader)
{
struct Struct : public Cell {
};
auto instance = std::make_shared<Struct>();
instance->As<Widget>()->Initialize("", x, y, width, height, table, TEXTFIELD_DEFAULT);
if (isHeader) {
instance->backgroundColor = Vec4(0.2f, 0.2f, 0.2f, 1.0f);
instance->highlightColor = Vec4(0.35f, 0.35f, 0.35f, 1.0f);
instance->SetColor(instance->backgroundColor, WIDGETCOLOR_SUNKEN);
}
return instance;
}
void Cell::MouseEnter(const int x, const int y)
{
TextField::MouseEnter(x, y);
SetColor(highlightColor, WIDGETCOLOR_SUNKEN);
}
void Cell::MouseLeave(const int x, const int y)
{
TextField::MouseEnter(x, y);
SetColor(backgroundColor, WIDGETCOLOR_SUNKEN);
}
Table.h
#pragma once
#include "UltraEngine.h"
class Cell;
using namespace UltraEngine;
class Table : public Panel
{
protected:
Table();
public:
int rowCount = 0;
int columnCount = 0;
vector<std::shared_ptr<Cell>> headers;
static std::shared_ptr<Table> create(const int x, const int y, const int width, const int height, int columnCount, int rowCount, shared_ptr<Widget> parent);
vector<vector<std::shared_ptr<Cell>>> cells;
};
Table.cpp
#include "UltraEngine.h"
#include "Table.h"
#include "Cell.h"
using namespace UltraEngine;
Table::Table()
{
}
std::shared_ptr<Table> Table::create(const int x, const int y, const int width, const int height, int columnCount, int rowCount, shared_ptr<Widget> parent)
{
struct Struct : public Table {
};
auto instance = std::make_shared<Struct>();
instance->Initialize("", x, y, width, height, parent, UltraEngine::PanelStyle::PANEL_DEFAULT);
instance->SetColor(0, 0, 0, 0);
instance->columnCount = columnCount;
instance->rowCount = rowCount;
int cellWidth = Floor (float(width) / float(columnCount));
int cellHeight = Floor(float(height) / float(rowCount + 1));
instance->cells.resize(columnCount);
instance->headers.resize(columnCount);
for (int i = 0; i < columnCount; i++) {
instance->cells[i].resize(rowCount);
instance->headers[i] = Cell::create(cellWidth * i, 0, cellWidth, cellHeight, instance, true);
instance->headers[i]->SetText(WString("Header ") + WString(i));
}
for (int i = 0; i < columnCount; i++) {
for (int j = 0; j < rowCount; j++) {
instance->cells[i][j] = (Cell::create(cellWidth * i, cellHeight * (j+1) , cellWidth, cellHeight, instance));
instance->cells[i][j]->SetText(WString("Cell ") + WString(i) + WString(',') + WString(j));
}
}
return instance;
}
main.cpp
#include "UltraEngine.h"
#include "UI/Table.h"
using namespace UltraEngine;
int main(int argc, const char* argv[])
{
auto displays = GetDisplays();
auto window = CreateWindow("Ultra Engine", 0, 0, 800, 600, displays[0], WINDOW_TITLEBAR | WINDOW_RESIZABLE | WINDOW_CENTER);
auto ui = CreateInterface(window);
Table::create(0, 0, 500, 500, 3, 7, ui->root);
while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
{
const Event ev = WaitEvent();
switch (ev.id)
{
case EVENT_QUIT:
case EVENT_WINDOWCLOSE:
return 0;
break;
default: break;
}
}
return 0;
}