Skip to content

Commit

Permalink
Setup basic visualization of continually generated names floating thr…
Browse files Browse the repository at this point in the history
…ough 2D space
  • Loading branch information
kesac committed Jun 12, 2024
1 parent b048a26 commit c4a579f
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 28 deletions.
120 changes: 104 additions & 16 deletions Syllabore/Syllabore.Visualizer/MainVisualizer.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,136 @@
using Godot;
using Syllabore;
using System;
using System.Data;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Syllabore.Visualizer;
using static Godot.Control;

public partial class MainVisualizer : Node2D
public partial class MainVisualizer : Node2D, ITemporalController
{
private static readonly string DefaultVowels = "ae";
private static readonly string DefaultLeadingConsonants = "str";
private static readonly string DefaultTrailingConsonants = "lmn";

private SyllableGenerator _syllableGenerator;
private NameGenerator _nameGenerator;
private PackedScene _nameFloaterScene;

private int _speed = 100;
private Random _random = new Random();
private int _xPadding = 100;
private int _xLeftPadding = 300; // Make room for the menu stuff
private int _xRightPadding = 100; // Ensure names fit

public bool IsSlowed { get; set; }
public bool IsPaused { get; set; }

public override void _Ready()
{
_nameGenerator = new NameGenerator("ae","strl");
_syllableGenerator = new SyllableGenerator()
.WithVowels(DefaultVowels)
.WithLeadingConsonants(DefaultLeadingConsonants)
.WithTrailingConsonants(DefaultTrailingConsonants);

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

this.RefreshEditorTextValues();
}

public void RefreshEditorTextValues()
{

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;

this.RefreshShiftingLabels();

}

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;

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

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

this.RefreshShiftingLabels();
}

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)
{

}

public void PauseFloaters()
{
this.IsPaused = true;
this.IsSlowed = false;
}

public void SlowDownFloaters()
{
this.IsPaused = false;
this.IsSlowed = true;
}

public void ResumeFloaters()
{
this.IsSlowed = false;
this.IsPaused = false;
}

public void GenerateName()
{
if (!this.IsPaused && !this.IsSlowed && _syllableGenerator.Vowels.Count > 0)
{
var windowSize = GetViewportRect().Size;

var xRoundTo = 20;
var yRoundTo = 20;

var xPosition = _xLeftPadding + _random.Next((int)windowSize.X - (_xLeftPadding + _xRightPadding));
xPosition = (xPosition / xRoundTo) * xRoundTo; // Round to nearest 50

var windowSize = GetViewportRect().Size;
//var yPosition = (int)windowSize.Y;
var yPosition = -20; // Start off screen
yPosition = (yPosition / yRoundTo) * yRoundTo;

var xRoundTo = 50;
var yRoundTo = 20;
var instance = (NameFloater)_nameFloaterScene.Instantiate();

var xPosition = _xPadding + _random.Next((int)windowSize.X - (2 * _xPadding));
xPosition = (xPosition / xRoundTo) * xRoundTo; // Round to nearest 50
instance.SetTemporalController(this);

var yPosition = (int)windowSize.Y;
yPosition = (yPosition / yRoundTo) * yRoundTo;
instance.Text = _nameGenerator.Next();

var instance = (NameFloater)_nameFloaterScene.Instantiate();
instance.Speed = 100; // Base speed only; the instance will adjust itself based on the TemporalController's state
instance.Position = new Vector2(xPosition, yPosition);

instance.Text = _nameGenerator.Next();
instance.Speed = 100;
instance.Position = new Vector2(xPosition, yPosition);
//connect instance label to the the MainVisualizer's PauseFloater method
var nameLabel = instance.GetNode<Label>("NameLabel");
nameLabel.MouseEntered += this.SlowDownFloaters;
nameLabel.MouseExited += this.ResumeFloaters;

this.GetTree().CurrentScene.AddChild(instance);
this.GetTree().CurrentScene.AddChild(instance);
}
}

}
88 changes: 84 additions & 4 deletions Syllabore/Syllabore.Visualizer/MainVisualizer.tscn
Original file line number Diff line number Diff line change
@@ -1,17 +1,97 @@
[gd_scene load_steps=2 format=3 uid="uid://dvet30uohxhwh"]
[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"]

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

[node name="MainVisualizer" type="Node2D"]
script = ExtResource("1_42c8l")

[node name="ColorRect" type="ColorRect" parent="."]
offset_right = 1159.0
offset_bottom = 651.0
[node name="BlackBackground" type="ColorRect" parent="."]
show_behind_parent = true
offset_right = 1200.0
offset_bottom = 800.0
color = Color(0, 0, 0, 1)

[node name="GenerateNamesTimer" type="Timer" parent="."]
wait_time = 0.5
autostart = true

[node name="Editor" type="Panel" parent="."]
anchors_preset = 4
anchor_top = 0.5
anchor_bottom = 0.5
offset_left = 13.0
offset_top = 17.0
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="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="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="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"

[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"]
49 changes: 43 additions & 6 deletions Syllabore/Syllabore.Visualizer/NameFloater.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
using Godot;
using Syllabore.Visualizer;
using System;
using static Godot.Control;
using System.Xml;

public partial class NameFloater : Node2D
{
[Export]
public int Speed = 10;

private ITemporalController _temporalController;


public override void _Ready()
{
this.GetNode<Label>("NameLabel").MouseFilter = MouseFilterEnum.Stop;
}

public void SetTemporalController(ITemporalController temporalController)
{
_temporalController = temporalController;
}

public void HighlightText()
{
this.GetNode<Label>("NameLabel").Modulate = new Color(1, 1, 0);
}

public void NormalizeText()
{
this.GetNode<Label>("NameLabel").Modulate = new Color(1, 1, 1);
}

public string Text {
get
{
Expand All @@ -19,13 +45,24 @@ public string Text {

public override void _Process(double delta)
{
if (this.Position.Y < -20)
if (_temporalController != null && !_temporalController.IsPaused)
{
this.QueueFree();
}
else
{
this.Position += Vector2.Up * Speed * (float)delta;
if (this.Position.Y > 820) // -20
{
// Remove the floater once it's off screen
this.QueueFree();
}
else
{
var adjustedSpeed = Speed;

if (_temporalController.IsSlowed)
{
adjustedSpeed = Speed / 8;
}

this.Position += Vector2.Down * adjustedSpeed * (float)delta;
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions Syllabore/Syllabore.Visualizer/NameFloater.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ font_size = 26
script = ExtResource("1_qwpa5")

[node name="NameLabel" type="Label" parent="."]
offset_right = 40.0
offset_bottom = 23.0
offset_right = 75.0
offset_bottom = 36.0
text = "Name"
label_settings = SubResource("LabelSettings_6ep6f")

[connection signal="mouse_entered" from="NameLabel" to="." method="HighlightText"]
[connection signal="mouse_exited" from="NameLabel" to="." method="NormalizeText"]

0 comments on commit c4a579f

Please sign in to comment.