diff --git a/CMakeLists.txt b/CMakeLists.txt index f690441..f68c2e4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,10 +22,7 @@ project(vis set(CMAKE_EXPORT_COMPILE_COMMANDS 1) -add_executable(vis - src/main.cpp - src/calendar.cpp - ) +add_subdirectory(src) find_library(NCURSES_LIB NAMES ncurses) target_link_libraries(vis ncurses) diff --git a/RESOURCES.md b/RESOURCES.md index 2c664f5..bc7e650 100644 --- a/RESOURCES.md +++ b/RESOURCES.md @@ -2,6 +2,6 @@ > **Note:** VIS mainly uses the NCURSES library, therefore, expect the majority of resources to be some sort of documentation for NCURSES. -- [INVISIBLE_ISLAND - Ncurses Docs](https://invisible-island.net/ncurses/man/ncurses.3x.html) + - [INVISIBLE_ISLAND - Ncurses Docs](https://invisible-island.net/ncurses/man/ncurses.3x.html) - [TLDP - NCURSES Programming HOWTO](https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) - [JBWYATT - Ncurses Programming Guide](https://jbwyatt.com/ncurses.html) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e77f1ee --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,7 @@ +add_subdirectory(engine) +add_subdirectory(calendar) + +add_executable(vis main.cpp) + +target_link_libraries(vis engine) +target_link_libraries(vis calendar) diff --git a/src/calendar/CMakeLists.txt b/src/calendar/CMakeLists.txt new file mode 100644 index 0000000..19b6b07 --- /dev/null +++ b/src/calendar/CMakeLists.txt @@ -0,0 +1 @@ +add_library(calendar SHARED calendar.cpp) diff --git a/src/calendar.cpp b/src/calendar/calendar.cpp similarity index 92% rename from src/calendar.cpp rename to src/calendar/calendar.cpp index 56c779f..c462cc7 100644 --- a/src/calendar.cpp +++ b/src/calendar/calendar.cpp @@ -41,11 +41,15 @@ void Calendar::set_year(int year) { this->info.current_day = this->info.current_month_days; } +calendar_information Calendar::get_info() { + return this->info; +} + Calendar::Calendar(calendar_information *preinfo) { - this->info = *preinfo; - if (preinfo != nullptr) { + this->info = *preinfo; + + if (preinfo != nullptr) return; - } this->info = calendar_information(); } diff --git a/src/calendar.hpp b/src/calendar/calendar.hpp similarity index 88% rename from src/calendar.hpp rename to src/calendar/calendar.hpp index 8b9c7da..9049e22 100644 --- a/src/calendar.hpp +++ b/src/calendar/calendar.hpp @@ -1,3 +1,6 @@ +#ifndef CALENDAR_HPP +#define CALENDAR_HPP + struct calendar_information { int current_day; int current_month; @@ -16,6 +19,8 @@ class Calendar { calendar_information get_info(); Calendar(calendar_information *preinfo = nullptr); - private: + + private: calendar_information info; }; +#endif diff --git a/src/engine/CMakeLists.txt b/src/engine/CMakeLists.txt new file mode 100644 index 0000000..cde64e3 --- /dev/null +++ b/src/engine/CMakeLists.txt @@ -0,0 +1 @@ +add_library(engine SHARED engine.cpp) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp new file mode 100644 index 0000000..ea94d55 --- /dev/null +++ b/src/engine/engine.cpp @@ -0,0 +1,42 @@ +#include +#include +#include "engine.hpp" + +Engine::Engine(int padding, calendar_view_mode view_mode) { + this->padding = padding; + this->view_mode = view_mode; + this->cells_table = std::vector(); + + calendar_information date_info = calendar_information(); + this->calendar = new Calendar(&date_info); +} + +Engine::~Engine() { + delete this->calendar; +} + +void Engine::draw(WINDOW *win) { + this->cells_table.clear(); + + box(win, 0, 0); + + calendar_information date_info = this->calendar->get_info(); + + + switch (this->view_mode) { + case MONTH_VIEW: + for (int i = 1; i <= date_info.current_month_days; i++) + mvwprintw(win, i, 3, "D%d", i); + break; + case MONTHS_VIEW: + for (int i = 1; i <= 12; i++) + wprintw(win, "M%d", i); + break; + } + + wprintw(win, "\n"); + + mvwprintw(win, LINES-1, 0, "Year: %d, Month: %d, Day: %d", date_info.current_year, date_info.current_month, date_info.current_day); + + wrefresh(win); +} diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp new file mode 100644 index 0000000..c16f0ee --- /dev/null +++ b/src/engine/engine.hpp @@ -0,0 +1,37 @@ +#ifndef ENGINE_HPP +#define ENGINE_HPP + +#include +#include +#include "../calendar/calendar.hpp" + +struct engine_child { + int x; + int y; +}; + +enum calendar_view_mode { + MONTH_VIEW = 0, + MONTHS_VIEW, +}; + +// title: drawing engine +// description: use for drawing the TUI and interact with the cells +class Engine { + public: + Engine(int padding = 10, calendar_view_mode view_mode = MONTH_VIEW); + ~Engine(); + + void draw(WINDOW *win); + + Calendar *calendar; + + calendar_view_mode view_mode; + int padding; // padding + + private: + std::vector cells_table; + +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index c3baf49..e482c6f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,23 +1,72 @@ #include #include +#include #include -#include -#include "calendar.hpp" +#include "calendar/calendar.hpp" +#include "engine/engine.hpp" -#define MIN_WIDTH 100 -#define MIN_HEIGHT 40 +Engine engine = Engine(10, MONTH_VIEW); +WINDOW *main_win; -using namespace std; +void sig_winch(int sig) { + if (isendwin()) return; + endwin(); + wclear(main_win); + wrefresh(main_win); + + wresize(main_win, LINES, COLS); + + // redraw the TUI after the resize signal + engine.draw(main_win); + + flushinp(); +} + +void signals() { + signal(SIGWINCH, sig_winch); +} int main() { - initscr(); + initscr(); + refresh(); + noecho(); - int active_cell = 0; + main_win = newwin(LINES, COLS, 0, 0); + + // handle required signals + signals(); for (;;) { + wclear(main_win); + wrefresh(main_win); + engine.draw(main_win); + + char ch = getch(); + int month; + switch (ch) { + case 'k': + month = engine.calendar->get_info().current_month; + if (month >= 12) { + month = 12; + break; + } - refresh(); + engine.calendar->set_month(++month); + break; + case 'j': + month = engine.calendar->get_info().current_month; + if (month <= 1) { + month = 1; + break; + } + + engine.calendar->set_month(--month); + break; + } } + + endwin(); + return 0; }