Skip to content

Commit

Permalink
Componentized grapheme editing and added support for probability mani…
Browse files Browse the repository at this point in the history
…pulation in visualizer
  • Loading branch information
kesac committed Jun 13, 2024
1 parent 3d0b08e commit 37a9255
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 82 deletions.
63 changes: 63 additions & 0 deletions Syllabore/Syllabore.Visualizer/GraphemeEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Godot;
using System;

public partial class GraphemeEditor : Control
{
public event Action GraphemeEditorChanged;

protected virtual void OnGraphemeEditorChanged()
{
GraphemeEditorChanged?.Invoke();
}

protected virtual void OnVSliderChanged(float value)
{
this.UpdatePercentageLabel(value);
GraphemeEditorChanged?.Invoke();
}

public string LabelText
{
get
{
return this.GetNode<Label>("LeftPanel/Label").Text;
}
set
{
this.GetNode<Label>("LeftPanel/Label").Text = value;
}
}

public string Text
{
get
{
return this.GetNode<TextEdit>("LeftPanel/TextEdit").Text;
}
set
{

this.GetNode<TextEdit>("LeftPanel/TextEdit").Text = value;
}
}

public double Probability
{
get
{
var vSlider = this.GetNode<VSlider>("RightPanel/VSlider");
return vSlider.Value / vSlider.MaxValue; // Normalize to [0,1]
}
set
{
var vSlider = this.GetNode<VSlider>("RightPanel/VSlider");
this.GetNode<VSlider>("RightPanel/VSlider").Value = value * vSlider.MaxValue; // Normalize to [0,100]
this.UpdatePercentageLabel(value * vSlider.MaxValue);
}
}

private void UpdatePercentageLabel(double value)
{
this.GetNode<Label>("RightPanel/PercentageLabel").Text = $"{(int)value}%";
}
}
73 changes: 73 additions & 0 deletions Syllabore/Syllabore.Visualizer/GraphemeEditor.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[gd_scene load_steps=3 format=3 uid="uid://dfh0hshtdjms1"]

[ext_resource type="Script" path="res://GraphemeEditor.cs" id="1_7by6n"]
[ext_resource type="LabelSettings" uid="uid://bk6ghtaibqhya" path="res://EditorLabelGray.tres" id="1_886yt"]

[node name="GraphemeEditor" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_right = -940.0
offset_bottom = -700.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_7by6n")

[node name="LeftPanel" type="Panel" parent="."]
layout_mode = 0
offset_right = 170.0
offset_bottom = 75.0

[node name="Label" type="Label" parent="LeftPanel"]
layout_mode = 0
offset_left = 8.0
offset_top = 2.0
offset_right = 166.0
offset_bottom = 25.0
text = "Grapheme Label"
label_settings = ExtResource("1_886yt")
vertical_alignment = 1

[node name="TextEdit" type="TextEdit" parent="LeftPanel"]
layout_mode = 0
offset_left = 6.0
offset_top = 25.0
offset_right = 165.0
offset_bottom = 69.0

[node name="RightPanel" type="Panel" parent="."]
offset_left = 177.0
offset_right = 252.0
offset_bottom = 75.0

[node name="VSlider" type="VSlider" parent="RightPanel"]
layout_mode = 0
offset_left = 47.0
offset_top = 28.0
offset_right = 73.0
offset_bottom = 64.0
value = 100.0

[node name="PercentageLabel" type="Label" parent="RightPanel"]
layout_mode = 0
offset_left = 7.0
offset_top = 28.0
offset_right = 49.0
offset_bottom = 64.0
text = "100%"
vertical_alignment = 1

[node name="Label" type="Label" parent="RightPanel"]
layout_mode = 0
offset_left = 1.0
offset_top = 2.0
offset_right = 74.0
offset_bottom = 25.0
text = "Probability"
label_settings = ExtResource("1_886yt")
horizontal_alignment = 1
vertical_alignment = 1

[connection signal="text_changed" from="LeftPanel/TextEdit" to="." method="OnGraphemeEditorChanged"]
[connection signal="value_changed" from="RightPanel/VSlider" to="." method="OnVSliderChanged"]
51 changes: 32 additions & 19 deletions Syllabore/Syllabore.Visualizer/MainVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,63 @@ public override void _Ready()
_syllableGenerator = new SyllableGenerator()
.WithVowels(DefaultVowels)
.WithLeadingConsonants(DefaultLeadingConsonants)
.WithTrailingConsonants(DefaultTrailingConsonants);
.WithTrailingConsonants(DefaultTrailingConsonants)
.AllowEmptyStrings(true);

_nameGenerator = new NameGenerator(_syllableGenerator).UsingSyllableCount(2, 3);
_nameFloaterScene = GD.Load<PackedScene>("res://NameFloater.tscn");

this.RefreshEditorTextValues();
this.InitializeEditorTextValues(); // This must occur before event handlers are setup

this.GetNode<GraphemeEditor>("Editor/LeadingConsonantsPanel").GraphemeEditorChanged += this.RefreshSyllableGenerator;
this.GetNode<GraphemeEditor>("Editor/VowelsPanel").GraphemeEditorChanged += this.RefreshSyllableGenerator;
this.GetNode<GraphemeEditor>("Editor/TrailingConsonantsPanel").GraphemeEditorChanged += this.RefreshSyllableGenerator;

}

