|
| 1 | +/* |
| 2 | +* Covariant Script Darwin Extension |
| 3 | +* |
| 4 | +* This program is free software: you can redistribute it and/or modify |
| 5 | +* it under the terms of the GNU Affero General Public License as published |
| 6 | +* by the Free Software Foundation, either version 3 of the License, or |
| 7 | +* (at your option) any later version. |
| 8 | +* |
| 9 | +* This program is distributed in the hope that it will be useful, |
| 10 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +* GNU Affero General Public License for more details. |
| 13 | +* |
| 14 | +* You should have received a copy of the GNU Affero General Public License |
| 15 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +* |
| 17 | +* Copyright (C) 2017 Michael Lee(李登淳) |
| 18 | + |
| 19 | +* Github: https://github.com/mikecovlee |
| 20 | +*/ |
| 21 | +#ifndef CS_DEBUG |
| 22 | +#define DARWIN_DISABLE_LOG |
| 23 | +#endif |
| 24 | +#define DARWIN_FORCE_BUILTIN |
| 25 | + |
| 26 | +#include <darwin/darwin.hpp> |
| 27 | +#include <covscript/extension.hpp> |
| 28 | +#include <covscript/cni.hpp> |
| 29 | + |
| 30 | +static cs::extension darwin_ext; |
| 31 | +static cs::extension darwin_ui_ext; |
| 32 | +static cs::extension darwin_drawable_ext; |
| 33 | +static cs::extension_t darwin_ui_ext_shared = cs::make_shared_extension(darwin_ui_ext); |
| 34 | +static cs::extension_t darwin_drawable_ext_shared = cs::make_shared_extension(darwin_drawable_ext); |
| 35 | +namespace cs_impl { |
| 36 | + template<> |
| 37 | + cs::extension_t &get_ext<std::shared_ptr<darwin::drawable>>() |
| 38 | + { |
| 39 | + return darwin_drawable_ext_shared; |
| 40 | + } |
| 41 | + |
| 42 | + template<> |
| 43 | + constexpr const char *get_name_of_type<darwin::colors>() |
| 44 | + { |
| 45 | + return "cs::darwin::colors"; |
| 46 | + } |
| 47 | + |
| 48 | + template<> |
| 49 | + constexpr const char *get_name_of_type<darwin::pixel>() |
| 50 | + { |
| 51 | + return "cs::darwin::pixel"; |
| 52 | + } |
| 53 | + |
| 54 | + template<> |
| 55 | + constexpr const char *get_name_of_type<std::shared_ptr<darwin::drawable>>() |
| 56 | + { |
| 57 | + return "cs::darwin::drawable"; |
| 58 | + } |
| 59 | +} |
| 60 | +namespace darwin_cs_ext { |
| 61 | + using namespace cs; |
| 62 | + using drawable_t=std::shared_ptr<darwin::drawable>; |
| 63 | + darwin::sync_clock clock(30); |
| 64 | + |
| 65 | +// Type Constructor |
| 66 | + var pixel(char ch, darwin::colors fc, darwin::colors bc) |
| 67 | + { |
| 68 | + return var::make_protect<darwin::pixel>(ch, true, false, fc, bc); |
| 69 | + } |
| 70 | + |
| 71 | + var picture(number w, number h) |
| 72 | + { |
| 73 | + return var::make_protect<drawable_t>(new darwin::picture(w, h)); |
| 74 | + } |
| 75 | + |
| 76 | +// Darwin Main Function |
| 77 | + void load() |
| 78 | + { |
| 79 | + darwin::runtime.load("./darwin.module"); |
| 80 | + } |
| 81 | + |
| 82 | + void exit() |
| 83 | + { |
| 84 | + darwin::runtime.exit(); |
| 85 | + } |
| 86 | + |
| 87 | + bool is_kb_hit() |
| 88 | + { |
| 89 | + return darwin::runtime.is_kb_hit(); |
| 90 | + } |
| 91 | + |
| 92 | + char get_kb_hit() |
| 93 | + { |
| 94 | + return darwin::runtime.get_kb_hit(); |
| 95 | + } |
| 96 | + |
| 97 | + void fit_drawable() |
| 98 | + { |
| 99 | + darwin::runtime.fit_drawable(); |
| 100 | + } |
| 101 | + |
| 102 | + var get_drawable() |
| 103 | + { |
| 104 | + return var::make<drawable_t>(darwin::runtime.get_drawable(), [](darwin::drawable *) {}); |
| 105 | + } |
| 106 | + |
| 107 | + void update_drawable() |
| 108 | + { |
| 109 | + darwin::runtime.update_drawable(); |
| 110 | + clock.sync(); |
| 111 | + clock.reset(); |
| 112 | + } |
| 113 | + |
| 114 | + void set_frame_limit(number freq) |
| 115 | + { |
| 116 | + clock.set_freq(freq); |
| 117 | + } |
| 118 | + |
| 119 | + void set_draw_line_precision(number prec) |
| 120 | + { |
| 121 | + darwin::drawable::draw_line_precision = prec; |
| 122 | + } |
| 123 | + |
| 124 | +// Darwin UI Function |
| 125 | + void message_box(const string &title, const string &message, const string &button) |
| 126 | + { |
| 127 | + std::size_t width = std::max(title.size(), std::max(message.size(), button.size())) + 4; |
| 128 | + darwin::sync_clock c(10); |
| 129 | + while (true) { |
| 130 | + c.reset(); |
| 131 | + if (darwin::runtime.is_kb_hit()) { |
| 132 | + darwin::runtime.get_kb_hit(); |
| 133 | + break; |
| 134 | + } |
| 135 | + darwin::runtime.fit_drawable(); |
| 136 | + std::size_t x(0.5 * (darwin::runtime.get_drawable()->get_width() - width)), y( |
| 137 | + 0.5 * (darwin::runtime.get_drawable()->get_height() - 6)); |
| 138 | + darwin::runtime.get_drawable()->clear(); |
| 139 | + darwin::runtime.get_drawable()->fill_rect(x, y, width, 6, |
| 140 | + darwin::pixel(' ', true, false, darwin::colors::white, |
| 141 | + darwin::colors::white)); |
| 142 | + darwin::runtime.get_drawable()->fill_rect(x, y, width, 1, |
| 143 | + darwin::pixel(' ', true, false, darwin::colors::white, |
| 144 | + darwin::colors::cyan)); |
| 145 | + darwin::runtime.get_drawable()->fill_rect(x + 0.5 * (width - button.size() - 2), y + 4, button.size() + 2, |
| 146 | + 1, darwin::pixel(' ', true, false, darwin::colors::white, |
| 147 | + darwin::colors::cyan)); |
| 148 | + darwin::runtime.get_drawable()->draw_string(x + 0.5 * (width - title.size()), y, title, |
| 149 | + darwin::pixel(' ', true, false, darwin::colors::black, |
| 150 | + darwin::colors::cyan)); |
| 151 | + darwin::runtime.get_drawable()->draw_string(x + 0.5 * (width - message.size()), y + 2, message, |
| 152 | + darwin::pixel(' ', true, false, darwin::colors::black, |
| 153 | + darwin::colors::white)); |
| 154 | + darwin::runtime.get_drawable()->draw_string(x + 0.5 * (width - button.size()), y + 4, button, |
| 155 | + darwin::pixel(' ', true, false, darwin::colors::white, |
| 156 | + darwin::colors::cyan)); |
| 157 | + darwin::runtime.update_drawable(); |
| 158 | + c.sync(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + var input_box(const string &title, const string &message, string buff, bool format) |
| 163 | + { |
| 164 | + darwin::sync_clock c(10); |
| 165 | + while (true) { |
| 166 | + c.reset(); |
| 167 | + if (darwin::runtime.is_kb_hit()) { |
| 168 | + char ch = darwin::runtime.get_kb_hit(); |
| 169 | + if (ch == '\n' || ch == '\r') |
| 170 | + break; |
| 171 | + else if (ch == '\b' || ch == 127) { |
| 172 | + if (!buff.empty()) |
| 173 | + buff.pop_back(); |
| 174 | + } |
| 175 | + else |
| 176 | + buff += ch; |
| 177 | + } |
| 178 | + darwin::runtime.fit_drawable(); |
| 179 | + std::size_t width = std::max(title.size(), std::max(message.size(), buff.size())) + 4; |
| 180 | + std::size_t x(0.5 * (darwin::runtime.get_drawable()->get_width() - width)), y( |
| 181 | + 0.5 * (darwin::runtime.get_drawable()->get_height() - 6)); |
| 182 | + darwin::runtime.get_drawable()->clear(); |
| 183 | + darwin::runtime.get_drawable()->fill_rect(x, y, width, 6, |
| 184 | + darwin::pixel(' ', true, false, darwin::colors::white, |
| 185 | + darwin::colors::white)); |
| 186 | + darwin::runtime.get_drawable()->fill_rect(x, y, width, 1, |
| 187 | + darwin::pixel(' ', true, false, darwin::colors::white, |
| 188 | + darwin::colors::cyan)); |
| 189 | + darwin::runtime.get_drawable()->fill_rect(x + 2, y + 4, width - 4, 1, |
| 190 | + darwin::pixel(' ', true, false, darwin::colors::white, |
| 191 | + darwin::colors::black)); |
| 192 | + darwin::runtime.get_drawable()->draw_string(x + 0.5 * (width - title.size()), y, title, |
| 193 | + darwin::pixel(' ', true, false, darwin::colors::black, |
| 194 | + darwin::colors::cyan)); |
| 195 | + darwin::runtime.get_drawable()->draw_string(x + 0.5 * (width - message.size()), y + 2, message, |
| 196 | + darwin::pixel(' ', true, false, darwin::colors::black, |
| 197 | + darwin::colors::white)); |
| 198 | + darwin::runtime.get_drawable()->draw_string(x + 2, y + 4, buff, |
| 199 | + darwin::pixel(' ', true, false, darwin::colors::white, |
| 200 | + darwin::colors::black)); |
| 201 | + darwin::runtime.update_drawable(); |
| 202 | + c.sync(); |
| 203 | + } |
| 204 | + if (format) |
| 205 | + return parse_value(buff); |
| 206 | + else |
| 207 | + return buff; |
| 208 | + } |
| 209 | + |
| 210 | +// Drawable Function |
| 211 | + void load_from_file(const drawable_t &pic, const string &path) |
| 212 | + { |
| 213 | + std::ifstream infs(path); |
| 214 | + if (!infs.is_open()) |
| 215 | + throw lang_error("File is not exist."); |
| 216 | + std::deque<char> buff; |
| 217 | + while (!infs.eof()) |
| 218 | + buff.push_back(infs.get()); |
| 219 | + darwin::unserial_picture(pic.get(), buff); |
| 220 | + } |
| 221 | + |
| 222 | + void save_to_file(const drawable_t &pic, const string &path) |
| 223 | + { |
| 224 | + std::ofstream outfs(path); |
| 225 | + if (!outfs.is_open()) |
| 226 | + throw lang_error("Write file failed."); |
| 227 | + std::deque<char> buff; |
| 228 | + darwin::serial_picture(pic.get(), buff); |
| 229 | + for (auto &ch:buff) |
| 230 | + outfs.put(ch); |
| 231 | + outfs.flush(); |
| 232 | + } |
| 233 | + |
| 234 | + void clear(const drawable_t &pic) |
| 235 | + { |
| 236 | + pic->clear(); |
| 237 | + } |
| 238 | + |
| 239 | + void fill(const drawable_t &pic, const darwin::pixel &pix) |
| 240 | + { |
| 241 | + pic->fill(pix); |
| 242 | + } |
| 243 | + |
| 244 | + void resize(const drawable_t &pic, number w, number h) |
| 245 | + { |
| 246 | + pic->resize(w, h); |
| 247 | + } |
| 248 | + |
| 249 | + number get_height(const drawable_t &pic) |
| 250 | + { |
| 251 | + return pic->get_height(); |
| 252 | + } |
| 253 | + |
| 254 | + number get_width(const drawable_t &pic) |
| 255 | + { |
| 256 | + return pic->get_width(); |
| 257 | + } |
| 258 | + |
| 259 | + darwin::pixel get_pixel(const drawable_t &pic, number x, number y) |
| 260 | + { |
| 261 | + return pic->get_pixel(x, y); |
| 262 | + } |
| 263 | + |
| 264 | + void draw_pixel(const drawable_t &pic, number x, number y, const darwin::pixel &pix) |
| 265 | + { |
| 266 | + pic->draw_pixel(x, y, pix); |
| 267 | + } |
| 268 | + |
| 269 | + void draw_line(const drawable_t &pic, number x1, number y1, number x2, number y2, const darwin::pixel &pix) |
| 270 | + { |
| 271 | + pic->draw_line(x1, y1, x2, y2, pix); |
| 272 | + } |
| 273 | + |
| 274 | + void draw_rect(const drawable_t &pic, number x, number y, number w, number h, const darwin::pixel &pix) |
| 275 | + { |
| 276 | + pic->draw_rect(x, y, w, h, pix); |
| 277 | + } |
| 278 | + |
| 279 | + void fill_rect(const drawable_t &pic, number x, number y, number w, number h, const darwin::pixel &pix) |
| 280 | + { |
| 281 | + pic->fill_rect(x, y, w, h, pix); |
| 282 | + } |
| 283 | + |
| 284 | + void draw_triangle(const drawable_t &pic, number x1, number y1, number x2, number y2, number x3, number y3, |
| 285 | + const darwin::pixel &pix) |
| 286 | + { |
| 287 | + pic->draw_triangle(x1, y1, x2, y2, x3, y3, pix); |
| 288 | + } |
| 289 | + |
| 290 | + void fill_triangle(const drawable_t &pic, number x1, number y1, number x2, number y2, number x3, number y3, |
| 291 | + const darwin::pixel &pix) |
| 292 | + { |
| 293 | + pic->fill_triangle(x1, y1, x2, y2, x3, y3, pix); |
| 294 | + } |
| 295 | + |
| 296 | + void draw_string(const drawable_t &pic, number x, number y, const string &str, const darwin::pixel &pix) |
| 297 | + { |
| 298 | + pic->draw_string(x, y, str, pix); |
| 299 | + } |
| 300 | + |
| 301 | + void draw_picture(const drawable_t &pic, number x, number y, const drawable_t &p) |
| 302 | + { |
| 303 | + pic->draw_picture(x, y, *p); |
| 304 | + } |
| 305 | + |
| 306 | + void init() |
| 307 | + { |
| 308 | + // Namespaces |
| 309 | + darwin_ext.add_var("ui", var::make_protect<extension_t>(darwin_ui_ext_shared)); |
| 310 | + darwin_ext.add_var("drawable", var::make_protect<extension_t>(darwin_drawable_ext_shared)); |
| 311 | + // Colors |
| 312 | + darwin_ext.add_var("black", var::make_constant<darwin::colors>(darwin::colors::black)); |
| 313 | + darwin_ext.add_var("white", var::make_constant<darwin::colors>(darwin::colors::white)); |
| 314 | + darwin_ext.add_var("red", var::make_constant<darwin::colors>(darwin::colors::red)); |
| 315 | + darwin_ext.add_var("green", var::make_constant<darwin::colors>(darwin::colors::green)); |
| 316 | + darwin_ext.add_var("blue", var::make_constant<darwin::colors>(darwin::colors::blue)); |
| 317 | + darwin_ext.add_var("pink", var::make_constant<darwin::colors>(darwin::colors::pink)); |
| 318 | + darwin_ext.add_var("yellow", var::make_constant<darwin::colors>(darwin::colors::yellow)); |
| 319 | + darwin_ext.add_var("cyan", var::make_constant<darwin::colors>(darwin::colors::cyan)); |
| 320 | + // Type Constructor |
| 321 | + darwin_ext.add_var("pixel", var::make_protect<callable>(cni(pixel), true)); |
| 322 | + darwin_ext.add_var("picture", var::make_protect<callable>(cni(picture), true)); |
| 323 | + // Darwin Main Function |
| 324 | + darwin_ext.add_var("load", var::make_protect<callable>(cni(load))); |
| 325 | + darwin_ext.add_var("exit", var::make_protect<callable>(cni(exit))); |
| 326 | + darwin_ext.add_var("is_kb_hit", var::make_protect<callable>(cni(is_kb_hit))); |
| 327 | + darwin_ext.add_var("get_kb_hit", var::make_protect<callable>(cni(get_kb_hit))); |
| 328 | + darwin_ext.add_var("fit_drawable", var::make_protect<callable>(cni(fit_drawable))); |
| 329 | + darwin_ext.add_var("get_drawable", var::make_protect<callable>(cni(get_drawable))); |
| 330 | + darwin_ext.add_var("update_drawable", var::make_protect<callable>(cni(update_drawable))); |
| 331 | + darwin_ext.add_var("set_frame_limit", var::make_protect<callable>(cni(set_frame_limit))); |
| 332 | + darwin_ext.add_var("set_draw_line_precision", var::make_protect<callable>(cni(set_draw_line_precision))); |
| 333 | + // Darwin UI Function |
| 334 | + darwin_ui_ext.add_var("message_box", var::make_protect<callable>(cni(message_box))); |
| 335 | + darwin_ui_ext.add_var("input_box", var::make_protect<callable>(cni(input_box))); |
| 336 | + // Drawable Function |
| 337 | + darwin_drawable_ext.add_var("load_from_file", var::make_protect<callable>(cni(load_from_file))); |
| 338 | + darwin_drawable_ext.add_var("save_to_file", var::make_protect<callable>(cni(save_to_file))); |
| 339 | + darwin_drawable_ext.add_var("clear", var::make_protect<callable>(cni(clear))); |
| 340 | + darwin_drawable_ext.add_var("fill", var::make_protect<callable>(cni(fill))); |
| 341 | + darwin_drawable_ext.add_var("resize", var::make_protect<callable>(cni(resize))); |
| 342 | + darwin_drawable_ext.add_var("get_height", var::make_protect<callable>(cni(get_height))); |
| 343 | + darwin_drawable_ext.add_var("get_width", var::make_protect<callable>(cni(get_width))); |
| 344 | + darwin_drawable_ext.add_var("get_pixel", var::make_protect<callable>(cni(get_pixel))); |
| 345 | + darwin_drawable_ext.add_var("draw_pixel", var::make_protect<callable>(cni(draw_pixel))); |
| 346 | + darwin_drawable_ext.add_var("draw_line", var::make_protect<callable>(cni(draw_line))); |
| 347 | + darwin_drawable_ext.add_var("draw_rect", var::make_protect<callable>(cni(draw_rect))); |
| 348 | + darwin_drawable_ext.add_var("fill_rect", var::make_protect<callable>(cni(fill_rect))); |
| 349 | + darwin_drawable_ext.add_var("draw_triangle", var::make_protect<callable>(cni(draw_triangle))); |
| 350 | + darwin_drawable_ext.add_var("fill_triangle", var::make_protect<callable>(cni(fill_triangle))); |
| 351 | + darwin_drawable_ext.add_var("draw_string", var::make_protect<callable>(cni(draw_string))); |
| 352 | + darwin_drawable_ext.add_var("draw_picture", var::make_protect<callable>(cni(draw_picture))); |
| 353 | + } |
| 354 | +} |
| 355 | + |
| 356 | +cs::extension *cs_extension() |
| 357 | +{ |
| 358 | + darwin_cs_ext::init(); |
| 359 | + return &darwin_ext; |
| 360 | +} |
0 commit comments