This repository has been archived on 2022-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
patterns/command/remoteundostatusmacro/remotecontrol.d

64 lines
1.6 KiB
D
Raw Normal View History

2022-11-15 20:25:25 +00:00
module command.remoteundostatusmacro.remotecontrol;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.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("%23s", (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";
}
}