public void RefreshEditorTextValues()
public void InitializeEditorTextValues()
{

var vowels = string.Join(string.Empty, _syllableGenerator.Vowels.Select(x => x.Value));
var leadingConsonants = string.Join(string.Empty, _syllableGenerator.LeadingConsonants.Select(x => x.Value));
var trailingConsonants = string.Join(string.Empty, _syllableGenerator.TrailingConsonants.Select(x => x.Value));

this.GetNode<TextEdit>("Editor/Vowels").Text = vowels;
this.GetNode<TextEdit>("Editor/LeadingConsonants").Text = leadingConsonants;
this.GetNode<TextEdit>("Editor/TrailingConsonants").Text = trailingConsonants;
var leadingConsonantPanel = this.GetNode<GraphemeEditor>("Editor/LeadingConsonantsPanel");
leadingConsonantPanel.LabelText = "Leading Consonants";
leadingConsonantPanel.Text = leadingConsonants;
leadingConsonantPanel.Probability = _syllableGenerator.Probability.ChanceLeadingConsonantExists.Value;

var vowelPanel = this.GetNode<GraphemeEditor>("Editor/VowelsPanel");
vowelPanel.LabelText = "Vowels";
vowelPanel.Text = vowels;
vowelPanel.Probability = _syllableGenerator.Probability.ChanceVowelExists.Value;

this.RefreshShiftingLabels();
var trailingConsonantPanel = this.GetNode<GraphemeEditor>("Editor/TrailingConsonantsPanel");
trailingConsonantPanel.LabelText = "Trailing Consonants";
trailingConsonantPanel.Text = trailingConsonants;
trailingConsonantPanel.Probability = _syllableGenerator.Probability.ChanceTrailingConsonantExists.Value;

}

public void RefreshSyllableGenerator()
{
var vowels = this.GetNode<TextEdit>("Editor/Vowels").Text;
var leadingConsonants = this.GetNode<TextEdit>("Editor/LeadingConsonants").Text;
var trailingConsonants = this.GetNode<TextEdit>("Editor/TrailingConsonants").Text;
var vowels = this.GetNode<GraphemeEditor>("Editor/VowelsPanel").Text;
var leadingConsonants = this.GetNode<GraphemeEditor>("Editor/LeadingConsonantsPanel").Text;
var trailingConsonants = this.GetNode<GraphemeEditor>("Editor/TrailingConsonantsPanel").Text;

_syllableGenerator = new SyllableGenerator()
.WithVowels(vowels)
.WithLeadingConsonants(leadingConsonants)
.WithTrailingConsonants(trailingConsonants);
.WithTrailingConsonants(trailingConsonants)
.AllowEmptyStrings(true);

_nameGenerator = new NameGenerator(_syllableGenerator).UsingSyllableCount(2, 3);
_syllableGenerator.Probability.ChanceLeadingConsonantExists = this.GetNode<GraphemeEditor>("Editor/LeadingConsonantsPanel").Probability;
_syllableGenerator.Probability.ChanceVowelExists = this.GetNode<GraphemeEditor>("Editor/VowelsPanel").Probability;
_syllableGenerator.Probability.ChanceTrailingConsonantExists = this.GetNode<GraphemeEditor>("Editor/TrailingConsonantsPanel").Probability;

this.RefreshShiftingLabels();
}
_nameGenerator = new NameGenerator(_syllableGenerator)
.UsingSyllableCount(2, 3);

public void RefreshShiftingLabels()
{
this.GetNode<ShiftingLabel>("Editor/VowelLabel").Values = _syllableGenerator.Vowels.Select(x => x.Value).ToList();
this.GetNode<ShiftingLabel>("Editor/LeadingConsonantLabel").Values = _syllableGenerator.LeadingConsonants.Select(x => x.Value).ToList();
this.GetNode<ShiftingLabel>("Editor/TrailingConsonantLabel").Values = _syllableGenerator.TrailingConsonants.Select(x => x.Value).ToList();
}

public override void _Process(double delta)
Expand Down
110 changes: 47 additions & 63 deletions Syllabore/Syllabore.Visualizer/MainVisualizer.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[gd_scene load_steps=4 format=3 uid="uid://dvet30uohxhwh"]

