/* 88888888 888888888888 88888888888888 8888888888888888 888888888888888888 888888 8888 888888 88888 88 88888 888888 8888 888888 88888888888888888888 88888888888888888888 8888888888888888888888 8888888888888888888888888888 88888888888888888888888888888888 88888888888888888888 888888888888888888888888 888888 8888888888 888888 888 8888 8888 888 888 888 OCTOBANANA Licensed under the MIT License Copyright (c) 2020 Brett Robinson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef WINDOW_HH #define WINDOW_HH #include "ob/text.hh" #include "ob/term.hh" #include "ob/prism.hh" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace aec = OB::Term::ANSI_Escape_Codes; template struct Vec2n; template Vec2n operator*(Vec2n lhs, Vec2n const& rhs); template Vec2n operator/(Vec2n lhs, Vec2n const& rhs); template Vec2n operator+(Vec2n lhs, Vec2n const& rhs); template Vec2n operator-(Vec2n lhs, Vec2n const& rhs); template std::ostream& operator<<(std::ostream& os, Vec2n const& obj); template bool operator<(Vec2n const& rhs, Vec2n const& lhs); template bool operator<=(Vec2n const& rhs, Vec2n const& lhs); template bool operator>(Vec2n const& rhs, Vec2n const& lhs); template bool operator>=(Vec2n const& rhs, Vec2n const& lhs); template bool operator==(Vec2n const& rhs, Vec2n const& lhs); template bool operator!=(Vec2n const& rhs, Vec2n const& lhs); template struct Vec2n { T x {0}; T y {0}; Vec2n(T const x, T const y) : x {x}, y {y} {} Vec2n() = default; Vec2n(Vec2n&&) = default; Vec2n(Vec2n const&) = default; template Vec2n(Vec2n&& obj) { x = static_cast(obj.x); y = static_cast(obj.y); } template Vec2n(Vec2n const& obj) { x = static_cast(obj.x); y = static_cast(obj.y); } ~Vec2n() = default; Vec2n& operator=(Vec2n&&) = default; Vec2n& operator=(Vec2n const&) = default; Vec2n& operator*=(Vec2n const& obj); Vec2n& operator/=(Vec2n const& obj); Vec2n& operator+=(Vec2n const& obj); Vec2n& operator-=(Vec2n const& obj); friend Vec2n operator* <>(Vec2n lhs, Vec2n const& rhs); friend Vec2n operator/ <>(Vec2n lhs, Vec2n const& rhs); friend Vec2n operator+ <>(Vec2n lhs, Vec2n const& rhs); friend Vec2n operator- <>(Vec2n lhs, Vec2n const& rhs); friend std::ostream& operator<< <>(std::ostream& os, Vec2n const& obj); friend bool operator< <>(Vec2n const& lhs, Vec2n const& rhs); friend bool operator<= <>(Vec2n const& lhs, Vec2n const& rhs); friend bool operator> <>(Vec2n const& lhs, Vec2n const& rhs); friend bool operator>= <>(Vec2n const& lhs, Vec2n const& rhs); friend bool operator== <>(Vec2n const& lhs, Vec2n const& rhs); friend bool operator!= <>(Vec2n const& lhs, Vec2n const& rhs); }; template std::ostream& operator<<(std::ostream& os, Vec2n const& obj) { os << obj.x << ":" << obj.y; return os; } template Vec2n& Vec2n::operator*=(Vec2n const& obj) { x *= obj.x; y *= obj.y; return *this; } template Vec2n operator*(Vec2n lhs, Vec2n const& rhs) { return lhs *= rhs; } template Vec2n& Vec2n::operator/=(Vec2n const& obj) { x /= obj.x; y /= obj.y; return *this; } template Vec2n operator/(Vec2n lhs, Vec2n const& rhs) { return lhs /= rhs; } template Vec2n& Vec2n::operator+=(Vec2n const& obj) { x += obj.x; y += obj.y; return *this; } template Vec2n operator+(Vec2n lhs, Vec2n const& rhs) { return lhs += rhs; } template Vec2n& Vec2n::operator-=(Vec2n const& obj) { x -= obj.x; y -= obj.y; return *this; } template Vec2n operator-(Vec2n lhs, Vec2n const& rhs) { return lhs -= rhs; } template bool operator<(Vec2n const& lhs, Vec2n const& rhs) { return (lhs.x < rhs.x) && (lhs.y < rhs.y); } template bool operator<=(Vec2n const& lhs, Vec2n const& rhs) { return (lhs.x <= rhs.x) && (lhs.y <= rhs.y); } template bool operator>=(Vec2n const& lhs, Vec2n const& rhs) { return (lhs.x >= rhs.x) && (lhs.y >= rhs.y); } template bool operator>(Vec2n const& lhs, Vec2n const& rhs) { return (lhs.x > rhs.x) && (lhs.y > rhs.y); } template bool operator==(Vec2n const& lhs, Vec2n const& rhs) { return (lhs.x == rhs.x) && (lhs.y == rhs.y); } template bool operator!=(Vec2n const& lhs, Vec2n const& rhs) { return !(lhs == rhs); } template std::optional slope(Vec2n const& p1, Vec2n const& p2) { return p1.x == p2.x ? std::optional() : (p2.y - p1.y) / (p2.x - p1.x); } template T y_intercept(Vec2n const& p, T const& m) { return p.y - p.x * m; } template std::function line(Vec2n const& p1, Vec2n const& p2) { if (auto m = slope(p1, p2); m) { auto b = y_intercept(p1, m.value()); return [m = m.value(), b](T const& x) -> T { return m * x + b; }; } return [](T const& x) -> T { return x; }; } template bool contains(Vec2n const& point, Vec2n const& min, Vec2n const& max) { return (point.x >= min.x && point.x <= max.x) && (point.y >= min.y && point.y <= max.y); } template bool intersect(Vec2n const& amin, Vec2n const& amax, Vec2n const& bmin, Vec2n const& bmax) { return (amin.x <= bmax.x && amax.x >= bmin.x) && (amin.y <= bmax.y && amax.y >= bmin.y); } template Vec2n trunc(Vec2n obj) { obj.x = std::trunc(obj.x); obj.y = std::trunc(obj.y); return obj; } template Vec2n floor(Vec2n obj) { obj.x = std::floor(obj.x); obj.y = std::floor(obj.y); return obj; } template Vec2n ceil(Vec2n obj) { obj.x = std::ceil(obj.x); obj.y = std::ceil(obj.y); return obj; } template Vec2n abs(Vec2n obj) { obj.x = std::abs(obj.x); obj.y = std::abs(obj.y); return obj; } template Vec2n round(Vec2n obj) { obj.x = std::round(obj.x); obj.y = std::round(obj.y); return obj; } using Pos = Vec2n; using Size = Vec2n; using Vec2f = Vec2n; struct Style { friend std::ostream& operator<<(std::ostream& os, Style const& obj); enum Type : std::uint8_t { Clear = 0, Default, Bit_2, Bit_4, Bit_8, Bit_24, Bit_32, }; enum Attr : std::uint8_t { Null = 0, Bold = 1 << 0, Reverse = 1 << 1, Underline = 1 << 2, }; std::uint8_t type {Clear}; std::uint8_t attr {Null}; OB::Prism::RGBA fg; OB::Prism::RGBA bg; }; inline std::ostream& operator<<(std::ostream& os, Style const& obj) { os << static_cast(obj.attr) << " " << static_cast(obj.type) << " " << obj.fg << " " << obj.bg; return os; } struct Cell { int zidx; Style style; std::string text {" "}; }; class Buffer { public: Buffer(Size const size, Cell const& cell = {}); Buffer() = default; Buffer(Buffer&&) = default; Buffer(Buffer const&) = default; ~Buffer() = default; Buffer& operator=(Buffer&&) = default; Buffer& operator=(Buffer const&) = default; void operator()(Pos const pos, Cell const& cell); void operator()(Cell const& cell); void put(Pos const pos, Cell const& cell); void put(Cell const& cell); Cell& at(Pos const pos); Cell const& at(Pos const pos) const; std::vector& row(std::size_t const y); std::vector const& row(std::size_t const y) const; Cell& col(Pos const pos); Cell const& col(Pos const pos) const; Pos cursor() const; void cursor(Pos const pos); Size size() const; void size(Size const size, Cell const& cell = {}); bool empty() const; void clear(); private: Pos _pos; Size _size; std::vector> _value; }; // class Buffer class Window { public: // private: void winch(); void refresh(); void render(); void write(std::string& str); void render_file(std::ofstream& file); void write_file(std::ofstream& file, std::string& str); void term_fg(std::string& str, OB::Prism::RGBA rgba); void term_bg(std::string& str, OB::Prism::RGBA rgba); Size size; std::size_t frames {0}; std::size_t bsize {0}; Style style_base; Style style; Buffer buf; Buffer buf_prev; Buffer buf_1; Buffer buf_2; std::string line; bool clear {true}; }; #endif // WINDOW_HH