Skip to content

Commit

Permalink
display: Added content drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Laštovička committed Apr 25, 2022
1 parent e04e030 commit 1892f9f
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "app.hpp"

namespace cz::lastaapps::vimxel {
int App::run(vector<string> args) {
display::Display().draw();
return 0;
}
}
24 changes: 24 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/app.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef APP_H
#define APP_H
#include <vector>
#include <string>
#include "display/display.hpp"

using namespace std;
namespace cz::lastaapps::vimxel {

class App final {
public:
int run(vector<string> args);
private:
// Storage engine
// NCurses Init
// Table
// Display
// Vim
};


} // namespace cz::lastaapps::vimxel

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "contentDrawer.hpp"

namespace cz::lastaapps::vimxel::display {

ContentDrawer::ContentDrawer(
WINDOW* w,
const size_t colW, const size_t rowH,
shared_ptr<CellContract> ptr)
: Drawer(w), mColWidth(colW), mRowHeight(rowH), mCntr(ptr) { init(); }

ContentDrawer::ContentDrawer(
WINDOW* w,
const size_t colW, const size_t rowH,
table::Coordinates pos, table::Coordinates viewPort, shared_ptr<CellContract> ptr)
: Drawer(w, pos, viewPort), mColWidth(colW), mRowHeight(rowH), mCntr(ptr) { init(); }

ContentDrawer::~ContentDrawer() {
mCntr->unregisterCallback();
}
void ContentDrawer::init() {
mCntr->registerCallback(
[this](const Coordinates& coord) { renderItem(coord); });
}
void ContentDrawer::draw() {
}
inline Coordinates ContentDrawer::getCellsCount() const {
return Coordinates(mWinSize.x() / mColWidth, mWinSize.y() / mRowHeight);
}
inline void ContentDrawer::renderItem(const Coordinates& coord) {
const Coordinates point = getRealPoint(coord);
const string content = mCntr -> getDataAt(coord);
const bool selected = isSelected(coord);
if (selected) wattron(mWin, A_STANDOUT);
mvwprintw(mWin, point.y(), point.x(), "%.*s", (int)mColWidth, content.c_str());
wattroff(mWin, A_STANDOUT);
}
inline bool ContentDrawer::isSelected(const Coordinates& coord) {
return mPos.x() == coord.x() && mPos.y() == coord.y();
}
Coordinates ContentDrawer::getRealPoint(const Coordinates& coord) {
const Coordinates diff = coord - mViewPort;
return Coordinates(diff.x() * mColWidth, diff.y() * mRowHeight);
}

} // namespace cz::lastaapps::vimxel::display
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef CONTENT_DRAWER_H
#define CONTENT_DRAWER_H
#include <ncurses.h>
#include <memory>

