1 // celx_internal.h
2 //
3 // Copyright (C) 2003-2008, the Celestia Development Team
4 //
5 // Lua script extensions for Celestia. Internals that should only
6 // be needed by modules that implement a celx object.
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
12 
13 #ifndef _CELX_INTERNAL_H_
14 #define _CELX_INTERNAL_H_
15 
16 #include <map>
17 #include <string>
18 
19 class CelestiaCore;
20 class TimelinePhase;
21 
22 enum
23 {
24     Celx_Celestia = 0,
25     Celx_Observer = 1,
26     Celx_Object   = 2,
27     Celx_Vec3     = 3,
28     Celx_Matrix   = 4,
29     Celx_Rotation = 5,
30     Celx_Position = 6,
31     Celx_Frame    = 7,
32     Celx_CelScript= 8,
33     Celx_Font     = 9,
34     Celx_Image    = 10,
35     Celx_Texture  = 11,
36     Celx_Phase    = 12,
37 };
38 
39 
40 // select which type of error will be fatal (call lua_error) and
41 // which will return a default value instead
42 enum FatalErrors
43 {
44     NoErrors   = 0,
45     WrongType  = 1,
46     WrongArgc  = 2,
47     AllErrors = WrongType | WrongArgc,
48 };
49 
50 
51 class CelxLua;
52 
53 class CelxValue
54 {
55 public:
56     enum CelxType
57     {
58         Celx_Number,
59         Celx_String,
60         Celx_Nil,
61     };
62 
CelxValue()63     CelxValue() : type(Celx_Nil) {}
CelxValue(double d)64     CelxValue(double d) : type(Celx_Number), value_number(d) {}
CelxValue(const char * s)65     CelxValue(const char* s) : type(Celx_String), value_cstring(s) {}
66 
push(lua_State * l)67     void push(lua_State* l) const
68     {
69         switch (type)
70         {
71             case Celx_Number: lua_pushnumber(l, value_number); break;
72             case Celx_String: lua_pushstring(l, value_cstring); break;
73             case Celx_Nil: lua_pushnil(l); break;
74         }
75     }
76 
77 private:
78     CelxType type;
79     union
80     {
81         double value_number;
82         char* value_string;
83         const char* value_cstring;
84     };
85 };
86 
87 
88 class CelxLua
89 {
90 public:
91     CelxLua(lua_State* l);
92     ~CelxLua();
93 
94     bool isType(int index, int type) const;
95 
96     void setClass(int id);
97     void pushClassName(int id);
98     void* checkUserData(int index, int id);
99     void doError(const char* errorMessage);
100     void checkArgs(int minArgs, int maxArgs, const char* errorMessage);
101     void createClassMetatable(int id);
102     void registerMethod(const char* name, lua_CFunction fn);
103     void registerValue(const char* name, float value);
104 
105     void setTable(const char* field, lua_Number value);
106     void setTable(const char* field, const char* value);
107 
108     void newFrame(const ObserverFrame& f);
109     void newVector(const Vec3d& v);
110     void newRotation(const Quatd& q);
111     void newPosition(const UniversalCoord& uc);
112     void newObject(const Selection& sel);
113     void newPhase(const TimelinePhase& phase);
114 
115     Vec3d* toVector(int n);
116     Quatd* toRotation(int n);
117     UniversalCoord* toPosition(int n);
118     Selection* toObject(int n);
119     ObserverFrame* toFrame(int n);
120 
121     void push(const CelxValue& v1);
122     void push(const CelxValue& v1, const CelxValue& v2);
123 
124     CelestiaCore* appCore(FatalErrors fatalErrors = NoErrors);
125 
126     lua_Number safeGetNumber(int index,
127                              FatalErrors fatalErrors = AllErrors,
128                              const char* errorMessage = "Numeric argument expected",
129                              lua_Number defaultValue = 0.0);
130     const char* safeGetString(int index,
131                               FatalErrors fatalErrors = AllErrors,
132                               const char* errorMessage = "String argument expected");
133     bool safeGetBoolean(int index,
134                         FatalErrors fatalErrors = AllErrors,
135                         const char* errorMsg = "Boolean argument expected",
136                         bool defaultValue = false);
137 
138     LuaState* getLuaStateObject();
139 
140 
141     // String to flag mappings
142     typedef std::map<std::string, uint32> FlagMap;
143     typedef std::map<std::string, Color*> ColorMap;
144 
145     static void initMaps();
146     static void initRenderFlagMap();
147     static void initLabelFlagMap();
148     static void initBodyTypeMap();
149     static void initLocationFlagMap();
150     static void initOverlayElementMap();
151     static void initOrbitVisibilityMap();
152     static void initLabelColorMap();
153     static void initLineColorMap();
154 
155     static FlagMap RenderFlagMap;
156     static FlagMap LabelFlagMap;
157     static FlagMap LocationFlagMap;
158     static FlagMap BodyTypeMap;
159     static FlagMap OverlayElementMap;
160     static FlagMap OrbitVisibilityMap;
161     static ColorMap LineColorMap;
162     static ColorMap LabelColorMap;
163     static bool mapsInitialized;
164 
165     static const char* ClassNames[];
166 
167 private:
168     lua_State* m_lua;
169 };
170 
171 
172 
173 #endif // _CELX_INTERNAL_H_
174