| 1 | /**************************************************************************/ |
| 2 | /* camera_texture.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "camera_texture.h" |
| 32 | |
| 33 | #include "servers/camera/camera_feed.h" |
| 34 | |
| 35 | void CameraTexture::_bind_methods() { |
| 36 | ClassDB::bind_method(D_METHOD("set_camera_feed_id" , "feed_id" ), &CameraTexture::set_camera_feed_id); |
| 37 | ClassDB::bind_method(D_METHOD("get_camera_feed_id" ), &CameraTexture::get_camera_feed_id); |
| 38 | |
| 39 | ClassDB::bind_method(D_METHOD("set_which_feed" , "which_feed" ), &CameraTexture::set_which_feed); |
| 40 | ClassDB::bind_method(D_METHOD("get_which_feed" ), &CameraTexture::get_which_feed); |
| 41 | |
| 42 | ClassDB::bind_method(D_METHOD("set_camera_active" , "active" ), &CameraTexture::set_camera_active); |
| 43 | ClassDB::bind_method(D_METHOD("get_camera_active" ), &CameraTexture::get_camera_active); |
| 44 | |
| 45 | ADD_PROPERTY(PropertyInfo(Variant::INT, "camera_feed_id" ), "set_camera_feed_id" , "get_camera_feed_id" ); |
| 46 | ADD_PROPERTY(PropertyInfo(Variant::INT, "which_feed" ), "set_which_feed" , "get_which_feed" ); |
| 47 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "camera_is_active" ), "set_camera_active" , "get_camera_active" ); |
| 48 | } |
| 49 | |
| 50 | int CameraTexture::get_width() const { |
| 51 | Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id); |
| 52 | if (feed.is_valid()) { |
| 53 | return feed->get_base_width(); |
| 54 | } else { |
| 55 | return 0; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | int CameraTexture::get_height() const { |
| 60 | Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id); |
| 61 | if (feed.is_valid()) { |
| 62 | return feed->get_base_height(); |
| 63 | } else { |
| 64 | return 0; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | bool CameraTexture::has_alpha() const { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | RID CameraTexture::get_rid() const { |
| 73 | Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id); |
| 74 | if (feed.is_valid()) { |
| 75 | return feed->get_texture(which_feed); |
| 76 | } else { |
| 77 | if (_texture.is_null()) { |
| 78 | _texture = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
| 79 | } |
| 80 | return _texture; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Ref<Image> CameraTexture::get_image() const { |
| 85 | // not (yet) supported |
| 86 | return Ref<Image>(); |
| 87 | } |
| 88 | |
| 89 | void CameraTexture::set_camera_feed_id(int p_new_id) { |
| 90 | camera_feed_id = p_new_id; |
| 91 | notify_property_list_changed(); |
| 92 | } |
| 93 | |
| 94 | int CameraTexture::get_camera_feed_id() const { |
| 95 | return camera_feed_id; |
| 96 | } |
| 97 | |
| 98 | void CameraTexture::set_which_feed(CameraServer::FeedImage p_which) { |
| 99 | which_feed = p_which; |
| 100 | notify_property_list_changed(); |
| 101 | } |
| 102 | |
| 103 | CameraServer::FeedImage CameraTexture::get_which_feed() const { |
| 104 | return which_feed; |
| 105 | } |
| 106 | |
| 107 | void CameraTexture::set_camera_active(bool p_active) { |
| 108 | Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id); |
| 109 | if (feed.is_valid()) { |
| 110 | feed->set_active(p_active); |
| 111 | notify_property_list_changed(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | bool CameraTexture::get_camera_active() const { |
| 116 | Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id); |
| 117 | if (feed.is_valid()) { |
| 118 | return feed->is_active(); |
| 119 | } else { |
| 120 | return false; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | CameraTexture::CameraTexture() {} |
| 125 | |
| 126 | CameraTexture::~CameraTexture() { |
| 127 | if (_texture.is_valid()) { |
| 128 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 129 | RenderingServer::get_singleton()->free(_texture); |
| 130 | } |
| 131 | } |
| 132 | |