1 // =================================
  2 // Copyright (c) 2022 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.IO;
  8 using System.Message;
  9 
 10 namespace System.Screen
 11 {
 12     public class QuitMessage : Message
 13     {
 14         public override nothrow int Id() const
 15         {
 16             return systemScreenQuitMessageId;
 17         }
 18         public override nothrow string Name() const
 19         {
 20             return "quit";
 21         }
 22         static nothrow QuitMessage() : Message()
 23         {
 24             RegisterMessage<QuitMessage>(systemScreenQuitMessageId);
 25         }
 26     }
 27 
 28     public class KeyPressedMessage : Message
 29     {
 30         public override nothrow int Id() const
 31         {
 32             return systemScreenKeyPressedMessageId;
 33         }
 34         public override nothrow int Size() const
 35         {
 36             return cast<int>(base->Size() + sizeof(uchar));
 37         }
 38         public override nothrow string Name() const
 39         {
 40             return "keyPressed";
 41         }
 42         static nothrow KeyPressedMessage() : Message()
 43         {
 44             RegisterMessage<KeyPressedMessage>(systemScreenKeyPressedMessageId);
 45         }
 46         public nothrow KeyPressedMessage(uchar key_) : key(key_)
 47         {
 48         }
 49         public override void Write(MemoryWriter& writer)
 50         {
 51             base->Write(writer);
 52             writer.Write(cast<int>(key));
 53         }
 54         public override void Read(MemoryReader& reader)
 55         {
 56             base->Read(reader);
 57             key = cast<uchar>(reader.ReadInt());
 58         }
 59         public override bool Dispatch(Control* control)
 60         {
 61             return control->HandleKeyPressed(this);
 62         }
 63         public nothrow uchar Key() const
 64         {
 65             return key;
 66         }
 67         private uchar key;
 68     }
 69     
 70     public class WriteScreenMessage : Message
 71     {
 72         public override nothrow int Id() const
 73         {
 74             return systemScreenWriteScreenMessageId;
 75         }
 76         public override nothrow int Size() const
 77         {
 78             return cast<int>(base->Size() + sizeof(Rect) + sizeof(int));
 79         }
 80         public override nothrow string Name() const
 81         {
 82             return "writeScreen";
 83         }
 84         static nothrow WriteScreenMessage() : Message()
 85         {
 86             RegisterMessage<WriteScreenMessage>(systemScreenWriteScreenMessageId);
 87         }
 88         public nothrow WriteScreenMessage(const Rect& rect_InvalidateKind invalidateKind_) : rect(rect_)invalidateKind(invalidateKind_)
 89         {
 90         }
 91         public override nothrow bool IsDefaultWriteScreenMessage() const
 92         {
 93             return rect.IsDefault() && invalidateKind != InvalidateKind.forceInvalidate;
 94         }
 95         public override void Write(MemoryWriter& writer)
 96         {
 97             base->Write(writer);
 98             writer.Write(rect.location.x);
 99             writer.Write(rect.location.y);
100             writer.Write(rect.size.w);
101             writer.Write(rect.size.h);
102             writer.Write(cast<int>(invalidateKind));
103         }
104         public override void Read(MemoryReader& reader)
105         {
106             base->Read(reader);
107             rect.location.x = reader.ReadInt();
108             rect.location.y = reader.ReadInt();
109             rect.size.w = reader.ReadInt();
110             rect.size.h = reader.ReadInt();
111             invalidateKind = cast<InvalidateKind>(reader.ReadInt());
112         }
113         public override bool Dispatch(Control* control)
114         {
115             return control->HandleWriteScreen(this);
116         }
117         public nothrow const Rect& GetRect() const
118         {
119             return rect;
120         }
121         private Rect rect;
122         private InvalidateKind invalidateKind;
123     }
124     
125     public class TimerMessage : Message
126     {
127         public override nothrow int Id() const
128         {
129             return systemScreenTimerMessageId;
130         }
131         public override nothrow int Size() const
132         {
133             return cast<int>(base->Size() + sizeof(int));
134         }
135         public override nothrow string Name() const
136         {
137             return "timer";
138         }
139         static nothrow TimerMessage() : Message()
140         {
141             RegisterMessage<TimerMessage>(systemScreenTimerMessageId);
142         }
143         public nothrow TimerMessage(int timerId_) : timerId(timerId_)
144         {
145         }
146         public override void Write(MemoryWriter& writer)
147         {
148             base->Write(writer);
149             writer.Write(timerId);
150         }
151         public override void Read(MemoryReader& reader)
152         {
153             base->Read(reader);
154             timerId = reader.ReadInt();
155         }
156         public override bool Dispatch(Control* control)
157         {
158             return control->HandleTimerMessage(this);
159         }
160         public nothrow int TimerId() const
161         {
162             return timerId;
163         }
164         private int timerId;
165     }
166     
167     new class ConcreteMessageFactoryFunction<QuitMessage>;
168     new class ConcreteMessageFactoryFunction<KeyPressedMessage>;
169     new class ConcreteMessageFactoryFunction<WriteScreenMessage>;
170     new class ConcreteMessageFactoryFunction<TimerMessage>;
171 }