Skip to content

Commit

Permalink
Fix GDNative version of ported controls and add demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruvzg committed May 5, 2019
1 parent bc7d9c2 commit e27b25a
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 24 deletions.
10 changes: 10 additions & 0 deletions demos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ Rich text input control demo: input, cursor and selection, opening and saving `*
![disp demo screenshot](disp_sshot.png)

Shapaed string demo: fonts, string construction, rendering, line breaking and justification.

## `ctrls_builtin` and `ctrls_gdnative`

![ctrls demo screenshot](ctrls_sshot.png)

Ported Godot controls (Label and LineEdit) demo.

## `get_sample_texts_builtin` (module only)

Renders Noto font sample text to `.png` files.
95 changes: 95 additions & 0 deletions demos/ctrls_gdnative/Control.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
[gd_scene load_steps=8 format=2]

[ext_resource path="res://addons/libgdtl/classes/tl_label.gdns" type="Script" id=1]
[ext_resource path="res://fonts/NotoSans-Bold.ttf" type="DynamicFontData" id=2]
[ext_resource path="res://addons/libgdtl/classes/tl_line_edit.gdns" type="Script" id=3]
[ext_resource path="res://fonts/NotoNastaliqUrdu-Regular.ttf" type="DynamicFontData" id=4]

[sub_resource type="GDScript" id=1]
script/source = "extends Control

const TLFontFamily = preload(\"res://addons/libgdtl/classes/tl_font_family.gdns\")
const TLDynamicFontFace = preload(\"res://addons/libgdtl/classes/tl_dynamic_font_face.gdns\")

# Called when the node enters the scene tree for the first time.
func _ready():
var face1 = TLDynamicFontFace.new()
face1.load(\"res://fonts/NotoSans-Bold.ttf\")

var font1 = TLFontFamily.new()
font1.add_face(\"Default\", face1)

$TLLabel.set_base_font(font1)
$TLLabel.set_base_font_style(\"Default\")
$TLLabel.set_base_font_size(12)

var face2 = TLDynamicFontFace.new()
face2.load(\"res://fonts/NotoNastaliqUrdu-Regular.ttf\")

var font2 = TLFontFamily.new()
font2.add_face(\"Default\", face2)

$TLLineEdit.set_base_font(font2)
$TLLineEdit.set_base_font_style(\"Default\")
$TLLineEdit.set_base_font_size(12)
"
[sub_resource type="DynamicFont" id=2]
size = 12
font_data = ExtResource( 2 )
[sub_resource type="DynamicFont" id=3]
size = 12
font_data = ExtResource( 4 )
[node name="Control" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 1.0
margin_top = -1.0
margin_right = 1.0
margin_bottom = -1.0
script = SubResource( 1 )
[node name="TLLabel" type="Control" parent="."]
margin_left = 111.0
margin_top = 35.0
margin_right = 320.0
margin_bottom = 145.0
rect_min_size = Vector2( 209, 110 )
mouse_filter = 2
size_flags_vertical = 4
script = ExtResource( 1 )
text = "test232
Godot Label"
[node name="Label" type="Label" parent="."]
margin_left = 401.0
margin_top = 37.0
margin_right = 611.0
margin_bottom = 151.0
rect_min_size = Vector2( 210, 114 )
custom_fonts/font = SubResource( 2 )
text = "test232
Godot Label"
[node name="TLLineEdit" type="Control" parent="."]
margin_left = 100.0
margin_top = 159.0
margin_right = 322.0
margin_bottom = 200.0
rect_min_size = Vector2( 222, 41 )
focus_mode = 2
mouse_default_cursor_shape = 1
script = ExtResource( 3 )
text = "اور بازار سے لے آئے اگر ٹوٹ گیا"
secret_character = "*"
[node name="LineEdit" type="LineEdit" parent="."]
margin_left = 390.0
margin_top = 159.0
margin_right = 612.0
margin_bottom = 200.0
rect_min_size = Vector2( 222, 41 )
custom_fonts/font = SubResource( 3 )
text = "اور بازار سے لے آئے اگر ٹوٹ گیا"
1 change: 1 addition & 0 deletions demos/ctrls_gdnative/addons/libgdtl
7 changes: 7 additions & 0 deletions demos/ctrls_gdnative/default_env.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="Environment" load_steps=2 format=2]

[sub_resource type="ProceduralSky" id=1]

[resource]
background_mode = 2
background_sky = SubResource( 1 )
1 change: 1 addition & 0 deletions demos/ctrls_gdnative/fonts
Binary file added demos/ctrls_gdnative/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions demos/ctrls_gdnative/project.godot
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters

config_version=4

_global_script_classes=[ ]
_global_script_class_icons={

}

[application]

config/name="gdtl backported controls (builtin)"
run/main_scene="res://Control.tscn"
config/icon="res://icon.png"

[autoload]

TLConstants="*res://addons/libgdtl/constants.gd"

[editor_plugins]

enabled=PoolStringArray( "libgdtl" )

[rendering]

environment/default_environment="res://default_env.tres"
Binary file added demos/ctrls_sshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Binary file modified demos/input_sshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 44 additions & 8 deletions src/controls/tl_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#ifdef GODOT_MODULE
#include "core/translation.h"
#else
#include <StyleBox.hpp>
#include <TranslationServer.hpp>
#include <VisualServer.hpp>
#endif
Expand Down Expand Up @@ -114,6 +113,7 @@ void TLLabel::_notification(int p_what) {

Size2 string_size;
Size2 size = get_size();
#ifdef GODOT_MODULE
Ref<StyleBox> style = get_stylebox("normal", "Label");
Color font_color = get_color("font_color", "Label");
Color font_color_shadow = get_color("font_color_shadow", "Label");
Expand All @@ -122,16 +122,28 @@ void TLLabel::_notification(int p_what) {
int line_spacing = get_constant("line_spacing", "Label");

style->draw(ci, Rect2(Point2(0, 0), get_size()));

#else
Color font_color = Color(1, 1, 1);
Color font_color_shadow = Color(0, 0, 0, 0);
bool use_outline = false;
Point2 shadow_ofs(2, 2);
int line_spacing = 2;
#endif
int vbegin = 0, vsep = 0;

float total_h = 0.0;
int lines_visible = 0;
for (int i = lines_skipped; i < s_lines.size(); i++) {
total_h += s_lines[i]->get_height() + line_spacing;
#ifdef GODOT_MODULE
if (total_h > (get_size().height - get_stylebox("normal", "Label")->get_minimum_size().height + line_spacing)) {
break;
}
#else
if (total_h > (get_size().height + line_spacing)) {
break;
}
#endif
lines_visible++;
}

Expand Down Expand Up @@ -171,22 +183,30 @@ void TLLabel::_notification(int p_what) {
}

Vector2 ofs;
#ifdef GODOT_MODULE
ofs.y = style->get_offset().y + vbegin;
#else
ofs.y = vbegin;
#endif
for (int j = lines_skipped; j < s_lines.size(); j++) {
ofs.y += s_lines[j]->get_ascent();
switch (align) {
case ALIGN_FILL:
case ALIGN_LEFT: {

#ifdef GODOT_MODULE
ofs.x = style->get_offset().x;
#endif
} break;
case ALIGN_CENTER: {

ofs.x = int(size.width - s_lines[j]->get_width()) / 2;
} break;
case ALIGN_RIGHT: {

#ifdef GODOT_MODULE
ofs.x = int(size.width - style->get_margin(GLOBAL_CONST(MARGIN_RIGHT)) - s_lines[j]->get_width());
#else
ofs.x = int(size.width - s_lines[j]->get_width());
#endif
} break;
}
if (font_color_shadow.a > 0) {
Expand Down Expand Up @@ -216,9 +236,11 @@ void TLLabel::_notification(int p_what) {
}

Size2 TLLabel::get_minimum_size() const {

#ifdef GODOT_MODULE
Size2 min_style = get_stylebox("normal", "Label")->get_minimum_size();

#else
Size2 min_style(5, 5);
#endif
// don't want to mutable everything
if (_lines_dirty)
const_cast<TLLabel *>(this)->_reshape_lines();
Expand Down Expand Up @@ -255,9 +277,15 @@ int TLLabel::get_visible_line_count() const {
int lines_visible = 0;
for (int i = lines_skipped; i < s_lines.size(); i++) {
total_h += s_lines[i]->get_height() + line_spacing;
#ifdef GODOT_MODULE
if (total_h > (get_size().height - get_stylebox("normal", "Label")->get_minimum_size().height + line_spacing)) {
break;
}
#else
if (total_h > (get_size().height + line_spacing)) {
break;
}
#endif
lines_visible++;
}

Expand All @@ -272,12 +300,18 @@ int TLLabel::get_visible_line_count() const {

void TLLabel::_reshape_lines() {

#ifdef GODOT_MODULE
Ref<StyleBox> style = get_stylebox("normal", "Label");
int line_spacing = get_constant("line_spacing", "Label");

#else
int line_spacing = 2;
#endif
s_lines.clear();

#ifdef GODOT_MODULE
int width = (get_size().width - style->get_minimum_size().width);
#else
int width = get_size().width;
#endif

if (xl_text.length() == 0) {
minsize = Size2(width, _get_base_font_height());
Expand Down Expand Up @@ -662,6 +696,8 @@ void TLLabel::_register_methods() {
register_property<TLLabel, bool>("autowrap", &TLLabel::set_autowrap, &TLLabel::has_autowrap, false);
register_property<TLLabel, bool>("clip_text", &TLLabel::set_clip_text, &TLLabel::is_clipping_text, false);
register_property<TLLabel, bool>("uppercase", &TLLabel::set_uppercase, &TLLabel::is_uppercase, false);

register_method("_notification", &TLLabel::_notification);
}

#endif
Expand Down
Loading

0 comments on commit e27b25a

Please sign in to comment.