2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* style_box.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "style_box.h"
|
2020-03-27 08:44:44 +01:00
|
|
|
|
|
|
|
#include "scene/main/canvas_item.h"
|
2019-01-24 14:21:56 +01:00
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
Size2 StyleBox::get_minimum_size() const {
|
|
|
|
Size2 min_size = Size2(get_margin(SIDE_LEFT) + get_margin(SIDE_RIGHT), get_margin(SIDE_TOP) + get_margin(SIDE_BOTTOM));
|
|
|
|
Size2 custom_size;
|
|
|
|
GDVIRTUAL_CALL(_get_minimum_size, custom_size);
|
2022-03-12 05:38:44 +01:00
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
if (min_size.x < custom_size.x) {
|
|
|
|
min_size.x = custom_size.x;
|
|
|
|
}
|
|
|
|
if (min_size.y < custom_size.y) {
|
|
|
|
min_size.y = custom_size.y;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
return min_size;
|
2022-03-10 08:17:38 +01:00
|
|
|
}
|
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
void StyleBox::set_content_margin(Side p_side, float p_value) {
|
2020-12-22 17:24:29 +01:00
|
|
|
ERR_FAIL_INDEX((int)p_side, 4);
|
2019-10-10 11:48:58 +02:00
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
content_margin[p_side] = p_value;
|
2014-02-10 02:10:30 +01:00
|
|
|
emit_changed();
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
void StyleBox::set_content_margin_all(float p_value) {
|
2022-03-12 05:38:44 +01:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2023-01-19 17:14:09 +01:00
|
|
|
content_margin[i] = p_value;
|
2022-03-12 05:38:44 +01:00
|
|
|
}
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
void StyleBox::set_content_margin_individual(float p_left, float p_top, float p_right, float p_bottom) {
|
|
|
|
content_margin[SIDE_LEFT] = p_left;
|
|
|
|
content_margin[SIDE_TOP] = p_top;
|
|
|
|
content_margin[SIDE_RIGHT] = p_right;
|
|
|
|
content_margin[SIDE_BOTTOM] = p_bottom;
|
2022-03-12 05:38:44 +01:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
float StyleBox::get_content_margin(Side p_side) const {
|
2020-12-22 17:24:29 +01:00
|
|
|
ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
|
2019-10-10 11:48:58 +02:00
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
return content_margin[p_side];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 17:24:29 +01:00
|
|
|
float StyleBox::get_margin(Side p_side) const {
|
|
|
|
ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
|
2019-10-10 11:48:58 +02:00
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
if (content_margin[p_side] < 0) {
|
2020-12-22 17:24:29 +01:00
|
|
|
return get_style_margin(p_side);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2023-01-19 17:14:09 +01:00
|
|
|
return content_margin[p_side];
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Point2 StyleBox::get_offset() const {
|
2020-12-22 17:24:29 +01:00
|
|
|
return Point2(get_margin(SIDE_LEFT), get_margin(SIDE_TOP));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
void StyleBox::draw(RID p_canvas_item, const Rect2 &p_rect) const {
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_draw, p_canvas_item, p_rect);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-24 16:26:30 +01:00
|
|
|
Rect2 StyleBox::get_draw_rect(const Rect2 &p_rect) const {
|
2022-03-10 08:17:38 +01:00
|
|
|
Rect2 ret;
|
|
|
|
if (GDVIRTUAL_CALL(_get_draw_rect, p_rect, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
2019-11-24 16:26:30 +01:00
|
|
|
return p_rect;
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
CanvasItem *StyleBox::get_current_item_drawn() const {
|
|
|
|
return CanvasItem::get_current_item_drawn();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const {
|
|
|
|
bool ret = true;
|
|
|
|
GDVIRTUAL_CALL(_test_mask, p_point, p_rect, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void StyleBox::_bind_methods() {
|
2023-01-19 17:14:09 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_minimum_size"), &StyleBox::get_minimum_size);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2023-01-19 17:14:09 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_content_margin", "margin", "offset"), &StyleBox::set_content_margin);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_content_margin_all", "offset"), &StyleBox::set_content_margin_all);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_content_margin", "margin"), &StyleBox::get_content_margin);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_margin", "margin"), &StyleBox::get_margin);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_offset"), &StyleBox::get_offset);
|
2015-02-11 03:53:04 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("draw", "canvas_item", "rect"), &StyleBox::draw);
|
2023-01-19 17:14:09 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_current_item_drawn"), &StyleBox::get_current_item_drawn);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("test_mask", "point", "rect"), &StyleBox::test_mask);
|
2015-02-11 03:53:04 +01:00
|
|
|
|
2022-05-23 23:17:12 +02:00
|
|
|
ADD_GROUP("Content Margins", "content_margin_");
|
2023-01-19 17:14:09 +01:00
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_left", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_LEFT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_top", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_TOP);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_right", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_RIGHT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_bottom", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_BOTTOM);
|
2022-03-10 08:17:38 +01:00
|
|
|
|
|
|
|
GDVIRTUAL_BIND(_draw, "to_canvas_item", "rect")
|
2023-01-19 17:14:09 +01:00
|
|
|
GDVIRTUAL_BIND(_get_draw_rect, "rect")
|
|
|
|
GDVIRTUAL_BIND(_get_minimum_size)
|
|
|
|
GDVIRTUAL_BIND(_test_mask, "point", "rect")
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
StyleBox::StyleBox() {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2023-01-19 17:14:09 +01:00
|
|
|
content_margin[i] = -1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|