patterns/command/remote/remotecontrol.d

55 lines
1.3 KiB
D
Raw Normal View History

2022-12-05 07:38:19 +00:00
module command.remote.remotecontrol;
import command.remote.command;
import command.remote.nocommand;
import std.conv : to;
import std.stdio : writeln;
import std.algorithm.mutation : fill;
import std.array : split, back;
import std.format;
class RemoteControl
{
Command[] onCommands;
Command[] offCommands;
this()
{
onCommands = new Command[7];
offCommands = new Command[7];
Command noCommand = new NoCommand();
fill(onCommands, noCommand);
fill(offCommands, noCommand);
}
void setCommand(int slot, Command onCommand, Command offCommand)
{
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
void onButtonWasPressed(int slot)
{
onCommands[slot].execute();
}
void offButtonWasPressed(int slot)
{
offCommands[slot].execute();
}
override string toString() const
{
string s = "\n------ Remote Control -------\n";
for (int i = 0; i < 7; ++i)
{
s ~= "[slot " ~ i.to!string ~ "] "
~ format("%22s", (cast(Object)onCommands[i]).classinfo.name.split(".").back())
~ format("%23s", (cast(Object)offCommands[i]).classinfo.name.split(".").back())
~ "\n";
}
return s;
}
}