void Session::read(const JSON::Value &value) { creationTime = value.has("created") ? (uint64_t)Time::parse(value.getString("created")) : 0; lastUsed = value.has("last_used") ? (uint64_t)Time::parse(value.getString("last_used")) : 0; user = value.getString("user", ""); ip = value.has("ip") ? IPAddress(value.getString("ip")) : IPAddress(); }
void hake::exportImage(Path from, Path to, Json::Value& asset, bool premultiplyAlpha) { if (!Files::exists(to.parent())) Files::createDirectories(to.parent()); if (!premultiplyAlpha && (!asset.has("scale") || asset["scale"].number() == 1 || asset["scale"].number() == 0) && !asset.has("background")) KhaExporter::copyFile(from, to); else { int w; int h; int comp; u8* image = stbi_load(from.toString().c_str(), &w, &h, &comp, 4); if (image == nullptr) throw std::runtime_error("Could not read image."); if (premultiplyAlpha) { for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { float alpha = image[y * w * 4 + x * 4 + 3] / 255.0f; image[y * w * 4 + x * 4 + 0] = k_round(image[y * w * 4 + x * 4 + 0] * alpha); image[y * w * 4 + x * 4 + 1] = k_round(image[y * w * 4 + x * 4 + 1] * alpha); image[y * w * 4 + x * 4 + 2] = k_round(image[y * w * 4 + x * 4 + 2] * alpha); } } } if (asset.has("scale") && asset["scale"].number() != 0 && asset["scale"].number() != 1) { image = (u8*)scale((u32*)image, w, h, w * asset["scale"].number(), h * asset["scale"].number()); w *= asset["scale"].number(); h *= asset["scale"].number(); } // BufferedImage outImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR); // Graphics2D g2d = (Graphics2D)outImage.getGraphics(); // if (asset.circle) { // RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // g2d.addRenderingHints(hints); // g2d.setColor(new Color(1.0f, 1.0f, 1.0f, 0.0f)); // g2d.fillRect(0, 0, image.getWidth(null), image.getHeight(null)); // g2d.setColor(Color.BLACK); // g2d.fillOval(0, 0, image.getWidth(null), image.getHeight(null)); // g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP)); // } // g2d.drawImage(image, 0, 0, null); // g2d.dispose(); if (asset.has("background")) { int red = asset["background"]["red"].number(); int green = asset["background"]["green"].number(); int blue = asset["background"]["blue"].number(); image = removeColor(image, w, h, red, green, blue); } stbi_write_png(to.toString().c_str(), w, h, 4, image, 0); } }
void CompareJson(json::Value& lhs, json::Value& rhs, json::Value& output) { std::string lstr, rstr; if (rhs.type() != lhs.type()) return; switch (lhs.type()) { case json::Value::tString: lstr = lhs.getString(); rstr = rhs.getString(); if (!lstr.empty() && lstr[0] == '$' && lstr.back() == '$') { size_t slash = lstr.find('/'); std::string cat = lstr.substr(1, slash - 1); std::string name = lstr.substr(slash + 1, lstr.length() - slash - 2); output[cat][name] = rstr; } break; case json::Value::tArray: for (size_t i = 0; i < lhs.length(); ++i) { if (i < rhs.length()) { CompareJson(lhs[i], rhs[i], output); } } break; case json::Value::tObject: for (auto it = lhs.begin(); it != lhs.end(); ++it) { if (rhs.has(it.key())) { CompareJson(*it, rhs[it.key()], output); } } break; } }
void Node::load(Json::Value const& value) { auto& valuePosition=value.at("P"); auto& valueScale=value.at("S"); auto& valueRotation=value.at("R"); if(valuePosition.isArray()) { position=vec3{ valuePosition.at<Float>(0, 0.0f), valuePosition.at<Float>(1, 0.0f), valuePosition.at<Float>(2, 0.0f)}; } if(valueScale.isArray()) { scale=vec3{ valueScale.at<Float>(0, 1.0f), valueScale.at<Float>(1, 1.0f), valueScale.at<Float>(2, 1.0f)}; } if(valueRotation.isArray()) { rotation=vec3{ valueRotation.at<Float>(0, 0.0f), valueRotation.at<Float>(1, 0.0f), valueRotation.at<Float>(2, 0.0f) }; } if(value.has("G")) { auto scene=this->scene(); // TODO Fallback geometry (& meshes) for debugging (or leave blank and decide in renderer). geometry=value.at<String>("G", ""); if(!scene->renderer->geometryExists(geometry)) { // TODO Resource manager / resolve path from current script. String pathModel{XEAR_PATH_MODEL}; if(!pathModel.empty()) pathModel+="/"; auto data=data::Geometry<>::unserialize(pathModel+geometry+".av"); scene->renderer->geometryCreate(geometry, *data); } } auto& valueChildren=value.at("C"); if(valueChildren.isArray()) { for(auto& child : valueChildren) { auto nodeName=child.at<String>("N", ""); if(nodeName.empty()) continue; Node* node=nullptr; auto nodeType=child.at<Int>("T", 1); if((Int)NodeType::MESH==nodeType) { auto tmp=make_unique<scene::Mesh>(nodeName); node=tmp.get(); children.push_back(move(tmp)); } else { auto tmp=make_unique<scene::Node>(NODE_TYPES[nodeType], nodeName); node=tmp.get(); children.push_back(move(tmp)); } node->parent=this; node->load(child); node->load(); } } dbgi2("Loaded [" << *this << "]."); }