[ext_resource type="Script" path="res://MainVisualizer.cs" id="1_42c8l"]
[ext_resource type="PackedScene" uid="uid://cr4njnftd3faq" path="res://ShiftingLabel.tscn" id="2_dyjlh"]
[ext_resource type="PackedScene" uid="uid://dfh0hshtdjms1" path="res://GraphemeEditor.tscn" id="2_kt3in"]

[sub_resource type="LabelSettings" id="LabelSettings_sy3ib"]
font_size = 35
font_size = 30

[node name="MainVisualizer" type="Node2D"]
script = ExtResource("1_42c8l")
Expand All @@ -29,69 +29,53 @@ offset_right = 282.0
offset_bottom = 770.0
grow_vertical = 2

[node name="SyllaboreLabel" type="Label" parent="Editor"]
layout_mode = 0
offset_left = 5.0
offset_top = 5.0
offset_right = 261.0
offset_bottom = 66.0
text = "Syllabore"
label_settings = SubResource("LabelSettings_sy3ib")
horizontal_alignment = 1
vertical_alignment = 1

[node name="VowelLabel" parent="Editor" instance=ExtResource("2_dyjlh")]
layout_mode = 0
offset_left = 104.0
offset_top = 76.0
offset_right = 169.0
offset_bottom = 136.0
text = "A"

[node name="LeadingConsonantLabel" parent="Editor" instance=ExtResource("2_dyjlh")]
layout_mode = 0
offset_left = 29.0
offset_top = 72.0
offset_right = 88.0
offset_bottom = 135.0
text = "C"
[node name="TrailingConsonantsPanel" parent="Editor" instance=ExtResource("2_kt3in")]
layout_mode = 1
anchors_preset = -1
anchor_left = -0.00371747
anchor_right = 0.996283
offset_left = 9.0
offset_top = 243.0
offset_right = 9.0
offset_bottom = -427.0
metadata/_edit_use_anchors_ = true

[node name="TrailingConsonantLabel" parent="Editor" instance=ExtResource("2_dyjlh")]
layout_mode = 0
offset_left = 189.0
offset_top = 78.0
offset_right = 247.0
offset_bottom = 134.0
text = "T"
[node name="VowelsPanel" parent="Editor" instance=ExtResource("2_kt3in")]
layout_mode = 1
anchors_preset = -1
anchor_left = -0.00371747
anchor_right = 0.996283
offset_left = 9.0
offset_top = 155.0
offset_right = 3.0
offset_bottom = -521.0
metadata/_edit_use_anchors_ = true

[node name="Vowels" type="TextEdit" parent="Editor"]
custom_minimum_size = Vector2(75, 0)
layout_mode = 0
offset_left = 99.0
offset_top = 139.0
offset_right = 174.0
offset_bottom = 175.0
placeholder_text = "Vowels"
[node name="LeadingConsonantsPanel" parent="Editor" instance=ExtResource("2_kt3in")]
layout_mode = 1
anchors_preset = -1
anchor_left = -0.00371747
anchor_right = 0.996283
offset_left = 9.0
offset_top = 67.0
offset_right = 9.0
offset_bottom = -608.0
metadata/_edit_use_anchors_ = true

[node name="LeadingConsonants" type="TextEdit" parent="Editor"]
custom_minimum_size = Vector2(75, 0)
layout_mode = 0
offset_left = 19.0
offset_top = 139.0
offset_right = 94.0
offset_bottom = 176.0
placeholder_text = "Leading"

[node name="TrailingConsonants" type="TextEdit" parent="Editor"]
custom_minimum_size = Vector2(75, 0)
layout_mode = 0
offset_left = 179.0
offset_top = 139.0
offset_right = 254.0
offset_bottom = 174.0
placeholder_text = "Trailing"
[node name="GraphemeSectionLabel" type="Label" parent="Editor"]
layout_mode = 1
anchors_preset = -1
anchor_left = -0.0111524
anchor_top = 0.0172643
anchor_right = -0.0111524
anchor_bottom = 0.0172643
offset_left = 10.0
offset_top = -5.0
offset_right = 242.0
offset_bottom = 43.0
text = "Graphemes"
label_settings = SubResource("LabelSettings_sy3ib")
vertical_alignment = 1
metadata/_edit_use_anchors_ = true

[connection signal="timeout" from="GenerateNamesTimer" to="." method="GenerateName"]
[connection signal="text_changed" from="Editor/Vowels" to="." method="RefreshSyllableGenerator"]
[connection signal="text_changed" from="Editor/LeadingConsonants" to="." method="RefreshSyllableGenerator"]
[connection signal="text_changed" from="Editor/TrailingConsonants" to="." method="RefreshSyllableGenerator"]

0 comments on commit 37a9255

Please sign in to comment.