Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
al1-ce committed Dec 11, 2022
1 parent 7ce0a41 commit 0316a71
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 299 deletions.
8 changes: 8 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
"$dmd"
]
},
{
"label": "Build tui",
"type": "shell",
"command": "dub build :tui",
"problemMatcher": [
"$dmd"
]
},
{
"label": "Run TUI Test",
"type": "shell",
Expand Down
2 changes: 1 addition & 1 deletion core/sily/color.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ alias Color8 = (float R, float G, float B, float A = 255) => Color(R / 255.0f, G
/// Color structure with data accesible with `[N]` or swizzling
struct Color {
/// Color data
public float[4] data = fill!float(1, 4);
public float[4] data = [ 1.0f ];

/// Alias to allow easy `data` access
alias data this;
Expand Down
169 changes: 130 additions & 39 deletions core/sily/property.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ module sily.property;
import std.traits;

/**
Generates mixin for automatic property injection
Generates mixin for automatic property injection. All properties
are created as `public final @property`.
Important! If symbol with same name, as property going to be, is present
then D will completely override property with that symbol
Example:
---
// Generates property for "_data" with name "data"
Expand All @@ -14,6 +19,8 @@ mixin getter!_data;
mixin setter!_data;
// Prefix manipulation, works with getter! and setter! too
// _data -> data (removes "_" prefix, default behaviour)
mixin property!_data;
// __data -> data (removes supplied prefix)
mixin property!(__data, "__");
// __data -> gdata (replaces supplied prefix)
Expand All @@ -22,58 +29,142 @@ mixin property!(__data, "__", "g");
mixin property!(_data, "", "c");
// _data -> _data (can't match prefix, keeping as is)
mixin property!(_data, "A");
mixin property!(_data, "A", "B");
// _data -> propertyData (replaces entire name)
mixin property!(_data, "propertyData", true);
---
*/
private mixin template property(alias symbol, bool _genSetter, bool _genGetter,
string prefixStringRem, string prefixStringAdd) {

static string genGetter() {
import std.algorithm.searching: countUntil;
import std.string: format;
import std.array: replaceFirst;
string _symname = __traits(identifier, symbol);
string _getname = __traits(identifier, symbol);
string _type = typeof(symbol).stringof;

mixin template property(alias symbol, string prefixRem = "_", string prefixAdd = "") {
mixin( ___silyPropertyGenGetter!(symbol, prefixRem, prefixAdd, false) );
mixin( ___silyPropertyGenSetter!(symbol, prefixRem, prefixAdd, false) );
}

/// Ditto
mixin template setter(alias symbol, string prefixRem = "_", string prefixAdd = "") {
mixin( ___silyPropertyGenSetter!(symbol, prefixRem, prefixAdd, false) );
}

/// Ditto
mixin template getter(alias symbol, string prefixRem = "_", string prefixAdd = "") {
mixin( ___silyPropertyGenGetter!(symbol, prefixRem, prefixAdd, false) );
}

/// Ditto
mixin template property(alias symbol, string symbolRename, bool B) if (B == true) {
mixin( ___silyPropertyGenGetter!(symbol, "", symbolRename, true) );
mixin( ___silyPropertyGenSetter!(symbol, "", symbolRename, true) );
}

/// Ditto
mixin template setter(alias symbol, string symbolRename, bool B) if (B == true) {
mixin( ___silyPropertyGenSetter!(symbol, "", symbolRename, true) );
}

/// Ditto
mixin template getter(alias symbol, string symbolRename, bool B) if (B == true) {
mixin( ___silyPropertyGenGetter!(symbol, "", symbolRename, true) );
}

static string ___silyPropertyGenGetter(alias symbol, string prefixStringRem,
string prefixStringAdd, bool prefixAsRename = false)() {
import std.algorithm.searching: countUntil;
import std.string: format;
import std.array: replaceFirst;
string _symname = __traits(identifier, symbol);
string _getname = __traits(identifier, symbol);
string _type = typeof(symbol).stringof;
if (prefixAsRename) {
_getname = prefixStringAdd;
} else {
if (_symname.countUntil(prefixStringRem) == 0) {
_getname = _symname.replaceFirst(prefixStringRem, prefixStringAdd);
}
if (prefixStringRem == "") _getname = prefixStringAdd ~ _symname;
return "%s %s() @property { return %s; }".format(
_type, _getname, _symname
);
}
return "public final %s %s() @property { return %s; }".format(
_type, _getname, _symname
);
}

static string genSetter() {
import std.algorithm.searching: countUntil;
import std.string: format;
import std.array: replaceFirst;
string _symname = __traits(identifier, symbol);
string _setname = __traits(identifier, symbol);
string _type = typeof(symbol).stringof;
static string ___silyPropertyGenSetter(alias symbol, string prefixStringRem,
string prefixStringAdd, bool prefixAsRename = false)() {
import std.algorithm.searching: countUntil;
import std.string: format;
import std.array: replaceFirst;
string _symname = __traits(identifier, symbol);
string _setname = __traits(identifier, symbol);
string _type = typeof(symbol).stringof;
if (prefixAsRename) {
_setname = prefixStringAdd;
} else {
if (_symname.countUntil(prefixStringRem) == 0) {
_setname = _symname.replaceFirst(prefixStringRem, prefixStringAdd);
}
if (prefixStringRem == "") _setname = prefixStringAdd ~ _symname;
return "%s %s(%s p_val) @property { return (%s = p_val); }".format(
_type, _setname, _type, _symname
);
}

static if (_genGetter) mixin( genGetter() );
static if (_genSetter) mixin( genSetter() );
return "public final %s %s(%s p_val) @property { return (%s = p_val); }".format(
_type, _setname, _type, _symname
);
}

/// Ditto
mixin template property(alias symbol, string prefixRem = "_", string prefixAdd = "") {
mixin property!(symbol, true, true, prefixRem, prefixAdd);
}
/* -------------------------- String mixin version -------------------------- */

/// Ditto
mixin template setter(alias symbol, string prefixRem = "_", string prefixAdd = "") {
mixin property!(symbol, true, false, prefixRem, prefixAdd);
}
/**
Generates string for automatic property injection with string mixin.
All properties are created as `public final @property`
Example:
---
// Generates property for "_data" with name "data"
mixin(property!_data);
// Generates only getter for "_data" with name "data"
mixin(getter!_data);
// Generates only setter for "_data" with name "data"
mixin(setter!_data);
/// Ditto
mixin template getter(alias symbol, string prefixRem = "_", string prefixAdd = "") {
mixin property!(symbol, false, true, prefixRem, prefixAdd);
}
// Prefix manipulation, works with getter! and setter! too
// _data -> data (removes "_" prefix, default behaviour)
mixin(property!_data);
// __data -> data (removes supplied prefix)
mixin(property!(__data, "__"));
// __data -> gdata (replaces supplied prefix)
mixin(property!(__data, "__", "g"));
// _data -> c_data (adds new prefix)
mixin(property!(_data, "", "c"));
// _data -> _data (can't match prefix, keeping as is)
mixin(property!(_data, "A"));
mixin(property!(_data, "A", "B"));
// _data -> propertyData (replaces entire name)
mixin(property!(_data, "propertyData", true));
---
*/
// static string property(alias symbol, string prefixRem = "_", string prefixAdd = "")() {
// return ___silyPropertyGenGetter!(symbol, prefixRem, prefixAdd, false) ~ "\n" ~
// ___silyPropertyGenSetter!(symbol, prefixRem, prefixAdd, false);
// }

// /// Ditto
// static string setter(alias symbol, string prefixRem = "_", string prefixAdd = "")() {
// return ___silyPropertyGenSetter!(symbol, prefixRem, prefixAdd, false);
// }

// /// Ditto
// static string getter(alias symbol, string prefixRem = "_", string prefixAdd = "")() {
// return ___silyPropertyGenGetter!(symbol, prefixRem, prefixAdd, false);
// }

// /// Ditto
// static string property(alias symbol, string symbolRename, bool B)() if (B == true) {
// return ___silyPropertyGenGetter!(symbol, "", symbolRename, true) ~ "\n" ~
// ___silyPropertyGenSetter!(symbol, "", symbolRename, true);
// }

// /// Ditto
// static string setter(alias symbol, string symbolRename, bool B)() if (B == true) {
// return ___silyPropertyGenSetter!(symbol, "", symbolRename, true);
// }

// /// Ditto
// static string getter(alias symbol, string symbolRename, bool B)() if (B == true) {
// return ___silyPropertyGenGetter!(symbol, "", symbolRename, true);
// }
2 changes: 1 addition & 1 deletion core/sily/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ alias uvec4 = Vector4u;
/// Vector structure with data accesible with `[N]` or swizzling
struct Vector(T, size_t N) if (isNumeric!T && N > 0) {
/// Vector data
public T[N] data = fill!T(0, size);
public T[N] data = [ 0 ];

/// Alias to allow easy `data` access
alias data this;
Expand Down
18 changes: 16 additions & 2 deletions test/property
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env dub
/+ dub.sdl:
name "property"
name "propertytest"
dependency "sily" path="/g/sily-dlang/"
+/
module test.propertytest;

import std.stdio: writeln;
import sily.property;
Expand All @@ -25,6 +26,9 @@ void main() {
writeln(t.c__tref);
t.__tredd = "__tredd";
writeln(t.__tredd);
t.treedee = "treedee";
writeln(t.treedee);
writeln(t.tredd);
}

class Thingy {
Expand All @@ -39,9 +43,19 @@ class Thingy {
mixin property!_trea;
mixin getter!(_treb);
mixin setter!(_trec);
mixin property!(__tred, "__");
mixin getter!(__tred, "__");
mixin setter!(__tred, "__");
mixin property!(__tree, "__", "g");
mixin property!(__tref, "", "c");
mixin property!(__tredd, "__");
mixin property!(__tredd, "A");
mixin property!(__tredd, "treedee", true);
// mixin( property!(__tredd) );

// mixin(___silyPropertyGenSetter!(__tredd, "", "treedee", true));
// mixin(___silyPropertyGenGetter!(__tredd, "", "treedee", true));

// public string treedee(string a, string b) @property {
// return (__tredd = a ~ b);
// }
}
12 changes: 8 additions & 4 deletions test/tui
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ class MyApp: App {
// Render.colorMode = ColorMode.ansi8;
Render.colorMode = ColorMode.truecolor;
rootElement().addChild(new MyPanel(uvec2.zero, uvec2.zero, Colors.white));
rootElement().addChild(new Panel(uvec2(10, 10), uvec2(50, 20), Colors.gray));
rootElement().addChild(new MyLabel("My cool text"d, uvec2(35, 19), Colors.white, Colors.black));
Panel p = new Panel(uvec2(10, 10), uvec2(50, 20), Colors.gray);
rootElement().addChild(p);
p.addChild(new MyLabel("My cool text"d, uvec2(35, 19), Colors.white, Colors.black));
}

public override void input(InputEvent e) {
Expand Down Expand Up @@ -78,8 +79,11 @@ class MyLabel: Label {
mixin inheritConstructor;

public override void update(float delta) {
pos.x = 35 + (cast(int) (cos(Time.currTime) * 20.0f)) - length / 2;
pos.y = 19 + (cast(int) (sin(Time.currTime) * 10.0f));
uvec2 _pos = pos;
_pos.x = 35 + (cast(int) (cos(Time.currTime) * 20.0f)) - length / 2;
_pos.y = 19 + (cast(int) (sin(Time.currTime) * 10.0f));
pos = _pos;
// Element a = parent;
}

public override void render() {
Expand Down
Loading

0 comments on commit 0316a71

Please sign in to comment.