[add] Added VarStoreScheduledController.

This commit is contained in:
falsycat 2019-10-14 00:00:00 +00:00
parent ffcd02066e
commit ccb7d79b67
6 changed files with 90 additions and 12 deletions

View File

@ -18,14 +18,17 @@ posteffect [0..1] {
clip_left := 0.1; clip_left := 0.1;
clip_right := 0.1; clip_right := 0.1;
} }
variable [0..3.5] {
hoge = 1-time;
}
$repeat i 1 { $repeat i 2 {
$repeat j 20 { $repeat j 20 {
circle [$i..$i+2] { circle [$i+1..$i+3] {
color_r := 0.8; color_r := 0.8;
color_g := 0.1; color_g := 0.1;
color_b := 0.1; color_b := 0.1;
color_a := 1; color_a = hoge;
damage := 0.1; damage := 0.1;
nearness_coe := 0.01; nearness_coe := 0.01;

View File

@ -74,8 +74,7 @@ abstract class AbstractScheduledController : ScheduledControllerInterface {
} }
auto temp = varstore_[name]; auto temp = varstore_[name];
if (!temp.isNull) return temp; if (!temp.isNull) return temp;
// TODO: std constants return StandardVarStore.GetByName(name);
return Nullable!float.init;
} }
void SetParameter(ref in Parameter param, ref in VarStore vars) { void SetParameter(ref in Parameter param, ref in VarStore vars) {
if (param.name.length < 2 || param.name[0..2] != "__") { if (param.name.length < 2 || param.name[0..2] != "__") {

View File

@ -21,7 +21,8 @@ import sjplayer.Actor,
sjplayer.PostEffectControllerInterface, sjplayer.PostEffectControllerInterface,
sjplayer.ProgramSet, sjplayer.ProgramSet,
sjplayer.ScheduledControllerInterface, sjplayer.ScheduledControllerInterface,
sjplayer.VarStore; sjplayer.VarStore,
sjplayer.VarStoreScheduledController;
/// ///
class Context { class Context {
@ -55,6 +56,10 @@ class Context {
"circle", "circle",
CircleElementScheduledControllerFactory(programs, varstore), CircleElementScheduledControllerFactory(programs, varstore),
), ),
tuple(
"variable",
VarStoreScheduledControllerFactory(varstore),
),
); );
foreach (ref factory; factories) { foreach (ref factory; factories) {
factory[1]. factory[1].

View File

@ -21,10 +21,19 @@ class VarStore : VarStoreInterface {
case "actor_x": return Nullable!float(actor_.pos.x); case "actor_x": return Nullable!float(actor_.pos.x);
case "actor_y": return Nullable!float(actor_.pos.y); case "actor_y": return Nullable!float(actor_.pos.y);
default: return Nullable!float.init; default:
} }
return name in user_vars_?
Nullable!float(user_vars_[name]): Nullable!float.init;
}
///
void opIndexAssign(float value, string name) {
user_vars_[name] = value;
} }
private: private:
const Actor actor_; const Actor actor_;
float[string] user_vars_;
} }

View File

@ -0,0 +1,56 @@
/// License: MIT
module sjplayer.VarStoreScheduledController;
import std.range.primitives;
import sjscript;
import sjplayer.AbstractScheduledController,
sjplayer.ContextBuilderInterface,
sjplayer.VarStore,
sjplayer.util.Parameter;
///
class VarStoreScheduledController : AbstractScheduledController {
public:
///
alias VarStore = sjplayer.VarStore.VarStore;
///
this(VarStore varstore, in ParametersBlock[] operations) {
super(varstore, operations);
varstore_ = varstore;
}
override void SetParameter(
ref in Parameter param,
ref in AbstractScheduledController.VarStore vars) {
const x_nullable = varstore_[param.name];
auto x = x_nullable.isNull? 0f: x_nullable.get;
param.CalculateParameter(x, vars);
varstore_[param.name] = x;
}
private:
VarStore varstore_;
}
///
struct VarStoreScheduledControllerFactory {
public:
///
this(VarStore varstore) {
varstore_ = varstore;
}
///
void Create(R)(R params, ContextBuilderInterface builder)
if (isInputRange!R && is(ElementType!R == ParametersBlock)) {
auto product = new VarStoreScheduledController(
varstore_, SortParametersBlock(params));
builder.AddScheduledController(product);
}
private:
VarStore varstore_;
}

View File

@ -7,6 +7,7 @@ import std.algorithm,
std.format, std.format,
std.math, std.math,
std.traits, std.traits,
std.typecons,
std.variant; std.variant;
import sjscript.Expression, import sjscript.Expression,
@ -22,12 +23,17 @@ static assert(IsVarStore!(float[string]));
struct StandardVarStore { struct StandardVarStore {
public: public:
static float opIndex(string name) { static float opIndex(string name) {
const v = GetByName(name);
enforce(!v.isNull);
return v.get;
}
static Nullable!float GetByName(string name) {
switch (name) { switch (name) {
case "PI": return PI; case "PI": return Nullable!float(PI);
case "PI_2": return PI_2; case "PI_2": return Nullable!float(PI_2);
case "PI_4": return PI_4; case "PI_4": return Nullable!float(PI_4);
case "E": return E; case "E": return Nullable!float(E);
default: throw new Exception("undefined variable %s".format(name)); default: return Nullable!float.init;
} }
} }
static assert(IsVarStore!StandardVarStore); static assert(IsVarStore!StandardVarStore);