#include "../table/coordinate.hpp"
#include "../table/cellContract.hpp"
#include "drawer.hpp"
using namespace std;
using namespace cz::lastaapps::vimxel;
using namespace cz::lastaapps::vimxel::table;
namespace cz::lastaapps::vimxel::display {
class ContentDrawer final : public Drawer {
const size_t mColWidth, mRowHeight;
const shared_ptr<CellContract> mCntr;
Coordinates cellCount = getCellsCount();

public:
ContentDrawer(WINDOW * w, const size_t colW, const size_t rowH, shared_ptr<CellContract> cnt);
ContentDrawer(
WINDOW* w,
const size_t colW, const size_t rowH,
table::Coordinates pos, table::Coordinates viewPort,
shared_ptr<CellContract> cnt);
~ContentDrawer();
void draw() override;

private:
void init();
inline Coordinates getCellsCount() const;
inline void renderItem(const Coordinates &coord);
inline bool isSelected(const Coordinates &coord);
Coordinates getRealPoint(const Coordinates& coord);
};
} // namespace cz::lastaapps::vimxel::display
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void Display::drawSeparatingLines() {
mvhline(scrSize.y() - vimHeight - 1, 0, 0, scrSize.x());
mvvline(0, rowNamesWidth, 0, scrSize.y() - 1);
mvaddch(colNamesHeight, rowNamesWidth, ACS_PLUS);
mvaddch(scrSize.y() - vimHeight - 1, rowNamesWidth, ACS_PLUS);
mvaddch(scrSize.y() - vimHeight - 1, rowNamesWidth, ACS_BTEE);
move(0, 0);
}
void Display::refreshWindows() {
Expand Down
13 changes: 10 additions & 3 deletions Semestralni_prace/src/cz/lastaapps/vimxel/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#include "display/display.hpp"
#include "app.hpp"
#include <vector>
#include <string>
using namespace std;

int main(void) {
cz::lastaapps::vimxel::display::Display().draw();
int main(int cnt, char** args) {
vector<string> normal;
for (int i = 0; i < cnt; i++) {
normal.emplace_back(string(args[i]));
}
cz::lastaapps::vimxel::App().run(normal);
return 0;
}
14 changes: 14 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/table/cellContract.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "cellContract.hpp"


namespace cz::lastaapps::vimxel::table {
void CellContract::dataUpdatedAt(const Coordinates & coord) {
mCallback(coord);
}
void CellContract::registerCallback(Callback c) {
mCallback = c;
}
void CellContract::unregisterCallback() {
mCallback = Callback();
}
}
20 changes: 20 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/table/cellContract.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CELL_CONTRACT_H
#define CELL_CONTRACT_H
#include "coordinate.hpp"
#include <functional>
#include <list>

using namespace std;
namespace cz::lastaapps::vimxel::table {
class CellContract {
using Callback = function<void(const Coordinates&)>;
Callback mCallback;
public:
virtual ~CellContract() {};
virtual string getDataAt(const Coordinates & coord) const = 0;
void dataUpdatedAt(const Coordinates & coord);
void registerCallback(Callback);
void unregisterCallback();
};
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ bool Coordinates::operator<(const Coordinates& other) const {
return mY < other.mY;
}

Coordinates Coordinates::operator+(const Coordinates& other) const {
return Coordinates(mX + other.mX, mY + other.mY);
}
Coordinates Coordinates::operator-(const Coordinates& other) const {
return Coordinates(mX - other.mX, mY - other.mY);
}

Coordinates Coordinates::withX(const size_t val) const {
return Coordinates(val, mY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Coordinates final {
bool operator!=(const Coordinates& other) const;
bool operator<(const Coordinates& other) const;

Coordinates operator + (const Coordinates& other) const;
Coordinates operator - (const Coordinates& other) const;

size_t x() const;
size_t y() const;
const string xString() const;
Expand Down
26 changes: 26 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/table/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,35 @@ void Table::updateCell(const Coordinates& coord, const Cell& cell) {
else
// insert/update
mMap.insert_or_assign(coord, unique_ptr<const Cell>(&cell));

updateContracts(coord);
}
inline void Table::deleteCell(const Coordinates& coord) {
updateCell(coord, emptyCell);
}
shared_ptr<CellContract> Table::createCellContract() {
class TableCellContract final : public CellContract {
const Table& mT;
public:
TableCellContract(const Table & t) : mT(t) {}
string getDataAt(const Coordinates & coord) const override {
return mT.getCell(coord).getContent();
}
};
auto ptr = shared_ptr<CellContract>(dynamic_cast<CellContract*>(new TableCellContract(*this)));
mContracts.push_back(ptr);
return ptr;
}
void Table::updateContracts(const Coordinates& coord) {
for (size_t i = 0; i < mContracts.size(); i++) {
auto ptr = mContracts[i];
if (ptr.use_count() <= 1) {
mContracts.erase(mContracts.begin() + i);
i--;
} else {
ptr -> dataUpdatedAt(coord);
}
}
}

} // namespace cz::lastaapps::vimxel::table
6 changes: 6 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/table/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

#include "cell.hpp"
#include "coordinate.hpp"
#include "cellContract.hpp"
using namespace std;
namespace cz::lastaapps::vimxel::table {

class Table final {
private:
map<Coordinates, unique_ptr<const Cell>> mMap;
EmptyCell emptyCell;
vector<shared_ptr<CellContract>> mContracts;

public:
Table(const Table& other) = delete;
Expand All @@ -20,6 +22,10 @@ class Table final {
const Cell& getCell(const Coordinates& coord) const;
void updateCell(const Coordinates& coord, const Cell& cell);
void deleteCell(const Coordinates& coord);
shared_ptr<CellContract> createCellContract();

private:
void updateContracts(const Coordinates& coord);
};
} // namespace cz::lastaapps::vimxel::table
#endif

0 comments on commit 1892f9f

Please sign in to comment.