Console.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 #ifndef H_DC_CONSOLE_INCLUDED
33 #define H_DC_CONSOLE_INCLUDED
34 
35 #include <Dynacoe/Color.h>
36 #include <Dynacoe/Entity.h>
37 #include <Dynacoe/Modules/Module.h>
38 #include <Dynacoe/Interpreter.h>
39 #include <Dynacoe/Util/Chain.h>
40 #include <Dynacoe/Component.h>
41 #include <map>
42 
43 class ConsoleInputStream;
44 namespace Dynacoe {
45 
46 class DebugMessage;
47 class Shape2D;
48 
49 
50 
53  public:
54 
57  enum class MessageType{
58  Normal,
59  Warning,
60  Severe,
61  Fatal,
62  };
63 
64  ConsoleStream & operator=(const ConsoleStream &);
65 
66  using FinishedCallback = void (*)(const std::string & text, ConsoleStream::MessageType i);
67 
69  ConsoleStream(FinishedCallback);
70  ~ConsoleStream(); // calls finished callback with the formatted string
74  ConsoleStream operator<<(const Chain&);
75  ConsoleStream operator<<(MessageType);
77 
78  private:
79 
80  static MessageType type;
81  std::string str;
82  FinishedCallback finish;
83 };
84 
85 
95 class Console : public Module {
96  public:
97  friend class Engine;
98 
99  Console();
100 
101 
102 
105  enum class MessageMode{
106  Standard,
107  Disabled,
108  };
109 
112  static const char * End;
113 
114 
115 
116 
119  static ConsoleStream System();
120  static ConsoleStream Info ();
121  static ConsoleStream Error ();
122  static ConsoleStream Warning();
124 
125  // State
126 
129  static bool IsVisible() ;
130 
134  static void Show(bool doIt) ;
135 
139  static bool IsLocked() ;
140 
145  static void Lock(bool);
146 
147 
152  static void AddCommand(const std::string & name, Interpreter::Command * command);
153 
154 
158  static uint32_t GetNumLines();
159 
164  static std::string GetLine(uint32_t i);
165 
168  static void Clear();
169 
170 
173  static void OverlayMessageMode(MessageMode);
174 
178 
186  static void SetCommandCallback(DynacoeEvent((*)));
187 
188 
189  private:
190 
191 
192 
193 
194 
195 
196 
197  static void initBase();
198 
199  struct LineModel;
200  struct LineView;
201  static Shape2D * base;
202 
203 
204  static std::vector<LineModel*> lines;
205  static std::vector<LineView*> lineViews;
206 
207  static Entity * messages;
208 
209 
210 
211 
212  static bool locked;
213  static bool shown;
214  static bool inputActive;
215  static ConsoleInputStream * streamIn;
216 
217 
218  static MessageMode messageMode;
219  static uint32_t viewOffsetY;
220 
221 
222  static void AddDefaultCommands();
223  static void AdjustViewSize();
224  static void AcquireStreamOutput(const std::string &, ConsoleStream::MessageType);
225  static void ProcessStreamOutput();
226  static void ProcessStreamIteration(const std::string &, ConsoleStream::MessageType);
227  static int fontHeight;
228  static float basePositionOffsetRatio;
229 
230  static void PostMessageConsole(const std::string & c, ConsoleStream::MessageType);
231  static std::vector<std::pair<std::string, ConsoleStream::MessageType>> stream;
232  static Interpreter * interp;
233 
234 
235  public:
236  std::string GetName() {return "Console";}
237  void Init(); void InitAfter(); void RunBefore(); void RunAfter(); void DrawBefore(); void DrawAfter();
238  Backend * GetBackend();
239 };
240 }
241 
242 
243 #endif
A debugging utility used to view output from the Engine and interpret text commands from the user...
Definition: Console.h:95
Basic interactive object.
Definition: Entity.h:62
Backend.
Definition: Backend.h:56
static bool IsLocked()
Returns whether or not console has been locked.
Allows for easy string building and deconstructing.
Definition: Chain.h:52
static void Show(bool doIt)
Activates/deactivates the console.
The interpreter provides a means to execute dynamic runtime behavior through text. This is most conventionally useful via the Console.
Definition: Interpreter.h:48
Definition: AssetID.h:37
MessageType
Message classifications. For now, just determines the color of the message.
Definition: Console.h:57
static MessageMode GetOverlayMessageMode()
Returns the current message mode. MessageMode::Standard is the default.
Functor that represents a run command.
Definition: Interpreter.h:53
static void Clear()
Clears all recorded messages in the Console.
static void OverlayMessageMode(MessageMode)
Sets the mode by which to display incoming Console messages.
Streams output to the console. Normally not directly needed.
Definition: Console.h:52
Console output is only added to the console log.
static const char * End
Marks the ending of the line.
Definition: Console.h:112
static std::string GetLine(uint32_t i)
Returns the i'th most recent message, where 0 is the earliest line and GetNumLines()-1 is the most re...
static void AddCommand(const std::string &name, Interpreter::Command *command)
Adds an additional command to be recognized by the interactive interpreter.
static uint32_t GetNumLines()
Returns the number of lines of output currently held by the console.
static void Lock(bool)
Locks the console.
MessageMode
Types of message postings.
Definition: Console.h:105
An aspect that can express basic 2D objects, such as images and shapes.
Definition: Shape2D.h:46
static bool IsVisible()
Returns whether or not the console is showing.
Every console line added results in a message that briefly appears on screen. This is the default...
Main class that handles automated updates of Dynacoe.
Definition: Dynacoe.h:55
static void SetCommandCallback(DynacoeEvent((*)))
Sets a callback to be run for all user-entered commands, regardless of command issued. Whether to process the command normally is returned.