patterns/command/remoteundo/remotecontrol.d

64 lines
1.6 KiB
D
Raw Normal View History

2022-12-05 07:38:19 +00:00
module command.remoteundo.remotecontrol;
import command.remoteundo.command;
import command.remoteundo.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;
Command undoCommand;
this()
{
onCommands = new Command[7];
offCommands = new Command[7];
Command noCommand = new NoCommand();
fill(onCommands, noCommand);
fill(offCommands, noCommand);
undoCommand = noCommand;
}
void setCommand(int slot, Command onCommand, Command offCommand)
{
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
void onButtonWasPushed(int slot)
{
onCommands[slot].execute();
undoCommand = onCommands[slot];
}
void offButtonWasPushed(int slot)
{
offCommands[slot].execute();
undoCommand = offCommands[slot];
}
void undoButtonWasPushed()
{
undoCommand.undo();
}
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 ~ "[undo] " ~ (cast(Object)undoCommand).classinfo.name.split(".").back() ~ "\n";
}
}