2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2017-01-14 09:52:54 +00:00
|
|
|
/* patch_9_rect.cpp */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2017-01-14 09:52:54 +00:00
|
|
|
#include "patch_9_rect.h"
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
#include "servers/visual_server.h"
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
void NinePatchRect::_notification(int p_what) {
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
if (p_what==NOTIFICATION_DRAW) {
|
|
|
|
|
|
|
|
if (texture.is_null())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Size2 s=get_size();
|
|
|
|
RID ci = get_canvas_item();
|
2017-01-08 15:31:14 +00:00
|
|
|
VS::get_singleton()->canvas_item_add_nine_patch(ci,Rect2(Point2(),s),region_rect,texture->get_rid(),Vector2(margin[MARGIN_LEFT],margin[MARGIN_TOP]),Vector2(margin[MARGIN_RIGHT],margin[MARGIN_BOTTOM]),VS::NINE_PATCH_STRETCH,VS::NINE_PATCH_STRETCH,draw_center);
|
2015-08-24 12:39:16 +00:00
|
|
|
// draw_texture_rect(texture,Rect2(Point2(),s),false,modulate);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Vector<Point2> points;
|
|
|
|
points.resize(4);
|
|
|
|
points[0]=Point2(0,0);
|
|
|
|
points[1]=Point2(s.x,0);
|
|
|
|
points[2]=Point2(s.x,s.y);
|
|
|
|
points[3]=Point2(0,s.y);
|
|
|
|
Vector<Point2> uvs;
|
|
|
|
uvs.resize(4);
|
|
|
|
uvs[0]=Point2(0,0);
|
|
|
|
uvs[1]=Point2(1,0);
|
|
|
|
uvs[2]=Point2(1,1);
|
|
|
|
uvs[3]=Point2(0,1);
|
|
|
|
|
|
|
|
VisualServer::get_singleton()->canvas_item_add_primitive(ci,points,Vector<Color>(),uvs,texture->get_rid());
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
Size2 NinePatchRect::get_minimum_size() const {
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
return Size2(margin[MARGIN_LEFT]+margin[MARGIN_RIGHT],margin[MARGIN_TOP]+margin[MARGIN_BOTTOM]);
|
|
|
|
}
|
2017-01-12 21:27:27 +00:00
|
|
|
void NinePatchRect::_bind_methods() {
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
ClassDB::bind_method(_MD("set_texture","texture"), & NinePatchRect::set_texture );
|
|
|
|
ClassDB::bind_method(_MD("get_texture"), & NinePatchRect::get_texture );
|
|
|
|
ClassDB::bind_method(_MD("set_patch_margin","margin","value"), & NinePatchRect::set_patch_margin );
|
|
|
|
ClassDB::bind_method(_MD("get_patch_margin","margin"), & NinePatchRect::get_patch_margin );
|
|
|
|
ClassDB::bind_method(_MD("set_region_rect","rect"),&NinePatchRect::set_region_rect);
|
|
|
|
ClassDB::bind_method(_MD("get_region_rect"),&NinePatchRect::get_region_rect);
|
|
|
|
ClassDB::bind_method(_MD("set_draw_center","draw_center"), & NinePatchRect::set_draw_center );
|
|
|
|
ClassDB::bind_method(_MD("get_draw_center"), & NinePatchRect::get_draw_center );
|
2015-08-24 12:39:16 +00:00
|
|
|
|
2016-06-20 01:16:41 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("texture_changed"));
|
|
|
|
|
2015-08-24 12:39:16 +00:00
|
|
|
ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"),_SCS("get_texture") );
|
|
|
|
ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "draw_center"), _SCS("set_draw_center"),_SCS("get_draw_center") );
|
2016-06-03 16:54:53 +00:00
|
|
|
ADD_PROPERTYNZ( PropertyInfo( Variant::RECT2, "region_rect"), _SCS("set_region_rect"),_SCS("get_region_rect"));
|
2017-01-04 04:16:14 +00:00
|
|
|
|
|
|
|
ADD_GROUP("Patch Margin","patch_margin_");
|
|
|
|
ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_left",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_LEFT );
|
|
|
|
ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_top",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_TOP );
|
|
|
|
ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_right",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_RIGHT );
|
|
|
|
ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_bottom",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_patch_margin"),_SCS("get_patch_margin"),MARGIN_BOTTOM );
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
void NinePatchRect::set_texture(const Ref<Texture>& p_tex) {
|
2015-08-24 12:39:16 +00:00
|
|
|
|
2016-06-20 01:16:41 +00:00
|
|
|
if (texture==p_tex)
|
|
|
|
return;
|
2015-08-24 12:39:16 +00:00
|
|
|
texture=p_tex;
|
|
|
|
update();
|
|
|
|
//if (texture.is_valid())
|
|
|
|
// texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites
|
|
|
|
minimum_size_changed();
|
2016-06-20 01:16:41 +00:00
|
|
|
emit_signal("texture_changed");
|
2015-08-24 12:39:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
Ref<Texture> NinePatchRect::get_texture() const {
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
void NinePatchRect::set_patch_margin(Margin p_margin,int p_size) {
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
ERR_FAIL_INDEX(p_margin,4);
|
|
|
|
margin[p_margin]=p_size;
|
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
2016-06-03 20:04:44 +00:00
|
|
|
switch (p_margin) {
|
|
|
|
case MARGIN_LEFT:
|
|
|
|
_change_notify("patch_margin/left");
|
|
|
|
break;
|
|
|
|
case MARGIN_TOP:
|
|
|
|
_change_notify("patch_margin/top");
|
|
|
|
break;
|
|
|
|
case MARGIN_RIGHT:
|
|
|
|
_change_notify("patch_margin/right");
|
|
|
|
break;
|
|
|
|
case MARGIN_BOTTOM:
|
|
|
|
_change_notify("patch_margin/bottom");
|
|
|
|
break;
|
|
|
|
}
|
2015-08-24 12:39:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
int NinePatchRect::get_patch_margin(Margin p_margin) const{
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(p_margin,4,0);
|
|
|
|
return margin[p_margin];
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
void NinePatchRect::set_region_rect(const Rect2& p_region_rect) {
|
2016-06-03 16:54:53 +00:00
|
|
|
|
|
|
|
if (region_rect==p_region_rect)
|
|
|
|
return;
|
|
|
|
|
|
|
|
region_rect=p_region_rect;
|
|
|
|
|
|
|
|
item_rect_changed();
|
|
|
|
_change_notify("region_rect");
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
Rect2 NinePatchRect::get_region_rect() const {
|
2016-06-03 16:54:53 +00:00
|
|
|
|
|
|
|
return region_rect;
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
void NinePatchRect::set_draw_center(bool p_draw) {
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
draw_center=p_draw;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
bool NinePatchRect::get_draw_center() const{
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
return draw_center;
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
NinePatchRect::NinePatchRect() {
|
2015-08-24 12:39:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
margin[MARGIN_LEFT]=0;
|
|
|
|
margin[MARGIN_RIGHT]=0;
|
|
|
|
margin[MARGIN_BOTTOM]=0;
|
|
|
|
margin[MARGIN_TOP]=0;
|
2017-01-08 15:31:14 +00:00
|
|
|
|
2017-01-08 22:54:19 +00:00
|
|
|
set_mouse_filter(MOUSE_FILTER_IGNORE);
|
2015-08-24 12:39:16 +00:00
|
|
|
draw_center=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-12 21:27:27 +00:00
|
|
|
NinePatchRect::~NinePatchRect()
|
2015-08-24 12:39:16 +00:00
|
|
|
{
|
|
|
|
}
|